-
Notifications
You must be signed in to change notification settings - Fork 50
/
quantifier.pl
executable file
·1500 lines (1185 loc) · 46.1 KB
/
quantifier.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
# quantifier module from the miRDeep2 software package
# Copyright (C) 2009 - 2012, 2014 - 2017, 2019 Sebastian Mackowiak
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
######################
## Author: SM
## Date: 04/04/2019
## read normalization is now 1e6 * mature-reads/all_mature_reads (So reads per million mapped miRNA reads)
## precursors with 0 read mappings cryptic warning messages to stderr removed
## A warning message is issued now in the very end if no mature sequence mapped to any precursor sequence
## windows and macos linebreaks get removed when converting fasta files
## RNAfold is only run for precursors with read mappings
######################
use strict;
use warnings;
use File::Path;
use File::Basename;
use Getopt::Std;
use Cwd qw(abs_path);
check_install();
my %hash;
my %hash_star;
my %hash_sample;
my %hash_star_sample;
my %total;
my $total_t = 0;
my $zero_mat_pres=0;
my %mapcounts;
my %seen;
my $species = "none";
my $time= time();
my %organisms;
my %rorganisms;
my ($u, $v);
while(<DATA>){
chomp;
if(/^(\S+)\s+(\S+)$/){
$u=lc($1);
$v=lc($2);
$u =~ s/ //g;
$v =~ s/ //g;
$organisms{$u}=$v;
$rorganisms{$v}=$u;
}
}
## options
my %options=();
getopts("p:m:r:s:t:y:dokuc:nxg:e:f:vjT:PWU",\%options);
## number of mismatches when mapping reads to precursors, default one
my $mismatches = 1;
$mismatches = $options{'g'} if(defined $options{'g'});
my $threads=1;
$threads = $options{'T'} if($options{'T'});
my $upstream = 2;
my $downstream = 5;
$upstream = $options{'e'} if(defined $options{'e'});
$downstream = $options{'f'} if(defined $options{'f'});
if($options{'u'}){
print STDERR "\n\nAllowed species arguments that have an entry at UCSC\n\n";
for(keys %organisms){
print STDERR "$_\t$organisms{$_}\n";
}
die "\n";
}
my $usage="usage:
\tperl quantifier.pl [options] -p precursor.fa -m mature.fa -r reads.fa -s star.fa -t species -y [timestamp] -d [pdfs] -o [sort] -k [stringent] -g [number of mismatches in reads vs precursor mappings]
[options]
[mandatory parameters]
\t-u\tlist all values allowed for the species parameter that have an entry at UCSC
\t-p precursor.fa miRNA precursor sequences from miRBase
\t-m mature.fa miRNA sequences from miRBase
\t-P specify this option of your mature miRNA file contains 5p and 3p ids only
\t-r reads.fa your read sequences
[optional parameters]
\t-c [file] config.txt file with different sample ids... or just the one sample id -- deprecated
\t-s [star.fa] optional star sequences from miRBase
\t-t [species] e.g. Mouse or mmu
\t if not searching in a specific species all species in your files will be analyzed
\t else only the species in your dataset is considered
\t-y [time] optional otherwise its generating a new one
\t-d if parameter given pdfs will not be generated, otherwise pdfs will be generated
\t-o if parameter is given reads were not sorted by sample in pdf file, default is sorting
\t-k also considers precursor-mature mappings that have different ids, eg let7c
\t would be allowed to map to pre-let7a
\t-n do not do file conversion again
\t-x do not do mapping against precursor again
\t-g [int] number of allowed mismatches when mapping reads to precursors, default 1
\t-e [int] number of nucleotides upstream of the mature sequence to consider, default 2
\t-f [int] number of nucleotides downstream of the mature sequence to consider, default 5
\t-j do not create an output.mrd file and pdfs if specified\n
\t-W read counts are weighed by their number of mappings. e.g. A read maps twice so each position gets 0.5 added to its read profile
\t-U use only unique read mappings; Caveat: Some miRNAs have multiple precursors. These will be underestimated in their expression since the multimappers are excluded
\n";
if(not $options{'p'} or not $options{'r'}){
die $usage;
}
if(not $options{'w'} and not $options{'m'}){
die $usage;
}
if($options{'w'}){
$options{'m'}="$options{'p'}.dummy";
open IN,"$options{'p'}";
open OUT,">$options{'p'}.dummy";
while(<IN>){
if(/>/){print OUT;
}else{
print OUT substr($_,0,18),"\n";
}
}
close IN;
close OUT;
$options{'e'}=0;
$options{'f'}=0;
}
my $opt_m='';
if($options{'t'}){
$species = lc($options{'t'});
$species =~ s/ //g;
$opt_m = "-m $species";
if($rorganisms{$species}){
$species = $rorganisms{$species};
$opt_m = "-m $species";
}elsif($organisms{$species}){
}else{
warn "\n\nThe species $options{'t'} you specified is not available\nallowed species are\n";
`quantifier.pl -u`;
print STDERR "If your species is not in the list just omit the -t option\n";
#exit 1;
}
}
if($options{'y'}){
$time = $options{'y'}
}
my $opt_d ="";
if($options{'d'}){
$opt_d = "-d";
}
## sort pdf reads by sample
my $opt_o ='';
if(not $options{'o'}){
$opt_o = "-o";
}
my ( $name0, $path0, $extension0 ) = fileparse ( $options{'p'}, '\..*' );
my ( $name1, $path1, $extension1 );
( $name1, $path1, $extension1 ) = fileparse ( $options{'m'}, '\..*' );# if(not defined $options{'w'});
my ( $name2, $path2, $extension2 ) = fileparse ( $options{'r'}, '\..*' );
my ( $name3, $path3, $extension3 );
$name0.=$extension0;
$name1.=$extension1;
$name2.=$extension2;
if($options{'s'}){
if(-s "$options{'s'}"){
( $name3, $path3, $extension3 ) = fileparse ( $options{'s'}, '\..*' );
$name3.=$extension3;
}else{
print STDERR "The file $options{'s'} is empty or not found. It will be ignored for this analysis";
$options{'s'}=0;
}
}
my $dir="expression_analyses";
if(not -d $dir){
mkdir($dir);
}
my $outdir="${dir}/${dir}_${time}";
if(not -d $outdir){
mkdir($outdir);
}
## check if reads file has correct format by quickly checking the first line
open IN,"<$options{'r'}" or die "File $options{'r'} not found\n";
my $line = <IN>;
if($line !~ /^>\S\S\S_\d+_x\d+/){
die "\n$options{'r'} ids do not have the correct format
it must have the id line >SSS_INT_xINT\n
SSS is a three letter code indicating the sample origin
INT is just a running number
xINT is the number of read occurrences\n\n
You can use the mapper.pl module to create such a file from a fasta file with
mapper.pl $options{'r'} -c -m -s $options{'r'}.collapsed
See also the mapper.pl help for more information on preprocessing input files.
";
}
close IN;
my %samples;
print STDERR "getting samples and corresponding read numbers\n\n";
if(0){
if(not $options{'c'}){
open IN,"<$options{'r'}";
while(<IN>){
next if($_ !~ /^>/);
if(/^>(\S\S\S)_\S+_x(\d+)$/){
$samples{$1} += $2;
}
}
close IN;
}else{
if(-f $options{'c'}){
open IN,"<$options{'c'}";
while(<IN>){
chomp;
if(/^(\S+)\s+(\S+)$/){
open FIN,"<$1" or die "file $1 not found\n";
while(<FIN>){
next if($_ !~ /^>/);
if(/^>(\S\S\S)_\S+_x(\d+)$/){
$samples{$1} += $2;
}
}
close FIN;
}
}
close IN;
}else{
$samples{$options{'c'}}++;
}
}
for(keys %samples){
print STDERR "$_\t$samples{$_} reads\n";
}
print STDERR "\n\n";
}
##convert input files to bowtie accepting format
if(not $options{'n'}){
print STDERR "Converting input files\n";
ConvertFastaFile($options{'p'},$name0,'precursor',$species);
ConvertFastaFile($options{'m'},$name1,'mature',$species);
ConvertFastaFile($options{'r'},$name2,"","");
if($options{'s'}){
ConvertFastaFile($options{'s'},$name3,'star',$species);
}
}
if(not $options{'x'}){
chdir($outdir);
Mapping();
}else{
chdir($outdir);
}
##now analyze expression file
print STDERR "analyzing data\n";
ReadinPrecursorFile();
ReadinMatureMappingFile();
if($options{'s'}){
ReadinStarMappingFile();
}
ReadinReadsMappingFile();
chdir("../../");
PrintExpressionValues();
PrintExpressionValuesSamples();
if($options{'j'}){
print STDERR "exiting here and not creating mirdeep.mrd file\nif you want this created do not specify option -j\n";
exit;
}
print STDERR "\nCreating miRBase.mrd file\n\n";
CreateOutputMRD();
#CreateOutputMRD_orig();
my $opt_l ='-l';
if($options{'k'}){
$opt_l = '';
}
my $t;
my $command='';
## defines if 5p and 3p sequences in mature file and no star file given
my $opt_P="";
$opt_P="-P" if($options{'P'});
my $opt_W="";
if($options{'W'}){
$opt_W="-W $outdir/read_occ";
}
my $starf='';
if($options{'s'}){$starf ="-j $outdir/${name3}_mapped.arf";}
if($organisms{$species} ){
$command = "make_html2.pl -q $outdir/miRBase.mrd -k $name1 -t $organisms{$species} -y $time $opt_d $opt_o -i $outdir/${name1}_mapped.arf $starf $opt_l $opt_m -M miRNAs_expressed_all_samples_$time.csv $opt_P $opt_W";
print STDERR "$command\n";
$t=`$command`;
}elsif($rorganisms{$species}){
$command="make_html2.pl -q $outdir/miRBase.mrd -k $name1 -t $species -y $time $opt_d $opt_o -i $outdir/${name1}_mapped.arf $starf $opt_l $opt_m -M miRNAs_expressed_all_samples_$time.csv $opt_P $opt_W";
print STDERR "$command\n";
$t=`$command`;
}else{
$command = "make_html2.pl -q $outdir/miRBase.mrd -k $name1 -y $time $opt_d $opt_o -i $outdir/${name1}_mapped.arf $starf $opt_l $opt_m -M miRNAs_expressed_all_samples_$time.csv $opt_P $opt_W";
print STDERR "$command\n";
$t=`$command`;
}
if($zero_mat_pres){
print STDERR "\n\n\n
Warning: 0 mature sequences mapped to any of your given precursor sequences\n\n\n\n";
}
exit;
######################################
# #
# subroutines #
# #
######################################
sub Mapping{
my $err;
## build bowtie index
print STDERR "building bowtie index\n";
$err = `bowtie-build precursor.converted miRNA_precursor`;
## map mature sequences against precursors
print STDERR "mapping mature sequences against index\n";
# print STDERR "\nbowtie -f -v 0 -a --best --strata --norc miRNA_precursor $name1.converted ${name1}_mapped.bwt\n\n";
## do not map mature if options are
if(!$options{'w'}){
$err = `bowtie -p $threads -f -v 0 -a --best --strata --norc miRNA_precursor mature.converted ${name1}_mapped.bwt 2>bowtie_mature.out`;
}
## map reads against precursors
print STDERR "mapping read sequences against index\n";
$err=`bowtie -p $threads -f -v $mismatches -a --best --strata --norc miRNA_precursor $name2.converted ${name2}_mapped.bwt 2>bowtie_reads.out`;
read_stats("$name2.converted","${name2}_mapped.bwt");
if($options{'s'}){
print STDERR "mapping star sequences against index\n";
$err = `bowtie -p $threads -f -v 0 -a --best --strata --norc miRNA_precursor star.converted ${name3}_mapped.bwt 2>bowtie_star.out`;
}
}
sub read_stats{
my ($f1,$f2)=@_;
my %hash;
my $count = 0;
my %k2;
my $total = 0;
open IN,"$f1" or die "No reads file in fasta format given\n";
while(<IN>){
if(/^>*((\S\S\S)\S+_x(\d+))/){
next if($hash{$1});
$hash{$1} = 1;
$count+=$3;
$k2{$2}+=$3;
}
}
close IN;
my %hash2;
my $count2 = 0;
my %k22;
print STDERR "Mapping statistics\n";
open IN, "$f2" or die "No mapping file given\n";
while(<IN>){
if(/^>*((\S\S\S)\S+_x(\d+))/){
next if($hash2{$1});
$hash2{$1} = 1;
$count2+=$3;
$k22{$2}+=$3;
}
}
print STDERR "\n#desc\ttotal\tmapped\tunmapped\t%mapped\t%unmapped\n";
print STDERR "total: ",$count,"\t",$count2,"\t",$count-$count2,"\t";
printf STDERR "%.3f\t%.3f\n",100*$count2/$count,100*(1-($count2/$count));
foreach(sort keys %k2){
$k22{$_}||=0;
print STDERR "$_: ",$k2{$_},"\t",$k22{$_},"\t",$k2{$_}-$k22{$_},"\t";
printf STDERR "%.3f\t%.3f\n",100*$k22{$_}/$k2{$_},100*(1-($k22{$_}/$k2{$_}));
}
}
sub ConvertFastaFile{
my $file = shift;
my $ofile= shift;
my $des = shift;
my $sp = shift;
open IN,"$file" or die "File $file not found\n";
if($des eq ""){
open OUT,">$outdir/$ofile.converted" or die "file $outdir/$ofile.converted could not be created\n";
}else{
open OUT,">$outdir/$des.converted" or die "file $outdir/$des.converted could not be created\n";
}
my $line;
my $id;
my $tmpid;
my $seq;
my $first = 1;
my $sp_hits=0;
while($line = <IN>){
chomp $line;
## removes unwanted line break and carriage returns from M$ and apple files.
if($line =~ /(\S+)/){
$line=$1;
}
if($line =~ /^(>\S+)\s*(\S*)/){
$tmpid = $1;
if(not $first){
if($sp eq 'none'){
if($des eq ""){
if($seq !~ /N/){ ## skip reads that contain an N in the sequence
print OUT "$id\n$seq\n";
$sp_hits++;
}
}else{
if($seq !~ /N/){
print OUT "$id\n$seq\n";
$sp_hits++;
}
}
}elsif($id =~ /$sp/i){
if($des eq ""){
if($seq !~ /N/){ ## skip reads that contain an N in the sequence
print OUT "$id\n$seq\n";
$sp_hits++;
}
}else{
if($seq !~ /N/){
print OUT "$id\n$seq\n";
$sp_hits++;
}
}
}else{}
}else{
$first = 0;
}
$seq="";
$id = $tmpid;
}else{
$line = uc($line);
$line =~ s/U/T/g;
$seq .= $line;
}
}
if($sp eq 'none'){
if($des eq ""){
if($seq !~ /N/){ ## skip reads that contain an N in the sequence
print OUT "$id\n$seq\n";
$sp_hits++;
}
}else{
if($seq !~ /N/){
print OUT "$id\n$seq\n";
}
$seq="";
$sp_hits++;
}
}elsif($id =~ /$sp/i){
if($des eq ""){
if($seq !~ /N/){ ## skip reads that contain an N in the sequence
print OUT "$id\n$seq\n";
$sp_hits++;
}
}else{
if($seq !~ /N/){
print OUT "$id\n$seq\n";
$seq="";
$sp_hits++;
}
}
}else{}
close IN;
close OUT;
if(not $sp_hits){
die "\nError: No entrys for species \"$options{'t'} or $species\" found in file $file
Please make sure that the given species argument matches the species id in your file $file or say none\n\n\n";
}
}
sub ReadinPrecursorFile{
my $id='';
open IN,"precursor.converted" or die "Precursor file precursor.converted not found\n";
while(<IN>){
chomp;
if(/^>(\S+)/){
$id = $1;
$hash{$id}{'seq'} = "";
$hash_star{$id}{'seq'} = "";
## make it for different samples now
for my $sample (keys %samples){
$hash_sample{$sample}{$id}{'seq'} = "";
$hash_star_sample{$sample}{$id}{'seq'} = "";
$hash_sample{$sample}{$id}{'c'} = 0;
$hash_sample{$sample}{$id}{'end'} = $hash{$id}{'end'};
}
}else{
$hash{$id}{'seq'} = "$hash{$id}{'seq'}$_"; ## get complete precursor in one line
$hash_star{$id}{'seq'} = "$hash{$id}{'seq'}$_";
}
$hash{$id}{'c'} = 0;
$hash{$id}{'end'} = length($hash{$id}{'seq'});
$hash_star{$id}{'c'} = 0;
$hash_star{$id}{'end'} = length($hash_star{$id}{'seq'});
}
close IN;
}
sub ReadinMatureMappingFile{
my @line;
my $matches = 0;
open OUT,">mature2hairpin" or die "cannot create file mature2hairpin\n";
open IN,"${name1}_mapped.bwt" or die "Mature mapping file ${name1}_mapped.bwt not found \n";
my $cx = 0;
my $id1 ='';
my $id2='';
while(<IN>){
$id1= '';
$id2='';
@line = split(/\t/);
$id1 = $line[0]; ## this is the mature ID
$id2 = $line[2]; ## this is the precursor ID
## remove multiple endings if ambigous just for matching with precursor
$id1 =~ s/\-5p//g;
$id1 =~ s/\-3p//g;
## here is assumed that multiple precursor ids have 3 - in their id, seems to be ok so far
if($id2 =~/^(\w+\-\w+\-\w+)\-\d+$/){
$id2 = $1;
}
next if(not $options{'k'} and $id1 !~ /$id2/i and $id2 !~ /$id1/i); ## stringent mapping let7a only allowed to map pre-let7a if k is given
$cx++;
$hash{$line[2]}{'c'}++; ## how many mature mapped to this precursor
for my $sample(keys %samples){
$hash_sample{$sample}{$line[2]}{'c'}++;
}
$matches = $hash{$line[2]}{'c'};
## there is a problem, Hash id is from precursor sequence, mature 7a and 7b map to same precursor
$hash{$line[2]}{$matches}{'beg'} = $line[3]-$upstream;
$hash{$line[2]}{$matches}{'beg'} = 0 if($hash{$line[2]}{$matches}{'beg'} < 0);
$hash{$line[2]}{$matches}{'end'} = $line[3]+length($line[4])-1+$downstream;
$hash{$line[2]}{$matches}{'score'} = 0;
$hash{$line[2]}{$matches}{'mature'} = $line[0]; ## assign unique mature sequence to precursor
for my $sample(keys %samples){
$hash_sample{$sample}{$line[2]}{$matches}{'beg'} = $hash{$line[2]}{$matches}{'beg'};
$hash_sample{$sample}{$line[2]}{$matches}{'end'} = $hash{$line[2]}{$matches}{'end'};
$hash_sample{$sample}{$line[2]}{$matches}{'score'} = $hash{$line[2]}{$matches}{'score'};
$hash_sample{$sample}{$line[2]}{$matches}{'mature'} = $hash{$line[2]}{$matches}{'mature'};
}
print OUT "$line[2]\t$line[0]\n";
}
print "\n$cx mature mappings to precursors\n\n";
if(not $cx){$zero_mat_pres=1;}
close OUT;
close IN;
}
sub ReadinStarMappingFile{
my @line;
my $matches = 0;
open IN,"${name3}_mapped.bwt" or die "Mature mapping file ${name3}_mapped.bwt not found \n";
my $cx = 0;
my $ltmp = "qwertyuiop";
my $id1 ='';
my $id2='';
while(<IN>){
$id1= '';
$id2='';
@line = split(/\t/);
$id1 = $line[0]; ## this is the mature ID
$id2 = $line[2]; ## this is the precursor ID
## remove multiple endings if ambigous just for matching with precursor
$id1 =~ s/\*//g;
$id1 =~ s/\-5p//g;
$id1 =~ s/\-3p//g;
if($id1 =~/^(\w+\-\w+\-\w+)\-\d+$/){
$id1 = $1;
}
if($id2 =~/^(\w+\-\w+\-\w+)\-\d+$/){
$id2 = $1;
}
next if(not $options{'k'} and $id1 !~ /$id2/i and $id2 !~ /$id1/i);## maybe this can be removed
$cx++;
$hash_star{$line[2]}{'c'}++;
for my $sample(keys %samples){
$hash_star_sample{$sample}{$line[2]}{'c'}++;
}
#print "$line[2]\t$hash{$line[2]}{'c'}\n";
$matches = $hash_star{$line[2]}{'c'};
## there is a problem, Hash id is from precursor sequence, mature 7a and 7b map to same precursor
$hash_star{$line[2]}{$matches}{'beg'} = $line[3]-$upstream;
$hash_star{$line[2]}{$matches}{'beg'} = 0 if($hash_star{$line[2]}{$matches}{'beg'} < 0);
$hash_star{$line[2]}{$matches}{'end'} = $line[3]+length($line[4])-1+$downstream;
$hash_star{$line[2]}{$matches}{'score'} = 0;
$hash_star{$line[2]}{$matches}{'mature'} = $line[0];
for my $sample(keys %samples){
$hash_star_sample{$sample}{$line[2]}{$matches}{'beg'} = $hash_star{$line[2]}{$matches}{'beg'};
$hash_star_sample{$sample}{$line[2]}{$matches}{'end'} = $hash_star{$line[2]}{$matches}{'end'};
$hash_star_sample{$sample}{$line[2]}{$matches}{'score'} = $hash_star{$line[2]}{$matches}{'score'};
$hash_star_sample{$sample}{$line[2]}{$matches}{'mature'} = $hash_star{$line[2]}{$matches}{'mature'};
}
}
print "\n$cx star mappings to precursors\n\n";
}
sub ReadinReadsMappingFile{
my @line;
my $rb;
my $re;
my @scores;
my $len_sc;
my %ids=();
## get number of times a read was mapped, used for weighing
open IN,"${name2}_mapped.bwt" or die "Reads mapping File ${name2}_mapped.bwt not found \n";
while(<IN>){
if(/^(\S+)/){
$mapcounts{$1}++;
}
}
close IN;
open OUT,">read_occ" or die "Could not create file with read_occ\n";
for my $k(keys %mapcounts){
print OUT "$k\t$mapcounts{$k}\n";
}
close OUT;
open IN,"${name2}_mapped.bwt" or die "Reads mapping File ${name2}_mapped.bwt not found \n";
my $matched = 0;
my $sample;
while(<IN>){
$matched = 0;
@line = split(/\t/);
if($species ne "none"){
next if($line[2] !~ /$species/);
}
next if($options{'U'} and $mapcounts{$line[0]} > 1);
$rb = $line[3];
$re = ($line[3]+length($line[4])-1);
next if(not $hash{$line[2]}{'c'});
for(my $i = 1; $i <= $hash{$line[2]}{'c'}; $i++){
if($options{'w'}){## if consider complete precursor as mature seq
@scores = split(/x/,$line[0]);
$sample = $1 if($scores[0] =~ /^(\S\S\S)_/); ## get sample id here
$len_sc = $scores[$#scores];
if($options{'W'}){$len_sc /= $mapcounts{$line[0]};} ## weighing reads here
$hash{$line[2]}{$i}{'score'}+= $len_sc; ## hash of pre -> mature -> score
$hash_sample{$sample}{$line[2]}{$i}{'score'}+= $len_sc;
$total{$sample}+=$len_sc;
$total_t+=$len_sc;
# print "$line[2] ==== $line[0]\t$len_sc\n";
$matched = 1;
$hash{$line[2]}{'r'} += $len_sc;
$hash_sample{$sample}{$line[2]}{'r'}+= $len_sc;
}else{
if($rb >= $hash{$line[2]}{$i}{'beg'} and $re <= $hash{$line[2]}{$i}{'end'}){
@scores = split(/x/,$line[0]);
$sample = $1 if($scores[0] =~ /^(\S\S\S)_/); ## get sample id here
$len_sc = $scores[$#scores];
if($options{'W'}){$len_sc /= $mapcounts{$line[0]};} ## weighing reads here}
$hash{$line[2]}{$i}{'score'}+= $len_sc; ## hash of pre -> mature -> score
$hash_sample{$sample}{$line[2]}{$i}{'score'}+= $len_sc;
$total{$sample}+=$len_sc;
$total_t+=$len_sc;
# print "$line[2] ==== $line[0]\t$len_sc\n";
$matched = 1;
$hash{$line[2]}{'r'} += $len_sc;
$hash_sample{$sample}{$line[2]}{'r'}+= $len_sc;
}
}
}
for(my $i = 1; $i <= $hash_star{$line[2]}{'c'}; $i++){
if($options{'w'}){
@scores = split(/x/,$line[0]);
$sample = $1 if($scores[0] =~ /^(\S\S\S)_/); ## get sample id here
$len_sc = $scores[$#scores];
if($options{'W'}){$len_sc /= $mapcounts{$line[0]};} ## weighing reads here}
$hash_star{$line[2]}{$i}{'score'}+= $len_sc;
$hash_star_sample{$sample}{$line[2]}{$i}{'score'}+= $len_sc;
$matched = 1;
$hash{$line[2]}{'r'} += $len_sc;
$hash_sample{$sample}{$line[2]}{'r'}+= $len_sc;
}else{
if($rb >= $hash_star{$line[2]}{$i}{'beg'} and $re <= $hash_star{$line[2]}{$i}{'end'}){
@scores = split(/x/,$line[0]);
$sample = $1 if($scores[0] =~ /^(\S\S\S)_/); ## get sample id here
$len_sc = $scores[$#scores];
if($options{'W'}){$len_sc /= $mapcounts{$line[0]};} ## weighing reads here}
$hash_star{$line[2]}{$i}{'score'}+= $len_sc;
$hash_star_sample{$sample}{$line[2]}{$i}{'score'}+= $len_sc;
$total{$sample}+=$len_sc;
$total_t+=$len_sc;
$hash{$line[2]}{'r'} += $len_sc;
$hash_sample{$sample}{$line[2]}{'r'}+= $len_sc;
$matched = 1;
}
}
}
if(not $matched){
@scores = split(/x/,$line[0]);
$sample = $1 if($scores[0] =~ /^(\S\S\S)_/); ## get sample id here
$len_sc=$scores[$#scores];
if($options{'W'}){$len_sc /= $mapcounts{$line[0]};} ## weighing reads here}
$hash{$line[2]}{'r'} += $len_sc;
$hash_sample{$sample}{$line[2]}{'r'}+= $len_sc;
}
}
}
sub PrintExpressionValues{
my $mat;
open OUT1,">$outdir/miRNA_expressed.csv";
# open OUT1B,">miRNAs_expressed_$time.csv";
open OUT2,">$outdir/miRNA_not_expressed.csv";
print OUT1 "#miRNA\tread_count\tprecursor\n";
# print OUT1B "#miRNA\tread count\tprecursor\n";
print OUT2 "#miRNA\tread_count\n";
my %seen;
my %not_seen;
## check which mature sequences have a mapped read and which not;
for(sort keys %hash){
if($species ne "none"){
next if($_ !~ /$species/);
}
for(my $i = 1; $i <= $hash{$_}{'c'}; $i++){
if($hash{$_}{$i}{'score'} > 0){
print OUT1 "$hash{$_}{$i}{'mature'}\t$hash{$_}{$i}{'score'}\t$_\n";
# print OUT1B "$hash{$_}{$i}{'mature'}\t$hash{$_}{$i}{'score'}\t$_\n";
}else{
print OUT2 "$hash{$_}{$i}{'mature'}\t0\n";
}
if($hash{$_}{$i}{'score'} == 0){
print OUT1 "$hash{$_}{$i}{'mature'}\t$hash{$_}{$i}{'score'}\t$_\n";
# print OUT1B "$hash{$_}{$i}{'mature'}\t$hash{$_}{$i}{'score'}\t$_\n";
}
}
}
## now for the star sequences
for(sort keys %hash_star){
if($species ne "none"){
next if($_ !~ /$species/);
}
for(my $i = 1; $i <= $hash_star{$_}{'c'}; $i++){
if($hash_star{$_}{$i}{'score'} > 0){
print OUT1 "$hash_star{$_}{$i}{'mature'}\t$hash_star{$_}{$i}{'score'}\t$_\n";
# print OUT1B "$hash_star{$_}{$i}{'mature'}\t$hash_star{$_}{$i}{'score'}\t$_\n";
}else{
print OUT2 "$hash_star{$_}{$i}{'mature'}\t0\n";
}
}
}
print STDERR "Expressed miRNAs are written to $outdir/miRNA_expressed.csv
not expressed miRNAs are written to $outdir/miRNA_not_expressed.csv\n";
close OUT1;
#close OUT1B;
close OUT2;
}
sub PrintExpressionValuesSamples{
$total_t=1000000;
open OUTG,">miRNAs_expressed_all_samples_$time.csv";
print OUTG "#miRNA\tread_count\tprecursor\ttotal";
for my $sample(sort keys %hash_sample){
next if($sample =~ /config/);
print OUTG "\t$sample";
}
for my $sample(sort keys %hash_sample){
next if($sample =~ /config/);
print OUTG "\t$sample(norm)";
}
print OUTG "\n";
for(sort keys %hash){
if($species ne "none"){
next if($_ !~ /$species/);
}
if($options{'w'}){
my $i = 1;
print OUTG "$hash{$_}{$i}{'mature'}\t$hash{$_}{$i}{'score'}\t$_\t$hash{$_}{$i}{'score'}";
for my $sample(sort keys %hash_sample){
next if($sample =~ /config/);
$hash_sample{$sample}{$_}{$i}{'score'}||= 0;
print OUTG "\t$hash_sample{$sample}{$_}{$i}{'score'}";
}
for my $sample(sort keys %hash_sample){
next if($sample =~ /config/);
$hash_sample{$sample}{$_}{$i}{'score'}||= 0;
if($hash_sample{$sample}{$_}{$i}{'score'} > 0){
print OUTG "\t",sprintf("%.2f",$total_t*$hash_sample{$sample}{$_}{$i}{'score'}/($total{$sample}) );
}else{
print OUTG "\t0";
}
}
print OUTG "\n";
}else{
for(my $i = 1; $i <= $hash{$_}{'c'}; $i++){
printf OUTG ("%s\t%.2f\t%s\t%.2f",$hash{$_}{$i}{'mature'},$hash{$_}{$i}{'score'},$_,$hash{$_}{$i}{'score'});
for my $sample(sort keys %hash_sample){
next if($sample =~ /config/);
$hash_sample{$sample}{$_}{$i}{'score'}||= 0;
if($hash_sample{$sample}{$_}{$i}{'score'} > 0){
printf OUTG ("\t%.2f",$hash_sample{$sample}{$_}{$i}{'score'});
}else{
print OUTG "\t0";
}
}
for my $sample(sort keys %hash_sample){