Debian Codegraph @

bin/bp_seqfeature_load

raw
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 ## Used to output the 'usage' message
7 use Pod::Usage;
8
9 ## Used to parse command line options
10 use Getopt::Long;
11
12 ## Used to create temporary files, if necessary
13 use File::Spec;
14
15 ## BioPerl!
16 use Bio::DB::SeqFeature::Store;
17 use Bio::DB::SeqFeature::Store::GFF3Loader;
18
19
20
21 ## The available options. Note, these defaults are 'hard coded' into
22 ## the USAGE POD, so if you change one of the defaults (you shouldn't),
23 ## you should update the USAGE.
24
25 my $DSN = 'dbi:mysql:test';
26 my $SFCLASS = 'Bio::DB::SeqFeature';
27 my $ADAPTOR = 'DBI::mysql';
28 my $NAMESPACE;
29 my $VERBOSE = 1;
30 my $FAST = 0;
31 my $TMP = File::Spec->tmpdir();
32 my $IGNORE_SEQREGION = 0;
33 my $CREATE = 0;
34 my $USER = '';
35 my $PASS = '';
36 my $COMPRESS = 0;
37 my $INDEX_SUB = 1;
38 my $NOALIAS_TARGET = 0;
39 my $SUMMARY_STATS = 0;
40 my $NOSUMMARY_STATS = 0;
41 my $FTS = 0;
42
43 ## Two flags based on http://stackoverflow.com/questions/1232116
44 ## how-to-create-pod-and-use-pod2usage-in-perl
45 my $opt_help;
46 my $opt_man;
47
48 GetOptions( 'd|dsn=s' => \$DSN,
49 's|seqfeature=s' => \$SFCLASS,
50 'n|namespace=s' => \$NAMESPACE,
51 'a|adaptor=s' => \$ADAPTOR,
52 'v|verbose!' => \$VERBOSE,
53 'f|fast' => \$FAST,
54 'T|temporary-directory=s' => \$TMP,
55 'i|ignore-seqregion' => \$IGNORE_SEQREGION,
56 'c|create' => \$CREATE,
57 'u|user=s' => \$USER,
58 'p|password=s' => \$PASS,
59 'z|zip' => \$COMPRESS,
60 'S|subfeatures!' => \$INDEX_SUB,
61
62 ## Any good single letter choices here?
63 'noalias-target' => \$NOALIAS_TARGET,
64 'summary' => \$SUMMARY_STATS,
65 'N|nosummary' => \$NOSUMMARY_STATS,
66 'fts' => \$FTS,
67
68 ## I miss '--help' when it isn't there!
69 'h|help!' => \$opt_help,
70 'm|man!' => \$opt_man,
71 )
72 or pod2usage( -message =>
73 "\nTry 'bp_seqfeature_load.pl --help' for more information\n",
74 -verbose => 0,
75 -exitval => 2,
76 );
77
78 ## Should we output usage information?
79 pod2usage( -verbose => 1 ) if $opt_help;
80 pod2usage( -verbose => 2 ) if $opt_man;
81
82 ## Did we get any files to process?
83 @ARGV
84 or pod2usage( -message =>
85 "\nYou need to pass some GFF or fasta files to load\n",
86 -verbose => 0,
87 -exitval => 2,
88 );
89
90 pod2usage( -message => "\n--fts requires --create\n",
91 -verbose => 0,
92 -exitval => 2,
93 ) if ($FTS and not $CREATE);
94
95
96 ## POD
97
98 =head1 NAME
99
100 bp_seqfeature_load.pl - Load GFF into a SeqFeature database
101
102 =head1 DESCRIPTION
103
104 Pass any number of GFF or fasta format files (or GFF with embedded
105 fasta) to load the features and sequences into a SeqFeature
106 database. The database (and adaptor) to use is specified on the
107 command line. Use the --create flag to create a new SeqFeature
108 database.
109
110 =head1 SYNOPSIS
111
112 bp_seqfeature_load.pl [options] gff_or_fasta_file1 [gff_or_fasta_file2 [...]]
113
114 Try 'bp_seqfeature_load.pl --help' or '--man' for more information.
115
116 =head1 OPTIONS
117
118 =over 4
119
120 =item -d, --dsn
121
122 DBI data source (default dbi:mysql:test)
123
124 =item -n, --namespace
125
126 The table prefix to use (default undef) Allows several independent
127 sequence feature databases to be stored in a single database
128
129 =item -s, --seqfeature
130
131 The type of SeqFeature to create... RTSC (default Bio::DB::SeqFeature)
132
133 =item -a, --adaptor
134
135 The storage adaptor (class) to use (default DBI::mysql)
136
137 =item -v, --verbose
138
139 Turn on verbose progress reporting (default true) Use --noverbose to
140 switch this off.
141
142 =item -f, --fast
143
144 Activate fast loading. (default 0) Only available for some adaptors.
145
146 =item -T, --temporary-directory
147
148 Specify temporary directory for fast loading (default
149 File::Spec->tmpdir())
150
151 =item -i, --ignore-seqregion
152
153 If true, then ignore ##sequence-region directives in the GFF3 file
154 (default, create a feature for each region)
155
156 =item -c, --create
157
158 Create the database and reinitialize it (default false) Note, this
159 will erase previous database contents, if any.
160
161 =item -u, --user
162
163 User to connect to database as
164
165 =item -p, --password
166
167 Password to use to connect to database
168
169 =item -z, --zip
170
171 Compress database tables to save space (default false)
172
173 =item -S, --subfeatures
174
175 Turn on indexing of subfeatures (default true) Use --nosubfeatures to
176 switch this off.
177
178 =item --fts
179
180 Index the attribute table for full-text search (default false). Applicable
181 only when --create is specified. Currently applicable to the DBI::SQLite
182 storage adaptor only (using the most recent supported FTS indexing method,
183 which may not be portable to older DBI::SQLite versions).
184
185 =item --summary
186
187 Generate summary statistics for coverage graphs (default false) This
188 can be run on a previously loaded database or during the load. It will
189 default to true if --create is used.
190
191 =item -N, --nosummary
192
193 Do not generate summary statistics to save some space and load time (default if
194 --create is not specified, use this option to explicitly turn off summary
195 statistics when --create is specified)
196
197 =item --noalias-target
198
199 Don't create an Alias attribute whose value is the target_id in a
200 Target attribute (if the feature contains a Target attribute, the
201 default is to create an Alias attribute whose value is the target_id
202 in the Target attribute)
203
204 =back
205
206 Please see http://www.sequenceontology.org/gff3.shtml for information
207 about the GFF3 format. BioPerl extends the format slightly by adding a
208 ##index-subfeatures directive. Set this to a true value if you wish
209 the database to be able to retrieve a feature's individual parts (such
210 as the exons of a transcript) independently of the top level feature:
211
212 ##index-subfeatures 1
213
214 It is also possible to control the indexing of subfeatures on a
215 case-by-case basis by adding "index=1" or "index=0" to the feature's
216 attribute list. This should only be used for subfeatures.
217
218 Subfeature indexing is true by default. Set to false (0) to save lots
219 of database space and speed performance. You may use --nosubfeatures
220 to force this.
221
222 =cut
223
224
225
226
227
228 if ($FAST) {
229 -d $TMP && -w $TMP
230 or die "Fast loading is requested, but I cannot write into the directory $TMP";
231 $DSN .= ";mysql_local_infile=1" if $ADAPTOR =~ /mysql/i && $DSN !~ /mysql_local_infile/;
232 }
233
234 my @options;
235 @options = ($USER,$PASS) if $USER || $PASS;
236
237 my $store = Bio::DB::SeqFeature::Store->new
238 (
239 -dsn => $DSN,
240 -namespace => $NAMESPACE,
241 -adaptor => $ADAPTOR,
242 -tmpdir => $TMP,
243 -user => $USER,
244 -pass => $PASS,
245 -write => 1,
246 -create => $CREATE,
247 -compress => $COMPRESS,
248 -fts => $FTS,
249 )
250 or die "Couldn't create connection to the database";
251
252 $store->init_database('erase') if $CREATE;
253 $SUMMARY_STATS++ if $CREATE; # this is a good thing
254
255 my $loader = Bio::DB::SeqFeature::Store::GFF3Loader->new
256 (
257 -store => $store,
258 -sf_class => $SFCLASS,
259 -verbose => $VERBOSE,
260 -tmpdir => $TMP,
261 -fast => $FAST,
262 -ignore_seqregion => $IGNORE_SEQREGION,
263 -index_subfeatures => $INDEX_SUB,
264 -noalias_target => $NOALIAS_TARGET,
265 -summary_stats => $NOSUMMARY_STATS ? 0 : $SUMMARY_STATS,
266 )
267 or die "Couldn't create GFF3 loader";
268
269 # on signals, give objects a chance to call their DESTROY methods
270 $SIG{TERM} = $SIG{INT} = sub { undef $loader; undef $store; die "Aborted..."; };
271
272 $loader->load(@ARGV);
273
274 exit 0;