forked from grubbybio/pRNASeqTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhaseTank_v1.0.pl
executable file
·2311 lines (1431 loc) · 61.5 KB
/
PhaseTank_v1.0.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
use strict;
use warnings;
use Getopt::Long;
my $version_num = "1.0";
my $help_message = Full_help($version_num);
#die if no any input info:
unless($ARGV[0]){
die "$help_message" ;
}
#######################################DECLARE VARIABLES##########################################
my ($genome,$transcript_file,$ncrna_file,$miRNA_file,$degradome_file,$result_dir,$mixed_file);
my ($help,$version,$run_time,$now_time,$lib,$lib_num);
my ($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdst);
my ($pred_table,$alignment,$cluster,$phasiRNA,$TAS_GENE,$miRNA_target_TAS,$phasiRNA_target,$cascade_file,$target_genes,$run_log);
my (@libs,%chr_seq,%chr_len,%ref_info,%aborted_reads,%reads_beg,%reads_seq,%phased_ratio,%phased_num,%phased_score,%tasiRNA_target);
my $normalization = 20000000;
my $PHASED_RATIO = 0.3;
my $PHASED_NUMBER= 4;
my $PHASED_ABUN = 100;#the total phasiRNA reads abundance should more than $PHASED_ABUN.
my $PHASED_ISLAND = 105;
my $PHASED_DRIFT = 2;
my $phased_size = 21;
my $extended_maplen = 80;
my $most_map_time = 5;
my $min_reads_abun =1; # minimum reads abundance kept in the mixed reads libraries.
my $asrp = 0.129;
my $RSRP = 1;
my $percent = 0.05;
my $min_phasiRNA_abun = 50; #minimum phasiRNAs abundance kept for phasiRNA target prediction.
my $CALL_RSRP;
my $target_p;
my $target_m;
($sec,$min,$hour,$day,$mon,$year,$wday,$yday,$isdst) = localtime();
$year += 1900;
$mon += 1;
$run_time = sprintf("%d.%02d.%02d_%02d.%02d",$year,$mon,$day,$hour,$min);
$result_dir ="OUTPUT_$run_time";
$mixed_file = "Mixed_reads";
############################### OPTIONS #######################################################
#get options:
GetOptions ('genome=s' => \$genome,
'cdna=s' => \$transcript_file,
'lib=s' => \$lib,
'filter=s' => \$ncrna_file,
'miR=s' => \$miRNA_file,
'degradome=s' => \$degradome_file,
'target=s' => \$target_genes,
'trigger_miRNA!' => \$target_m,
'phasiRNA_target!' => \$target_p,
'ratio=f' => \$PHASED_RATIO,
'number=i' => \$PHASED_NUMBER,
'abun=i' => \$PHASED_ABUN,
'READ_abun=i' => \$min_reads_abun,
'phasiRNA_abun=i' => \$min_phasiRNA_abun,
'drift=i' => \$PHASED_DRIFT,
'size=i' => \$phased_size,
'nor=i' => \$normalization,
'island=i' => \$PHASED_ISLAND,
'extendLEN=i' => \$extended_maplen,
'max_hits=i' => \$most_map_time,
'per=f' => \$percent,
'rsrp=f' => \$RSRP,
'CALL_RSRP!' => \$CALL_RSRP,
'dir=s' => \$result_dir,
'version' => \$version,
'help' => \$help);
# If help, print and quit:
if ($help){
die "$help_message\n";
}
# If version, print version and quit:
if ($version){
die "PhaseTank version $version_num.\n";
}
unless ($transcript_file or $genome){
die "No transcript file or genome data, input transcript by '--cdna' or input genome data by '--genome'.\n";
}
unless ($lib){
die "No small RNA sequencing data, input by '--lib'.\n";
}
@libs = split /\,/,$lib;
$lib_num = @libs;
##warning if no corresponding files:
if ($target_m){
unless ($miRNA_file){
die "No miRNA_file input for searching triggered miRNAs\n";
}
unless ($degradome_file){
die "No degradome_file input for searching triggered miRNAs\n";
}
}
if ($target_p){
unless ($target_genes){
die "No target genes for phasiRNA target prediction, input the target file by '--target'.\n";
}
unless ($degradome_file){
die "no degradome_file input for searching targets of phasiRNAs\n";
}
}
if ($CALL_RSRP){
if ($genome){
die "PhaseTank do not use genome data to predict the RSRP value. Please remove your genome data\n";
}
unless ($transcript_file){
die "PhaseTank predict the RSRP value only using the whole cDNA data, input it by '--cdna'\n";
}
}
#################################### SET OUTPUT FILES ###########################################
#make directory for the output files, which referred as its running time:
system "mkdir $result_dir";
my $path="$result_dir" . "\/";
$pred_table = $path . "Pred_tab_$run_time";
$alignment = $path . "Align_$run_time";
$phasiRNA = $path . "PhasiRNA_$run_time";
$run_log = $path . "Run_log_$run_time";
$cluster = $path ."Excised_cluster_$run_time";
if ($target_m){
$TAS_GENE = "TAS_gene.fa";
}
if ($target_m and $target_p){
#if ($target_m){
$cascade_file = $path ."Cascades_$run_time";
}
#make directory for bowtie index files:
my $bowtie_index = "Bowtie-index";
#remove the previous files if it would be used again:
if (-d $bowtie_index){
system "rm $bowtie_index/*";
}else{
system "mkdir $bowtie_index";
}
if (-d $mixed_file){
system "rm $mixed_file";
}
################################### MAIN ########################################################
#Print the input files :
$now_time = Watch_time();
Print_report("\n############################## PhaseTank version$version_num ##############################\n\nRun time: $now_time\n\n");
Print_report("#Input files:\n\n");
if ($transcript_file){
Print_report( "\tRerference data: $transcript_file\n");
}elsif($genome){
Print_report( "\tRerference data: $genome\n");
}
Print_report("\tRead file:\n");
my $n=0;
foreach my $read(@libs){
$n++;
Print_report("\t\t\($n\): $read\n");
}
if ($ncrna_file){
Print_report("\tncRNA file: $ncrna_file\n");
}
if ($miRNA_file){
Print_report("\tmiRNA file: $miRNA_file\n");
}
if ($degradome_file){
Print_report("\tDegradome file: $degradome_file\n\n");
}
#Check the input files:
$now_time = Watch_time();
Print_report("##Begin to check files at $now_time..\n");
Check_input_files();
Print_report("\n##Finshed checking files\n");
#Normalize the reads abundance and merge them:
$now_time = Watch_time();
if ($lib_num > 1){
Print_report("\n\n#Begin to merge libraries at $now_time..\n\n");
}else{
Print_report("\n\n#Begin to normalize library at $now_time..\n\n");
}
Print_report("#Begin to merge libraries at $now_time..\n\n");
Merge_libraries();
if ($ncrna_file){
$now_time = Watch_time();
Print_report("\n#Begin to exclude other ncRNA mappable reads at $now_time..\n\n");
Exclude_ncRNAs() ;
}
if ($genome){
$now_time = Watch_time();
Print_report("\n#Begin to excise RNA_producing cluster from genome data at $now_time\n\n");
Produce_rna_cluster();
Print_report("\n\t#Finish cluster finding \n");
}
$now_time = Watch_time();
Print_report("\n#Begin to map reads to reference seq and parse the mapped reads data at $now_time\n");
Map_reads_Fill_hash($transcript_file,"transcript.fa",\%reads_beg,\%reads_seq);
$now_time = Watch_time();
Print_report("\n#Begin to parse reference information at $now_time\n");
Parse_ref_files();
Print_report("\n##Finished parsing files!\n\n");
#Activated by option '--CALL_RSRP':
if ($CALL_RSRP){
$now_time = Watch_time();
Print_report("\n#Begin to compute ASRP and RSRP_cut_off at $now_time\n\n");
Compute_RSRP();
}
#Print out the important values used:
Print_report("\n##Important Values in PhaseTank:\n\n");
Print_report("\tLibrary_number: $lib_num\n\tASRP: $asrp\n\tRSRP: $RSRP\n");
Print_report("\tphased ratio: $PHASED_RATIO\n\tphased num: $PHASED_NUMBER\n");
Print_report("\tphased size: $phased_size nt\n\n");
$now_time = Watch_time();
Print_report("#Begin to process transcripts into core program at $now_time..\n\n");
Core_process();
Print_report("#Ready to OUTPUT the files:\n\n");
Output_files();
#Activated by option '--target_m':
if ($target_m){
$now_time = Watch_time();
Print_report("#Begin to find the triggered miRNAs by CleaveLand4 at $now_time..\n\n");
$miRNA_target_TAS = $path . "miRNA_target_$run_time";
Call_Cleaveland_miRNA_target();
}
#Activated by option '--target_P':
if ($target_p){
$now_time = Watch_time();
Print_report("#Begin to find the phasiRNAs targeted transcripts by CleaveLand4 at $now_time, phasiRNAs abundance >= $min_phasiRNA_abun..\n\n");
$phasiRNA_target = $path . "PhasiRNA_target_$run_time";
Call_Cleaveland_phasiRNA_target();
}
#To find the internal correlations for regulating networks:
if ($target_m and $target_p){
$now_time = Watch_time();
Print_report("#Begin to find regulatory cascades at $now_time..\n");
Finding_cascades();
}
#To remove the Mixed_reads file merged by the reads libraries:
system `rm $mixed_file`;
$now_time = Watch_time();
Print_report("##Finished all processes in PhaseTank at $now_time!!\n\n#You can check the output files in directory './$result_dir/'.\n\n");
################################# HELP INFO #######################################################
sub Full_help {
my($ver) = @_;
my $help_info = "\nPhaseTank.pl version $ver
########################## PhaseTank_v1.0.pl ##########################
Copyright (c) 2014 Qingli Guo
Author:
Qingli Guo, Northwest A&F University, email:guoql.karen\@gmail.com
Version:
version 1.0; July 30 2014
#######################################################################
##Running PhaseTank from the following command:
Usage: \$perl PhaseTank.pl --genome <genome_file.fa> --lib <read_file_list> [options]
Or \$ perl PhaseTank.pl --cdna <cdna_file.fa> --lib <read_file_list> [options]
The followings are the detailed descriptions of the arguments and options in the use of PhaseTank:
Arguments:
--genome <string>. Supply PhaseTank with genome sequence in FASTA format as reference sequences. Or --cdna <string>. Also could supply PhaseTank with cdna sequence in FASTA format as reference sequences.
--lib <string>. Supply PhaseTank with a comma-separated list of file(s) containing reads in FASTA format.
Options:
--filter <string>. Supply PhaseTank with FASTA format of other ncRNA sequences. It can help PhaseTank to exclude the reads mapped to other ncRNAs (e.g. tRNA, rRNA, snoRNA).
--miR <string>. Supply PhaseTank with a list of miRNAs in FASTA format for miRNA-directed PHAS gene cleavage detection. This option will be ignored without ‘—trigger_miRNA’.
--degradome <string>. Supply PhaseTank with a set of degradome sequencing reads in FASTA format for phasiRNA targets prediction.
--target <string>. Supply PhaseTank with a FASTA format file containing the interested genes, among which to search the phasiRNA targets.
--trigger_miRNA. Tell PhaseTank to detect miRNA-directed TAS cleavage. It is inactive by default.
--phasiRNA_target. Tell PhaseTank to predict the phasiRNA targeting genes. It is inactive by default.
--ratio <float>. Set phased ratio cutoff value. The default is 0.3.
--number <int>. Set phased number cutoff value. The default is 4.
--abun <int>. The total abundance of phased reads in the phasiRNA cluster. Default is 100. Note, the default normalization level is per twenty millions (20,000,000, can be changed by ‘--nor <int>’), thus the default abundance value of 100 here is equal to setting 5 of RPM (reads per million).
--READ_abun <int>. The minimum reads abundance to keep for PhaseTank prediction. Default is 1, which means if one read abundance is less than 1, it will be abandoned.
--phasiRNA_abun <int>. Minimum read abundance of phasiRNAs for target prediction. This option will be ignored without ‘—phasiRNA_target’.
--drift <int>. Maximum phased drift. The default is 2.
--size <int>. Length of phased reads. The default is 21.
--nor <int>. Tell PhaseTank the normalization level for the libraries. Default is 20,000,000.
--island <int>. That is the maximum separation distance of two phasiRNAs in each cluster. The default is 84.
--extendLEN <int>. The length on each side of siRNA cluster (or phasiRNA cluster) will be excised from the reference sequence. The default is 80.
-- max_hits <int>. Tell PhaseTank the ‘-m’ cutoff while using Bowtie (‘-m’ represent the maximum mapped hits to the reference, if goes out the value, the reads will be filtered out). The default is 5 here. Note that with this parameter changed, the prediction results may fluctuate slightly in big and small dataset due to a few reads may be removed in the big dataset.
--per <float>. Within 0.01-1.00. The top percentage of RSRP value of sequences was put to the later program. The default is 0.05 (5%).
--rsrp <float>. The RSRP value for PhaseTank to filter the sequences. Default is 1.
--CALL_RSRP. Tell PhaseTank to estimate RSRP cutoff from the given reads libraries, which is set from the top 5% (default, can be changed by ‘—per <float>’) of RSRP value of sequences for the later processes. It is inactive by default. You could active this module by ‘--CALL_RSRP’ when you analyze other organisms (should use whole cDNA as input references). Or you also can use the default value instead.
--dir <string>. Set the directory in which PhaseTank will write its output files. The default is 'OUTPUT_run_time/'.
--help. Print the help message and quit.
--version. Print PhaseTank version number and quit.
Type \'PhaseTank.pl --help\' for full list of options
";
return $help_info;
}
##################################### SUB PROGRAMS ##########################################################
sub Check_input_files{
if ($transcript_file){
FASTA_examiner_cdna($transcript_file);
}
if ($genome){
FASTA_examiner($genome);
}
if ($ncrna_file){
FASTA_examiner($ncrna_file);
}
foreach my $file (@libs){
FASTA_examiner($file);
}
if ($degradome_file){
FASTA_examiner($degradome_file);
}
if ($miRNA_file){
FASTA_examiner($miRNA_file);
}
if ($degradome_file){
FASTA_examiner($degradome_file);
}
if ($target_genes){
FASTA_examiner($target_genes);
}
}
sub FASTA_examiner{
my ($file) = @_;
my $flag="no";
open FILE,"<$file" || die "FAIL: $file does not exsist!\n";
Print_report("\n\tChecking $file...\t");
while(my $line = <FILE>){
if ($line=~/^>/){
$flag="yes";
if ($line =~/\s/){
$line=~s/ /_/g;
}
}
}
unless ($flag eq "yes"){
die "ERROR in:$file is not in FASTA format.\n";
}
close(FILE);
}
sub FASTA_examiner_cdna{
my ($file) = @_;
my $flag="no";
open FILE,"<$file" || die "FAIL: $file does not exsist!\n";
Print_report("\n\tChecking $file...\n");
while(my $line = <FILE>){
if ($line=~/^>/){
$flag="yes";
if ($line =~/\s/){
$line=~s/ /_/g;
}
}
}
unless ($flag eq "yes"){
die "ERROR in:$file is not in FASTA format.\n";
}
close(FILE);
}
sub Merge_libraries{
my %hash;
foreach my $file(@libs){
open LIB,"<$file";
my $count=0;
my $abun=0;
my %tmp_hash=();
Print_report("\t",$file,"\t\t");
while( my $line1= <LIB>){
my $line2=<LIB>;
chomp ($line1,$line2);
if($line1 =~/^>/){
$count++;
my @tmps=split /_x/,$line1;
$abun+=$tmps[1];
$tmp_hash{$line2}=$tmps[1];
}
}
Print_report("lines:$count\t\tabun:$abun\n");
$count=0;
foreach my $seq( sort {$tmp_hash{$b} <=> $tmp_hash{$a}} keys %tmp_hash){
my $normarlized_abun=int($tmp_hash{$seq}/($abun * $lib_num)* $normalization);
$count++;
if (defined $hash{$seq}){
$hash{$seq}+=$normarlized_abun;
}else{
$hash{$seq}=$normarlized_abun;
}
}
close(LIB);
}
my $count=0;
open MIX,">$mixed_file";
foreach my $seq (sort {$hash{$b} <=> $hash{$a}} keys %hash){
my $abun = $hash{$seq};
if ($abun >$min_reads_abun){
$count++;
print MIX ">t$count","_x$abun\n",$seq,"\n";
}
}
close(MIX);
}
sub Parse_ref_files {
my ($seq,$beg,$end);
my $hash_name = "none";
my $flag;
open REF,"<$transcript_file";
while (my $line=<REF>){
chomp $line;
if ($line=~/^>(\S+)\s+(\d+)\s+(\d+)$/){
# to fill the hash and initialize the value for $hash_name and $seq;
if ($hash_name ne "none"){
$hash_name =~s/>//;
$ref_info{$hash_name}{'seq'} = $seq;
$ref_info{$hash_name}{'beg'} = $beg;
$ref_info{$hash_name}{'end'} = $end;
#remove the previous value:
$hash_name = ();
$seq = ();
$beg = ();
$end = ();
}
$hash_name = $1;
$beg=$2;
$end=$3;
}elsif($line=~/^>(\S+)$/){
if ($hash_name ne "none"){
$hash_name=~s/>//;
$beg = 1;
$end = length($seq);
$ref_info{$hash_name}{'seq'}=$seq;
$ref_info{$hash_name}{'beg'}=$beg;
$ref_info{$hash_name}{'end'}=$end;
#remove the previous value:
$hash_name = ();
$seq = ();
$beg = ();
$end = ();
}
$flag="alone";
$hash_name = $1;
}elsif ($line !~ /^>/){
$seq.=$line;
}
}
#fill %ref_info with the info of last transcript;
$hash_name =~s/>//;
$ref_info{$hash_name}{'seq'} = $seq;
if ($flag eq 'alone'){
$ref_info{$hash_name}{'beg'} = 1;
$ref_info{$hash_name}{'end'} = length($seq);
}else{
$ref_info{$hash_name}{'beg'} = $beg;
$ref_info{$hash_name}{'end'} = $end;
}
close(REF);
Print_report("\t$transcript_file has been parsed!\n\n");
}
sub Produce_rna_cluster{
my ($name,$line_num,$width,@loci,%chr_reads_beg,%chr_reads_seq);
my $id="chr.fa";
open CHR,"<$genome";
open CLUSTER,">$cluster";
Print_report("\tParsing $genome ..\n\n");
while (my $line=<CHR>){
chomp $line;
if ($line =~/^>(\S+)$/){
$name=$1;
$line_num=0;
}else{
$width=length($line) unless ($width);
$line_num++;
$chr_seq{$name}{$line_num}=$line;
$chr_len{$name}+=length($line);
}
}
close(CHR);
Map_reads_Fill_hash($genome,$id,\%chr_reads_beg,\%chr_reads_seq);
foreach my $chr (keys %chr_len){
@loci=();
foreach my $key ( sort{$chr_reads_beg{$chr}{$a} <=> $chr_reads_beg{$chr}{$b}} keys %{ $chr_reads_beg{$chr}}){
my $pos = $chr_reads_beg{$chr}{$key};
push @loci,$pos;
}
Define_cluster($chr,$width,\@loci);
}
close(CLUSTER);
$transcript_file = $cluster;
}
sub Define_cluster{
my ($chr,$width,$loci_ref)=@_;
my ($i,$lim,$cluster_beg,$cluster_end,$cluster_len,$cluster_num,$print_line);
$lim = @$loci_ref;
$cluster_num=0;
my @index=();
Print_report("\n\tSearching and extracting clusters from $chr ..\t");
for ($i = 0; $i<$lim; $i++){
if ($i == 0){
push @index, $i;
next;
}else{
my $distance=$$loci_ref[$i]-$$loci_ref[$i-1];
if ($distance<=100){
push @index,$i;
}else{
my $num=$#index;
if ($num<=3){
@index=();
push @index,$i;
next;
}
my $start_read_pos = $$loci_ref[$index[0]];
my $last_read_pos = $$loci_ref[$index[$num]];
next if ($last_read_pos-$start_read_pos <=3*$phased_size);
if ($start_read_pos>$extended_maplen){
$cluster_beg = $start_read_pos-$extended_maplen;
}else{
$cluster_beg = 1;
}
if ($last_read_pos+$extended_maplen+30<$chr_len{$chr}){
$cluster_end = $last_read_pos+$extended_maplen+30;
}else{
$cluster_end = $chr_len{$chr};
}
$cluster_len = $cluster_end-$cluster_beg+1;
$cluster_num++;
Extract_cluster_seq($chr,$cluster_beg,$cluster_end,$cluster_len,$cluster_num,$width,);
@index=();
push @index,$i;
}
}
}
Print_report("\tfound $cluster_num clusters\n\n");
}
sub Extract_cluster_seq{
my ($chr,$beg,$end,$len,$cluster_num,$width) = @_;
my($return_line,$cluster_seq,$remainder1,$remainder2,$remain,$seq1,$seq2,$seq3,$part1,$part3,$start_line,$end_line,$part2_beg,$part2_end,$step);
$remainder1 = $beg % $width;
$start_line = int($beg/$width) + 1;
$seq1=$chr_seq{$chr}{$start_line};
unless ($remainder1 == 0){
$part1 = substr($seq1,$remainder1-1);
}else{
$part1=$seq1;
}
$remain=$len-length($part1);
$step = int($remain/$width);
$part2_beg=$start_line+1;
$part2_end=$start_line+$step+1;
for (my $line=$part2_beg;$line<$part2_end;$line++){
$seq2.=$chr_seq{$chr}{$line};
}
unless ($seq2){
$seq2="";
}
$remainder2 = $remain % $width;
$seq3=$chr_seq{$chr}{$part2_end};
unless($remainder2 ==0){
$part3=substr($seq3,0,$remainder2);
}else{
$part3="";
}
$cluster_seq = $part1.$seq2.$part3;
print CLUSTER ">$chr","_$cluster_num\t$beg\t$end\n$cluster_seq\n";
}
sub Map_reads_Fill_hash{
my ($file,$id,$hash1_ref,$hash2_ref)=@_;
Print_report("#Making bowtie index for $file..\n\n");
my $bwt_ret1 = ` bowtie-build $file ./$bowtie_index/$id `;
Print_report("#Mapping reads to $file using bowtie..\n\n");
my $bwt_ret2 = ` bowtie -f -a -v 0 -m $most_map_time -x ./$bowtie_index/$id $mixed_file reads_mapped.bwt `;
my $map_count=0;
open READ,"<reads_mapped.bwt" || die "FAIL:reads_mapped.bwt file does not exsist!\n";
while (my $line = <READ>){
chomp $line;
my ($read_id,$str,$ref_id,$beg,$seq,$quality,$count) = split /\t/,$line ;
$map_count++;
my $key2 = "$read_id\t$str\t$map_count";
$beg+=2 if ($str eq '-'); #plus 2 is to adjust mapped position from anti-sense strand to sense strand;
$$hash1_ref{$ref_id}{$key2} = $beg;
$$hash2_ref{$ref_id}{$key2} = $seq;
}
close(READ);
system `rm reads_mapped.bwt`;
}
sub Exclude_ncRNAs {
my $ncrna_mapped="reads_mapped_ncrna.bwt";
my $new_reads="New_mixed_reads.fa";
Print_report("#Making bowtie index for $ncrna_file..\n\n");
my $bwt_ret1 = ` bowtie-build $ncrna_file ./$bowtie_index/ncrna.fa `;
Print_report("#Mapping reads to $ncrna_file using bowtie..\n\n");
my $bwt_ret2 = ` bowtie -f -a -v 0 ./$bowtie_index/ncrna.fa $mixed_file $ncrna_mapped`;
open READ,"<$ncrna_mapped" || die "FAIL:reads_mapped_ncrna.bwt file does not exsist!\n";
while (my $line =<READ>){
chomp $line;
my @tmps = split /\t/,$line;
$aborted_reads{$tmps[0]}="yes";
}
open NEWREADS,">$new_reads";
open MIXED,"<$mixed_file";
while (my $line1 =<MIXED>){
my $line2=<MIXED>;
if ($line1=~/^>(\S+)$/){
unless (defined $aborted_reads{$1}){
print NEWREADS "$line1$line2";
}
}
}
close(MIXED);
close(NEWREADS);
system `rm $mixed_file`;
$mixed_file=$new_reads;
system `rm $ncrna_mapped`;
Print_report("\nReads mapped to $ncrna_file has been excluded!\n\n");
}
sub Core_process {
open ALIGN, ">$alignment" || die "\t\n$alignment can not be written in!\n";
my $count=0;
foreach my $ref_id(keys %ref_info){
my @mapped_reads_plus =();
my @mapped_reads_minus = ();
my @mapped_reads = ();
my %bins = ();
my $abundance=0;
my $len=length ($ref_info{$ref_id}{'seq'});
my $srp = 0 ;
my $rsrp = 0;
my $start = 0;
my $end = $len-1;
# we discard the transcripts (a: srp lower than the cutoff; b: mapped on ssRNA);
Search_mapped_reads ($ref_id,\$abundance,\@mapped_reads_plus,\@mapped_reads_minus,\@mapped_reads);
$srp = $abundance / $len;
next if ($srp == 0);
$rsrp = sprintf("%.3f",log($srp/$asrp));
$ref_info {$ref_id}{'rsrp'} = $rsrp;
next if ($rsrp < $RSRP);
#if only single strand have mappable reads, we discard it:
my $mapped_plus = @mapped_reads_plus;
my $mapped_minus = @mapped_reads_minus;