forked from Dfam-consortium/RepeatMasker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TRF.pm
executable file
·1101 lines (870 loc) · 27.8 KB
/
TRF.pm
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
##---------------------------------------------------------------------------##
## File:
## @(#) TRF.pm
## Author:
## Robert M. Hubley [email protected]
## Description:
## A wrapper class for the Tandem Repeat Finder ( TRF )
## program written by Gary Benson, Department of Biomathematical
## Sciences Mount Sinai School of Medicine.
##
#******************************************************************************
#* Copyright (C) Institute for Systems Biology 2004 Developed by
#* 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.
#*
#******************************************************************************
# Implementation Details:
#
# File = sequences input file
# Match = matching weight
# Mismatch = mismatching penalty
# Delta = indel penalty
# PM = match probability (whole number)
# PI = indel probability (whole number)
# Minscore = minimum alignment score to report
# MaxPeriod = maximum period size to report
# [options] = one or more of the following :
# -m masked sequence file
# -f flanking sequence
# -d data file
# -h suppress html output
# -r no redundancy elimination
#
# bless( {
# 'matchWeight' => 2,
# 'mismatchPenalty' => 7,
# 'delta' => 7,
# 'pm' => 80,
# 'pi' => 10,
# 'minScore' => 50,
# 'maxPeriod' => 500,
# 'maskedOutputFile' => '/users/bob/seq.fasta.masked',
# 'pathToEngine' => '/usr/local/bin/trf',
# 'workDir' => '/tmp',
# 'sequenceFile' => '/users/bob/seq.fasta'
# },
# 'TRF' );
#
###############################################################################
# ChangeLog
#
# $Log$
#
###############################################################################
# To Do:
#
#
=head1 NAME
TRF
=head1 SYNOPSIS
use TRF
my $trfEngine = TRF->new(
pathToEngine=>"/usr/local/bin/trf",
workDir => "/tmp" );
$trfEngine->setMatchWeight( 2 );
$trfEngine->setMismatchPenalty( 7 );
$trfEngine->setDelta( 7 );
$trfEngine->setPm( 80 );
$trfEngine->setPi( 10 );
$trfEngine->setMinScore( 50 );
$trfEngine->setMaxPeriod( 500 );
$trfEngine->setMaskedOutputFile( "/users/bob/seq.fasta.masked" );
$trfEngine->setSequenceFile( "/users/bob/seq.fasta" );
my $results = $trfEngine->search();
Usage:
=head1 DESCRIPTION
An encapsulation class for the trf search engine. It is capable
for running searches and returning the results as a perl array of
TRFSearchResult objects.
The trf parser built into this object captures several types of
data from the trf output files.
Indices Period Copy Cons Perc Perc Score A C G T Entropy Consensus
Size Number Size Matches Indels (0-2)
---------------------------------------------------------------------------------------------
11376 11412 16 2.3 16 95 0 65 40 8 18 32 1.80 GTAAAGATTTGCACAT
...
Indices = Indices of the repeat relative to the start of the sequence.
Period Size = Period size of the repeat.
Copy Number = Number of copies aligned with the consensus pattern.
Cons Size = Size of consensus pattern (may differ slightly from the period size).
Perc Matches = Percent of matches between adjacent copies overall.
Perc Indels = Percent of indels between adjacent copies overall.
Score = Alignment score.
A, C, G, T = Percent composition for each of the four nucleotides.
Entropy = Entropy measure based on percent composition.
Consensus = Consensus sequence
Limitations:
TRF can only accept one sequence in the input file. The input sequence is
limited to 5MB.
=head1 SEE ALSO
=over 4
TRFSearchResult
=back
=head1 COPYRIGHT
Copyright 2005 Institute for Systems Biology
=head1 AUTHOR
Robert Hubley <[email protected]>
=head1 INSTANCE METHODS
=cut
package TRF;
use strict;
use POSIX qw(:sys_wait_h);
use Data::Dumper;
use File::Basename;
use File::Spec;
use Carp;
use FileHandle;
use IPC::Open3;
use TRFSearchResult;
use SearchResult;
use SearchResultCollection;
# For timing only
#use Time::HiRes qw(gettimeofday);
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
require Exporter;
@ISA = qw(Exporter SearchEngineI);
@EXPORT = qw();
@EXPORT_OK = qw();
%EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
my $CLASS = "TRF";
my $DEBUG = 0;
##-------------------------------------------------------------------------##
## Constructor
##-------------------------------------------------------------------------##
sub new {
my $class = shift;
my %nameValuePairs = @_;
croak $CLASS
. "::new: Missing path to search engine!\n\n"
. "use \$searchEngine = $CLASS->new( pathToEngine=>\"/usr/local/"
. "bin/trf\")\n"
if ( not defined $nameValuePairs{'pathToEngine'} );
croak $CLASS
. "::new: Missing a working directory!\n\n"
. "use \$searchEngine = $CLASS->new( workDir=>\"/tmp\");\n"
if ( not defined $nameValuePairs{'workDir'} );
# Create ourself as a hash
my $this = {};
# Bless this hash in the name of the father, the son...
bless $this, $class;
$this->setPathToEngine( $nameValuePairs{'pathToEngine'} );
$this->setWorkDir( $nameValuePairs{'workDir'} );
return $this;
}
##-------------------------------------------------------------------------##
## Get and Set Methods
##-------------------------------------------------------------------------##
##-------------------------------------------------------------------------##
=head2 get_setPathToEngine()
Use: my $value = getPathToEngine( );
Use: my $oldValue = setPathToEngine( $value );
Get/Set the fully qualified path to the search engine
binary file.
=cut
##-------------------------------------------------------------------------##
sub getPathToEngine {
my $this = shift;
return $this->{'pathToEngine'};
}
sub setPathToEngine {
my $this = shift;
my $value = shift;
croak $CLASS. "::setPathToEngine( $value ): Program does not exist!"
if ( not -x $value || `which $value` );
my $oldValue = $this->{'pathToEngine'};
$this->{'pathToEngine'} = $value;
return $oldValue;
}
##---------------------------------------------------------------------##
=head2 get_setMatchWeight()
Use: my $value = getMatchWeight( );
Use: my $oldValue = setMatchWeight( $value );
Get/Set the MatchWeight attribute.
=cut
##---------------------------------------------------------------------##
sub getMatchWeight {
my $this = shift;
return $this->{'matchWeight'};
}
sub setMatchWeight {
my $this = shift;
my $value = shift;
my $oldValue = $this->{'matchWeight'};
$this->{'matchWeight'} = $value;
return $oldValue;
}
##---------------------------------------------------------------------##
=head2 get_setMismatchPenalty()
Use: my $value = getMismatchPenalty( );
Use: my $oldValue = setMismatchPenalty( $value );
Get/Set the MismatchPenalty attribute.
=cut
##---------------------------------------------------------------------##
sub getMismatchPenalty {
my $this = shift;
return $this->{'mismatchPenalty'};
}
sub setMismatchPenalty {
my $this = shift;
my $value = shift;
my $oldValue = $this->{'mismatchPenalty'};
$this->{'mismatchPenalty'} = $value;
return $oldValue;
}
##---------------------------------------------------------------------##
=head2 get_setDelta()
Use: my $value = getDelta( );
Use: my $oldValue = setDelta( $value );
Get/Set the Delta attribute.
=cut
##---------------------------------------------------------------------##
sub getDelta {
my $this = shift;
return $this->{'delta'};
}
sub setDelta {
my $this = shift;
my $value = shift;
my $oldValue = $this->{'delta'};
$this->{'delta'} = $value;
return $oldValue;
}
##---------------------------------------------------------------------##
=head2 get_setPm()
Use: my $value = getPm( );
Use: my $oldValue = setPm( $value );
Get/Set the Pm attribute.
=cut
##---------------------------------------------------------------------##
sub getPm {
my $this = shift;
return $this->{'pm'};
}
sub setPm {
my $this = shift;
my $value = shift;
my $oldValue = $this->{'pm'};
$this->{'pm'} = $value;
return $oldValue;
}
##---------------------------------------------------------------------##
=head2 get_setPi()
Use: my $value = getPi( );
Use: my $oldValue = setPi( $value );
Get/Set the Pi attribute.
=cut
##---------------------------------------------------------------------##
sub getPi {
my $this = shift;
return $this->{'pi'};
}
sub setPi {
my $this = shift;
my $value = shift;
my $oldValue = $this->{'pi'};
$this->{'pi'} = $value;
return $oldValue;
}
##---------------------------------------------------------------------##
=head2 get_setMinScore()
Use: my $value = getMinScore( );
Use: my $oldValue = setMinScore( $value );
Get/Set the MinScore attribute.
=cut
##---------------------------------------------------------------------##
sub getMinScore {
my $this = shift;
return $this->{'minScore'};
}
sub setMinScore {
my $this = shift;
my $value = shift;
my $oldValue = $this->{'minScore'};
$this->{'minScore'} = $value;
return $oldValue;
}
##---------------------------------------------------------------------##
=head2 get_setMaxPeriod()
Use: my $value = getMaxPeriod( );
Use: my $oldValue = setMaxPeriod( $value );
Get/Set the MaxPeriod attribute.
=cut
##---------------------------------------------------------------------##
sub getMaxPeriod {
my $this = shift;
return $this->{'maxPeriod'};
}
sub setMaxPeriod {
my $this = shift;
my $value = shift;
my $oldValue = $this->{'maxPeriod'};
$this->{'maxPeriod'} = $value;
return $oldValue;
}
##---------------------------------------------------------------------##
=head2 get_setMaskedOutputFile()
Use: my $value = getMaskedOutputFile( );
Use: my $oldValue = setMaskedOutputFile( $value );
Get/Set the MaskedOutputFile attribute.
=cut
##---------------------------------------------------------------------##
sub getMaskedOutputFile {
my $this = shift;
return $this->{'maskedOutputFile'};
}
sub setMaskedOutputFile {
my $this = shift;
my $value = shift;
my $oldValue = $this->{'maskedOutputFile'};
$this->{'maskedOutputFile'} = $value;
return $oldValue;
}
##---------------------------------------------------------------------##
=head2 get_setWorkDir()
Use: my $value = getWorkDir( );
Use: my $oldValue = setWorkDir( $value );
Get/Set the WorkDir attribute.
=cut
##---------------------------------------------------------------------##
sub getWorkDir {
my $this = shift;
return $this->{'workDir'};
}
sub setWorkDir {
my $this = shift;
my $value = shift;
my $oldValue = $this->{'workDir'};
$this->{'workDir'} = $value;
return $oldValue;
}
##---------------------------------------------------------------------##
=head2 get_setSequenceFile()
Use: my $value = getSequenceFile( );
Use: my $oldValue = setSequenceFile( $value );
Get/Set the SequenceFile attribute.
=cut
##---------------------------------------------------------------------##
sub getSequenceFile {
my $this = shift;
return $this->{'sequenceFile'};
}
sub setSequenceFile {
my $this = shift;
my $value = shift;
my $oldValue = $this->{'sequenceFile'};
$this->{'sequenceFile'} = $value;
return $oldValue;
}
##-------------------------------------------------------------------------##
## General Object Methods
##-------------------------------------------------------------------------##
##-------------------------------------------------------------------------##
=head2 getParameters()
Use: my $trfParamString = getParameters( );
Convert object parameters into TRF command line parameters.
=cut
##-------------------------------------------------------------------------##
sub getParameters {
my $this = shift;
# Test if engine is available
my $engine = $this->getPathToEngine();
if ( !defined $engine || !-e "$engine" ) {
croak $CLASS
. "::search: The path to the search engine is undefined or\n"
. "is set incorrectly: $engine\n";
}
# Generate parameter line
my $parameters = " ";
my $value;
my $parameterName = "";
if ( defined( $value = $this->getSequenceFile() ) ) {
if ( -e $value ) {
$value = File::Spec->rel2abs( $value );
$parameters .= "$value ";
}
}
if ( defined( $value = $this->getMatchWeight() ) ) {
$parameters .= "$value ";
$parameterName = "$value";
}
else {
$parameters .= "2 ";
$parameterName .= "2";
}
if ( defined( $value = $this->getMismatchPenalty() ) ) {
$parameters .= "$value ";
$parameterName .= ".$value";
}
else {
$parameters .= "7 ";
$parameterName .= ".7";
}
if ( defined( $value = $this->getDelta() ) ) {
$parameters .= "$value ";
$parameterName .= ".$value";
}
else {
$parameters .= "7 ";
$parameterName .= ".7";
}
if ( defined( $value = $this->getPm() ) ) {
$parameters .= "$value ";
$parameterName .= ".$value";
}
else {
$parameters .= "80 ";
$parameterName .= ".80";
}
if ( defined( $value = $this->getPi() ) ) {
$parameters .= "$value ";
$parameterName .= ".$value";
}
else {
$parameters .= "10 ";
$parameterName .= ".10";
}
if ( defined( $value = $this->getMinScore() ) ) {
$parameters .= "$value ";
$parameterName .= ".$value";
}
else {
$parameters .= "50 ";
$parameterName .= ".50";
}
if ( defined( $value = $this->getMaxPeriod() ) ) {
$parameters .= "$value ";
$parameterName .= ".$value";
}
else {
$parameters .= "500 ";
$parameterName .= ".500";
}
if ( defined $this->getMaskedOutputFile() ) {
$parameters .= "-m ";
}
return ( "$engine $parameters", $parameterName );
}
##-------------------------------------------------------------------------##
=head2 search()
Use: my ( $resultCode, $trfArrayRef ) = search( );
or
Use: my ( $resultCode, $trfArrayRef )
= search( minScore=> 300,
...
);
Run the search and return a perl array containing TRFSearchResult.pm
objects.
=cut
##-------------------------------------------------------------------------##
sub search {
my $this = shift;
my %nameValuePairs = @_;
if ( %nameValuePairs ) {
while ( my ( $name, $value ) = each( %nameValuePairs ) ) {
my $method = "set" . _ucFirst( $name );
unless ( $this->can( $method ) ) {
croak( $CLASS . "::search: Instance variable $name doesn't exist." );
}
$this->$method( $value );
}
}
my $seqFileName = "";
my $value = "";
if ( defined( $value = $this->getSequenceFile() ) ) {
if ( -e $value ) {
$value = File::Spec->rel2abs( $value );
$seqFileName = basename( $value );
}
else {
croak $CLASS
. "::search: Error...sequence file ($value) does not exist!\n";
}
}
else {
croak $CLASS. "::search: Error sequence file undefined!\n";
}
# Form the command line
my ( $cmdLine, $parameterName ) = $this->getParameters();
my $workDir = ".";
$workDir = $this->getWorkDir()
if ( $this->getWorkDir() ne "" );
my $POUTPUT = new FileHandle;
my $errFile;
do {
$errFile = "trfResults-" . time() . "-$$.err";
} while ( -e $workDir . "/$errFile" );
my $pid;
print $CLASS
. "::search() Invoking search engine as: cd $workDir; $cmdLine "
. " 2>$errFile |\n"
if ( $DEBUG );
# DISABLE: For timing only
#my $t0 = gettimeofday( );
$pid = open( $POUTPUT, "cd $workDir; $cmdLine 2>$errFile |" )
or die "Could not run: $cmdLine: $!\n";
my $outFile;
do {
$outFile = $workDir . "/trfResults-" . time() . "-$$.out";
} while ( -e $outFile );
open OUT, ">$outFile";
while ( <$POUTPUT> ) {
print OUT $_;
}
close OUT;
close $POUTPUT;
# DISABLE: For timing only
#if ( $DEBUG ) {
# my $t1 = gettimeofday( );
# my $elapsed = $t1 - $t0;
# print "TRF runtime: $elapsed secs\n"
#}
my $resultCode = $?;
# The output of TRF is complex. There are diferrent filename
# grammers for single sequence vs multiple sequence etc. There
# are single output files per sequence.
#
# ie. If the filename is "file.fa" then:
#
# Single sequence: file.fa.#.#.#.#.#.#.#.*.txt.html
# Where #'s are the algorithm parameters. The "*" is the
# index of the file if the file was broken up ( starting from 1 ).
#
# Multiple sequences: file.fa.s#.#.#.#.#.#.#.#.*.txt.html
# Where "s#" is the sequence number in file.fa and
# "#" are the algorithm parameters. "*" Dito
#
my $searchResultColl = SearchResultCollection->new();
if ( -e "$workDir/$seqFileName.$parameterName.summary.html" ) {
open IN, "<$workDir/$seqFileName.$parameterName.summary.html"
or croak $CLASS
. "::search: Summary file $workDir/$seqFileName.$parameterName"
. ".summary.html could not be opened!\n";
my @seqFileIds = ();
while ( <IN> ) {
if ( /TARGET=/ ) {
my @recs = split( /A>/ );
foreach my $rec ( @recs ) {
if ( $rec =~
/TARGET=\"\S+\.(s\d+)\.\d+\.\d+\.\d+\.\d+\.\d+\.\d+\.\d+\.\d+\.html\"\s+HREF/
)
{
push @seqFileIds, $1;
}
}
}
}
close IN;
unlink( "$workDir/$seqFileName.$parameterName.summary.html" )
unless ( $DEBUG );
# Multiple sequences exist
my $seqIdx = 1;
foreach my $seqIdx ( @seqFileIds ) {
my $fragIdx = 1;
while ( 1 ) {
if (
-e "$workDir/$seqFileName.$seqIdx.$parameterName.$fragIdx.txt.html" )
{
my $tmpSearchColl =
parseOutput( searchOutput =>
"$workDir/$seqFileName.$seqIdx.$parameterName.$fragIdx.txt.html"
);
$searchResultColl->addAll( $tmpSearchColl );
unlink(
"$workDir/$seqFileName.$seqIdx.$parameterName.$fragIdx.txt.html" )
unless ( $DEBUG );
if ( !$DEBUG
&& -e "$workDir/$seqFileName.$seqIdx.$parameterName.$fragIdx.html" )
{
unlink(
"$workDir/$seqFileName.$seqIdx.$parameterName.$fragIdx.html" );
}
}
else {
last;
}
$fragIdx++;
}
}
}
else {
my $fragIdx = 1;
while ( 1 ) {
if ( -e "$workDir/$seqFileName.$parameterName.$fragIdx.txt.html" ) {
my $tmpSearchColl =
parseOutput( searchOutput =>
"$workDir/$seqFileName.$parameterName.$fragIdx.txt.html" );
$searchResultColl->addAll( $tmpSearchColl );
unlink( "$workDir/$seqFileName.$parameterName.$fragIdx.txt.html" )
unless ( $DEBUG );
if ( !$DEBUG
&& -e "$workDir/$seqFileName.$parameterName.$fragIdx.html" )
{
unlink( "$workDir/$seqFileName.$parameterName.$fragIdx.html" );
}
}
else {
last;
}
$fragIdx++;
}
}
if ( defined $searchResultColl
&& $searchResultColl->size() > 0 )
{
# The final result collection should be sorted by
# queryname and secondarily by query start position.
$searchResultColl->sort(
sub ($$) {
$_[ 0 ]->getQueryName cmp $_[ 1 ]->getQueryName()
|| $_[ 1 ]->getQueryStart() <=> $_[ 0 ]->getQueryStart();
}
);
}
## Detect signal failures only
if ( ($resultCode & 127) || ($resultCode == -1) ) {
return ( $resultCode, $searchResultColl, $outFile, "$workDir/$errFile" );
}else {
unlink "$workDir/$errFile" if ( -e "$workDir/$errFile" );
unlink $outFile if ( -e $outFile );
return ( $resultCode, $searchResultColl );
}
}
##---------------------------------------------------------------------##
=head1 CLASS METHODS
=cut
##---------------------------------------------------------------------##
##---------------------------------------------------------------------##
=head2 parseOutput()
Use: my $trfResultArrayRef = parseOutput(
searchOutput => $filename|$FH,
);
Parse the result of a search and return a perl array containing
TRFSearchResult objects.
=cut
##---------------------------------------------------------------------##
sub parseOutput {
my %nameValueParams = @_;
croak $CLASS. "::parseOutput() missing searchOutput parameter!\n"
if ( !exists $nameValueParams{'searchOutput'} );
my $TRFFILE;
if ( ref( $nameValueParams{'searchOutput'} ) !~ /GLOB|FileHandle/ ) {
print $CLASS
. "::parseOutput() Opening file "
. $nameValueParams{'searchOutput'} . "\n"
if ( $DEBUG );
open $TRFFILE, $nameValueParams{'searchOutput'}
or die $CLASS
. "::parseOutput: Unable to open "
. "results file: $nameValueParams{'searchOutput'} : $!";
}
else {
$TRFFILE = $nameValueParams{'searchOutput'};
}
my $resultColl = SearchResultCollection->new();
my $consensus = "";
my $nextLineIsConsensus = 0;
my $qryID = "";
my $qryLength = 0;
my $qryStart = 0;
my $qryEnd = 0;
my $sbjSeq = "";
my $qrySeq = "";
my $score = 0;
my $period = 0;
my $copyNum = 0;
my $conSize = 0;
my $matches = 0;
my $mismatches = 0;
my $indels = 0;
while ( <$TRFFILE> ) {
#print "$_\n";
if ( $nextLineIsConsensus ) {
s/[\n\r]+//g;
$consensus = $_;
# Done...save result
$qrySeq =~ s/\s//g;
$sbjSeq =~ s/\s//g;
my $sbjGaps = ( $sbjSeq =~ tr/-/-/ );
my $sbjLength = length( $sbjSeq ) - $sbjGaps;
my $percDiv =
sprintf( "%4.2f", $mismatches * 100 / ( $qryEnd + 1 - $qryStart ) );
my $qgap = $qrySeq =~ tr/-/-/;
my $sgap = $sbjSeq =~ tr/-/-/;
my $percIns =
sprintf( "%4.2f", $sgap * 100 / ( ( $sbjLength + 1 ) - 1 ) );
my $percDel =
sprintf( "%4.2f", $qgap * 100 / ( ( $qryEnd + 1 ) - $qryStart ) );
my $result = TRFSearchResult->new(
queryName => $qryID,
queryStart => $qryStart,
queryEnd => $qryEnd,
queryRemaining => ( $qryLength - $qryEnd ),
queryString => $qrySeq,
subjString => $sbjSeq,
orientation => "+",
subjName => "($consensus)n#Simple_repeat",
subjStart => 1,
subjEnd => $sbjLength,
subjRemaining => 0,
pctDiverge => $percDiv,
pctInsert => $percIns,
pctDelete => $percDel,
score => $score,
period => $period,
copyNumber => $copyNum,
consensusSize => $conSize
);
$resultColl->add( $result );
#print "RESULT: " .
# $result->toStringFormatted( SearchResult::AlignWithQuerySeq ) .
# "\n";
# Clear fields
$consensus = "";
$nextLineIsConsensus = 0;
$qryStart = 0;
$qryEnd = 0;
$sbjSeq = "";
$qrySeq = "";
$score = 0;
$period = 0;
$copyNum = 0;
$conSize = 0;
$matches = 0;
$mismatches = 0;
$indels = 0;
next;
}
# Look for sequence identifier line
if ( /^Sequence:\s+(\S+).*$/ ) {
$qryID = $1;
}
elsif ( /^Length:\s+(\d+).*$/ ) {
$qryLength = $1;
}
elsif ( /^\s+Indices:\s+(\d+)--(\d+)\s+Score:\s+(\d+).*$/ ) {
$qryStart = $1;
$qryEnd = $2;
$score = $3;
}
elsif (
/^\s+Period size:\s+(\d+)\s+Copynumber:\s+([\d\.]+)\s+Consensus size:\s+(\d+).*$/
)
{
$period = $1;
$copyNum = $2;
$conSize = $3;
}
elsif ( /\s+(\d+)\s+([ACGTBDHVRYKMSWXN\-\s]+)\s*$/ ) {
if ( $1 == 1 ) {
if ( $qryStart == 1 && $qrySeq eq "" ) {
# No pre-padding.
$qrySeq .= $2;
}
elsif ( $qrySeq ne "" ) {
$sbjSeq .= $2;
}
next;
}
next if ( $1 < $qryStart || $1 > $qryEnd );
$qrySeq .= $2;
}
elsif ( /^Matches:\s+(\d+),\s+Mismatches:\s+(\d+),\s+Indels:\s+(\d+)\s*$/ )
{
$matches = $1;
$mismatches = $2;