-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfasta_tool
More file actions
1640 lines (1337 loc) · 42 KB
/
Copy pathfasta_tool
File metadata and controls
1640 lines (1337 loc) · 42 KB
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
# Interpreted by shell on systems that don't support the shebang...
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
if 0; # ...but perl will ignore the eval
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../lib";
use lib "$FindBin::Bin/../perl/lib";
use Getopt::Long;
use Bio::SeqIO;
use IO::All;
#-----------------------------------------------------------------------------
#----------------------------------- MAIN ------------------------------------
#-----------------------------------------------------------------------------
my $usage = "
Synopsis:
fasta_tool [-options] fasta_file
Description: The script takes a fasta file and can search it, reformat
it, and manipulate it in a variety of ways that can prove very usful.
For options that provide the ability to evaluate code, use Perl.
Options:
--chunks <integer>
Split a single multi-fasta file into the given number of sub
files specified by chunks.
--split
Split a multi-fasta into individual files. One for each fasta.
--break <integer>
Break a the sequence from a single-fasta file into a multi-fasta
file with subsequences of the given size.
--eval_code <code>
Run the given code on (\$seq_obj, \$seq or \$header). If
the code block returns a positive value then the sequence is
printed. This can be used to build complex and custom filters.
--eval_all <code>
Run the given code on (\$seq_obj, \$seq or \$header).
Prints all sequences regardless of the return value of the
evaled code. This can but used to perform operations (e.g. soft
to hard masking with s/[a-z]/N/g, but still print every sequence
even if it's unaltered.
--extract_ids <id_file.txt>
Extract all of the sequences who's IDs are found in the given
file.
--grep_header <pattern>
Grep through a multi fasta file and print out only the fasta
sequences that have a match in the header. Use grepv_header for
negation.
--grepv_header <pattern>
Grep through a multi fasta file and print out only the fasta
sequences that DO NOT have a match in the header.
--grep_seq <pattern>
Grep through a multi fasta file and print out only the fasta
sequences that have a match in the sequence. Use grepv_seq for
negation.
--grepv_seq <pattern>
Grep through a multi fasta file and print out only the fasta
sequences that DO NOT have a match in the sequence.
--wrap <integer>
Wrap the sequence output to a given number of columns.
--translate <string>
Translate a given nucleotide sequence to protein sequence.
Accepts 0,1,2 (for the phase) or 'maker' if you want to use the
frame from MAKER produced headers
--trim_maker_utr
Prints MAKER produced transcipts without the leading and
trailing UTR sequence
--seq_only
Print only the sequence (without the header) to STDOUT. This
can also be accomplished with grep -v '>' fasta_file.
--nt_count
Print the number and percentage of every nt/aa found in the
sequence.
--summary
For functions that can report data for every sequence
(nt_count), use this flag to report only summary data for all
sequences combined.
--length
Print the length of each sequence.
--mapable_length
Print the mapable length (remove all Ns) of each sequence.
--total_length
Print the total length of all sequences.
--n50
Calculate the N-50 (http://en.wikipedia.org/wiki/N50_statistic)
of the sequences in the file.
--tab
Print the header and sequence on the same line separated by a
tab.
--table
Print in table format rather than fasta format.
--print
Print the sequence. Use in conjuction with 'wrap' or other
formatting commands to reformat the sequence.
--reverse
Reverse the order of the sequences in a fasta file.
--rev_seq
Reverse the sequence (the order of the nt/aa).
--comp_seq
Complement the nucleotide sequence.
--rev_comp
Reverse compliment a sequence. Same as --rev_seq && --comp_seq
together.
--uniq
Print only uniq sequences. This method only compares complete
sequences.
--uniq_sub
Print only uniq sequences, but also check that shorter sequences
are not perfect substrings of longer sequences.
--shuffle_order
Randomize the order of the sequences in a multi-fasta file.
--shuffle_seq
Randomize the sequence (the order of the nt/aa).
--shuffle_codon
Randomize the order of the codons in a nucleotide sequence.
--shuffle_pick
Pick a given number of sequences from a multi-fasta file.
--select
Pass in a file with IDs and return sequences with these IDs.
--remove
Pass in a file with IDs and remove sequences with these IDs.
--map_ids
Pass in a file with two columns of IDs and map the IDs in the
fasta headers from the first column of the ID file to the second
column of the ID file. If an ID in the fasta header is not
found in the first column of the ID file then issue a warning,
but leave the ID unmapped.
--fix_prot
Fix protein fasta files for use as blast database. Removes spaces
and '*' and replaces any non amino acid codes with C.
--subseq
Grab a sub-sequence from a fasta file based on coordinates. The
requested coordinates are in the form seqid:start-end;
--filter_horter
Filter (remove) entries shorter than the filter. IE \"--filter_shorter 800\"
will filter all sequences <= 800 basepairs long.
--filter_longer
Filter (remove entries longer than the filter. IE \"--filter_longer 800\"
will filter all sequences > 800 basepairs long.
--mask_fasta
Masks the genome using coordinates from a gff3 file
";
my ($summary, $chunks, $split, $break, $eval_code, $eval_all,
$extract_ids, $grep_header, $grepv_header, $grep_seq, $grepv_seq,
$wrap, $count, $translate, $seq_only, $nt_count, $length,
$mapable_length, $total_length, $n50, $tab, $reverse, $rev_seq,
$comp_seq, $rev_comp, $uniq, $uniq_sub, $shuffle_order,
$shuffle_seq, $shuffle_codon, $shuffle_pick, $select_file,
$remove_file, $print, $mRNAseq, $EST, $trim_maker_utr, $table,
$map_ids, $fix_prot, $subseq, $tile, $filter_shorter,
$filter_longer,$mask_fasta);
GetOptions('summary' => \$summary,
'chunks=i' => \$chunks,
'split' => \$split,
'break=i' => \$break,
'eval_code=s' => \$eval_code,
'eval_all=s' => \$eval_all,
'extract_ids=s' => \$extract_ids,
'grep_header=s' => \$grep_header,
'grep_seq=s' => \$grep_seq,
'grepv_header=s' => \$grepv_header,
'grepv_seq=s' => \$grepv_seq,
'wrap=i' => \$wrap,
'count' => \$count,
'translate=s' => \$translate,
'trim_maker_utr' => \$trim_maker_utr,
'seq_only' => \$seq_only,
'nt_count' => \$nt_count,
'length' => \$length,
'mapable_length' => \$mapable_length,
'total_length' => \$total_length,
'n50' => \$n50,
'tab' => \$tab,
'table' => \$table,
'print' => \$print,
'reverse' => \$reverse,
'rev_seq' => \$rev_seq,
'comp_seq' => \$comp_seq,
'rev_comp' => \$rev_comp,
'uniq' => \$uniq,
'uniq_sub' => \$uniq_sub,
'shuffle_order' => \$shuffle_order,
'shuffle_seq' => \$shuffle_seq,
'shuffle_codon' => \$shuffle_codon,
'shuffle_pick=i' => \$shuffle_pick,
'remove=s' => \$remove_file,
'select=s' => \$select_file,
'fix_prot' => \$fix_prot,
'mRNAseq' => \$mRNAseq,
'EST' => \$EST,
'map_ids=s' => \$map_ids,
'subseq=s' => \$subseq,
'tile=s' => \$tile,
'filter_shorter=i' => \$filter_shorter,
'filter_longer=i' => \$filter_longer,
'mask_fasta=s' => \$mask_fasta
);
my $file = shift;
unless ($file || ! -t STDIN){
print $usage;
exit;
}
if ($rev_comp) {$rev_seq++; $comp_seq++}
my @warning = ('WARN', 'method_not_thouroughly_tested', $0);
handle_message(@warning) if grep {$_} ($extract_ids,
$reverse,
$rev_seq,
$comp_seq,
$shuffle_order,
$shuffle_seq,
$shuffle_codon,
$shuffle_pick,
$mask_fasta
);
# These functions handle their own printing;
$print++ unless grep {$_} ($chunks,
$split,
$break,
$eval_code,
$eval_all,
$extract_ids,
$grep_header,
$grep_seq,
$grepv_header,
$grepv_seq,
$count,
$translate,
$seq_only,
$nt_count,
$length,
$mapable_length,
$total_length,
$n50,
$reverse,
$rev_seq,
$comp_seq,
$uniq,
$uniq_sub,
$shuffle_order,
$shuffle_seq,
$shuffle_codon,
$shuffle_pick,
$remove_file,
$select_file,
$mRNAseq,
$EST,
$map_ids,
$subseq,
$tile,
$filter_shorter,
$filter_longer,
$mask_fasta
);
$nt_count++ if $summary;
if(defined $translate && $translate !~ /^\d+$/ && $translate ne 'maker'){
$translate = 0;
}
my $IN;
if (! $file && ! -t STDIN) {
open ($IN, "<&=STDIN") or handle_message('FATAL', 'cant_open_stdin');
}
elsif (! -e $file) {
handle_message('FATAL', 'file_does_not_exist', $file);
}
elsif (! -r $file) {
handle_message('FATAL', 'file_is_not_readable', $file);
}
else {
open ($IN, $file) or handle_message('FATAL', 'cant_open_file_for_reading', $file);
}
#Bioperl object for main fasta input file.
my $seq_io = Bio::SeqIO->new(-fh => $IN,
-format => 'Fasta');
chunks($file, $chunks) if $chunks;
split_fasta() if $split;
break_fasta($break) if $break;
eval_code($eval_code) if $eval_code;
eval_all($eval_all) if $eval_all;
extract_ids($extract_ids) if $extract_ids;
grep_header($grep_header) if $grep_header;
grep_seq($grep_seq) if $grep_seq;
grepv_header($grepv_header) if $grepv_header;
grepv_seq($grepv_seq) if $grepv_seq;
translate() if defined($translate);
trim_maker_utr() if $trim_maker_utr;
seq_only() if $seq_only;
nt_count() if $nt_count;
seq_length() if $length;
mapable_length() if $mapable_length;
total_length() if $total_length;
n50() if $n50;
tab() if $tab;
reverse_order() if $reverse;
rev_comp() if $rev_seq;
rev_comp() if $comp_seq;
uniq() if $uniq;
uniq_sub() if $uniq_sub;
shuffle_order() if $shuffle_order;
shuffle_seq() if $shuffle_seq;
shuffle_codon() if $shuffle_codon;
shuffle_pick($shuffle_pick) if $shuffle_pick;
remove_ids($remove_file) if $remove_file;
select_ids($select_file) if $select_file;
map_ids($map_ids) if $map_ids;
subseq($file, $subseq) if $subseq;
fix_prot() if $fix_prot;
print_seq() if $print;
mRNAseq() if $mRNAseq;
EST() if $EST;
filter_shorter($filter_shorter) if $filter_shorter;
filter_longer($filter_longer) if $filter_longer;
mask_fasta($mask_fasta) if $mask_fasta;
#-----------------------------------------------------------------------------
#-------------------------------- SUBROUTINES --------------------------------
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
#
# --chunks <integer>
#
# Split a single multi-fasta file into the given number of sub files.
#
#-----------------------------------------------------------------------------
sub chunks {
my($file, $chunks) = @_;
my $outfile_base; #Create a base name for the output file.
($outfile_base = $file) =~ s/\.[^\.]*$//; #Input file name minus it's extension.
my $file_size = io($file)->size; #What's the size of our input file.
#How many chunks should the input file be split into?
my $chunk_size = int($file_size/$chunks) + ($file_size % $chunks);
#How many digits should the output file iterations be sprintf'ed to=?
my $digits = int(log($chunks)/log(10)) + 1;
#I felt like using a closure today.
my $file_counter = make_counter();
my $out;
my $file_name;
#Loop over each sequence
while ( my $seq = $seq_io->next_seq() ) {
#Get an Bio::SeqIO fh if we don't have one, or if the current output file
#has grown too large.
if (! $out || (io($file_name)->size > $chunk_size)) {
($out, $file_name) = get_output_stream($outfile_base, $digits, $file_counter);
}
#Write it to the file.
$out->write_seq($seq);
}
}
#-----------------------------------------------------------------------------
sub get_output_stream{
my ($outfile_base, $digits, $file_counter) = @_;
my $file_count = $file_counter->();
#Build/format the file_name.
$file_count = sprintf "%0${digits}s", $file_count;
my $file_name = $outfile_base . "_$file_count" . '.fasta';
#Get Bio::SeqIO object
my $out = Bio::SeqIO->new(-file => ">$file_name",
-format => 'fasta');
return ($out, $file_name);
}
#-----------------------------------------------------------------------------
sub make_counter {
#Initialize the counter.
my $file_counter = 0;
#Increment the counter.
return sub{$file_counter++}
}
#-----------------------------------------------------------------------------
#
# --split
#
# Split a multi-fasta into individual files. One for each fasta.
#
#-----------------------------------------------------------------------------
sub split_fasta {
while ( my $seq = $seq_io->next_seq() ) {
my $file_out = $seq->display_id . ".fasta";
#Get Bio::SeqIO object
my $out = Bio::SeqIO->new(-file => ">$file_out",
-format => 'fasta');
#Write it to the file.
$out->write_seq($seq);
}
}
#-----------------------------------------------------------------------------
#
# --break <integer>
#
# Break a the sequences from a fasta file into subsequences of the
# given size.
#
#-----------------------------------------------------------------------------
sub break_fasta {
my ($break) = @_;
SEQ:
while ( my $seq_obj = $seq_io->next_seq() ) {
my $header = get_header($seq_obj);
my $seq = $seq_obj->seq;
my $length = length($seq);
my $start = 0;
my $counter;
while ($start < $length) {
$break = $length - $start if $start + $break > $length;
my $sub_seq = substr($seq, $start, $break);
#print join("\t", $start, $break, $length);
#print "\n";
print_this_seq($header, $sub_seq);
$start += $break;
}
}
exit(0);
}
#-----------------------------------------------------------------------------
#
# --eval_code <code>
#
# Run the given code on (\$seq_obj, \$seq or \$header). If the code
# block returns a positive value then the sequence is printed. This can be
# used to build complex and custom filters.
#
#-----------------------------------------------------------------------------
sub eval_code {
my ($code) = @_;
while (my $seq_obj = $seq_io->next_seq) {
my $header = get_header($seq_obj);
my $seq = $seq_obj->seq;
my $return_value = eval $code;
handle_message('FATAL', 'error_in_code_ref',
"$code : $@") if $@;
next unless $return_value;
print_this_seq($header, $seq);
}
}
#-----------------------------------------------------------------------------
#
# --eval_all <code>
#
# Run the given code on (\$seq_obj, \$seq or \$header). Prints all
# sequences regardless of the return value of the evaled code. This can
# but used to perform operations (e.g. soft to hard masking with
# s/[a-z]/N/g, but still print every sequence even if it's unaltered.
#
#-----------------------------------------------------------------------------
sub eval_all {
my ($code) = @_;
while (my $seq_obj = $seq_io->next_seq) {
my $header = get_header($seq_obj);
my $seq = $seq_obj->seq;
eval $code;
handle_message('FATAL', 'error_in_code_ref',
"$code : $@") if $@;
print_this_seq($header, $seq);
}
}
#-----------------------------------------------------------------------------
#
# --extract_ids <id_file.txt>
#
# Extract all of the sequences who's IDs are found in the given file.
#
#-----------------------------------------------------------------------------
sub extract_ids {
my $id_file = shift;
open(my $IN, '<', $id_file) or handle_message('FATAL',
'cant_open_file_for_reading',
$id_file);
my %ids = map {$_ => 1 unless (/^\#/ || ! $_)} (<$IN>);
while (my $seq_obj = $seq_io->next_seq) {
my $id = $seq_obj->display_id;
my $header = get_header($seq_obj);
my $seq = $seq_obj->seq;
if (exists $ids{$id}) {
print_this_seq($header, $seq);
}
}
}
#-----------------------------------------------------------------------------
#
# --grep_header <pattern>
#
# Grep through a multi fasta file and print out only the fasta
# sequences that have a match in the header. Use grepv_header for
# negation.
#
#-----------------------------------------------------------------------------
sub grep_header {
my ($pattern) = @_;
while (my $seq_obj = $seq_io->next_seq) {
my $header = get_header($seq_obj);
$header .= " " . $seq_obj->description;
my $seq = $seq_obj->seq;
if ($header =~ /$pattern/) {
print_this_seq($header, $seq);
}
}
}
#-----------------------------------------------------------------------------
#
# --grep_header <pattern>
#
# Grep through a multi fasta file and print out only the fasta
# sequences that DO NOT have a match in the header.
#
#-----------------------------------------------------------------------------
sub grepv_header {
my ($pattern) = @_;
while (my $seq_obj = $seq_io->next_seq) {
my $header = get_header($seq_obj);
my $seq = $seq_obj->seq;
if ($header !~ /$pattern/) {
print_this_seq($header, $seq);
}
}
}
#-----------------------------------------------------------------------------
{my $i = 0;
sub mRNAseq {
my $size = 50;
while (my $seq_obj = $seq_io->next_seq) {
my $seq = $seq_obj->seq;
my $len = length($seq);
for (my $j = 0; $j < $len/5; $j++){
my $range = $len - $size;
my $start; my $end;
if($range < 0){
$start = 0 ;
$end = $len - 1;
}
else{
$start = int(rand($range));
$end = $start + $size - 1;
}
my $l = $end - $start + 1;
my $header = "sequence_".$i++;
print_this_seq($header, substr($seq, $start, $l));
}
}
}
#-----------------------------------------------------------------------------
sub EST {
while (my $seq_obj = $seq_io->next_seq) {
my $seq = $seq_obj->seq;
my $len = length($seq);
my $range = $len - 500;
my $min = 250;
if($range < 0 || $min < 0){
my $header = "sequence_".$i++;
print_this_seq($header, $seq);
next;
}
my $A = int(rand($range) + $min);
my $start = int(rand($range) + $min);
my $end = int(rand($range));
my $B = int(rand($range) + $min);
my $l = abs($end - $start + 1);
if($l > 250){
my $header = "sequence_".$i++;
print_this_seq($header, substr($seq, $start, $l));
}
$l = abs($A - 0 + 1);
if($l > 250){
my $header = "sequence_".$i++;
print_this_seq($header, substr($seq, 0, $l));
}
$l = abs($len - $B + 1);
if($l > 250){
my $header = "sequence_".$i++;
print_this_seq($header, substr($seq, $B, $l));
}
}
}
}
#-----------------------------------------------------------------------------
#
# --grep_seq <pattern>
#
# Grep through a multi fasta file and print out only the fasta
# sequences that have a match in the sequence. Use grepv_seq for
# negation.
#
#-----------------------------------------------------------------------------
sub grep_seq {
my ($pattern) = @_;
while (my $seq_obj = $seq_io->next_seq) {
my $header = get_header($seq_obj);
my $seq = $seq_obj->seq;
$seq =~ s/\s//g;
if ($seq =~ /$pattern/) {
print_this_seq($header, $seq);
}
}
}
#-----------------------------------------------------------------------------
#
# --grepv_seq <pattern>
#
# Grep through a multi fasta file and print out only the fasta
# sequences that DO NOT have a match in the sequence.
#
#-----------------------------------------------------------------------------
sub grepv_seq {
my ($pattern) = @_;
while (my $seq_obj = $seq_io->next_seq) {
my $header = get_header($seq_obj);
my $seq = $seq_obj->seq;
$seq =~ s/\s//g;
if ($seq !~ /$pattern/) {
print_this_seq($header, $seq);
}
}
}
#-----------------------------------------------------------------------------
#
# --fix_prot
#
# Fix protein fasta files for use as blast database. Removes spaces
# and '*' and replaces any non amino acid codes with C.
#
#-----------------------------------------------------------------------------
sub fix_prot {
while (my $seq_obj = $seq_io->next_seq) {
my $header = get_header($seq_obj);
my $seq = $seq_obj->seq;
$seq =~ s/[\s\*]//g;
$seq =~ s/[^abcdefghiklmnpqrstvwyzxABCDEFGHIKLMNPQRSTVWYZX\-\n]/C/g;
next if($seq eq ''); #skip empty fasta entries
print_this_seq($header, $seq);
}
}
#-----------------------------------------------------------------------------
#
# --translate <string>
#
# Translate a given nucleotide sequence to protein sequence.
# Accepts 0,1,2 (for the phase) or 'maker' if you want to use the
# frame from MAKER produced headers
#
#-----------------------------------------------------------------------------
sub translate {
while (my $seq_obj = $seq_io->next_seq) {
my $header = get_header($seq_obj);
my $frame;
my $offset;
if($translate eq 'maker'){
$header =~ /offset:(\d+)/;
$frame = ($1 % 3);
$offset = ($1 - $frame)/3;
}
else{
$frame = $translate % 3;
$offset = ($translate - $frame)/3;
}
my $pep_seq = $seq_obj->translate(-frame => $frame)->seq;
$pep_seq = substr($pep_seq, $offset);
$pep_seq =~ s/^([^\*]+).*/$1/;
print_this_seq($header, $pep_seq);
}
}
#-----------------------------------------------------------------------------
#
# --trim_maker_utr
#
# Prints MAKER produced transcipts without the leading and
# trailing UTR sequence
#
#-----------------------------------------------------------------------------
sub trim_maker_utr {
while (my $seq_obj = $seq_io->next_seq) {
my $header = get_header($seq_obj);
my $frame;
my $offset;
$header =~ /offset:(\d+)/;
handle_message('WARN', 'non_maker_transcripts', $header)
if(! defined $1 || $1 eq '');
$frame = ($1 % 3);
$offset = ($1 - $frame)/3; #peptide offet without frame
my $tra_seq = $seq_obj->seq;
my $pep_seq = $seq_obj->translate(-frame => $frame)->seq;
$pep_seq = substr($pep_seq, $offset);
$pep_seq =~ s/^([^\*]+\*?).*/$1/;
$offset = 3 * $offset + $frame; #make transcript offset
my $length = 3 * length($pep_seq); #length of substring to get
my $fix = $offset + $length - length($tra_seq);
$length -= $fix if($fix > 0);
$tra_seq = substr($tra_seq, $offset, $length);
print_this_seq($header, $tra_seq);
}
}
#-----------------------------------------------------------------------------
#
# --seq_only
#
# Print only the sequence (without the header) to STDOUT. This
# can also be accomplished with grep -v '>' fasta_file.
#
#-----------------------------------------------------------------------------
sub seq_only {
while (my $seq_obj = $seq_io->next_seq) {
my $seq = $seq_obj->seq;
$seq = wrap_seq($seq, $wrap) if $wrap;
print $seq . "\n";
}
}
#-----------------------------------------------------------------------------
#
# --nt_count
#
# Print the number and percentage of every nt/aa found in the
# sequence.
#
# --summary
# For functions that can report data for every sequence (nt_count),
# use this flag to report only summary data for all sequences combined.
#
#-----------------------------------------------------------------------------
sub nt_count {
my %all_seq_count;
my $total_count;
while (my $seq_obj = $seq_io->next_seq) {
my %this_seq_count;
my $this_count;
my $id = $seq_obj->display_id;
my $seq = $seq_obj->seq;
$seq =~ s/\s//g;
my @nts = split //, $seq;
for my $nt (@nts) {
$all_seq_count{$nt}++;
$this_seq_count{$nt}++;
$this_count++;
$total_count++;
}
next if $summary;
print "$id:\n";
print '-' x 80;
print "\n";
for my $nt (sort keys %this_seq_count) {
my $round = sprintf ("%.4f", $this_seq_count{$nt} / $this_count * 100);
print join "\t", ($nt,
$this_seq_count{$nt},
$round,
);
print '%' . "\n";
}
my %this_report;
map {$this_report{aA} += $this_seq_count{$_} if $this_seq_count{$_}} qw(a A);
map {$this_report{tT} += $this_seq_count{$_} if $this_seq_count{$_}} qw(t T);
map {$this_report{gG} += $this_seq_count{$_} if $this_seq_count{$_}} qw(g G);
map {$this_report{cC} += $this_seq_count{$_} if $this_seq_count{$_}} qw(c C);
map {$this_report{aAtT} += $this_report{$_} if $this_report{$_}} qw(aA tT);
map {$this_report{gGcC} += $this_report{$_} if $this_report{$_}} qw(gG cC);
map {$this_report{aAtTgGcC} += $this_report{$_} if $this_report{$_}} qw(aAtT gGcC);
map {$this_report{atgc} += $this_seq_count{$_} if $this_seq_count{$_}} qw(a t g c);
map {$this_report{nN} += $this_seq_count{$_} if $this_seq_count{$_}} qw(n N);
map {$this_report{atgcnN} += $this_seq_count{$_} if $this_seq_count{$_}} qw(atgc nN);
for my $key (sort keys %this_report) {
print join "\t", ($key,
$this_report{$key},
sprintf ("%.4f", $this_report{$key} / $this_count * 100),
);
print '%' . "\n";
}
print "\n\n";
}
print "All sequences combined:\n";
print '-' x 80;
print "\n";
for my $nt (sort keys %all_seq_count) {
print join "\t", ($nt,
$all_seq_count{$nt},
sprintf ("%.4f", $all_seq_count{$nt} / $total_count * 100),
);
print '%' . "\n";
}
my %all_report;
map {$all_report{aA} += $all_seq_count{$_} if $all_seq_count{$_}} qw(a A);
map {$all_report{tT} += $all_seq_count{$_} if $all_seq_count{$_}} qw(t T);
map {$all_report{gG} += $all_seq_count{$_} if $all_seq_count{$_}} qw(g G);
map {$all_report{cC} += $all_seq_count{$_} if $all_seq_count{$_}} qw(c C);
map {$all_report{aAtT} += $all_report{$_} if $all_report{$_}} qw(aA tT);
map {$all_report{gGcC} += $all_report{$_} if $all_report{$_}} qw(gG cC);
map {$all_report{aAtTgGcC} += $all_report{$_} if $all_report{$_}} qw(aAtT gGcC);
map {$all_report{atgc} += $all_seq_count{$_} if $all_seq_count{$_}} qw(a t g c);
map {$all_report{nN} += $all_seq_count{$_} if $all_seq_count{$_}} qw(n N);
map {$all_report{atgcnN} += $all_seq_count{$_} if $all_seq_count{$_}} qw(atgc nN);
for my $key (sort keys %all_report) {
print join "\t", ($key,
$all_report{$key},
sprintf ("%.4f", $all_report{$key} / $total_count * 100),
);
print '%' . "\n";
}
print "\n";
print "Total nts\t$total_count\n";
}