forked from kentnf/KTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmRNAtool.pl
executable file
·1561 lines (1350 loc) · 48.8 KB
/
mRNAtool.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/perl
=head1
mRNAtool.pl -- tools for mRNA analysis
=cut
use strict;
use warnings;
use IO::File;
use FindBin;
use Statistics::Basic qw(:all nofill);
use Getopt::Std;
use Bio::SeqIO;
my $version = 0.1;
my $debug = 0;
my %options;
getopts('a:b:c:d:e:f:g:i:j:k:l:m:n:o:p:q:r:s:t:u:v:w:x:y:z:h', \%options);
unless (defined $options{'t'} ) { usage($version); }
# checking parameters
my %ss = ('SE'=>1,'SS'=>1,'PE'=>1,'PS'=>1);
$options{'s'} = "SS" unless defined $options{'s'};
die "[ERR]sequencing-method: $options{'s'}\n" unless defined $ss{$options{'s'}};
if (defined $options{'l'} && $options{'l'} ne "fr-firststrand" && $options{'l'} ne "fr-secondstrand" && $options{'l'} ne "fr-unstranded" ) {
die "[ERR]library-type: $options{'l'}\n";
} else {
$options{'l'} = "fr-firststrand";
}
if ($options{'t'} eq 'align') { rnaseq_align(\%options, \@ARGV); } # parse multi dataset
elsif ($options{'t'} eq 'tport') { rnaseq_tport(\%options, \@ARGV); } # parse multi dataset
elsif ($options{'t'} eq 'count') { rnaseq_count(\%options, \@ARGV); } # parse multi dataset
elsif ($options{'t'} eq 'norm') { rnaseq_norm(\%options, \@ARGV); } # parse single dataset
elsif ($options{'t'} eq 'corre') { rnaseq_corre(\%options, \@ARGV); } # parse single dataset
elsif ($options{'t'} eq 'unmap') { rnaseq_unmap(\%options, \@ARGV); } # parse single dataset
elsif ($options{'t'} eq 'isize') { rnaseq_isize(\%options, \@ARGV); } # parse single dataset, get insert size for paired end dataset
elsif ($options{'t'} eq 'Dassembly') { rnaseq_Dassem(\%options, \@ARGV);} # denovo assemble RNASeq reads
elsif ($options{'t'} eq 'Rassembly') { rnaseq_Rassem(\%options, \@ARGV);} # denovo assemble RNASeq reads
elsif ($options{'t'} eq 'mapping') { rnaseq_map(\%options, \@ARGV); } # align read to cDNA reference
elsif ($options{'t'} eq 'ctgFeature') { rnaseq_ctgFeature(\%options, \@ARGV);}# generate feature bed for reads aligned to cDNA reference
elsif ($options{'t'} eq 'blastn') { rnaseq_map(\%options, \@ARGV); } # blast assembled contigs to nt to remove contanmiantion
elsif ($options{'t'} eq 'novoclean') { rnaseq_novoclean(\%options, \@ARGV);} # clean assembled contigs using seqclean
elsif ($options{'t'} eq 'translate') { rnaseq_translate(\%options, \@ARGV);} # translate EST to protein
elsif ($options{'t'} eq 'annotate') { rnaseq_annotate(\%options, \@ARGV);} # generate annotation command by AHRD
elsif ($options{'t'} eq 'fixanno') { rnaseq_fixanno(\%options, \@ARGV);} # fix annotation command by AHRD
elsif ($options{'t'} eq 'pipeline') { pipeline(); } # print pipelines
else { usage($version); }
#################################################################
# kentnf: subroutine for denovo pipeline #
#################################################################
=head2
rnaseq_novoclean: clean contamination after de novo assembly
=cut
sub rnaseq_novoclean
{
my ($options, $files) = @_;
my $usage = qq'
USAGE: $0 -t novoclean Trinity_kn.fasta
* this tool is not a script like others, but a pipeline description.
';
print $usage and exit unless (@$files == 2);
my $seqclean_bin = $FindBin::RealBin."/bin/seqclean/seqclean";
die "[ERR]seqclean not exist\n" unless -s $seqclean_bin;
print qq'
$usage
# A. remove contamination by blastn
\$blastTool.pl
[integrade honghe blast method to this method]
# B. remove reads without complete degradation for strand specific samples.
# C. remove rRNA for de novo asembled transcripts:
$seqclean_bin $$files[1] -s $$files[0] -c 15 -l 200 -o seqclean_output_cycle1
-s rRNA sequence file (rRNA_silva111)
-l min seq length (seq will be discard short than -l)
-c cpu number
# D. remove duplication
\$ iAssembler.pl
';
exit;
}
=head2
rnaseq_Dassem: denovo assembly using trinity
=cut
sub rnaseq_Dassem
{
my ($options, $files) = @_;
my $usage = qq'
USAGE: $0 Dassembly [options] input1.fq input2.fq ...... | input1_r1_fq,input1_r2.fq input2_r1_fq,input2_r2.fq ...
example: Trinity --seqType fq --JM 50G --left EB_l1_1.clean.PE.fq,FB_l1_1.clean.PE.fq --right EB_l1_2.clean.PE.fq,FB_l1_2.clean.PE.fq --SS_lib_type RF --output assembly_k3 --min_kmer_cov 3 --CPU 8 --bflyCPU 8 --inchworm_cpu 8 --path_reinforcement_distance 25 > assembly_SS_k3.log
';
}
=head2
rnaseq_Rassem: reference based assemly using cufflinks
=cut
sub rnaseq_Rassem
{
}
=head2
rnaseq_mapping: map rnaseq reads to cDNA reference
=cut
sub rnaseq_mapping
{
my ($options, $files) = @_;
my $usage = qq'
USAGE: $0 -t mapping [options] reference.fasta [input1.fq input2.fq ... | input1_r1_fq,input1_r2.fq input2_r1_fq,input2_r2.fq ...]
-p cpu
-a BWA|bowtie
-n edit distance (default:0)
-m mode (1: keep unmapped reads, 2: generate aligned bam file)
-l fr-firststrand, fr-secondstrand, fr-unstranded
';
# check input files
my @files = @$files;
print $usage and exit if (scalar(@files) < 2);
my $reference = shift @files;
print "[ERR]no reference\n $usage" and exit unless -s $reference;
my $err = 0;
foreach my $f (@files) {
my @a = split(/,/, $f);
if (scalar(@a) == 1) { print "[ERR]no input read $f\n" unless -s $f; $err = 1; }
elsif (scalar(@a) == 2) { print "[ERR]no input read1 $a[0]\n" unless -s $a[0]; print "[ERR]no input read2 $a[1]\n" unless -s $a[1]; $err = 1; }
else { print "[ERR]input read $f\n"; $err = 1; }
exit if $err;
}
# check input parameters
my $distance = 0; $distance = $$options{'n'} if (defined $$options{'n'} && $distance > 0 && $distance < 4);
my $cpu = 32; $cpu = $$options{'p'} if (defined $$options{'p'} && $$options{'p'} > 0);
my $program = 'bowtie'; $program = $$options{'a'} if (defined $$options{'a'} && $$options{'a'} eq 'bwa');
my $maxins = 100000;
my $cmd;
foreach my $f (@files) {
my @a = split(/,/, $f);
if (scalar(@a) == 1) {
my $unmap = $a[0].".unmap";
my $out_sam = $a[0].".sam";
$cmd = "bowtie -v $distance -k 1 -p $cpu --un $unmap -S $reference $a[0] $out_sam";
} else {
my $unmap = $a[0].".unmap";
my $out_sam = $a[0].".sam";
$cmd = "bowtie -v $distance -X $maxins -k 1 -p $cpu --un $unmap -S $reference -m1 $a[0] -m2 $a[1] $out_sam";
}
}
}
=head2
rnaseq_ctgFeature: convert contigs to feature
=cut
sub rnaseq_ctgFeature
{
my ($options, $files) = @_;
my $usage = qq'
USAGE: $0 -t ctgFeature contig.fasta > feature.bed
* after generate the feature, you could count the reads using count tool
';
print $usage and exit unless defined $$files[0];
die "[ERR]file not exist $$files[0]\n" unless -s $$files[0];
my $in = Bio::SeqIO->new(-format=>'fasta', -file=>$$files[0]);
while(my $inseq = $in->next_seq) {
print $inseq->id,"\t0\t",$inseq->length,"\t", $inseq->id,"\t",$inseq->length,"\t+\n",
}
}
=head2
rnaseq_translate: translate transcript to protein
=cut
sub rnaseq_translate
{
my ($options, $files) = @_;
my $usage = qq'
USAGE: $0 -t translate [options] transcript.fasta > protein.fasta
-m method used for translate
6frame: 6frame translate (default)
3frame: 3frame translate (for strand-specific)
estscan: translate using estscan (for denovo assembled transcripts)
-n min length of protein (default: 100)
-t matrix_file for estscan
* the output translated proteins need to be select for best result
';
# check input files and parameters
print "$usage" and exit unless defined $$files[0];
print "[ERR]no input transcripts\n" and exit unless -s $$files[0];
my $method = '6frame';
if (defined $$options{'m'} && ($$options{'m'} eq '3frame' || $$options{'m'} eq 'estscan' || $$options{'m'} eq '6frame')) {
$method = $$options{'m'};
} else {
print "[ERR]method $$options{'m'}\n" and exit;
}
my $min_len = 100;
$min_len = $$options{'n'} if (defined $$options{'n'} && $$options{'n'} > 5);
# array for order of 6 frames
my @frames = ('0F','1F', '2F','0R','1R','2R');
if ($method eq '6frame' || $method eq '3frame')
{
my $in = Bio::SeqIO->new(-format=>'fasta', -file=>$$files[0]);
while(my $inseq = $in->next_seq)
{
my $sequence = $inseq->seq;
#my $revcom_seq = reverse($sequence);
#$revcom_seq =~ tr/ATCGNatcgn/TAGCNtagcn/;
if ( $inseq->alphabet eq "rna" || $inseq->alphabet eq "dna" )
{
# translate to proteins
my @prots;
if ($method eq '6frame') {
@prots = Bio::SeqUtils->translate_6frames($inseq);
} else {
@prots = Bio::SeqUtils->translate_3frames($inseq);
}
# filter translated proteins
for (my $i = 0; $i < @prots; $i++)
{
my $frame = $frames[$i];
my $tseq = $prots[$i]->seq;
my $tid = $inseq->id."_".$frame;
#print ">$tid\n$tseq\n";
my @t = split(//, $tseq);
my ($n_start, $n_end, $n_len, $a_len, $a_seq, $a_switch);
$a_seq = ''; $a_switch = 0;
for( my $j=0; $j<@t; $j++)
{
if ($t[$j] eq "M" && $a_switch == 0) {
$a_switch = 1;
$n_start = $j * 3 + 1;
}
$a_seq.=$t[$j] if $a_switch == 1;
if ($t[$j] eq "*" && $a_switch == 1) {
$a_switch = 0;
$n_end = $j * 3 + 3;
# output protein seq
print "[ERR]no protein start $a_seq\n" and exit unless $a_seq =~ m/^M/;
print "[ERR]no protein end $a_seq\n" and exit unless $a_seq =~ m/\*$/;
$a_len = length($a_seq) - 1;
$n_len = $n_end - $n_start + 1;
if ($a_len >= $min_len) {
print ">$tid $n_start-$n_end:$n_len translated to $a_len\n$a_seq\n";
}
$a_seq = '';
$n_start = '';
$n_end = '';
}
}
}
}
else
{
die "[ERR]input is not nucleotied $inseq->id\n";
}
}
} # end of 6frame and 3 frame method
else
{
print "just put ESTScan command to here";
}
}
=head2
rnaseq_annotate: generate command for rnaseq contig annotation
=cut
sub rnaseq_annotate
{
my ($options, $files) = @_;
my $usage = qq'
>> pipeline for annotation
1. AHRD, input file is input_ctg.fa
\$ blastall -p blastx -i input_ctg.fa -o input_ctg_tr.pairwise -d uniprot_trembl_plant -e 0.0001 -v 200 -b 200 -m 0 -a 64
\$ blastall -p blastx -i input_ctg.fa -o input_ctg_sp.pairwise -d uniprot_sprot.fasta -e 0.0001 -v 200 -b 200 -m 0 -a 64
\$ blastall -p blastx -i input_ctg.fa -o input_ctg_at.pairwise -d TAIR10_pep_20110103_representative_gene_model_parse_desc -e 0.0001 -v 200 -b 200 -m 0 -a 64
\$ mRNAtool.pl -t annotate -n 1 input_ctg.fa input_ctg_sp.pairwise input_ctg_at.pairwise input_ctg_tr.pairwise
* the output file is input_ctg.ahrd.csv
* the blast file should be input by order (SP, AT, and TR)
* please use "-n" if the input seq is nucleotie and blastx
2. GO annotation (using blast result from AHRD)
\$ parse_blast.pl input_ctg_tr.pairwise 5 > input_ctg_tr.blast.table
\$ parse_blast.pl input_ctg_sp.pairwise 5 > input_ctg_sp.blast.table
\$ cat input_ctg_tr.blast.table input_ctg_sp.blast.table > input_ctg_uniport.blast.table
\$ go_link_gene.pl input_ctg_uniport.blast.table parsed_go_mapping_file[option] > gene_GO
\$ go_generate_associate.pl gene_GO organism > associate_file
\$ go_enrichment.pl -i list_gene_id -a associate_file
\$ go_slim.pl -a associate_file
3. Pathway annotation
* parse the AHRD csv file then fed to PathwayTools to perform analysis
';
my @files = @$files;
print $usage and exit unless scalar (@files) == 4;
foreach my $f (@files) {
print "[ERR]file not exist $f\n" and exit unless -s $f;
# parse blast file if from code quest
}
my $output_file = $files[0];
$output_file =~ s/\.fa$//;
$output_file =~ s/\.fasta$//;
$output_file .= ".ahrd.csv";
my ($p1, $p2, $p3) = (0.5, 0.3, 0.2);
if (defined $$options{'n'}) { ($p1, $p2, $p3) = (0.6, 0.4, 0.0); }
my $ahrd_fdr = ${FindBin::RealBin}."/bin/AHRD";
# generate temp xml file for AHRD
my $temp_ahrd_yml = 'temp_ahrd.yml';
my $fh = IO::File->new(">".$temp_ahrd_yml) || die $!;
print $fh qq'
proteins_fasta: $files[0]
blast_dbs:
swissprot:
weight: 100
file: $files[1]
blacklist: $ahrd_fdr/blacklist_descline.txt
filter: $ahrd_fdr/filter_descline_sprot.txt
token_blacklist: $ahrd_fdr/blacklist_token.txt
description_score_bit_score_weight: 0.2
tair:
weight: 50
file: $files[2]
blacklist: $ahrd_fdr/blacklist_descline.txt
filter: $ahrd_fdr/filter_descline_tair.txt
token_blacklist: $ahrd_fdr/blacklist_token.txt
description_score_bit_score_weight: 0.4
trembl:
weight: 10
file: $files[3]
blacklist: $ahrd_fdr/blacklist_descline.txt
filter: $ahrd_fdr/filter_descline_trembl.txt
token_blacklist: $ahrd_fdr/blacklist_token.txt
description_score_bit_score_weight: 0.4
#interpro_database: $ahrd_fdr/interpro_31.xml
#interpro_result: $ahrd_fdr/interpro_result.raw
#gene_ontology_result: $ahrd_fdr/go_results.csv
token_score_bit_score_weight: $p1
token_score_database_score_weight: $p2
token_score_overlap_score_weight: $p3
description_score_relative_description_frequency_weight: 0.6
output: $output_file
';
# run AHRD
run_cmd("java -Xmx20g -jar $ahrd_fdr/ahrd.jar $temp_ahrd_yml") unless -s $output_file;
}
=head2
rnaseq_fixanno: fix ahrd annotation result
=cut
sub rnaseq_fixanno
{
my ($options, $files) = @_;
my $usage = qq'
USAGE: $0 -t fixanno [options] input_blast1 input_blast2 ... input_blastN
-a ahrd_file (output of ahrd annotation)
-e evalue (report blast hit report) [default: 1e-5]
';
print $usage and exit unless $$files[0];
foreach my $blast ( @$files ) {
die "[ERR]blast file not exist: $blast\n" unless -s $blast;
}
my $evalue = 1e-5;
$evalue = $$options{'e'} if (defined $$options{'e'} && $$options{'e'} > 0);
my $ahrd_result;
$ahrd_result = $$options{'a'} if defined $$options{'a'};
die "[ERR]ahrd file not exist: $ahrd_result\n" unless -s $ahrd_result;
$evalue = 'NA' if $ahrd_result; # disable evalue cutoff
# main: put blast hit to hash
# key: protein/transcript ID
# value: hit
my %id_hit;
my $id_hit = \%id_hit;
foreach my $blast ( @$files )
{
print "Parsing blast file: $blast ....\n";
$id_hit = parse_blast($blast, $id_hit, $evalue);
}
# parse ahrd result
if (defined $ahrd_result) {
my $out = IO::File->new(">".$ahrd_result.".fixed") || die $!;
my $fh = IO::File->new($ahrd_result) || die $!;
for (1 .. 3) {
my $line = <$fh>;
print $out $line;
}
while(<$fh>)
{
chomp;
# Protein-Accession Blast-Hit-Accession AHRD-Quality-Code Human-Readable-Description Interpro-ID (Description) Gene-Ontology-ID (Name)
my @a = split(/\t/, $_);
my $id = $a[0];
$a[3] = "No hit" unless defined $$id_hit{$id};
print $out join("\t", @a)."\n";
}
$fh->close;
$out->close;
}
print "done\n";
}
# parse blast reulst
sub parse_blast
{
my ($blast, $id_hit, $evalue) = @_;
my ($hit_num, $query_name);
my $query_hit_num = 0;
my $in = Bio::SearchIO->new(-format=>'blast', -file=>$blast);
while(my $result = $in->next_result)
{
$hit_num = 0;
$query_name = $result->query_name;
while(my $hit = $result->next_hit)
{
if ($evalue eq 'NA') {
if ($query_name ne $hit->name) {
$hit_num++;
}
}
else
{
if (($query_name ne $hit->name) && ($hit->significance < $evalue)) {
$hit_num++;
}
}
}
if ($hit_num > 0)
{
$$id_hit{$query_name} = 1;
$query_hit_num++;
}
}
print "No. of hit for $blast: $query_hit_num\n";
return $id_hit;
}
#################################################################
# kentnf: subroutine for reference pipeline #
#################################################################
=head2
rnaseq_align: align rnaseq_read to reference genome, generate report
=cut
sub rnaseq_align
{
my ($options, $files) = @_;
my $usage = qq'
USAGE: $0 [options] input1.fq input2.fq ...... | input1_r1_fq,input1_r2.fq input2_r1_fq,input2_r2.fq ...
-a [String] aligner name, tophat or hisat (default: tophat)
-s [String] Sequencing method for read files: (required)
PE (paired-end);
SE (single-end);
PS (paired strand-specific);
SS (single strand-specific);
-d [String] Indexed database of genome sequences (required)
-l [String] Library type, fr-unstranded, fr-firststrand, fr-secondstrand (required)
-p [Integer] number of CPUs used for megablast clustering (default = 1)
-r [Integer] mate-inner-dist (default = 100)
-v [Integer] mate-std-dev (default = 20)
-n [Integer] read mismatches (default = 0)
-g [Integer] max-multihits (default : 20)
-f [String] gene feature file (optional)
* input file could be fq, fa, or gzip file
';
# check & correct parameters
die "[ERR]no database\n$usage" unless defined $$options{'d'};
my @index_files = (
$$options{'d'}.".1.bt2", $$options{'d'}.".2.bt2",
$$options{'d'}.".3.bt2", $$options{'d'}.".4.bt2",
$$options{'d'}.".5.bt2", $$options{'d'}.".6.bt2",
$$options{'d'}.".rev.1.bt2", $$options{'d'}.".rev.2.bt2",
$$options{'d'}.".rev.5.bt2", $$options{'d'}.".rev.6.bt2"
);
my $cpu = 20; $cpu = $$options{'p'} if defined $$options{'p'};
my $mismatch = 1; $mismatch = $$options{'n'} if defined $$options{'n'};
foreach my $index_file (@index_files) {
unless(-s $index_file) { die "[ERR] $index_file not exist\n"; }
}
# set the bin folder to ENV path
$ENV{'PATH'} = ${FindBin::RealBin}."/bin".":".$ENV{'PATH'};
my ($tophat_bin, $hisat_bin) = ('tophat2', 'hisat');
if (-s "${FindBin::RealBin}/bin/tophat-2.0.11.Linux_x86_64/tophat2" ) {
$tophat_bin = "${FindBin::RealBin}/bin/tophat-2.0.11.Linux_x86_64/tophat2";
}
#print "Topaht2:\n".$tophat_bin."\n";
my $aligner = 'hisat';
$aligner = 'tophat' if (defined $$options{'a'} && $$options{'a'} eq 'tophat');
# check input files
# key: output_suffix; value: input_file
my @input_files;
foreach my $f ( @$files )
{
if ($f =~ m/,/)
{
my @a = split(/,/, $f);
die "[ERR]file $a[0] or $a[1] not exist\n" unless (-s $a[0] && -s $a[1]);
my $fm1 = check_seq_format($a[0]);
my $fm2 = check_seq_format($a[1]);
die "[ERR]file $a[0] and $a[1] have diff format\n" if $fm1 ne $fm2;
push(@input_files, [$a[0],$a[1],$fm1]);
}
else
{
die "[ERR]file $f not exist\n" unless -s $f;
my $fm1 = check_seq_format($f);
push(@input_files, [$f,'',$fm1]);
}
}
die "[ERR]no input files\n" if (scalar(@input_files) == 0);
# main
# 1. create header of report file
my $report_stat = "";
if ($$options{'s'} =~ m/^S/) {
$report_stat.="#sample\ttotal\tmapped\t%mapped\tmhit\t%mhit\n";
} else {
$report_stat.="#sample\ttotal\tmapped\t%mapped\tmhit\t%mhit";
$report_stat.="\tLeftMap\t%LeftMap\tLeftMhit\t%LeftMhit\tRightMap\t%RightMap\tRightMhit\t%RightMhit\n";
}
# 2. tophat mapping
foreach my $f (@input_files)
{
my ($r1, $r2, $seq_type) = @$f;
my ($input, $output, $format, $library, $report_file);
# run tophat
my $cmd_align = '';
if ($aligner eq 'tophat')
{
if ($r2) { $input = "$r1 $r2"; } else { $input = $r1; }
$output = $r1."_tophat";
$report_file = $output."/align_summary.txt";
$cmd_align = "$tophat_bin -o $output --library-type $$options{'l'} -p $cpu ";
$cmd_align.= "--segment-mismatches $mismatch --read-mismatches $mismatch --read-edit-dist $mismatch --read-gap-length $mismatch ";
$cmd_align.= "--max-multihits 20 --segment-length 25 $$options{'d'} $input";
}
else
{
if ($r2) { $input = "-1 $r1 -2 $r2"; } else { $input = "-U $r1"; }
$output = $r1."_hisat";
if ($seq_type eq 'fasta') { $format = '-f'; }
elsif ($seq_type eq 'fastq') { $format = '-q'; }
else { die "[ERR]seq froamt $r1, $r2, $seq_type\n"; }
if ($$options{'l'} eq 'fr-firststrand') {
if ($r2) { $library = 'RF'; } else { $library = "R"; }
} elsif ($$options{'l'} eq 'fr-secondstrand') {
if ($r2) { $library = 'FR'; } else { $library = "F"; }
} else {
$library = '';
}
$report_file = "$output.report.txt";
my $penalty = 1;
my $score_min = $penalty * $mismatch;
if ($library) { $library = "--rna-strandness $library"; }
$cmd_align = "$hisat_bin --time -p $cpu --no-unal $library $format ";
$cmd_align.= "-k 20 --mp $penalty,$penalty --rdg 0,$penalty --rfg 0,$penalty --np $penalty --score-min C,-$score_min,0 --ignore-quals ";
$cmd_align.= "-x $$options{'d'} $input -S $output.sam 1>$report_file 2>&1";
}
# print $cmd_align."\n"; next;
run_cmd($cmd_align);
# parse report file
my $report_info = parse_align_report_file($report_file, $r1);
$report_stat.= $report_info;
}
# output report information to file
my $stat_file = "report_ralign_stat.txt";
my $out = IO::File->new(">".$stat_file) || die $!;
print $out $report_stat;
$out->close;
}
=head2
parse_align_report_file: child of rnaseq_align
=cut
sub parse_align_report_file
{
my ($report_file, $f) = @_;
my $rinfo = `cat $report_file`;
chomp($rinfo); my @r = split(/\n/, $rinfo);
my $report_info = '';
my ($total, $mapped, $mhit, $lefttotal, $leftmap, $leftmhit, $righttotal, $rightmap, $rightmhit, $unmap, $single_hit);
if ($report_file =~ m/align_summary\.txt/)
{
if (scalar(@r) > 10)
{
if ( $r[1] =~ m/Input\s+:\s+(\d+)/ ) { $lefttotal = $1; }
if ( $r[2] =~ m/Mapped\s+:\s+(\d+)/ ) { $leftmap = $1; }
if ( $r[3] =~ m/of these:\s+(\d+)/ ) { $leftmhit = $1; }
if ( $r[5] =~ m/Input\s+:\s+(\d+)/ ) { $righttotal = $1; }
if ( $r[6] =~ m/Mapped\s+:\s+(\d+)/ ) { $rightmap = $1; }
if ( $r[7] =~ m/of these:\s+(\d+)/ ) { $rightmhit = $1; }
if ( $r[10] =~ m/Aligned pairs:\s+(\d+)/ ) { $mapped = $1; }
if ( $r[11] =~ m/of these:\s+(\d+)/ ) { $mhit = $1; }
print "[WARN]Left ($lefttotal) is not same as right ($righttotal)\n" if $lefttotal ne $righttotal;
$total = $lefttotal;
$report_info.="$f\t$total\t$mapped\t".sprintf("%.2f", ($mapped/$total)*100);
$report_info.="\t$mhit\t".sprintf("%.2f", ($mhit/$mapped)*100);
$report_info.="\t$leftmap\t".sprintf("%.2f", ($leftmap/$lefttotal)*100);
$report_info.="\t$leftmhit\t".sprintf("%.2f", ($leftmhit/$leftmap)*100);
$report_info.="\t$rightmap\t".sprintf("%.2f", ($rightmap/$righttotal)*100);
$report_info.="\t$rightmhit\t".sprintf("%.2f", ($rightmhit/$rightmap)*100)."\n";
}
else
{
if ( $r[1] =~ m/Input\s+:\s+(\d+)/ ) { $total = $1; }
if ( $r[2] =~ m/Mapped\s+:\s+(\d+)/ ) { $mapped = $1;}
if ( $r[3] =~ m/of these:\s+(\d+)/ ) { $mhit = $1; }
$report_info.="$f\t$total\t$mapped\t".sprintf("%.2f", ($mapped/$total)*100);
$report_info.="\t$mhit\t".sprintf("%.2f", ($mhit/$mapped)*100)."\n";
}
}
else
{
if ($rinfo =~ m/concordantly/)
{
foreach my $r (@r)
{
if ($r =~ m/(\d+) reads; of these:/) { $total = $1; }
if ($r =~ m/(\d+) \(\S+\) aligned concordantly 0 times/) { $unmap = $1; }
if ($r =~ m/(\d+) \(\S+\) aligned concordantly exactly 1 time/) { $single_hit = $1; }
if ($r =~ m/(\d+) \(\S+\) aligned concordantly >1 times/) { $mhit = $1; }
}
}
else
{
foreach my $r (@r)
{
if ($r =~ m/(\d+) reads; of these:/) { $total = $1; }
if ($r =~ m/(\d+) \(\S+\) aligned 0 times/) { $unmap = $1; }
if ($r =~ m/(\d+) \(\S+\) aligned exactly 1 time/) { $single_hit = $1; }
if ($r =~ m/(\d+) \(\S+\) aligned >1 times/) { $mhit = $1; }
}
}
$mapped = $single_hit + $mhit;
$report_info.="$f\t$total\t$mapped\t".sprintf("%.2f", ($mapped/$total)*100);
$report_info.="\t$mhit\t".sprintf("%.2f", ($mhit/$mapped)*100)."\n";
}
return $report_info;
}
=head2
rnaseq_tport: parse tophat report file to generate result
=cut
sub rnaseq_tport
{
my ($options, $files) = @_;
my $usage = qq'
USAGE: $0 align_summary1.txt align_summary2.txt ... align_summary3.txt > report_tophat.txt
';
print $usage and exit if scalar(@$files) == 0;
# checking input files
foreach my $f (@$files) {
print "[ERR]no file $f\n" and exit unless -s $f;
}
# parse report file
my $report_tophat;
my $report_line_num = 0;
foreach my $f (@$files) {
my $rinfo = `cat $f`;
chomp($rinfo);
my @r = split(/\n/, $rinfo);
my ($total, $mapped, $mhit, $lefttotal, $leftmap, $leftmhit, $righttotal, $rightmap, $rightmhit);
if (scalar @r == 14 || scalar @r == 13)
{
if ( $r[1] =~ m/Input\s+:\s+(\d+)/ ) { $lefttotal = $1; }
if ( $r[2] =~ m/Mapped\s+:\s+(\d+)/ ) { $leftmap = $1; }
if ( $r[3] =~ m/of these:\s+(\d+)/ ) { $leftmhit = $1; }
if ( $r[5] =~ m/Input\s+:\s+(\d+)/ ) { $righttotal = $1; }
if ( $r[6] =~ m/Mapped\s+:\s+(\d+)/ ) { $rightmap = $1; }
if ( $r[7] =~ m/of these:\s+(\d+)/ ) { $rightmhit = $1; }
if ( $r[10] =~ m/Aligned pairs:\s+(\d+)/ ) { $mapped = $1; }
if ( $r[11] =~ m/of these:\s+(\d+)/ ) { $mhit = $1; }
print "[WARN]Left ($lefttotal) is not same as right ($righttotal)\n" if $lefttotal ne $righttotal;
$total = $lefttotal;
$report_tophat.="$f\t$total\t$mapped\t".sprintf("%.2f", ($mapped/$total)*100);
$report_tophat.="\t$mhit\t".sprintf("%.2f", ($mhit/$mapped)*100);
$report_tophat.="\t$leftmap\t".sprintf("%.2f", ($leftmap/$lefttotal)*100);
$report_tophat.="\t$leftmhit\t".sprintf("%.2f", ($leftmhit/$leftmap)*100);
$report_tophat.="\t$rightmap\t".sprintf("%.2f", ($rightmap/$righttotal)*100);
$report_tophat.="\t$rightmhit\t".sprintf("%.2f", ($rightmhit/$rightmap)*100)."\n";
$report_line_num = 14 if scalar(@r) > $report_line_num;
}
elsif (scalar @r == 5)
{
if ( $r[1] =~ m/Input\s+:\s+(\d+)/ ) { $total = $1; }
if ( $r[2] =~ m/Mapped\s+:\s+(\d+)/ ) { $mapped = $1;}
if ( $r[3] =~ m/of these:\s+(\d+)/ ) { $mhit = $1; }
$report_tophat.="$f\t$total\t$mapped\t".sprintf("%.2f", ($mapped/$total)*100);
$report_tophat.="\t$mhit\t".sprintf("%.2f", ($mhit/$mapped)*100)."\n";
$report_line_num = 5 if scalar(@r) > $report_line_num;
}
else
{
print "[WARN]erro report format $f\n";
}
}
my $report_head = '';
if ($report_line_num == 14) {
$report_head.="#sample\ttotal\tmapped\t%mapped\tmhit\t%mhit";
$report_head.="\tLeftMap\t%LeftMap\tLeftMhit\t%LeftMhit\tRightMap\t%RightMap\tRightMhit\t%RightMhit\n";
} else {
$report_head.="#sample\ttotal\tmapped\t%mapped\tmhit\t%mhit\n";
}
$report_tophat = $report_head.$report_tophat;
my $tophat_report_file = "report_tophat.txt";
my $out = IO::File->new(">".$tophat_report_file) || die $!;
print $out $report_tophat;
$out->close;
}
=head2
rnaseq_count: count and normalization for rnaseq analysis
generate raw count and rpkm dataset for statistics analysis
=cut
sub rnaseq_count
{
my ($options, $files) = @_;
my $usage = qq'
USAGE: $0 [options] input1.bam input2.bam ......
-s [String] Sequencing method for read files: (required)
PE (paired-end);
SE (single-end);
PS (paired strand-specific);
SS (single strand-specific);
-f [String] gene feature file
-a [Num] extend 5\' length of feature position
-b [Num] extend 3\' length of feature position
-l [String] Library type, fr-unstranded, fr-firststrand, fr-secondstrand (required)
-o [String] prefix of output file (default : exp)
';
# check parameters
die "[ERR]no input file\n$usage" if (scalar(@$files) == 0);
die "[ERR]no feature file\n$usage" unless defined $$options{'f'} && -s $$options{'f'};
my $feature_bed = $$options{'f'};
die "[ERR]sequencing method\n$usage" unless defined $$options{'s'};
my $sequencing = $$options{'s'};
die "[ERR]library type\n$usage" unless defined $$options{'l'};
my $library = $$options{'l'};
my $output_prefix = "exp";
$output_prefix = $$options{'o'} if defined $$options{'o'};
my $output_exp_raw = $output_prefix."_raw_count.txt";
die "[ERR]raw count exist\n" if -s $output_exp_raw;
my ($p5extend, $p3extend) = (0,0);
$p5extend = $$options{'a'} if defined $$options{'a'} && $$options{'a'} > 0;
$p3extend = $$options{'b'} if defined $$options{'b'} && $$options{'b'} > 0;
# check alignment file
foreach my $f ( @$files ) {
die "[ERR]no alignment file $f\n" unless -s $f;
die "[ERR]no bam file $f\n" unless $f =~ m/\.(bam|sam)$/;
}
# load feature into to hash
# set bin size
# change it base on genome size and density of feature
# set it small will use more memory, set it big will take more time to locate read to feature
my $bin_size = 1000;
my %feature_struc = load_feature_bed($feature_bed, $bin_size, $p5extend, $p3extend);
# count expression for alignment
# key: file_name, feature_id, strand (sense or antisense)
# value: raw count
my %exp;
my %flag_plus = ('147' => 1, '99' => 1, '145' => 1, '97' => 1,
'355' => 1, '403'=> 1, '353' => 1, '401'=> 1);
my %flag_minus= ('163' => 1, '83' => 1, '161' => 1, '81' => 1,
'339' => 1, '419'=> 1, '337' => 1, '417'=> 1);
foreach my $f ( @$files ) {
my $sam = $f;
$sam =~ s/\.bam/\.sam/ if $f =~ m/\.bam$/;
run_cmd("samtools view -h -o $sam $f") unless -s $sam;
# uniq reads for Paired reads
my %PE_uniq = ();
# code for count expression
my $fh = IO::File->new($sam) || die $!;
while(<$fh>)
{
chomp;
next if $_ =~ m/^@/;
my @a = split(/\t/, $_);
my $chr = $a[2];
my $read_start = $a[3]; # get read start for single end
my $mapped_len = parse_cigar($a[5]);
my $read_end = $a[3] + $mapped_len - 1; # get read end for single end
my $flag = $a[1];
# uniq the Paired reads, count only once for each fragment
if ($sequencing =~ m/^P/) {
my $key = '';
if ($a[6] eq '=') # paired reads aligned to same reference
{
$a[6] = $a[2];
if ($a[3] < $a[7]) {
$key = $a[0]."\t".$a[2]."\t".$a[3]."\t".$a[6]."\t".$a[7];
$read_start = $a[3];
$read_end = $a[3] + $a[8] - 1;
} else {
$key = $a[0]."\t".$a[6]."\t".$a[7]."\t".$a[2]."\t".$a[3];
$read_start = $a[7];
$read_end = $a[7] + abs($a[8]) - 1;
}
}
else # paired reads aligned to diff reference
{
$key = $a[0]."\t".$a[2]."\t".$a[3]."\t".$a[6]."\t".$a[7];
}
if ( defined $PE_uniq{$key} ) { next; }
else { $PE_uniq{$key} = 1; }
}
my $bin_start_pos = int($read_start/$bin_size);
my $bin_end_pos = int($read_end/$bin_size);
my %feature_pos = (
$bin_start_pos-1 => 1,
$bin_start_pos =>1,
$bin_start_pos+1 => 1,
$bin_end_pos-1 => 1,
$bin_end_pos => 1,
$bin_end_pos+1 => 1
);
my $feature = '';
foreach my $fpos (sort {$a<=>$b} keys %feature_pos ) {
$feature .= $feature_struc{$chr."\t".$fpos} if defined $feature_struc{$chr."\t".$fpos};
}
next unless $feature; # the read do not overlapped with feature
my %uniq_feature;
my $uniq_feature = '';
chomp ($feature);
my @fea = split(/\n/, $feature);
foreach my $fea (@fea) {
unless ( defined $uniq_feature{$fea} ) {
$uniq_feature{$fea} = 1;
$uniq_feature .= $fea."\n";
}
}
# main: count reads number with strand-specific method
# just count reads at this step
my $fstat = "";
if ($sequencing =~ m/^S/) {
$fstat = "P" if ($flag eq '0' || $flag eq '256');
$fstat = "M" if ($flag eq '16' || $flag eq '272');
} else {
$fstat = "P" if defined $flag_plus{$flag};
$fstat = "M" if defined $flag_minus{$flag};
}
next unless $fstat; # skip if do n
chomp($uniq_feature); @fea = split(/\n/, $uniq_feature);
foreach my $fea (@fea) {
my @b = split(/\t/, $fea);
if (($read_start >= $b[1] && $read_start <= $b[2]) || ($read_end >= $b[1] && $read_end <= $b[2]))
{
if ( defined $exp{$f}{$b[3]}{$fstat} ) { $exp{$f}{$b[3]}{$fstat}++; }
else { $exp{$f}{$b[3]}{$fstat}=1; }
}
}
}
$fh->close;
unlink($sam) if $f =~ m/\.bam$/; # remove sam for saving space
}
# output raw count
my $sense_raw_table = "FeatureID";
my $antisense_raw_table = "FeatureID";
foreach my $f (@$files) {
$sense_raw_table.="\t".$f;
$antisense_raw_table.="\t".$f;
}
$sense_raw_table.="\n";
$antisense_raw_table.="\n";
my $fh = IO::File->new($feature_bed) || die $!;
while(<$fh>)
{
chomp;
next if $_ =~ m/^#/;
my @a = split(/\t/, $_);
my ($fid, $length, $strand) = ($a[3], $a[4], $a[5]);
$sense_raw_table.=$fid;
$antisense_raw_table.=$fid;
foreach my $f (@$files)
{
my ($ct_plus, $ct_minus) = (0,0);
$ct_plus = $exp{$f}{$fid}{'P'} if defined $exp{$f}{$fid}{'P'};
$ct_minus = $exp{$f}{$fid}{'M'} if defined $exp{$f}{$fid}{'M'};
my $ct = $ct_plus + $ct_minus;
if ($sequencing =~ m/E$/)
{
$sense_raw_table.= "\t".$ct;
}
else # count raw count for strand specific read
{
if ( $library eq 'fr-firststrand' )
{
if ($strand eq "+") {
$sense_raw_table.= "\t".$ct_minus;
$antisense_raw_table.= "\t".$ct_plus;
} else {
$sense_raw_table.= "\t".$ct_plus;
$antisense_raw_table.= "\t".$ct_minus;
}