-
Notifications
You must be signed in to change notification settings - Fork 0
/
queryRepeatDatabase.pl
executable file
·725 lines (628 loc) · 17.9 KB
/
queryRepeatDatabase.pl
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
#!/usr/bin/perl
## Modified from Rev 1.95 (See comments below) by Adam Diehl. Revisions made
## 10/14/2016:
## 1) Altered -class option to allow lists of terms.
## 2) Added -exclude option to toggle negative matching to -class args.
##---------------------------------------------------------------------------##
## File:
## @(#) queryRepeatDatabase.pl
## Author:
## Robert M. Hubley [email protected]
## Description:
## A utility script to assist in querying the monolithic
## RM repeat sequence database.
##
#******************************************************************************
#* Copyright (C) Institute for Systems Biology 2003-2005 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: queryRepeatDatabase.pl,v $
# Revision 1.95 2015/08/07 19:06:52 rhubley
# Cleanup before a distribution
#
#
###############################################################################
#
# To Do:
#
=head1 NAME
queryRepeatDatabase.pl - Query the RepeatMasker repeat database.
=head1 SYNOPSIS
queryRepeatDatabase.pl [-version] [-species <species> |
-stage <stage num> |
-class <class1,...,classN> |
-exclude |
-id <id>]
[-stat]
[-tree]
[-clade]
[-lib <library>]
=head1 DESCRIPTION
A utility script to query the RepeatMasker repeat database.
The options are:
=over 4
=item -version
Displays the version of the program
=back
=over 4
=item -species "species name"
The full name ( case insensitive ) of the species you would like
to search for in the database. This will return all the repeats
which would be used in a RepeatMasker search against this species.
This includes repeats contained in the clade given by "species name"
and ancestral repeats of "species name". Lastly ubiquitous
sequences such as RNAs and simple repeats are also included.
=item -clade
This will modify the default behaviour of the species option and
return only the repeats which are specific to your species or any
of it descendents. This is useful for identifying how rich
the database of repeats is for a given species/clade.
=item -stage <stage num>
The number of the RepeatMasker stage for which you would like
repeats. In the past these stages were individual libraries
with the following general names:
Stage Library
----- -------
0 species.lib
10 is.lib
15 rodspec.lib
20 humspec.lib
25 simple.lib
30 at.lib
35 sinecutlib
40 shortcutlib
45 cutlib
50 shortlib
55 longlib
60 mirs.lib
65 mir.lib
70 retrovirus.lib
75 l1.lib
=item -class <class>
Retrieve all elements of (a) particular class(es). Multiple classes
may be given as a comma-delimited list. For example:
DNA
SINE
LINE
LTR
Other
RC
Satellite
tRNA
Simple_repeat
Unknown
snRNA
=item -exclude
Exclude rather than include matches to the class(es) given. Requires
-class!
=item -id <id>
Retrieve only a single id from the database.
=item -stat
Returns statistics on the sequences
=item -tree
Prints the taxonomy tree for all species in the database.
=item -lib <library>
Specify a repeat library.
=back
=head1 SEE ALSO
ReapeatMasker
=head1 COPYRIGHT
Copyright 2005-2011 Robert Hubley, Institute for Systems Biology
=head1 AUTHOR
Robert Hubley <[email protected]>
=cut
#
# Module Dependence
#
use strict;
use FindBin;
use lib $FindBin::Bin;
use lib "$FindBin::Bin/repeatmasker_libs";
use Getopt::Long;
use Data::Dumper;
use FastaDB;
use RepbaseEMBL;
use DFAM;
use Taxonomy;
use File::Basename;
#
# Version
#
my $Version = 0.1;
my $DEBUG = 0;
#
# Option processing
# e.g.
# -t: Single letter binary option
# -t=s: String parameters
# -t=i: Number parameters
#
my @getopt_args = (
'-version', # print out the version and exit
'-dfam',
'-embl',
'-fasta',
'-species=s',
'-clade',
'-stage=i',
'-class=s',
'-quiet',
'-id=s',
'-tree',
'-stat',
'-exclude',
'-lib=s',
'-lib-dir=s'
);
my %options = ();
Getopt::Long::config( "noignorecase", "bundling_override" );
unless ( GetOptions( \%options, @getopt_args ) ) {
usage();
}
sub usage {
print "$0 - $Version\n";
exec "pod2text $0";
exit( 1 );
}
if ( $options{'version'} ) {
print "$Version\n";
exit;
}
print STDERR "$ARGV[0]\n";
my $fileFormat = "embl";
my $lib_dir = "$FindBin::Bin/../Libraries";
if ( exists $options{'lib-dir'} ) {
$lib_dir = "$options{'lib-dir'}";
}
my $RMLib;
if ( exists $options{'lib'} ) {
$RMLib = $options{'lib'};
} else {
$RMLib = $lib_dir . "/RepeatMaskerLib.embl";
}
if ( exists $options{'dfam'} ) {
$fileFormat = "dfam";
if ( !exists $options{'lib'} ) {
$RMLib = $lib_dir . "/Dfam.hmm";
}
} elsif ( exists $options{'fasta'} ) {
$fileFormat = "fasta";
if ( !exists $options{'lib'} ) {
$RMLib = $lib_dir . "/RepeatMasker.lib";
}
}
my $exclude = 0;
if ( exists $options{'exclude'} ) {
if ( !exists $options{'class'} ) {
die "Error: -exclude requires -class <class(es)>. See -help!\n";
}
$exclude = 1;
}
if ( !-f $RMLib ) {
print "Error! Could not find the RepeatMasker library: $RMLib!\n";
exit;
}
if ( $fileFormat eq "fasta" ) {
open LIBVER, "<$FindBin::Bin/../Libraries/version"
|| die "Could not "
. "open library version file: $FindBin::Bin/../Libraries/version!";
}
else {
open LIBVER, "<$RMLib" || die "Could not open library: $RMLib!";
}
my $rmLibraryVersion = "Unknown";
while ( <LIBVER> ) {
if ( /RepBase Update (\S+), RM database version (\S+)/ ) {
$rmLibraryVersion = $2;
last;
}
if ( /^[C#][C#]\s+RELEASE\s+(\S+);/ ) {
$rmLibraryVersion = $1;
last;
}
## Release: Dfam_2.0
if ( /^#\s+Release:\s+(Dfam_[\d\.]+)/ )
{
$rmLibraryVersion = $1;
last;
}
}
close LIBVER;
unless ( $options{'quiet'} ) {
print STDERR "queryRepeatDatabase\n";
print STDERR "===================\n";
print STDERR "RepeatMasker Database: " . basename( $RMLib ) . "\n";
print STDERR "Version: $rmLibraryVersion\n";
}
# Open up the taxonomy database
my $taxDB;
if ( exists $options{'lib-dir'} ) {
$taxDB = "$lib_dir/taxonomy.dat";
} else {
$taxDB = "$FindBin::Bin/../Libraries/taxonomy.dat";
}
my $tax = Taxonomy->new( taxonomyDataFile => $taxDB );
if ( $options{'tree'} ) {
my $db = RepbaseEMBL->new( fileName => $RMLib );
&_displayLibraryTaxonomy( library_ref => $db, taxonomy_ref => $tax );
exit;
}
my $species = $options{'species'};
my $realSpeciesName = "";
if ( $species ne "" ) {
if ( ( $realSpeciesName = $tax->isSpecies( $species ) ) eq "" ) {
print "Species " . $species
. " is not in the database. "
. "Here is a list of possible similar\nsounding substitutes:\n";
foreach my $species ( $tax->getSimilarSoundingSpecies( $species ) ) {
print " $species\n";
}
exit;
}
}
unless ( $options{'quiet'} ) {
if ( $options{'clade'} ) {
print "Clade: $species ( $realSpeciesName )\n";
}
elsif ( $options{'species'} ) {
print "Species: $species ( $realSpeciesName )\n";
}
}
my $stagePattern = "";
$stagePattern = $options{'stage'} if ( defined $options{'stage'} );
my $idPattern = "";
$idPattern = $options{'id'} if ( defined $options{'id'} );
my @classPatterns = ();
if ( defined $options{'class'} ) {
@classPatterns = split /,/, $options{'class'};
}
my $db = undef;
my $seqCount = 0;
my @ids = ();
if ( $fileFormat eq "fasta" ) {
$db = FastaDB->new( fileName => $RMLib, openMode => SeqDBI::ReadOnly );
$seqCount = scalar( $db->getIDs() ) + 1;
@ids = $db->getIDs();
}
elsif ( $fileFormat eq "embl" ) {
$db = RepbaseEMBL->new( fileName => $RMLib );
$seqCount = $db->getRecordCount();
}
elsif ( $fileFormat eq "dfam" ) {
$db = DFAM->new( fileName => $RMLib );
$seqCount = $db->getRecordCount();
}
my $totalLength = 0;
my $totalNumber = 0;
my $cladeCnt = 0;
my $equalCnt = 0;
my $cladeLen = 0;
my $ancestCnt = 0;
my $ancestLen = 0;
my $equalLen = 0;
my %uniqSpeciesLabels = ();
for ( my $i = 0 ; $i < $seqCount ; $i++ ) {
# Look for species match
my $isDescendant = 0;
my $isAncestor = 0;
my $isEqual = 0;
if ( $realSpeciesName ne "" ) {
my $match = 0;
if ( $db->isa( "FastaDB" ) ) {
my $descLine = $db->getDescription( $ids[ $i ] );
while ( $descLine =~ /@([\w\.]+)/ig ) {
my $name = $1;
$name =~ s/_/ /g;
# $isDescendant = $tax->isA( $name, $realSpeciesName );
# $isAncestor = $tax->isA( $realSpeciesName, $name );
# if ( $options{'clade'} ) {
# $cladeCnt++ if ( $isDescendant == 1 );
# $match = 1 if ( $isDescendant == 1 );
# last;
# }
# else {
# $cladeCnt++ if ( $isDescendant == 1 && !$isAncestor == 1 );
# $cladeCnt++ if ( $isDescendant == 1 && $isAncestor == 1 );
# $ancestCnt++ if ( !$isDescendant == 1 && $isAncestor == 1 );
# $match = 1 if ( $isDescendant == 1 || $isAncestor == 1 );
# last;
# }
if ( $tax->isSpecies( $name ) eq $realSpeciesName ) {
$isEqual = 1;
}
elsif ( $tax->isA( $name, $realSpeciesName ) == 1 ) {
$isDescendant = 1;
}
elsif ( $tax->isA( $realSpeciesName, $name ) == 1 ) {
$isAncestor = 1;
}
if ( $isEqual ) {
$equalCnt++;
$match = 1;
last;
}
elsif ( $isDescendant ) {
$cladeCnt++;
$match = 1;
last;
}
elsif ( ( !$options{'clade'} )
&& $isAncestor )
{
$ancestCnt++;
$match = 1;
last;
}
}
}
else {
my $record = $db->getRecord( $i );
foreach my $name ( $record->getRMSpeciesArray() ) {
$name =~ s/_/ /g;
if ( $tax->isSpecies( $name ) eq $realSpeciesName ) {
$isEqual = 1;
}
elsif ( $tax->isA( $name, $realSpeciesName ) == 1 ) {
$isDescendant = 1;
}
elsif ( $tax->isA( $realSpeciesName, $name ) == 1 ) {
$isAncestor = 1;
}
if ( $isEqual ) {
$equalCnt++;
$match = 1;
last;
}
elsif ( $isDescendant ) {
$cladeCnt++;
$match = 1;
last;
}
elsif ( ( !$options{'clade'} )
&& $isAncestor )
{
$ancestCnt++;
$match = 1;
last;
}
}
}
next if ( $match == 0 );
} # End - Look for species match
# Look for stages
if ( $stagePattern ne "" ) {
my $match = 0;
if ( $db->isa( "FastaDB" ) ) {
my $descLine = $db->getDescription( $ids[ $i ] );
if ( ( $stagePattern == 0 && $descLine =~ /\[S:\]/ )
|| $descLine =~ /\[S:(\d+,)*$stagePattern(,\d+)*\]/ )
{
$match = 1;
}
}
else {
my $record = $db->getRecord( $i );
my @stages = $record->getRMSearchStagesArray();
foreach my $stage ( @stages ) {
if ( $stage eq $stagePattern ) {
$match = 1;
last;
}
}
}
next if ( $match == 0 );
} # End - Look for stage match
# Look for id match
if ( $idPattern ne "" ) {
my $match = 0;
if ( $db->isa( "FastaDB" ) ) {
if ( $ids[ $i ] =~ /$idPattern/i ) {
$match = 1;
}
}
else {
my $record = $db->getRecord( $i );
if ( $record->getId() =~ /$idPattern/i ) {
$match = 1;
}
}
next if ( $match == 0 );
} # End - Look for id match
# Look for class match(es)
if ( @classPatterns ) {
my $match = 0;
foreach my $classPattern (@classPatterns) {
$match = &match_class_pattern($db, $i, $classPattern);
last if ($match);
}
if ($exclude) {
next if ($match);
} else {
next if (!$match);
}
} # End - Look for class match
if ( $db->isa( "FastaDB" ) ) {
my $seq = $db->getSequence( $ids[ $i ] );
if ( defined $options{'stat'} ) {
my $seqLen = length( $seq );
print ">" . $ids[ $i ] . " Length = $seqLen bp\n";
$totalLength += $seqLen;
if ( $isAncestor ) {
$ancestLen += $seqLen;
}
elsif ( $isDescendant ) {
$cladeLen += $seqLen;
}
elsif ( $isEqual ) {
$equalLen += $seqLen;
}
++$totalNumber;
$uniqSpeciesLabels{$species}++;
}
else {
my $descLine = $db->getDescription( $ids[ $i ] );
print ">" . $ids[ $i ];
if ( $descLine ne "" ) {
print " " . $descLine;
}
print "\n";
$seq =~ s/(\S{50})/$1\n/g;
$seq .= "\n"
unless ( $seq =~ /.*\n+$/s );
print $seq;
}
}
elsif ( $fileFormat eq "embl" ) {
my $record = $db->getRecord( $i );
my $seq = $record->getSequence();
if ( defined $options{'stat'} ) {
my $seqLen = length( $seq );
print ">"
. $record->getId() . "#"
. $record->getRMType() . "/"
. $record->getRMSubType()
. " Length = "
. "$seqLen bp";
foreach my $name ( $record->getRMSpeciesArray() ) {
print " \@$name";
$uniqSpeciesLabels{$name}++;
}
print "\n";
$totalLength += $seqLen;
if ( $isAncestor ) {
$ancestLen += $seqLen;
}
elsif ( $isDescendant ) {
$cladeLen += $seqLen;
}
elsif ( $isEqual ) {
$equalLen += $seqLen;
}
++$totalNumber;
}
else {
print ">" . $record->getId() . "#" . $record->getRMType();
if ( $record->getRMSubType() ne "" ) {
print "/" . $record->getRMSubType();
}
print " ";
print $record->getDescription();
print "\n";
$seq =~ s/(\S{50})/$1\n/g;
$seq .= "\n"
unless ( $seq =~ /.*\n+$/s );
print $seq;
}
}
elsif ( $fileFormat eq "dfam" ) {
my $record = $db->getRecord( $i );
if ( defined $options{'stat'} ) {
my $seqLen = 0;
print ">"
. $record->getId() . "#"
. $record->getRMType() . "/"
. $record->getRMSubType()
. " Length = "
. "$seqLen bp";
foreach my $name ( $record->getRMSpeciesArray() ) {
print " \@$name";
}
print "\n";
$totalLength += $seqLen;
$ancestLen += $seqLen if ( $isAncestor == 1 && !$isDescendant == 1 );
$cladeLen += $seqLen if ( $isDescendant == 1 );
++$totalNumber;
}
else {
print "" . $record->getRecordLines();
}
}
}
print "\n";
if ( defined $options{'stat'} ) {
if ( defined $options{'species'} ) {
if ( !$options{'clade'} ) {
print "$ancestCnt ancestral and ubiquitous sequence(s) with a "
. "total length of "
. $ancestLen . " bp\n";
}
print
"$equalCnt $realSpeciesName specific repeats with a total length of $equalLen bp\n";
print "$cladeCnt lineage specific sequence(s) with a total length of "
. $cladeLen . " bp\n";
print "-" x ( 80 ) . "\n";
}
else {
print "$totalNumber sequence(s) with a total length of "
. $totalLength . " bp\n";
print ""
. scalar( keys( %uniqSpeciesLabels ) )
. " species labels defined\n";
}
}
######################## S U B R O U T I N E S ############################
##-------------------------------------------------------------------------##
## Use: my _displayLibraryTaxonomy( library_ref => value,
## taxonomy_ref => value );
##
## library_ref : A reference to a RepbaseEMBL object
## taxonomy_ref : A reference to a Taxonomy object
##
## Returns
## Prints the species tree for all repeat species in the
## RepeatMasker library.
##
##-------------------------------------------------------------------------##
sub _displayLibraryTaxonomy {
my %parameters = @_;
print ""
. ( &caller( 0 ) )[ 0 ] . "::"
. ( &caller( 0 ) )[ 3 ] . "( "
. @{ [ %parameters ] }
. "): Called\n"
if ( $DEBUG );
my $libRef = $parameters{'library_ref'};
my $taxRef = $parameters{'taxonomy_ref'};
my $seqCount = $libRef->getRecordCount();
my %uniqSpecies = ();
my %actualSpecies = ();
for ( my $i = 0 ; $i < $seqCount ; $i++ ) {
my $record = $libRef->getRecord( $i );
foreach my $name ( $record->getRMSpeciesArray() ) {
$name =~ s/_/ /g;
$name = $tax->isSpecies( $name );
$uniqSpecies{$name}++;
$actualSpecies{$name}++;
my @ancSpecs = $taxRef->getLineage( $name );
foreach my $ancSpec ( @ancSpecs ) {
next if ( $ancSpec eq $name );
$uniqSpecies{$ancSpec}++;
}
}
}
print "" . $taxRef->getTree( "root", \%uniqSpecies, \%actualSpecies ) . "\n";
}
sub match_class_pattern {
my ($db, $i, $classPattern) = @_;
my $match = 0;
if ( $db->isa( "FastaDB" ) ) {
if ( $ids[ $i ] =~ /\#$classPattern/i ) {
$match = 1;
}
} else {
my $record = $db->getRecord( $i );
if ( $record->getRMType() =~ /$classPattern/i ) {
$match = 1;
}
}
return $match;
}
1;