| 1 | #!/usr/local/bin/perl | ||
| 2 | # $Id: load_genbank.pl,v 1.1 2008-10-16 17:01:27 lstein Exp $ | ||
| 3 | use strict; | ||
| 4 | use Bio::DB::GFF; | ||
| 5 | use Getopt::Long; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | load_genbank.pl - Load a Bio::DB::GFF database from GENBANK files. | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | % load_genbank.pl -d genbank -f localfile.gb | ||
| 14 | % load_genbank.pl -d genbank -a AP003256 | ||
| 15 | |||
| 16 | NOTE: The script bp_genbank2gff.pl in the BioPerl distribution is the | ||
| 17 | same as this script. | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | This script loads a Bio::DB::GFF database with the features contained | ||
| 22 | in a either a local genbank file or an accession that is fetched from | ||
| 23 | genbank. Various command-line options allow you to control which | ||
| 24 | database to load and whether to allow an existing database to be | ||
| 25 | overwritten. | ||
| 26 | |||
| 27 | This script currently only uses MySQL, though it is a proof-of- | ||
| 28 | principle and could easily be extended to work with other RDMS | ||
| 29 | that are supported by GFF through adaptors. | ||
| 30 | |||
| 31 | =head1 COMMAND-LINE OPTIONS | ||
| 32 | |||
| 33 | Command-line options can be abbreviated to single-letter options. | ||
| 34 | e.g. -d instead of --database. | ||
| 35 | |||
| 36 | --create Force creation and initialization of database | ||
| 37 | --dsn <dsn> Data source (default dbi:mysql:test) | ||
| 38 | --user <user> Username for mysql authentication | ||
| 39 | --pass <password> Password for mysql authentication | ||
| 40 | --proxy <proxy> Proxy server to use for remote access | ||
| 41 | --file Arguments that follow are Genbank/EMBL file names (default) | ||
| 42 | --accession Arguments that follow are genbank accession numbers | ||
| 43 | --stdout Write converted GFF file to stdout rather than loading | ||
| 44 | |||
| 45 | =head1 SEE ALSO | ||
| 46 | |||
| 47 | L<Bio::DB::GFF>, L<bulk_load_gff.pl>, L<load_gff.pl> | ||
| 48 | |||
| 49 | =head1 AUTHOR | ||
| 50 | |||
| 51 | Scott Cain, cain@cshl.org | ||
| 52 | |||
| 53 | Lincoln Stein, lstein@cshl.org | ||
| 54 | |||
| 55 | Copyright (c) 2003 Cold Spring Harbor Laboratory | ||
| 56 | |||
| 57 | This library is free software; you can redistribute it and/or modify | ||
| 58 | it under the same terms as Perl itself. See DISCLAIMER.txt for | ||
| 59 | disclaimers of warranty. | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | package Bio::DB::GFF::Adaptor::biofetch_to_stdout; | ||
| 64 | use CGI 'escape'; | ||
| 65 | use Bio::DB::GFF::Util::Rearrange; | ||
| 66 | use Bio::DB::GFF::Adaptor::biofetch; | ||
| 67 | use vars '@ISA'; | ||
| 68 | @ISA = 'Bio::DB::GFF::Adaptor::biofetch'; | ||
| 69 | |||
| 70 | sub new { | ||
| 71 | my $class = shift; | ||
| 72 | my $self = bless {},$class; | ||
| 73 | my ($proxy) = rearrange(['PROXY'],@_); | ||
| 74 | if ($proxy) { | ||
| 75 | my @args = ref($proxy) ? @$proxy : eval $proxy; | ||
| 76 | $self->{_proxy} = \@args if @args; | ||
| 77 | } | ||
| 78 | $self; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub load_gff_line { | ||
| 82 | my ($self,$options) = @_; | ||
| 83 | # synthesize GFF3-compatible line | ||
| 84 | my @attributes; | ||
| 85 | if (my $parent = $options->{gname}) { | ||
| 86 | push @attributes,"Parent=".escape($parent) unless $options->{method} =~ /^gene$/; | ||
| 87 | push @attributes,"ID=".escape($parent); | ||
| 88 | } | ||
| 89 | if (my $tstart = $options->{tstart}) { | ||
| 90 | my $tstop = $options->{tstop}; | ||
| 91 | my $target = escape($options->{gname}); | ||
| 92 | push @attributes,"Target=$target+$tstart+$tstop"; | ||
| 93 | } | ||
| 94 | my %a; | ||
| 95 | if (my $attributes = $options->{attributes}) { | ||
| 96 | for my $a (@$attributes) { | ||
| 97 | my ($tag,$value) = @$a; | ||
| 98 | push @{$a{escape($tag)}},escape($value); | ||
| 99 | } | ||
| 100 | for my $a (keys %a) { | ||
| 101 | push @attributes,"$a=".join(',',@{$a{$a}}); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | my $last_column = join ';',@attributes; | ||
| 105 | if ($options->{method} eq 'origin') { | ||
| 106 | print "##sequence-region $options->{gname} $options->{start} $options->{stop}\n"; | ||
| 107 | } | ||
| 108 | $$options{score} ||='.'; | ||
| 109 | $$options{source} ||='genbank'; | ||
| 110 | print join("\t",@{$options}{qw(ref source method start stop score strand phase)},$last_column),"\n"; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub load_sequence_string { | ||
| 114 | my $self = shift; | ||
| 115 | my ($acc,$seq) = @_; | ||
| 116 | $seq =~ s/(.{1,60})/$1\n/g; | ||
| 117 | print ">$acc\n\L$seq\U\n"; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub setup_load { | ||
| 121 | my $self = shift; | ||
| 122 | print "##gff-version 3\n"; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub finish_load { } | ||
| 126 | |||
| 127 | 1; | ||
| 128 | |||
| 129 | package main; | ||
| 130 | |||
| 131 | my ($DSN,$ADAPTOR,$CREATE,$USER,$PASSWORD,$FASTA,$ACC,$FILE,$PROXY,$STDOUT); | ||
| 132 | |||
| 133 | GetOptions ('dsn:s' => \$DSN, | ||
| 134 | 'user:s' => \$USER, | ||
| 135 | 'password:s' => \$PASSWORD, | ||
| 136 | 'accession' => \$ACC, | ||
| 137 | 'file' => \$FILE, | ||
| 138 | 'proxy:s' => \$PROXY, | ||
| 139 | stdout => \$STDOUT, | ||
| 140 | create => \$CREATE) or die <<USAGE; | ||
| 141 | Usage: $0 [options] <gff file 1> <gff file 2> ... | ||
| 142 | Load a Bio::DB::GFF database from GFF files. | ||
| 143 | |||
| 144 | Options: | ||
| 145 | --create Force creation and initialization of database | ||
| 146 | --dsn <dsn> Data source (default dbi:mysql:test) | ||
| 147 | --user <user> Username for mysql authentication | ||
| 148 | --pass <password> Password for mysql authentication | ||
| 149 | --proxy <proxy> Proxy server to use for remote access | ||
| 150 | --file Arguments that follow are Genbank/EMBL file names (default) | ||
| 151 | --accession Arguments that follow are genbank accession numbers | ||
| 152 | |||
| 153 | This script loads a Bio::DB::GFF database with the features contained | ||
| 154 | in a either a local genbank file or an accession that is fetched from | ||
| 155 | genbank. Various command-line options allow you to control which | ||
| 156 | database to load and whether to allow an existing database to be | ||
| 157 | overwritten. | ||
| 158 | |||
| 159 | This script currently only uses MySQL, though it is a proof-of- | ||
| 160 | principle and could easily be extended to work with other RDMS | ||
| 161 | that are supported by GFF through adaptors. | ||
| 162 | |||
| 163 | USAGE | ||
| 164 | ; | ||
| 165 | |||
| 166 | # some local defaults | ||
| 167 | $DSN ||= 'dbi:mysql:test'; | ||
| 168 | $ADAPTOR = $STDOUT ? 'biofetch_to_stdout' : 'biofetch'; | ||
| 169 | |||
| 170 | my @auth; | ||
| 171 | push @auth,(-user=>$USER) if defined $USER; | ||
| 172 | push @auth,(-pass=>$PASSWORD) if defined $PASSWORD; | ||
| 173 | push @auth,(-proxy=>$PROXY) if defined $PROXY; | ||
| 174 | |||
| 175 | my $db = Bio::DB::GFF->new(-adaptor=>$ADAPTOR,-dsn => $DSN,@auth) | ||
| 176 | or die "Can't open database: ",Bio::DB::GFF->error,"\n"; | ||
| 177 | |||
| 178 | if ($CREATE) { | ||
| 179 | $db->initialize(1); | ||
| 180 | } | ||
| 181 | |||
| 182 | die "you must specify either an accession to retrieve from\nembl or a local file containing data in embl format\n" | ||
| 183 | unless @ARGV; | ||
| 184 | |||
| 185 | if ($ACC && !$FILE) { | ||
| 186 | while ($_ = shift) { | ||
| 187 | print STDERR "Loading $_..."; | ||
| 188 | my $result = $db->load_from_embl(/^NC_/?'refseq':'embl' => $_); | ||
| 189 | print STDERR $result ? "ok\n" : "failed\n"; | ||
| 190 | } | ||
| 191 | } else { | ||
| 192 | while ($_ = shift) { | ||
| 193 | print STDERR "Loading $_...\n"; | ||
| 194 | my $result = $db->load_from_file($_); | ||
| 195 | print STDERR $result ? "ok\n" : "failed\n"; | ||
| 196 | } | ||
| 197 | } |