-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathdeal_fasta.pl
executable file
·1936 lines (1771 loc) · 68.5 KB
/
deal_fasta.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
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/env perl
## #!/usr/bin/perl
### Author email : hs738@cornell.edu or biosunhh@gmail.com
### Deal with fasta file, learn from fasta.pl done by fanwei.
###version 1.0 2006-7-12
### copy the function -cut in fasta.pl written by fanwei.
### add a function for picking out sequences according to annotations.
### add a function sample picking transfered from fasta.pl.
###version 1.1 2006-7-13
### add a function to pick out fragment from a single sequence.From fasta.pl
### add a funcionn to change letters to capital or lower.Copy from fasta.pl
###version 1.1.1 2006-7-24
### add a parameter -nres to regexps.
### add the function get_attribute() from fasta.pl. It is very useful.
### 2006-7-25 change the way to read in fasta to fit multi files reading.And to fit STDIN for pipeline procedure.
###version 1.1.2 2006-7-27
### begin to add a function to locate a site(a special sequence) to a database sequence.
###version 1.1.3 2006-12-27 16:12
### change the method to calculate GC content, exclude runs of 25 Ns region.
### 2007-1-9 9:43 add a reverse complemented strand fragment pick.
### 2007-06-27 add a para "cut_dir" for cut funtion, to define which dir to write to.
### 2007-07-18 edit qual and frag func; add a new parsing method for para;
### 2007-08-02 add a para for calc N50;
### 2007-08-03 add a function to chop key's unexpected words.
### 2007-8-31 9:19 Here i have updated the computing method for function get_attribute(), it is really a new test for me! Well, it can save very much more time when calculating large number of sequences; And i give out a fuction get_fasta_seq(), which should be added to other functions; Well, I love it! So i want to give a version to this perl script.
### 2007-9-10 16:16 edit N50 subroutine.
### 2007-9-11 11:29 更新了一些函数的写法, 包括cut_fasta, 用法上还差不多保留, 增加了cut_prefix参数, 更改了自动生成文件名方式, 支持STDIN输入;
### 2007-9-11 16:00 此次更新结束;
### 2007-9-28 edit sub rcSeq;
### 2007-11-29 add a output for qual_c
### 2008-1-4 13:01:38 edit get_fasta_seq subroutine to correct error when the former sequence is empty.
### 2009-1-12 edit sub rootine "frag" to abtain para like "-10--5"
### 2011-1-18 add more annotation for rcseq subroutine.
### 2013-08-01 Default of "-listNum" changed to "min".
### 2013-10-30 Edit &siteList to return also match sequence.
### 2013-10-30 Add functions : mask_seq_by_list ; extract_seq_by_list ;
### 2013-11-01 Add function : extract_seq_by_list; And edit some small bug in mask_seq_by_list() with warnings.
### 2013-11-01 Edit sub openFH() to deal with .gz/.bz2 files.
### 2014-02-25 Add sub keep_len to extract .fa sequences by length.
### 2014-03-12 Add -listSeq to control if output match_seq for -listSite.
### 2015-04-09 Add -rmDefinition to keep only sequence ID in the definition line.
### 2015-04-10 Reorder sequences according to an input seqID list.
### 2015-06-26 Use -joinR12 to join to .fasta R1/R2 files.
### 2016-01-21 Use '-sep_soapdenovo_ctg_name' to separate soapdenovo contig names.
### 2016-05-02 Add '-scf2ctg out_prefix' to separate scaffolds into contigs.
### 2016-08-30 Add '-jn_byID' to join sequences from different files with the same IDs.
### 2016-08-30 Replace AA seq with corresponding Nucl sequences.
### 2019-01-16 Change the definition of -frame; It means the first base's frame (+|-(1/2/3)) before, but now it means the first base position of the first frame, which is same to blastx and transeq.
### 2019-08-09 Add -chop_agp to mask -chop_info
### 2022-01-19 Change the meaning of '-frame', and use -blastx_frame to fit blastx and transeq.
### Without -blastx_frame, it fits my deal_gff3.pl output CDS.
### With -blastx_frame, it fits the blastx and transeq output.
### 2022-10-26 Add -replaceSeq to change sequence according to a list.
use strict;
use warnings;
use File::Spec; # File::Spec->catfile("","",...);
use Getopt::Long;
use fileSunhh;
use LogInforSunhh;
use mathSunhh;
use fastaSunhh;
my %opts;
sub usage {
my $version = 'v1.0';
$version = 'v1.1';
$version = 'v1.2'; ### 2007-10-30 10:42:44 edit sub site_list(), add a output value "Match length".
$version = 'v1.3'; ### 2008-1-4 13:01:38 edit get_fasta_seq subroutine to correct error when the former sequence is empty. And add a little function to move out repeat sequences according to key of them. 2008-1-4 13:15:51
$version = 'v1.4'; ### 2009-1-12 edit sub rootine "frag" to abtain para like "-10--5"
$version = 'v1.5'; ### Merge tableSeq.pl and extractSeq.pl functions.
my $last_time = '2013-11-01';
print STDOUT <<HELP;
#******* Instruction of this program *********#
Introduction:Deal with fasta format file, learn from fasta.pl done by fanwei.Should be used in Linux ENV.
Last modified: $last_time
Version: $version
Usage: $0 <fasta_file | STDIN>
-help output help information;
-cut<num> cut fasta file with the specified number of sequences in each subfile.Cited from fasta.pl.
A little change to fit different input directory type;
-cut_size<num> Use this when need to control the maximal total bp size in separated files.
-cut_prefix prefix for cutted files; Default 'pre';
-cut_dir cutted files in this dir if given.
-res<regexps> pick out sequences whose annotations(key+definition) match the regular expression.Output to STDOUT;
-nres<regexps> opposite to -res.
-uniqSeq [boolean]. remove repeat sequences according to their keys.
-uniqSeq_bySeq [Boolean]. Remove repeat sequences according to their sequences.
-sample<num-num> output sequences according to sequence order.0 for 1st or last one;
-frag<start-end> output a fragment of the squence in single sequence fasta file;Start position is 1; May be s1-e1:s2-e2...
-frag_width<num> number of characters each line when output;
-frag_head whether print out the title of the sequences;
-frag_c give out complemented string as NUCL;
-frag_r give out reverse string;
-upper/lower upperize/lowerize charaters of all the sequences;
-table If given,this program will only give out annotations of the output sequences, with '|'
transfered to "\\t"; not so useful;
-max_num<num> max record numbers output for every file. No effection to -cut function;At least one.
-attribute<item> head:seq:key:len:GC:mask:AG:GC3:seq_line, output atrribution of sequences.
seq_line : Similar to 'seq', but all blanks are removed.
-GC_excln<num> when calculating CG content, it will calculate total length excluding runs of <num>
Ns or more. Default 25. We only exclude Ns, no dealing with Xs!
-listSite<sequence> a sequence for searcing.
-listNum [Min|Max]
-listBoth Both strands.
-listSeq [Boolean] Output match part of reference sequence if given.
-N50 calc N50;
-N50_minLen [INT] Minimum length of sequences used for calculating N50.
-N50_GenomSize [INT] Estimated genome size used for calculate NG50;
-N50_levels [String] Quantile levels used to calculate. Default [00 05 10 15 25 50 60 70 80 90 95 96 97 98 99 100]
-N50_byNumber [Boolean]
-chopKey 'expr'
-startCodonDist calc input CDSs\' start codon usage distribution;
-comma3 output only a comma separated list (no spaces) of atg, gtg, ttg start proportions, in that order
-rmDefinition [Boolean] Remove definition except sequence ID.
-maskByList [Boolean] A trigger for masking .fasta sequences by list.
-maskList [Filename] regions to be masked, in format: [seqID Start End]
-maskType [X/N/uc/lc] Telling how to modify regions listed.
X/N - Changed to X/N;
uc/lc - Changed to upcase/lowercase;
-elseMask [Boolean] Mask region not listed instead.
-drawByList [Boolean] A trigger for extracting .fasta sequences by list.
-drawList [Filename] regions to be extracted, in format: [RawSeqID Start End Strand(+/-) NewSeqID]
-drawLcol [String] Give the columns to use. Cols(RawSeqID,Start,End,Strand,NewSeqID).
Default "0,1,2,3,4"
-drawIDmatch [Boolean] Seqs whose IDs contain RawSeqID from drawList will be extracted.
-drawWhole [Boolean] Ignore position (and strand) information if given. Retrieve the whole sequence.
-dropMatch [Boolean] Drop seqs with matching ID. Only useful with -drawWhole .
-reorderByList [Filename] A file with the first column as sequence ID list.
Then only output ordered fasta sequences in the list.
-keep_len "min_len-max_len". Extract sequences whose lengths are between min_len and max_len.
-baseCount [Boolean] Calculate A/T/G/C/N numbers in sequences.
-baseCountByWind [filename] File of window list. Format: "ChromID\\tWindS\\tWindE\\n"
-fa2fq [Boolean] Transform fasta format to fastq format.
-fa2fqQChar [Character] Character used for quality line in fastq output.
-fq2fa [Boolean] Transform fastq format to fasta format.
-replaceID [Boolean] Replace fasta seqID
-replaceIDlist [filename] file recording oldID and newID.
-replaceIDcol [0,1] "oldID_col,newID_col"
-replaceIDadd [Boolean] keep old ID in head line if given.
-replaceSeq [Boolean] Replace fasta sequence
-replaceSeqList [filename] In table with 'seqID, start, end, new_seqID, sequence_to_use'.
-chop_seq [Boolean] Chop each sequences to small pieces.
-chop_len [100] Length of small pieces.
-chop_step [chop_len] Distance of two closest pieces' start position.
-chop_min [0] Minimum length of pieces.
-chop_noPos [Boolean] Default add raw position of pieces.
-chop_info [Boolean] If given, the output is a table instead of a sequence file.
-chop_agp [Boolean] If given, the output is an AGP file. This overwrites -chop_info option.
-joinR12 [Boolean] Input files as paired, and join R1/R2 reads in a same stream.
-rna2dna [Boolean] Transform 'U' to 'T'.
-rmTailXN [Boolean] Remove continuous [\*XNxn]+ from the tail of sequence.
-rmTailX_prot [Boolean] Remove continuous [\*X]+ from the tail of sequence.
-scf2ctg ['out_prefix'] Output out_prefix.ctg.fa and out_prefix.ctg2scf.agp files.
-sep_soapdenovo_ctg_name [Boolean] Separate soapdenovo contig names.
-jn_byID [Boolean] for joining aln.fa
-aa2cds [filename_cds.fa] Get AA positioned CDS sequences.
-cds2aa [Boolean]
-codon_table [1] Could be 1,2,3,4,5,6,7,8,9,10
-infer_frame [Boolean] Infer frame from the definition line by '[frame=\\d+]' format information. This will overwrite -frame option;
-frame [1] Could be 1,2,3 (plus strand), -1,-2,-3 (reverse strand). Different meaning of blastx and transeq by default.
-blastx_frame [Boolean] This will change the way to parse -frame and -infer_frame. Required to fit blastx and transeq frames.
-loc_4d [Boolean]
#******* Instruction of this program *********#
HELP
exit (1);
}
GetOptions(\%opts,"help!",
"cut:i","details!","cut_dir:s","cut_prefix:s", "cut_size:i",
"max_num:i",
"res:s","nres:s","table!",
"attribute:s","GC_excln:i",
"sample:s",
"frag:s","frag_width:i","frag_head!","frag_c!","frag_r!",
"qual:s","qual_width:i","qual_r!","qual_head!",
"listSite:s","listNum:s","listBoth!","listSeq!",
"N50!", "N50_minLen:i", "N50_GenomSize:i", "N50_levels:s", "N50_byNumber!",
"chopKey:s","startCodonDist!","comma3!",
"rmDefinition!",
"uniqSeq!", "uniqSeq_bySeq!",
"upper!","lower!",
"maskByList!", "maskList:s", "maskType:s", "elseMask!",
"drawByList!", "drawList:s", "drawLcol:s", "drawWhole!", "drawIDmatch!", "dropMatch!",
"keep_len:s",
"baseCount!", "baseCountByWind:s",
"fa2fq!", "fa2fqQChar:s", "fq2fa!",
"replaceID!", "replaceIDlist:s", "replaceIDcol:s", "replaceIDadd!",
"replaceSeq!", "replaceSeqList:s",
"reorderByList:s",
"chop_seq!", "chop_len:i", "chop_step:i", "chop_min:i", "chop_noPos!", "chop_info!", "chop_agp!",
"joinR12!",
"rna2dna!",
"rmTailXN!",
"rmTailX_prot!",
"scf2ctg:s", # out_prefix
"sep_soapdenovo_ctg_name!",
"jn_byID!",
"aa2cds:s", # filename_cds.fa
"cds2aa!",
"codon_table:i", "frame:i", "infer_frame!", "blastx_frame!",
"loc_4d!",
);
&usage if ($opts{"help"});
!@ARGV and -t and &usage;
(keys %opts) == 0 and &usage;
#****************************************************************#
#--------------Main-----Function-----Start-----------------------#
#****************************************************************#
# Making File handles for reading;
our @InFp = () ; # 2007-8-29 16:07 全局变量!
if ( !@ARGV )
{
@InFp = (\*STDIN);
}
else
{
for (@ARGV) {
push( @InFp, &openFH($_,'<') );
}
}
## Global settings
my %goodStr = qw(
+ F
1 F
f F
F F
plus F
forward F
- R
-1 R
r R
minus R
reverse R
R R
);
my %codon_tbl;
# &setup_codon_tbl();
$opts{'codon_table'} //= 1;
$opts{'frame'} //= 1;
&cut_fasta() if( (exists $opts{cut} && $opts{cut}) || (exists $opts{'cut_size'} && $opts{'cut_size'}) );
&res_match_seqs() if( (defined $opts{res} and $opts{res} ne '') || (defined $opts{nres} and $opts{nres} ne '') );
&get_sample() if(defined $opts{"sample"} and $opts{sample} ne '');
&frag() if(defined $opts{frag} and $opts{frag} ne '');
&upper_lower() if($opts{upper} || $opts{lower});
&get_attribute() if(defined $opts{attribute} and $opts{attribute} ne '');
&site_list() if(exists $opts{listSite});
&qual() if (defined $opts{qual} and $opts{qual} ne '');
&N50() if ($opts{N50});
&editkey() if ( exists $opts{chopKey} );
&rmDefinition() if ( exists $opts{'rmDefinition'} ) ;
&startCodonDist() if ( defined $opts{startCodonDist} and $opts{startCodonDist} );
&uniqSeq() if ( $opts{uniqSeq} ) ;
&uniqSeq_bySeq() if ( $opts{'uniqSeq_bySeq'} );
&mask_seq_by_list() if ( $opts{maskByList} );
&extract_seq_by_list() if ( $opts{drawByList} );
&keep_len() if ( defined $opts{keep_len} );
&baseCount() if ( $opts{baseCount} );
&fa2fq() if ( $opts{fa2fq} );
&fq2fa() if ( $opts{fq2fa} );
&replaceID() if ( $opts{'replaceID'} );
&replaceSeq() if ( $opts{'replaceSeq'} );
&reorderSeq($opts{'reorderByList'}) if ( defined $opts{'reorderByList'} );
&chop_seq() if ( $opts{'chop_seq'} );
&joinR12() if ( $opts{'joinR12'} );
&rna2dna() if ( $opts{'rna2dna'} );
&rmTailXN() if ( $opts{'rmTailXN'} );
&rmTailX_prot() if ( $opts{'rmTailX_prot'} );
&sep_ctg_name() if ( $opts{'sep_soapdenovo_ctg_name'} );
&scf2ctg_fas() if ( defined $opts{'scf2ctg'} );
&jn_byID() if ( $opts{'jn_byID'} );
&aa2cds() if ( defined $opts{'aa2cds'} );
&cds2aa() if ( $opts{'cds2aa'} );
&cds2aa() if ( $opts{'loc_4d'} );
for (@InFp) {
close ($_);
}
#test
#test
#****************************************************************#
#--------------Subprogram------------Start-----------------------#
#****************************************************************#
sub cds2aa {
my $frame = $opts{'frame'};
my %bb_4d;
if ( $opts{'loc_4d'} ) {
my %t = %{ &fastaSunhh::get_4d_codon($opts{'codon_table'}) };
%bb_4d = map { uc(substr($_, 0, 2)) => $t{$_}[0] } keys %t;
print STDOUT join("\t", qw/geneID cds_posi codon aa_posi aa/)."\n";
}
for (my $i=0; $i<@InFp; $i++) {
my $fh1 = $InFp[$i];
RD:
while ( !eof($fh1) ) {
for ( my ($relHR1, $get1) = &get_fasta_seq($fh1); defined $relHR1; ($relHR1, $get1) = &get_fasta_seq($fh1) ) {
$relHR1->{'seq'} =~ s/[\s]//g; # Keep '-' for position.
$relHR1->{'len'} = length($relHR1->{'seq'});
my $t_seq = uc($relHR1->{'seq'});
$t_seq =~ tr!X!N!;
my $t_frame = $frame;
if ( $opts{'infer_frame'} and $relHR1->{'head'} =~ m!\[frame=([+-]?\d+)\]!i ) {
$t_frame = $1;
$t_frame =~ m!^[+-]?(1|2|3)$! or do { &tsmsg("[Wrn] Skip bad frame information [$t_frame]\n"); $t_frame = $frame; };
}
unless ($opts{'blastx_frame'}) {
if ($t_frame > 0) {
$t_frame == 2 and $t_seq = substr($t_seq, 2); # y = (4-x) % 3;
$t_frame == 3 and $t_seq = substr($t_seq, 1);
} elsif ($t_frame < 0) {
&rcSeq(\$t_seq, 'rc');
$t_frame == -2 and $t_seq = substr($t_seq, 2);
$t_frame == -3 and $t_seq = substr($t_seq, 1);
} else {
&stopErr("[Err] Bad frame number [$t_frame]\n");
}
} else {
if ($t_frame > 0) {
$t_frame > 1 and $t_seq = substr($t_seq, $t_frame-1);
} elsif ($t_frame < 0) {
&rcSeq(\$t_seq, 'rc');
$t_frame < -1 and $t_seq = substr($t_seq, -$t_frame-1);
} else {
&stopErr("[Err] Bad frame number [$t_frame]\n");
}
}
my $t_len_0 = length($t_seq);
if ( $t_len_0 > 0 ) {
my $aa_seq = '';
my $t_len = $t_len_0;
my $t_res = $t_len_0 % 3;
$t_res > 0 and $t_seq .= ( 'N' x (3-$t_res) );
$t_len = length($t_seq);
if ( $opts{'loc_4d'} ) {
my $aa_idx = 0;
for (my $j=0; $j<$t_len; $j+=3) {
$j+3 > $t_len_0 and last;
$aa_idx ++;
my $bb = substr($t_seq, $j, 2);
defined $bb_4d{$bb} or next;
unless ($opts{'blastx_frame'}) {
my $pp = '';
$t_frame == 1 and $pp = $j+3;
$t_frame == 2 and $pp = 2 + $j+3;
$t_frame == 3 and $pp = 1 + $j+3;
$t_frame < 0 and $pp = $t_len_0-($j+3)+1;
print STDOUT join("\t", $relHR1->{'key'},, $pp, substr($t_seq, $j, 3), $aa_idx, $bb_4d{$bb})."\n";
} else {
# $t_frame can be only 1/2/3/-1/-2/-3
if ($t_frame > 0) {
print STDOUT join("\t", $relHR1->{'key'}, ($t_frame-1)+$j+3, substr($t_seq, $j, 3), $aa_idx, $bb_4d{$bb})."\n";
} else {
# $t_frame < 0
print STDOUT join("\t", $relHR1->{'key'}, $t_len_0-($j+3)+1, substr($t_seq, $j, 3), $aa_idx, $bb_4d{$bb})."\n";
}
}
}
} else {
for (my $j=0; $j<$t_len; $j+=3) {
my $bbb = substr($t_seq, $j, 3);
my ($aa)= &fastaSunhh::bbb2aa($bbb, $opts{'codon_table'});
$aa_seq .= $aa;
}
my $dispR = &Disp_seq(\$aa_seq, $opts{'frag_width'});
print STDOUT ">$relHR1->{'head'} [frame=$t_frame]\n$$dispR";
undef($dispR);
}
} else {
&tsmsg("[Wrn] sequence [$relHR1->{'key'}] has zero length.\n");
print STDOUT ">$relHR1->{'head'} [frame=$t_frame]\n\n";
}
}
}#End while() RD:
}
return;
}# cds2aa()
sub aa2cds {
&setup_codon_tbl();
my %stop_codon;
for my $tc (qw/TAA TGA TAG/) {
$stop_codon{$tc} = 1;
}
my %cds_seq;
my $fh_cds = &openFH( $opts{'aa2cds'} , '<' );
while (<$fh_cds>) {
if ( m/^\s*\>\s*(\S+)/ ) {
$cds_seq{'tk'} = $1;
$cds_seq{ 'seq' }{ $cds_seq{'tk'} } = '';
} else {
$cds_seq{ 'seq' }{ $cds_seq{'tk'} } .= $_;
}
}
close($fh_cds);
delete($cds_seq{'tk'});
for my $tk (keys %{$cds_seq{'seq'}}) {
$cds_seq{'seq'}{$tk} =~ s![\s\-]!!g;
$cds_seq{'seq'}{$tk} = uc($cds_seq{'seq'}{$tk});
$cds_seq{'len'}{$tk} = length($cds_seq{'seq'}{$tk});
}
my %prot_seq;
$prot_seq{'nn'} = 0;
for (my $i=0; $i<@InFp; $i++) {
my $fh1 = $InFp[$i];
while (<$fh1>) {
if ( m/^\s*\>\s*(\S+)/ ) {
$prot_seq{'tk'} = $1;
$prot_seq{'seq'}{$prot_seq{'tk'}} = '';
$prot_seq{'nn'} ++;
$prot_seq{'ord'}{ $prot_seq{'tk'} } = $prot_seq{'nn'};
} else {
$prot_seq{'seq'}{$prot_seq{'tk'}} .= $_;
}
}
delete($prot_seq{'tk'});
}
delete($prot_seq{'nn'});
for my $tk (sort { $prot_seq{'ord'}{$a} <=> $prot_seq{'ord'}{$b} } keys %{$prot_seq{'seq'}}) {
unless ( defined $cds_seq{'seq'}{ $tk } ) {
&tsmsg("[Err] Skip prot_seq [$tk] because of lack of cds seq.\n");
next;
}
$prot_seq{'seq'}{$tk} =~ s!\s!!g;
$prot_seq{'seq'}{$tk} = uc($prot_seq{'seq'}{$tk});
my @aa = split(//, $prot_seq{'seq'}{$tk});
my $aa2cds_seq = '';
my $aa_pos = -1;
for (my $j=0; $j<@aa; $j++) {
if ($aa[$j] eq '-') {
$aa2cds_seq .= "---";
} else {
$aa_pos ++;
my $bbb = substr( $cds_seq{'seq'}{$tk}, $aa_pos*3, 3 );
my $bbb_len = length($bbb);
$bbb_len > 0 or &stopErr("[Err] Failed to extract bbb at [$tk " . ($aa_pos*3+1) . "]\n");
if ( $bbb_len < 3 ) {
my $addN = 'N' x (3-$bbb_len);
$bbb .= $addN;
}
my ($curr_bbb2aa) = &fastaSunhh::bbb2aa( $bbb, $opts{'codon_table'} );
$curr_bbb2aa eq uc($aa[$j]) or &stopErr("[Err] Inconsistency between AA and CDS at seq_pos=$j [aa_pos=$aa_pos] [$aa[$j] vs $curr_bbb2aa vs $bbb]\n");
$aa2cds_seq .= "$bbb";
}
}
if ( ($aa_pos+1)*3 < $cds_seq{'len'}{$tk} ) {
my $res = $cds_seq{'len'}{$tk} % 3;
my $t_seq = $cds_seq{'seq'}{$tk};
$res > 0 and $t_seq .= ( 'N' x (3-$res) );
my $t_pos = length($t_seq)/3-1;
for (my $ip = $t_pos; $ip > $aa_pos; $ip--) {
my $bbb = substr( $t_seq, $ip*3, 3 );
my ($curr_bbb2aa) = &fastaSunhh::bbb2aa( $bbb, $opts{'codon_table'} );
$curr_bbb2aa eq '*' or $curr_bbb2aa eq 'X' or &stopErr("[Err] CDS seq of [$tk] is longer than prot seq.\n");
}
}
$aa2cds_seq =~ s!(.{60})!$1\n!g;
chomp($aa2cds_seq);
print STDOUT ">$tk\n$aa2cds_seq\n";
}
}# aa2cds()
# 2016-08-30
sub jn_byID {
my %jn_seq;
my $nn = 0;
for (my $i=0; $i<@InFp; $i++) {
my $fh1 = $InFp[$i];
my ($k, %ks);
RD:
while ( !eof($fh1) ) {
$_ = readline($fh1);
chomp;
if (m/^\s*\>\s*(\S+)/) {
$k = $1;
$ks{$k} = '';
} else {
$ks{$k} .= $_;
}
}
# close($fh1);
for my $tk (keys %ks) {
$ks{$tk} =~ s!\s!!g;
unless (defined $jn_seq{'cnt'}{$tk}) {
$jn_seq{'ord'}{$tk} = $nn;
$nn ++;
}
$jn_seq{'seq'}{$tk} .= $ks{$tk};
$jn_seq{'cnt'}{$tk} ++;
}
}
for my $tk (sort { $jn_seq{'ord'}{$a} <=> $jn_seq{'ord'}{$b} } keys %{$jn_seq{'cnt'}}) {
unless ( $jn_seq{'cnt'}{$tk} == $#InFp+1 ) {
&tsmsg("[Err] Skip not fully supported seq [$tk] [cnt=$jn_seq{'cnt'}{$tk}]\n");
next;
}
$jn_seq{'seq'}{$tk} =~ s!(.{100})!$1\n!g;
chomp( $jn_seq{'seq'}{$tk} );
print STDOUT ">$tk\n$jn_seq{'seq'}{$tk}\n";
}
}# jn_byID ()
# 2016-05-02
sub scf2ctg_fas {
$opts{'scf2ctg'} //= 'out_prefix';
my $oFh_1 = &openFH( "$opts{'scf2ctg'}.ctg.fa", '>' );
my $oFh_2 = &openFH( "$opts{'scf2ctg'}.ctg2scf.agp", '>' );
for (my $i=0; $i<@InFp; $i++) {
my $fh1 = $InFp[$i];
RD:
while ( !eof($fh1) ) {
for ( my ($relHR1, $get1) = &get_fasta_seq($fh1); defined $relHR1; ($relHR1, $get1) = &get_fasta_seq($fh1) ) {
$relHR1->{'seq'} =~ s/\s//g;
my $scfID = $relHR1->{'key'};
my $num = 0;
pos( $relHR1->{'seq'} ) = 0;
my @agp_lines;
while ( $relHR1->{'seq'} =~ m/\G(?:.*?)([^Nn]+)/gs ) {
$num ++;
my ($s, $e, $len) = ($-[1]+1, $+[1], $+[1]-$-[1]);
my $ctgID = "${scfID}_$num";
print {$oFh_1} ">$ctgID [$s,$e,$len]\n$1\n";
if ( scalar(@agp_lines) > 0 ) {
my $tp = $agp_lines[-1];
push(@agp_lines, [ $scfID, $tp->[2]+1, $s-1, $tp->[3]+1, 'N', $s-1-$tp->[2], 'scaffold', 'yes', 'paired-ends' ]);
$tp = $agp_lines[-1];
push(@agp_lines, [ $scfID, $s, $e, $tp->[3]+1, 'W', $ctgID, 1, $e-$s+1, '+' ]);
} else {
push(@agp_lines, [ $scfID, $s, $e, 1, 'W', $ctgID, 1, $e-$s+1, '+' ]);
}
pos($relHR1->{'seq'}) = $+[1];
}
for my $t_a (@agp_lines) {
print {$oFh_2} join("\t", @$t_a)."\n";
}
}
}
}
exit(0);
return();
}# scf2ctg ()
# 2016-01-21
sub sep_ctg_name {
print STDOUT join("\t", qw/key len coverage/)."\n";
for (my $i=0; $i<@InFp; $i+=1) {
my $fh1 = $InFp[$i];
while ( <$fh1> ) {
m/^>(\S+)/ or next;
m/^>(\S+)\s+length\s+(\d+)\s+cvg_([\d.]+)_tip_\d+\s*$/ or die "Bad name: $_\n";
my ($tk, $tl, $tc) = ($1, $2, $3);
print STDOUT join("\t", $tk, $tl, $tc)."\n";
}
}
return;
}# sep_ctg_name()
# 2015-11-24
sub rmTailX_prot {
for (my $i=0; $i<@InFp; $i+=1) {
my $fh1 = $InFp[$i];
RD:
while ( !eof($fh1) ) {
for ( my ($relHR1, $get1) = &get_fasta_seq($fh1); defined $relHR1; ($relHR1, $get1) = &get_fasta_seq($fh1) ) {
$relHR1->{'seq'} =~ s/\s//g;
$relHR1->{'seq'} =~ s/[\*X]+$//i;
print STDOUT ">$relHR1->{'head'}\n$relHR1->{'seq'}\n";
}
}#End while() RD:
}#End for
}# sub rmTailX_prot()
# 2015-10-01
sub rmTailXN {
for (my $i=0; $i<@InFp; $i+=1) {
my $fh1 = $InFp[$i];
RD:
while ( !eof($fh1) ) {
for ( my ($relHR1, $get1) = &get_fasta_seq($fh1); defined $relHR1; ($relHR1, $get1) = &get_fasta_seq($fh1) ) {
$relHR1->{'seq'} =~ s/\s//g;
$relHR1->{'seq'} =~ s/[\*XN]+$//i;
print STDOUT ">$relHR1->{'head'}\n$relHR1->{'seq'}\n";
}
}#End while() RD:
}#End for
}# sub rmTailXN()
# 2015-09-26
sub rna2dna {
for (my $i=0; $i<@InFp; $i+=1) {
my $fh1 = $InFp[$i];
RD:
while ( !eof($fh1) ) {
for ( my ($relHR1, $get1) = &get_fasta_seq($fh1); defined $relHR1; ($relHR1, $get1) = &get_fasta_seq($fh1) ) {
$relHR1->{'seq'} =~ tr/Uu/Tt/; # tr/// can treat strings with "\n";
print STDOUT ">$relHR1->{'head'}\n$relHR1->{'seq'}\n";
}
}#End while() RD:
}#End for
}#sub rna2dna()
# 2015-06-26
# "joinR12!"
sub joinR12 {
for (my $i=0; $i<@InFp; $i+=2) {
my $fh1 = $InFp[$i];
my $fh2 = $InFp[$i+1];
RD:
while ( !eof($fh1) and !eof($fh2) ) {
for ( my ($relHR1, $get1) = &get_fasta_seq($fh1); defined $relHR1; ($relHR1, $get1) = &get_fasta_seq($fh1) ) {
my ($relHR2, $get2) = &get_fasta_seq($fh2);
print STDOUT ">$relHR1->{'head'}\n$relHR1->{'seq'}\n";
print STDOUT ">$relHR2->{'head'}\n$relHR2->{'seq'}\n";
}
}#End while() RD:
}#End for
}#sub joinR12()
# 2015-06-15
# "chop_len:i"
sub chop_seq {
$opts{'chop_len'} //= 100;
$opts{'chop_step'} //= $opts{'chop_len'};
$opts{'chop_min'} //= 0;
if ( $opts{'chop_agp'} ) {
;
} elsif ($opts{'chop_info'}) {
print STDOUT join("\t", qw/key WI WS WE GC AG Wkey len N wN/)."\n";
}
for my $fh ( @InFp ) {
for ( my ($relHR, $get) = &get_fasta_seq($fh); defined $relHR; ($relHR, $get) = &get_fasta_seq($fh) ) {
$relHR->{'seq'} =~ s!\s!!g;
my $seqLen = length( $relHR->{'seq'} );
my $tkey = $relHR->{'key'} ;
my %cnt;
if ( $opts{'chop_agp'} ) {
;
} elsif ( $opts{'chop_info'} ) {
$cnt{'N'} = ( $relHR->{'seq'} =~ tr/nN/nN/ );
}
for (my $i=1; ($i-1) * $opts{'chop_step'} + 1 < $seqLen ; $i++) {
my $s = ($i-1) * $opts{'chop_step'} + 1 ;
my $e = $s + $opts{'chop_len'} - 1;
$e > $seqLen and $e = $seqLen;
$e-$s+1 >= $opts{'chop_min'} or next;
my $sub_seq = substr( $relHR->{'seq'}, $s-1, $e-$s+1 );
my $tag = " [${s}-${e}]";
$opts{'chop_noPos'} and $tag = '';
if ( $opts{'chop_agp'} ) {
print STDOUT join("\t", $tkey, $s, $e, $i, "W", "${tkey}_$i", 1, $e-$s+1, "+")."\n";
} elsif ( $opts{'chop_info'} ) {
my $c_a = ( $sub_seq =~ tr/aA/aA/ );
my $c_t = ( $sub_seq =~ tr/tT/tT/ );
my $c_g = ( $sub_seq =~ tr/gG/gG/ );
my $c_c = ( $sub_seq =~ tr/cC/cC/ );
my $c_all = ($c_a+$c_t+$c_g+$c_c);
my $gc = ( $c_all > 0 ) ? sprintf("%0.4f", ($c_g+$c_c)/$c_all) : 'Null' ;
my $ag = ( $c_all > 0 ) ? sprintf("%0.4f", ($c_g+$c_a)/$c_all) : 'Null' ;
my $wn = $e-$s+1 - $c_all;
print STDOUT join("\t", $tkey, $i, $s, $e, $gc, $ag, "${tkey}_$i", $seqLen, $cnt{'N'}, $wn)."\n";
} else {
print STDOUT ">${tkey}_$i${tag}\n$sub_seq\n";
}
$e >= $seqLen and last;
}
}
}
return;
}# sub chop_seq
# 2015-04-10
# "reorderByList:s"
sub reorderSeq {
my $lisFh = &openFH( shift, '<' );
my %seqOrd;
my $nn = 0;
while (<$lisFh>) {
chomp; m/^\s*(#|$)/ and next;
m/^(\S+)/ or next;
$nn ++;
$seqOrd{$1} = $nn;
}
close($lisFh);
my %seq;
for my $fh ( @InFp ) {
for ( my ($relHR, $get) = &get_fasta_seq($fh); defined $relHR; ($relHR, $get) = &get_fasta_seq($fh) ) {
defined $seqOrd{ $relHR->{'key'} } or next;
$seq{ $relHR->{'key'} } = $relHR;
}
}
for my $tk ( sort { $seqOrd{$a} <=> $seqOrd{$b} } keys %seqOrd ) {
defined $seq{$tk} or next;
my $relHR = $seq{$tk};
print STDOUT ">$relHR->{'head'}\n$relHR->{'seq'}\n";
}
}# sub reorderSeq ()
# 2015-02-26
# "replaceID!", "replaceIDlist:s", "replaceIDcol:s",
sub replaceID {
my $lisFh = &openFH( $opts{'replaceIDlist'}, '<' );
$opts{'replaceIDcol'} = $opts{'replaceIDcol'} // '0,1';
my ( $cOLD, $cNEW ) = map { int($_) } &parseCol( $opts{'replaceIDcol'} );
my %old2new;
while (<$lisFh>) {
chomp; m/^\s*$/ and next;
my @ta = split(/\t/, $_);
my ($idO, $idN) = @ta[$cOLD, $cNEW];
$old2new{ $idO } = $idN;
}
close($lisFh);
for my $fh ( @InFp ) {
for ( my ($relHR, $get) = &get_fasta_seq($fh); defined $relHR; ($relHR, $get) = &get_fasta_seq($fh) ) {
my $kO = $relHR->{'key'};
my $kN = $kO;
defined $old2new{ $kO } and $kN = $old2new{ $kO };
if ( $opts{'replaceIDadd'} ) {
$relHR->{'head'} = "$kN $relHR->{'head'}";
} else {
defined $old2new{ $kO } and substr($relHR->{'head'}, 0, length( $kO )) = $kN;
# defined $old2new{ $kO } and $relHR->{'head'} =~ s!^$kO\b!$kN!;
}
print STDOUT ">$relHR->{'head'}\n$relHR->{'seq'}\n";
}
}
}#End sub replaceID
# 10/26/2022
sub replaceSeq {
my $lisFh = &openFH( $opts{'replaceSeqList'}, '<' );
my %old2new;
while (<$lisFh>) {
chomp; m/^\s*$/ and next;
my @ta = split(/\t/, $_); # (seqID, start, end, new_seqID, sequence_to_use)
$ta[3] //= $ta[0];
$ta[4] //= '';
push(@{$old2new{$ta[0]}}, [@ta[1,2,3,4]]);
}
close($lisFh);
for (keys %old2new) {
@{$old2new{$_}} = sort { $a->[0]<=>$b->[0] } @{$old2new{$_}};
for (my $i=1; $i<@{$old2new{$_}}; $i++) {
$old2new{$_}[$i][0] > $old2new{$_}[$i-1][1] or &stopErr("[Err] [replaceSeq] Overlapped regions: $old2new{$_}[$i][0] > $old2new{$_}[$i-1][1]\n");
}
@{$old2new{$_}} = reverse(@{$old2new{$_}});
}
my $fs_obj = fastaSunhh->new();
for my $fh ( @InFp ) {
my %ss = %{$fs_obj->save_seq_to_hash('faFh'=>$fh)};
for my $k1 (sort {$ss{$a}{'Order'} <=> $ss{$b}{'Order'}} keys %ss) {
unless (defined $old2new{$k1}) {
chomp($ss{$k1}{'seq'});
print STDOUT ">$ss{$k1}{'head'}\n$ss{$k1}{'seq'}\n";
next;
}
$ss{$k1}{'seq'} =~ s!\s!!g;
my $newID;
for my $a1 (@{$old2new{$k1}}) {
$a1->[3] //= '';
substr($ss{$k1}{'seq'}, $a1->[0]-1, $a1->[1]-$a1->[0]+1) = $a1->[3];
$newID //= $a1->[2];
}
$ss{$k1}{'seq'} =~ s!(.{60})!$1\n!g;
chomp($ss{$k1}{'seq'});
print STDOUT ">$newID\n$ss{$k1}{'seq'}\n";
}
}
}#End sub replaceID
# 2014-03-18
sub fa2fq {
my $qBase = 'A';
defined $opts{fa2fqQChar} and $opts{fa2fqQChar} ne '' and length($opts{fa2fqQChar}) == 1 and $qBase = $opts{fa2fqQChar};
for my $fh ( @InFp ) {
for ( my ($relHR, $get) = &get_fasta_seq($fh); defined $relHR; ($relHR, $get) = &get_fasta_seq($fh) ) {
$relHR->{seq} =~ s/\s//g;
my $qSeq = $qBase x length($relHR->{seq});
print STDOUT "\@$relHR->{head}\n$relHR->{seq}\n+\n$qSeq\n";
}
}
}#End sub fa2fq
sub fq2fa {
for my $fh (@InFp) {
my $id = readline($fh);
my $seq = readline($fh);
readline($fh); readline($fh);
$id =~ s/^\@/>/ or die "Failed to fit line format: $id\n";
print STDOUT "$id$seq";
}
}
# 2014-02-25 baseCount
sub baseCount {
if ( $opts{'baseCountByWind'} ) {
my $is_oHead = 0;
# print STDOUT join("\t", qw/Key A T G C N O All OtherCols/)."\n";
my %seq;
for my $fh (@InFp) {
for ( my ($relHR, $get) = &get_fasta_seq($fh); defined $relHR; ($relHR, $get) = &get_fasta_seq($fh) ) {
$relHR->{seq} =~ s/\s//g;
$seq{ $relHR->{'key'} } = $relHR->{'seq'};
}#End for my ($relHR, $get)
}#End for my $fh
my $fh = &openFH( $opts{'baseCountByWind'}, '<' );
while (<$fh>) {
chomp;
my @ta = split(/\t/, $_);
if ( $ta[0] =~ m/^(ChromID|key|chr|chrID)$/i ) {
$is_oHead == 0 and print STDOUT join("\t", qw/Key A T G C N O All/, $_)."\n";
$is_oHead = 1;
next;
}
my ($id, $s, $e) = @ta[0,1,2];
if ( defined $seq{$id} ) {
my $seqLen = length($seq{$id});
if ( $s > $seqLen ) {
print STDOUT join("\t", $id, qw/0 0 0 0 0 0 0/, $_)."\n";
next;
}
$e > $seqLen and $e = $seqLen;
my $subseq = substr( $seq{$id}, $s-1, $e-$s+1 );
my $ba = ( $subseq =~ tr/Aa/Aa/ );
my $bt = ( $subseq =~ tr/Tt/Tt/ );
my $bg = ( $subseq =~ tr/Gg/Gg/ );
my $bc = ( $subseq =~ tr/Cc/Cc/ );
my $bn = ( $subseq =~ tr/Nn/Nn/ );
my $bother = $e-$s+1 - $ba - $bt - $bg - $bc - $bn;
print STDOUT join("\t", $id, $ba, $bt, $bg, $bc, $bn, $bother, $e-$s+1, $_)."\n";
} else {
print STDOUT join("\t", $id, qw/0 0 0 0 0 0 0/, $_)."\n";
}
}
close($fh);
} else {
print STDOUT join("\t", qw/Key A T G C N O All/)."\n";
for my $fh (@InFp) {
for ( my ($relHR, $get) = &get_fasta_seq($fh); defined $relHR; ($relHR, $get) = &get_fasta_seq($fh) ) {
$relHR->{seq} =~ s/\s//g;
my $all = length($relHR->{seq});
if ($all > 0) {
my $ba = ($relHR->{seq} =~ tr/Aa/Aa/);
my $bt = ($relHR->{seq} =~ tr/Tt/Tt/);
my $bg = ($relHR->{seq} =~ tr/Gg/Gg/);
my $bc = ($relHR->{seq} =~ tr/Cc/Cc/);
my $bn = ($relHR->{seq} =~ tr/Nn/Nn/);
my $bother = $all - $ba - $bt - $bg - $bc - $bn;
print STDOUT join("\t", $relHR->{key}, $ba, $bt, $bg, $bc, $bn, $bother, $all)."\n";
}else{
print STDOUT join("\t", $relHR->{key}, 0,0,0,0,0,0,0)."\n";
}
}#End for my ($relHR, $get)
}#End for my $fh
}
}# sub baseCount
# 2014-02-25 extract sequences by length
sub keep_len {
my ($min_len, $max_len) = (-1, -1);
if ( $opts{keep_len} =~ m!^\s*(\d+)\-(\d+)\s*$! ) {
($min_len, $max_len) = ($1, $2);
} elsif ( $opts{keep_len} =~ m!^\s*\-(\d+)\s*$! ) {
$max_len = $1;
} elsif ( $opts{keep_len} =~ m!^\s*(\d+)\-\s*$! ) {
$min_len = $1;
} else {
die "[Err] Failed to parse option [-keep_len $opts{keep_len}]\n";
}
for my $fh (@InFp) {
for ( my ($relHR, $get) = &get_fasta_seq($fh); defined $relHR; ($relHR, $get) = &get_fasta_seq($fh) ) {
(my $ss = $relHR->{seq}) =~ s/\s//g;
my $ll = length($ss);
$min_len > 0 and $ll < $min_len and next;
$max_len > 0 and $ll > $max_len and next;
print STDOUT ">$relHR->{head}\n$relHR->{seq}\n";
}# End for sequence retrieval
}#End for file.
}# sub keep_len
# 2013-10-30 给定列表, 提取列表内序列
# Related parameters: "drawByList!", "drawList:s", "drawLcol:s", "drawWhole!", "drawIDmatch!", "dropMatch!"
sub extract_seq_by_list {
# Parameters.
defined $opts{drawLcol} or $opts{drawLcol} = "0,1,2,3,4";
my ($cRID, $cS, $cE, $cStr, $cNID) = &parseCol( $opts{drawLcol} );
$cRID = int($cRID);
# (defined $cRID and $cRID ne '') or die "[Err]At least to assign column number for key(ID).\n";
($cS, $cE, $cStr, $cNID) = map { (defined $_ and $_ ne '') ? $_ : 'U'; } ($cS, $cE, $cStr, $cNID);
warn "[Msg]-drawLcol set as [$cRID,$cS,$cE,$cStr,$cNID]\n";
# Read in list file.
my %dRegion;
my @Keys;
my $lisFh = &openFH($opts{drawList},'<');
while (<$lisFh>) {
chomp;
my @ta = split(/\t/, $_);
defined $ta[$cRID] or next;
$ta[$cRID] =~ /^\s*$/ and next;
defined $dRegion{ $ta[$cRID] } or push( @Keys, $ta[$cRID] );
my @tadd;
for my $tid ( $cS,$cE,$cStr,$cNID ) {
push( @tadd, ($tid eq 'U') ? undef() : $ta[$tid] );
}
push(@{ $dRegion{ $ta[$cRID] } }, [ @tadd ]);
}
close($lisFh);
# Read in and draw from sequence file.
my %is_get;
my $seq_num = 0;
for my $fh (@InFp) {
for ( my ($relHR, $get) = &get_fasta_seq($fh); defined $relHR; ($relHR, $get) = &get_fasta_seq($fh) ) {
my $is_match = 0; # if this sequence ID matches.
$seq_num ++;
$seq_num % 5e3 == 1 and print STDERR "[Msg] $seq_num sequences.\n";
if ($opts{drawIDmatch}) {
FIND: foreach my $tt ( @Keys ) {
if (index($relHR->{key}, $tt) != -1) {
# This sequence matches the list.
$is_match = 1;
# output sequence if not -dropMatch .
$opts{dropMatch} or &outDrawnSeq( $relHR, \%dRegion, $tt, $opts{drawWhole} );
last FIND;
}#End if the key matches.
}#End FIND:foreach
} elsif (defined $dRegion{ $relHR->{key} }) {
# This sequence matches the list.
$is_match = 1;
$opts{dropMatch} or &outDrawnSeq( $relHR, \%dRegion, $relHR->{key}, $opts{drawWhole} );
} else {
# Nothing happens.
;
}#End if ( checking if this sequence matches the list)
# Output sequence if this not matching and -dropMatch assigned.
( $opts{dropMatch} and $is_match == 0 ) and &outDrawnSeq( $relHR );
}# End for sequence retrieval
}#End for file.
}# End sub extract_seq_by_list
# Inner function for sub extract_seq_by_list()
# &outDrawnSeq( $relHR, \%dRegion, $tt, $opts{drawWhole} );
sub outDrawnSeq ($$$$) {
my ($faR, $drR, $drK, $opts_drawWhole) = @_;