-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathBuildDatabase
executable file
·351 lines (299 loc) · 8.78 KB
/
BuildDatabase
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/perl
##---------------------------------------------------------------------------##
## File:
## @(#) BuildDatabase.pl
## Author:
## Arian Smit <[email protected]>
## Robert Hubley <[email protected]>
## Description:
## A utility for creating a WU-Blast/RepeatModeler XDF database
## from a set of fasta files.
##
#******************************************************************************
#* Copyright (C) Institute for Systems Biology 2004 Developed by
#* Arian Smit and Robert Hubley.
#*
#* This work is licensed under the Open Source License v2.1. To view a copy
#* of this license, visit http://www.opensource.org/licenses/osl-2.1.php or
#* see the license.txt file contained in this distribution.
#*
###############################################################################
#
# To Do:
#
#
=head1 NAME
BuildDatabase - Format FASTA files for use with RepeatModeler
=head1 SYNOPSIS
BuildDatabase [-options] -name "mydb" <seqfile(s) in fasta format>
or
BuildDatabase [-options] -name "mydb"
-dir <dir containing fasta files *.fa, *.fasta,
*.fast, *.FA, *.FASTA, *.FAST, *.dna,
and *.DNA >
or
BuildDatabase [-options] -name "mydb"
-batch <file containing a list of fasta files>
=head1 DESCRIPTION
This is basically a wrapper around AB-Blast's and NCBI Blast's
DB formating programs. It assists in aggregating files for processing
into a single database. Source files can be specified by:
- Placing the names of the FASTA files on the command
line.
- Providing the name of a directory containing FASTA files
with the file suffixes *.fa or *.fasta.
- Providing the name of a manifest file which contains the
names of FASTA files ( fully qualified ) one per line.
NOTE: Sequence identifiers are not preserved in this database. Each
sequence is assigned a new GI ( starting from 1 ). The
translation back to the original sequence is preserved in the
*.translation file.
The options are:
=over 4
=item -h(elp)
Detailed help
=item -name <database name>
The name of the database to create.
=item -dir <directory>
The name of a directory containing fasta files to be processed. The
files are recognized by their suffix. Only *.fa and *.fasta files
are processed.
=item -batch <file>
The name of a file which contains the names of fasta files to process.
The files names are listed one per line and should be fully qualified.
=back
=head1 SEE ALSO
=over 4
RepeatModeler, RMBlast
=back
=head1 COPYRIGHT
Copyright 2004-2019 Institute for Systems Biology
=head1 AUTHOR
Robert Hubley <[email protected]>
=cut
#
# Module Dependence
#
use strict;
use FindBin;
use lib $FindBin::RealBin;
use Getopt::Long;
use Pod::Text;
use File::Temp qw/ tempfile tempdir /;
use Cwd;
# RepeatModeler Libraries
use RepModelConfig;
use lib $RepModelConfig::configuration->{'REPEATMASKER_DIR'}->{'value'};
#
# Class Globals & Constants
#
my $CLASS = "BuildDatabase";
my $DEBUG = 0;
$DEBUG = 1 if ( $RepModelConfig::DEBUGALL == 1 );
my $version = $RepModelConfig::VERSION;
#
# Option processing
# e.g.
# -t: Single letter binary option
# -t=s: String parameters
# -t=i: Number paramters
#
my @opts = qw( help dir=s batch=s name=s );
# Add configuration parameters as additional command-line options
push @opts, RepModelConfig::getCommandLineOptions();
#
# Provide the POD text from this file and
# from the config file by merging them
# together. The heading "CONFIGURATION
# OVERRIDES" provides the insertion point
# for the configuration POD.
#
sub usage {
my $p = Pod::Text->new();
$p->output_fh( *STDOUT );
my $pod_str;
open IN, "<$0"
or die "Could not open self ($0) for generating documentation!";
while ( <IN> ) {
if ( /^=head1\s+CONFIGURATION OVERRIDES\s*$/ ) {
my $c_pod = RepModelConfig::getPOD();
if ( $c_pod ) {
$pod_str .= $_ . $c_pod;
}
}
else {
$pod_str .= $_;
}
}
close IN;
print "$0 - $version\n";
$p->parse_string_document( $pod_str );
exit( 1 );
}
#
# Get the supplied command line options, and set flags
#
my %options = ();
unless ( &GetOptions( \%options, @opts ) ) {
usage();
}
# Print the internal POD documentation if something is missing
if ( ( $#ARGV == -1 && !$options{'batch'} && !$options{'dir'} )
|| $options{'help'} )
{
print "No query sequence file indicated\n\n";
usage();
}
#
# Resolve configuration settings using the following precedence:
# command line first, then environment, followed by config
# file.
#
RepModelConfig::resolveConfiguration( \%options );
my $config = $RepModelConfig::configuration;
my $XDFORMAT_PRGM = $config->{'ABBLAST_DIR'}->{'value'} . "/xdformat";
my $NCBIBLASTDB_PRGM = $config->{'RMBLAST_DIR'}->{'value'} . "/makeblastdb";
my $NCBIDBCMD_PRGM = $config->{'RMBLAST_DIR'}->{'value'} . "/blastdbcmd";
my $engine = "rmblast";
if ( $options{'engine'} && $options{'engine'} =~ /wublast|abblast/i ) {
die "ERROR: \"-engine $options{'engine'}\" is deprecated, this verison of RepeatModeler uses rmblast only.\n";
}
die "Missing database name!\n" if ( !defined $options{'name'} );
my @fileList = ();
# Directory specified
if ( $options{'dir'} ) {
if ( -d $options{'dir'} ) {
# Process the directory
opendir DIR, $options{'dir'}
or die "Cannot open dir $options{'dir'}!\n";
my $file;
while ( defined( $file = readdir( DIR ) ) ) {
if ( $file =~ /\.fa$/i
|| $file =~ /\.fasta$/i
|| $file =~ /\.fast$/i
|| $file =~ /\.dna/i )
{
push @fileList, "$options{'dir'}/$file";
}
}
closedir( DIR );
}
else {
die "Directory $options{'dir'} doesn't exist!\n";
}
}
# Batch specified
if ( $options{'batch'} ) {
if ( -s $options{'batch'} ) {
open BATCH, "<$options{'batch'}"
or die "Cannot open batch file $options{'batch'}!\n";
while ( <BATCH> ) {
s/[\n\r\s]+//g;
if ( -s $_ ) {
push @fileList, $_;
}
else {
die "File $_ from batch $options{'batch'} does not exist!\n";
}
}
close BATCH;
}
else {
die "Batch file $options{'batch'} doesn't exist!\n";
}
}
# Push remaining command line files
foreach my $file ( @ARGV ) {
if ( -f $file ) {
push @fileList, $file;
}
else {
die "Command line fasta file $file does not exist!\n";
}
}
#
# Sanitize sequence names
#
my $IDX;
my $index = 1;
open $IDX, ">$options{'name'}.translation"
or die $CLASS . ": Cannot open file $options{'name'}.translation. This may occur if your current working directory is not writable.\n";
my $results = "";
my $dbSeqs = 0;
my $dbSize = 0;
print "Building database $options{'name'}:\n";
# Sanitizing the names of all input databases and placing
# in one big tempfile
# $fh = tempfile();
my ( $SEQ, $seqFilename ) = tempfile( DIR => ".", UNLINK => 1 );
my $file;
for ( my $i = 0 ; $i <= $#fileList ; $i++ ) {
$file = $fileList[ $i ];
print " Reading $file...\n";
if ( $file =~ /^.*\.gz$/ ) {
open IN, "gunzip -c $file|"
or die $CLASS . ": Cannot open file using gunzip $file\n";
}
else {
open IN, "<$file" or die $CLASS . ": Cannot open file $file\n";
}
while ( <IN> ) {
if ( /^\>\s*(\S+)\s+(.*)/ ) {
print $SEQ ">gi|$index\n";
print $IDX "$1\t$index\n";
$index++;
}
else {
print $SEQ $_;
}
}
close IN;
}
close $SEQ;
$file = $seqFilename;
if ( $engine eq "rmblast" ) {
my $cmd = "$NCBIBLASTDB_PRGM -blastdb_version 4 -out $options{'name'} -parse_seqids -dbtype nucl -in $file 2>&1";
print "Running: $cmd\n" if ( $DEBUG );
$results = `$cmd`;
if ( $? ) {
print "The makeblastdb program exited with code " . ($? >> 8) . ". Please check your input file(s) for potential formating errors.\n$NCBIBLASTDB_PRGM returned: $results\n";
print "The command used was: $cmd\n";
die;
}
}
else {
$results = `$XDFORMAT_PRGM -n -o $options{'name'} $file 2>&1`;
while ( $results =~
/sequences\s*\(letters\)\s*(?:written|appended):\s*([\d,]+)\s+\(([\d,]+)\).*$/mg
)
{
my $size = $2;
my $seqs = $1;
$size =~ s/,//g;
$dbSize += $size;
$seqs =~ s/,//g;
$dbSeqs += $seqs;
last;
}
}
if ( $engine eq "abblast" ) {
$results = `$XDFORMAT_PRGM -n -X $options{'name'} 2>&1`;
}
else {
$results = `$NCBIDBCMD_PRGM -db $options{'name'} -info 2>&1`;
while (
$results =~ /\s+([\d\,]+)\s+sequences;\s+([\d\,]+)\s+total bases.*$/mg )
{
my $size = $2;
my $seqs = $1;
$size =~ s/,//g;
$dbSize += $size;
$seqs =~ s/,//g;
$dbSeqs += $seqs;
last;
}
}
print "Number of sequences (bp) added to database: $dbSeqs ( $dbSize bp )\n";
close $IDX;
unlink( $seqFilename ) if ( -e $seqFilename );
1;