forked from Dfam-consortium/RepeatModeler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuildDatabase
executable file
·373 lines (320 loc) · 9.49 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/u1/local/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.
#*
###############################################################################
# ChangeLog:
#
# $Log: BuildDatabase,v $
# Revision 1.6 2017/03/31 17:25:39 rhubley
# - Lots-o-improvements. Starting preparation for release.
#
# Revision 1.5 2012/08/22 18:46:24 rhubley
# -- a refresh checkin and adding a new utility
#
# Revision 1.4 2010/06/10 18:15:44 rhubley
# - Fixed a bug that Arian found with RepeatScout models refined by the
# RMBLAST engine. The problem had to do with a rev/compl operation
# in BuildRSConsensi
#
# - Readying RepeatModeler for release with RMBLAST
#
# Revision 1.3 2010/06/04 22:55:18 rhubley
# - Some bug fixes.
# - Changes to support rmblast package
#
# Revision 1.2 2010/04/21 17:46:20 rhubley
# -- working toward a RMBlast version of RepeatModeler
#
# Revision 1.1 2010/04/20 23:33:28 rhubley
# - Replacement for BuildXDFDatabase
#
# Revision 1.12 2008/05/01 23:03:00 rhubley
# Cleanup before a 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 -engine <engine name>
The name of the search engine we are using. I.e abblast/wublast or
ncbi (rmblast version).
=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, ABBlast, NCBIBlast
=back
=head1 COPYRIGHT
Copyright 2004-2017 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 Cwd;
# RepeatModeler Libraries
use RepModelConfig;
use lib $RepModelConfig::REPEATMASKER_DIR;
#
# Class Globals & Constants
#
my $CLASS = "BuildDatabase";
my $DEBUG = 0;
$DEBUG = 1 if ( $RepModelConfig::DEBUGALL == 1 );
#
# Option processing
# e.g.
# -t: Single letter binary option
# -t=s: String parameters
# -t=i: Number paramters
#
my @opts = qw( help dir=s engine=s batch=s name=s );
#
# Get the supplied command line options, and set flags
#
my %options = ();
unless ( &GetOptions( \%options, @opts ) ) {
exec "pod2text $0";
exit( 1 );
}
# 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";
# This is a nifty trick so we don't have to have
# a duplicate "USAGE()" subroutine. Instead we
# just recycle our POD docs. See PERL POD for more
# details.
exec "pod2text $0";
die;
}
my $engine = $RepModelConfig::DEFAULT_SEARCH_ENGINE;
if ( $options{'engine'} )
{
if ( $options{'engine'} =~ /wublast/i || $options{'engine'} =~ /abblast/i )
{
die "RepeatModeler doesn't appear to be configured to use the "
. "$options{'engine'} search engine. Please re-run configure before "
. "using this or other RepeatModeler programs.\n"
if ( ! -x $RepModelConfig::XDFORMAT_PRGM );
$engine = "abblast";
}elsif ( $options{'engine'} =~ /ncbi/i )
{
die "RepeatModeler doesn't appear to be configured to use the "
. "$options{'engine'} search engine. Please re-run configure before "
. "using this or other RepeatModeler programs.\n"
if ( ! -x $RepModelConfig::NCBIBLASTDB_PRGM );
$engine = "ncbi";
}else {
print "I don't understand -engine $options{'engine'}\n";
exec "pod2text $0";
die;
}
}
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"
|| die $CLASS . ": Cannot open file $options{'name'}.translation\n";
my $results = "";
my $dbSeqs = 0;
my $dbSize = 0;
print "Building database $options{'name'}:\n";
for ( my $i = 0 ; $i <= $#fileList ; $i++ ) {
my $file = $fileList[ $i ];
print " Adding $file to database\n";
#
# Sanitize sequence names
#
open IN, "<$file" || die $CLASS . ": Cannot open file $file\n";
open SEQ, ">tmpSeqData.fa"
|| die $CLASS . ": Cannot open file tmpSeqData.fa\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 = "tmpSeqData.fa";
if ( $engine eq "ncbi" )
{
if ( $i == 0 )
{
print "Running: $RepModelConfig::NCBIBLASTDB_PRGM -out $options{'name'} -parse_seqids -dbtype nucl -in $file 2>&1n" if ( $DEBUG );
$results =
`$RepModelConfig::NCBIBLASTDB_PRGM -out $options{'name'} -parse_seqids -dbtype nucl -in $file 2>&1`;
}
else {
$results =
`$RepModelConfig::NCBIBLASTDB_PRGM -out $options{'name'} -parse_seqids -dbtype nucl -in "$options{'name'} $file" 2>&1`;
}
}else
{
if ( $i == 0 )
{
$results =
`$RepModelConfig::XDFORMAT_PRGM -n -o $options{'name'} $file 2>&1`;
}
else {
$results =
`$RepModelConfig::XDFORMAT_PRGM -n -a $options{'name'} $file 2>&1`;
}
# TODO: Check for errors from xdformat
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 = `$RepModelConfig::XDFORMAT_PRGM -n -X $options{'name'} 2>&1`;
}else
{
$results =
`$RepModelConfig::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( "tmpSeqData.fa" ) if ( -s "tmpSeqData.fa" );
1;