-
Notifications
You must be signed in to change notification settings - Fork 13
/
CleaveLand4.pl
executable file
·1547 lines (1279 loc) · 54.3 KB
/
CleaveLand4.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 -w
use strict;
use Getopt::Std;
use Math::CDF 'pbinom';
my $version_number = "4.5";
my $help = help_message($version_number);
# if there are no arguments, return the help message and quit
unless($ARGV[0]) {
die "$help";
}
# declare global variables
our($opt_h,$opt_v,$opt_q,$opt_d,$opt_e,$opt_p,$opt_o,$opt_g,$opt_u,$opt_n,$opt_a,$opt_r,$opt_t,$opt_c);
# Initialize opt_p and opt_c defaults
$opt_p = 1;
$opt_c = 4;
# get options
getopts('o:d:e:g:u:n:p:r:c:hvqlat');
# If user wants help or version, give it, and die
if($opt_h) {
die "$help";
}
if($opt_v) {
die "CleaveLand4.pl version $version_number\n";
}
# Start speaking to user, unless quiet mode is on
unless($opt_q) {
print STDERR "\nCleaveLand4 version $version_number\n";
print STDERR `date`;
}
# Parse options to determine the mode
our($mode);
$mode = determine_mode();
unless($mode) {
die "$help";
}
unless($opt_q) {
print STDERR "\nMode: $mode\n";
print STDERR "Checking Dependencies\n";
}
# Check dependencies
unless($mode == 4) {
my $bowtie_check = check_bowtie_version();
unless($bowtie_check) {
die "FAIL\n\n$help";
}
my $bowtie_build_check = check_bowtie_build_version();
unless($bowtie_build_check) {
die "FAIL\n\n$help";
}
}
if(($mode == 1) or ($mode == 2)) {
my $RNAplex_check = check_RNAplex();
unless($RNAplex_check) {
die "FAIL\n\n$help";
}
my $GSTAr_check = check_GSTAr_version();
unless($GSTAr_check) {
die "FAIL\n\n$help";
}
}
my $samtools_version;
if(($mode == 1) or ($mode == 3)) {
$samtools_version = check_samtools();
unless($samtools_version) {
die "FAIL\n\n$help";
}
}
if($opt_o) {
my $R_check = check_R_version();
unless($R_check) {
die "FAIL\n\n$help";
}
}
unless($opt_q) {
print STDERR "\n";
}
# Option p must be a number between 0 and 1
unless($opt_p =~ /^[\.\d]+$/) {
die "FATAL: Invalid entry for option p. Must be a number >0 and <= 1\n\n$help";
}
unless(($opt_p > 0) and ($opt_p <= 1)) {
die "FATAL: Invalid entry for option p. Must be a number >0 and <= 1\n\n$help";
}
# Option c must be an integer between 0 and 4
unless($opt_c =~ /^\d$/) {
die "FATAL: Invlaid entry for option c. Must be an integer between 0 and 4\n\n$help";
}
unless(($opt_c >= 0) and ($opt_c <= 4)) {
die "FATAL: Invlaid entry for option c. Must be an integer between 0 and 4\n\n$help";
}
# If option o is on, ensure the directory is created, and write the R script
my $R_script;
if($opt_o) {
if(-d $opt_o) {
unless($opt_q) {
print STDERR "T-plot directory $opt_o already exists, new T-plots will be placed there.\n";
}
} else {
system "mkdir $opt_o";
unless($opt_q) {
print STDERR "Created directory $opt_o for T-plots.\n";
}
}
$R_script = write_R_script();
}
# if $opt_a is on for modes 3 or 4 warn the user
if((($mode == 3) or ($mode == 4)) and $opt_a) {
# not suppressed because it is a warning
print STDERR "\nWARNING: option a is irrelevant when running in mode $mode \!\n";
}
# Do simple verification that any file paths passed in are readable
my $readable = verify_readable();
unless($readable) {
die "$help";
}
# Get a degradome density file path
my $dd_file;
if(($mode == 1) or ($mode == 3)) {
$dd_file = make_deg_density($samtools_version);
} else {
$dd_file = $opt_d;
}
# Get info from degradome file
my %deg_header = get_dd_header($dd_file);
unless(%deg_header) {
die "$help";
}
my %deg_data = get_dd_data($dd_file); ## key is tx id, value is all entries for the locus (inc newlines)
my %deg_tx_lens = get_dd_tx_lens($dd_file); ## key is tx id, value is length of the tx
unless($opt_q) {
print STDERR "Loaded degradome density data from file $dd_file\n";
print STDERR "\tTranscriptome: $deg_header{'tx_name'}\n";
}
# get a GSTAr file path
my $GSTAr_file;
if(($mode == 1) or ($mode == 2)) {
$GSTAr_file = make_GSTAr_file();
} else {
$GSTAr_file = $opt_g;
}
# get info from GSTAr file
my %G_header = get_G_header($GSTAr_file);
unless(%G_header) {
die "$help";
}
unless($opt_q) {
print STDERR "Loaded GSTAr alignment file $GSTAr_file\n";
print STDERR "\tTranscriptome: $G_header{'transcripts'}\n";
print STDERR "\tQueries: $G_header{'queries'}\n";
print STDERR "\tRanked by: $G_header{'sort'}\n";
}
# Transcriptomes must match!
unless($G_header{'transcripts'} eq $deg_header{'tx_name'}) {
die "\nFATAL: Transcriptome has to be the same for the degradome density file and the GSTAr alignment file\!\n$help";
}
# Print a header to STDOUT
print "\# CleaveLand4 $version_number\n";
print "\# ";
print `date`;
print "\# Degradome Density File: $dd_file\n";
print "\# Query Alignment file: $GSTAr_file\n";
print "\# Transcriptome: $G_header{'transcripts'}\n";
print "\# P-value cutoff: $opt_p\n";
print "\# Category cutoff: $opt_c\n";
print "\# Output Format: ";
if($opt_t) {
print "Tabular\n";
} else {
print "Pretty\n";
}
# Work through all of the alignments
unless($opt_q) {
print STDERR "\nBeginning analysis phase...\n\n";
}
(open(G, "$GSTAr_file")) || die "\nFATAL: Failed to open GSTAr file $GSTAr_file in main\n";
my $gline;
my @gfields = ();
my $cat;
my $last_q_name = "NULL";
my $rank;
my $chance;
my $p_val;
my $t_file;
my %nr_tabular = ();
my $slice_site;
my $ext_entry;
my $cat_digit;
while (<G>) {
chomp;
if($_ =~ /^\#/) {
next;
}
if($_ =~ /^Query\tTranscript\tTStart/) {
if($opt_t) {
print "SiteID\t$_\tDegradomeCategory\tDegradomePval\tTplot_file_path\n";
}
next; ## column names
}
@gfields = split ("\t", $_);
$gline = $_;
# track the ranks
if($gfields[0] ne $last_q_name) {
$rank = 0;
unless($last_q_name eq "NULL") {
unless($opt_q) {
print STDERR "Finished with Query: $last_q_name\n"
}
}
}
$last_q_name = $gfields[0];
++$rank;
$cat = check_cat(\%deg_data,\$gfields[1],\$gfields[4]);
if($cat =~ /cat(\d+)$/) {
$cat_digit = $1;
} else {
next;
}
$chance = $deg_header{$cat} / $deg_header{'tx_ef_size'};
$p_val = 1 - (pbinom(0,$rank,$chance));
$slice_site = "$gfields[1]" . ":" . "$gfields[4]";
if(($p_val <= $opt_p) and ($cat_digit <= $opt_c)) {
# check for redundancy
if(exists($nr_tabular{$slice_site})) {
my @old_fields = split ("\t", $nr_tabular{$slice_site});
if($p_val < $old_fields[-1]) {
# the new one is better than the old one
$ext_entry = "$gline\t$cat_digit\t$p_val";
$nr_tabular{$slice_site} = $ext_entry;
}
} else {
$ext_entry = "$gline\t$cat_digit\t$p_val";
$nr_tabular{$slice_site} = $ext_entry;
}
}
}
close G;
unless($opt_q) {
print STDERR "Finished with Query: $last_q_name\n"
}
my $n_ok = scalar(keys %nr_tabular);
unless($opt_q) {
print STDERR "\nFound a total of $n_ok non-redundant putative slicing sites with p-values <= $opt_p and categories <= $opt_c\. Outputting results.\n";
}
my @sorted_slice_sites = sort(keys %nr_tabular);
foreach my $sss (@sorted_slice_sites) {
@gfields = split ("\t", $nr_tabular{$sss});
# Make T-plot if user wanted one
if($opt_o) {
$t_file = make_t_plot($opt_o,$deg_data{$gfields[1]}, $deg_tx_lens{$gfields[1]}, $nr_tabular{$sss}, $gfields[-2], $gfields[-1]);
} else {
$t_file = "NOT_CREATED";
}
# Report to STDOUT
if($opt_t) {
tabular_report_hit($nr_tabular{$sss},$t_file,$sss);
} else {
pretty_report_hit($deg_data{$gfields[1]}, $nr_tabular{$sss}, $gfields[-2], $gfields[-1], $dd_file, $t_file, $sss);
}
}
# Delete the T-plot script if you wrote it and it's still there
if(($opt_o) and ($R_script)) {
system "rm -f $R_script";
}
# Have an A-1 day!
exit;
####################
# Here be sub-routines
sub help_message {
my($version) = @_;
my $message = "
CleaveLand4.pl : Finding sliced targets of small RNAs from degradome data
Version: $version
Usage: CleaveLand4.pl \[options\] > \[out.txt\]
Options:
-h Print help message and quit
-v Print version and quit
-q Quiet mode .. no log/progress information to STDERR
-a Sort small RNA / transcript alignments by Allen et al. score instead of default MFEratio -- for GSTAr
-t Output in tabular format instead of the default verbose format
-r [float >0..1] Minimum Free Energy Ratio cutoff. Default: 0.65 -- for GSTAr
-o [string] : Produce T-plots in the directory indicated by the string. If the dir does not exist, it will be created
-d [string] : Path to degradome density file.
-e [string] : Path to FASTA-formatted degradome reads.
-g [string] : Path to GSTAr-created tabular formatted query-transcript alignments.
-u [string] : Path to FASTA-formatted small RNA queries
-n [string] : Path to FASTA-formatted transcriptome
-p [float >0..1] : p-value for reporting. Default is 1 (no p-value filtering).
-c [integer 0..4] : Maximum category for reporting. Default is 4 (all categories reported).
Modes:
1. Align degradome data, align small RNA queries, and analyze.
REQUIRED OPTIONS: -e, -u, -n
DISALLOWED OPTIONS: -d, -g
2. Use existing degradome density file, align small RNA queries, and analyze.
REQUIRED OPTIONS: -d, -u, -n
DISALLOWED OPTIONS: -e, -g
3. Align degradome data, use existing small RNA query alignments, and analyze.
REQUIRED OPTIONS: -e, -n, -g
DISALLOWED OPTIONS: -d, -u
IRRELEVANT OPTIONS: -a, -r
4. Use existing degradome density file and existing small RNA query alignments, and analyze.
REQUIRED OPTIONS: -d, -g
DISALLOWED OPTIONS: -e, -u
IRRELEVANT OPTIONS: -a, -r
Dependencies (must be in PATH):
R \[only if making T-plots\]]
GSTAr.pl \[modes 1 and 2\ .. Version 1.0 or higher]
bowtie (0.12.x OR 1.x) \[modes 1, 2, and 3\]
bowtie-build \[modes 1, 2, and 3\]
RNAplex (from Vienna RNA Package) \[modes 1 and 2\]
samtools \[modes 1 and 3\]
Documentation: perldoc CleaveLand4.pl
";
return $message;
}
sub check_bowtie_version {
unless($opt_q) {
print STDERR "\tbowtie: ";
}
open(BTV, "which bowtie |");
my $vline = <BTV>;
close BTV;
unless($vline) {
return 0;
}
if($vline =~ /bowtie$/) {
print STDERR "PASS: $vline";
return 1;
} else {
return 0;
}
}
sub check_bowtie_build_version {
unless($opt_q) {
print STDERR "\tbowtie-build: ";
}
open(BTV, "which bowtie-build |");
my $vline = <BTV>;
close BTV;
unless($vline) {
return 0;
}
if($vline =~ /bowtie-build$/) {
print STDERR "PASS: $vline";
return 1;
} else {
return 0;
}
}
sub check_RNAplex {
unless($opt_q) {
print STDERR "\tRNAplex: ";
}
(open(RD, "RNAplex --version |")) || return 0;
my $v = <RD>;
close RD;
unless($opt_q) {
print STDERR "PASS $v";
}
return 1;
}
sub check_samtools {
unless($opt_q) {
print STDERR "\tsamtools: ";
}
(open(SAM, "samtools 2>&1 |")) || return 0;
my $version;
while (<SAM>) {
chomp;
if($_ =~ /^Version/) {
$version = $_;
}
}
close SAM;
if($version) {
unless($opt_q) {
print STDERR "PASS $version\n";
}
return $version;
} else {
return 0;
}
}
sub check_GSTAr_version {
unless($opt_q) {
print STDERR "\tGSTAr: ";
}
(open(GS, "GSTAr.pl -v 2>&1 |")) || return 0;
my $version = <GS>;
close GS;
if($version =~ /version (\d+)\.\d/) { ## must be higher than 0.x as of CleaveLand 4.1
if($1 < 1) {
return 0;
} else {
unless($opt_q) {
print STDERR "PASS $version";
}
return 1;
}
} else {
return 0;
}
}
sub check_R_version {
unless($opt_q) {
print STDERR "\tR: ";
}
(open(R, "R --version |")) || return 0;
my $version = <R>;
close R;
if($version =~ /version/) {
unless($opt_q) {
print STDERR "PASS $version";
}
return 1;
} else {
return 0;
}
}
sub check_ebwt {
my($base) = @_;
unless($opt_q) {
print STDERR "Bowtie index files for $base ";
}
my @ebwt_suffixes = qw(.1.ebwt .2.ebwt .3.ebwt .4.ebwt .rev.1.ebwt .rev.2.ebwt);
my $ebwt_count = 0;
my $ebwt_file;
foreach my $ebwt_suffix (@ebwt_suffixes) {
$ebwt_file = "$base" . "$ebwt_suffix";
if(-r $ebwt_file) {
++$ebwt_count;
}
}
if($ebwt_count == 6) {
unless($opt_q) {
print STDERR "PRESENT\n";
}
return 1;
} elsif ($ebwt_count > 0) {
unless($opt_q) {
print STDERR "INCOMPLETE only $ebwt_count of the 6 expeted index files were found.\n";
}
die "\tABORTING .. please clean up old incompleted ebwt files for transcript $base and try again\n";
} else {
unless($opt_q) {
print STDERR "ABSENT\n";
}
return 0;
}
}
sub bowtie_build {
my($fasta) = @_;
unless($opt_q) {
print STDERR "Building bowtie index for file $fasta ...\n";
}
my $log = "$fasta" . "_bowtie_build_log.txt";
system("bowtie-build $fasta $fasta > $log");
unless($opt_q) {
print STDERR " Done\n";
print STDERR "Log for bowtie-build job is at $log\n";
}
return 1;
}
sub determine_mode {
if(($opt_e) and ($opt_u) and ($opt_n)) {
# Looks like mode 1. Check for conflicts
# e is incompatible with d, u is incompatible with g
if($opt_d) {
print STDERR "\nFATAL: Incompatible options e and d.\n";
return 0;
} elsif ($opt_g) {
print STDERR "\nFATAL: Incompatible options u and g.\n";
return 0;
} else {
return 1;
}
} elsif (($opt_d) and ($opt_u) and ($opt_n)) {
# Looks like mode 2. Check for conflicts
# e is incompatible with d, u is incompatible with g
if($opt_e) {
print STDERR "\nFATAL: Incompatible options e and d.\n";
return 0;
} elsif ($opt_g) {
print STDERR "\nFATAL: Incompatible options u and g.\n";
return 0;
} else {
return 2;
}
} elsif (($opt_e) and ($opt_n) and ($opt_g)) {
# Looks like mode 3. Check for conflicts.
# e is incompatible with d, u is incompatible with g
if($opt_d) {
print STDERR "\nFATAL: Incompatible options e and d.\n";
return 0;
} elsif ($opt_u) {
print STDERR "\nFATAL: Incompatible options u and g.\n";
return 0;
} else {
return 3;
}
} elsif (($opt_d) and ($opt_g)) {
# Looks like mode 4. Check for conflicts.
# e is incompatible with d, u is incompatible with g
if($opt_e) {
print STDERR "\nFATAL: Incompatible options e and d.\n";
return 0;
} elsif ($opt_u) {
print STDERR "\nFATAL: Incompatible options u and g.\n";
return 0;
} else {
return 4;
}
} else {
print STDERR "\nFATAL: Option combination does not conform to any of the four modes.\n";
return 0;
}
}
sub verify_readable {
my $fail = 0;
if($opt_d) {
unless(-r $opt_d) {
print STDERR "FATAL: File $opt_d from option d was not readable\n";
++$fail;
}
}
if($opt_e) {
unless(-r $opt_e) {
print STDERR "FATAL: File $opt_e from option e was not readable\n";
++$fail;
}
}
if($opt_g) {
unless(-r $opt_g) {
print STDERR "FATAL: File $opt_g from option g was not readable\n";
++$fail;
}
}
if($opt_u) {
unless(-r $opt_u) {
print STDERR "FATAL: File $opt_u from option u was not readable\n";
++$fail;
}
}
if($opt_n) {
unless(-r $opt_n) {
print STDERR "FATAL: File $opt_n from option n was not readable\n";
++$fail;
}
}
if($fail) {
return 0;
} else {
return 1;
}
}
sub make_deg_density {
my($stv) = @_; ## samtools version
unless($opt_q) {
print STDERR "\nInitiating alignment of degradome reads to transcriptome.\n";
}
# Search for the bowtie indices
my $ebwt_check = check_ebwt($opt_n);
# If absent, call bowtie-build
unless($ebwt_check) {
bowtie_build($opt_n);
}
my $bam_name = "$opt_n" . "_sorted";
# call the bowtie --> samtools pipeline to make a sorted bam file
# Keep it quiet if needed
# samtools sort syntax depends on samtools version.
my $sort_call;
if($stv =~ /^Version: 1\./) {
my $tmp_sort = "$bam_name" . "_sorttemp";
my $full_bam_name = "$bam_name" . '.bam';
$sort_call = "samtools sort -T $tmp_sort -O bam - > $full_bam_name";
} else {
$sort_call = "samtools sort - $bam_name";
}
if($opt_q) {
system "bowtie -f -v 1 --best -k 1 --norc -S $opt_n $opt_e 2> /dev/null \| sed -e 's/SO:unsorted/SO:coordinate/' 2> /dev/null \| samtools view -S -b -u - 2> /dev/null \| $sort_call 2> /dev/null";
} else {
system "bowtie -f -v 1 --best -k 1 --norc -S $opt_n $opt_e \| sed -e 's/SO:unsorted/SO:coordinate/' \| samtools view -S -b -u - 2> /dev/null \| $sort_call 2> /dev/null";
}
# Verify the bam file is there
my $bam_file = "$bam_name" . ".bam";
unless(-r $bam_file) {
die "ABORT: No bam file $bam_file found after bowtie call in sub-routine make_deg_density\n";
}
# report to user
unless($opt_q) {
print STDERR "Alignments complete. Generating degradome density file.\n";
}
# parse the header to get all of the tx lengths
# also track the total number of characters
my %tx_lens = ();
my $tx_chars = 0;
(open(HEADER, "samtools view -H $bam_file \|")) || die "ABORT: Failed to open bam file $bam_file in sub-routine make_deg_density\n";
while (<HEADER>) {
chomp;
if($_ =~ /^\@SQ.*SN:(\S+).*LN:(\d+)/) {
$tx_lens{$1} = $2;
$tx_chars += $2;
} elsif ($_ =~ /^\@SQ/) {
die "ABORT: Failure to parse bam SQ header line at $_\n";
}
}
close HEADER;
# create the degradome density file and write header
my $dd_file_tmp = "$opt_e" . "_dd.txt" . ".tmp";
(open(DD, ">$dd_file_tmp")) || die "ABORT: Failed to create initial tmp degradome density file $dd_file_tmp in sub-routine make_deg_density\n";
print DD "\# CleaveLand4 degradome density\n";
print DD "\# ";
print DD `date`;
print DD "\# Degradome Reads:$opt_e\n";
print DD "\# Transcriptome:$opt_n\n";
print DD "\# TranscriptomeCharacters:$tx_chars\n";
# go through the alignments
(open(SAM, "samtools view $bam_file \|")) || die "ABORT: Failed to open bam file $bam_file in sub-routine make_deg_density\n";
my @samfields = ();
my $last_tx = "NULL";
my %freq = ();
my $outstring;
my %read_sizes = ();
my %categorized = (
0 => 0,
1 => 0,
2 => 0,
3 => 0,
4 => 0,
);
while (<SAM>) {
chomp;
# no headers
if($_ =~ /^@/) {
next;
}
@samfields = split ("\t", $_);
# no unmapped reads
if($samfields[1] & 4) {
next;
}
# paranoid but just in case no rev comps either
if($samfields[1] & 16) {
next;
}
# if it is a new tx, report the last one
if(($samfields[2] ne $last_tx) and ($last_tx ne "NULL")) {
# Report
$outstring = report_dd(\%freq,\%tx_lens,\$last_tx,\%categorized);
print DD "$outstring\n";
# clear
%freq = ();
}
# record
++$freq{$samfields[3]};
++$read_sizes{(length $samfields[9])};
# reset
$last_tx = $samfields[2];
}
close SAM;
# Report the last one
$outstring = report_dd(\%freq,\%tx_lens,\$last_tx,\%categorized);
print DD "$outstring\n";
close DD;
# remove the bam file
system "rm -f $bam_file";
# calculate the mean read size
my $read_sum;
my $read_n;
foreach my $size (keys %read_sizes) {
$read_n += $read_sizes{$size};
$read_sum += ($size * $read_sizes{$size});
}
my $mean_read = int($read_sum / $read_n);
# estimate number of tx positions that could have possibly had a read
my $est_tx_avail = $tx_chars - ((scalar(keys %tx_lens)) * $mean_read);
# re-open the DD file to add the final information to the header
(open(DDTMP, "$dd_file_tmp")) || die "ABORT: Failed to re-open temp dd file in sub-routine make_deg_density\n";
my $dd_file = $dd_file_tmp;
$dd_file =~ s/\.tmp//g;
(open(DD, ">$dd_file")) || die "ABORT: Failed to open final dd file in sub-routine make_deg_density\n";
my $done = 0;
while (<DDTMP>) {
if($done) {
print DD "$_";
} elsif ($_ =~ /^\@/) {
print DD "\# Mean Degradome Read Size:$mean_read\n";
print DD "\# Estimated effective Transcriptome Size:$est_tx_avail\n";
print DD "\# Category 0:$categorized{'0'}\n";
print DD "\# Category 1:$categorized{'1'}\n";
print DD "\# Category 2:$categorized{'2'}\n";
print DD "\# Category 3:$categorized{'3'}\n";
print DD "\# Category 4:$categorized{'4'}\n";
$done = 1;
print DD "$_";
} else {
print DD "$_";
}
}
close DDTMP;
close DD;
# remove DD tmp file
system "rm -f $dd_file_tmp";
# report to user
unless($opt_q) {
print STDERR "Degradome density file completed ... $dd_file\n";
}
return $dd_file;
}
sub report_dd {
my($freq,$lens,$id,$cat) = @_; ## passed by reference .. hash, hash, scalar
my $out;
# print the info for the transcript
$out .= "\@ID:$$id\n";
if(exists($$lens{$$id})) {
$out .= "\@LN:$$lens{$$id}\n";
} else {
die "ABORT: Failed to find length of transcript $$id in hash in sub-routine report_dd\n";
}
# get the max and mean
my $max = 0;
my $mean;
my $pos;
my $sum;
my $occupied = 0;
foreach $pos (keys %$freq) {
++$occupied;
$sum += $$freq{$pos};
if($$freq{$pos} > $max) {
$max = $$freq{$pos};
}
}
$mean = $sum / $occupied;
# how many positions are maximal?
my $n_max = 0;
foreach $pos (keys %$freq) {
if($$freq{$pos} == $max) {
++$n_max;
}
}
# now write it
my @sorted = sort {$a <=> $b} (keys %$freq);
foreach $pos (@sorted) {
$out .= "$pos\t";
$out .= "$$freq{$pos}\t";
if($$freq{$pos} == 1) {
$out .= "4\n";
++$$cat{'4'};
} elsif ($$freq{$pos} <= $mean) {
$out .= "3\n";
++$$cat{'3'};
} elsif ($$freq{$pos} == $max) {
if($n_max > 1) {
$out .= "1\n";
++$$cat{'1'};
} else {
$out .= "0\n";
++$$cat{'0'};
}
} else {
$out .= "2\n";
++$$cat{'2'};
}
}
return $out;
}
sub get_dd_header {
my($file) = @_;
(open(DD, "$file")) || die "ABORT: Failed to open degradome density file $file in sub-routine get_dd_header\n";
my $one = <DD>;
chomp $one;
unless($one eq "\# CleaveLand4 degradome density") {
print STDERR "FATAL: File $file does not have the expected header for a CleaveLand4 degradome density file\n";
return 0;
}
my %out = ();
while (<DD>) {
chomp;
unless($_ =~ /^\#/) {
last;
}
if($_ =~ /Transcriptome:(\S+)/) {
$out{'tx_name'} = $1;
} elsif ($_ =~ /Estimated effective Transcriptome Size:(\d+)/) {
$out{'tx_ef_size'} = $1;
} elsif ($_ =~ /Category 0:(\d+)/) {
$out{'cat0'} = $1;
} elsif ($_ =~ /Category 1:(\d+)/) {
$out{'cat1'} = $1;
} elsif ($_ =~ /Category 2:(\d+)/) {
$out{'cat2'} = $1;
} elsif ($_ =~ /Category 3:(\d+)/) {
$out{'cat3'} = $1;
} elsif ($_ =~ /Category 4:(\d+)/) {
$out{'cat4'} = $1;
}
}
close DD;
if((exists($out{'tx_name'})) and
(exists($out{'tx_ef_size'})) and
(exists($out{'cat0'})) and
(exists($out{'cat1'})) and
(exists($out{'cat2'})) and
(exists($out{'cat3'})) and
(exists($out{'cat4'}))) {
return %out;
} else {
print STDERR "FATAL: File $file does not have the expected header for a CleaveLand4 degradome density file\n";
return 0;
}
}
sub get_dd_data {
my($file) = @_;
(open(DD, "$file")) || die "ABORT: Failed to open dd file $file in sub-routine get_dd_data\n";
my %out = ();
my $string;
my $last_id;
while (<DD>) {
if($_ =~ /^\@ID:(\S+)/) {
if($last_id) {
$out{$last_id} = $string;
}
$string = '';
$last_id = $1;
} elsif ($_ =~ /^\d/) {
$string .= $_;
}
}
close DD;
## last one
$out{$last_id} = $string;
return %out;
}
sub get_dd_tx_lens {
my($file) = @_;
(open(DD, "$file")) || die "ABORT: Failed to open dd file $file in sub-routine get_dd_data\n";
my %out = ();
my $id;
my $length;
while (<DD>) {
if($_ =~ /^\@ID:(\S+)/) {
$id = $1;
} elsif ($_ =~ /^\@LN:(\d+)/) {
$length = $1;
$out{$id} = $length;
}
}
close DD;
return %out;
}
sub make_GSTAr_file {
unless($opt_q) {
print STDERR "Initiating GSTAr run.\n";
}
my $command = "GSTAr.pl -t";
if($opt_q) {
$command .= " -q";
}
if($opt_a) {
$command .= " -a";
}
if($opt_r) {
$command .= " -r $opt_r";
}
$command .= " $opt_u $opt_n";
my $file = "$opt_u" . "_GSTAr.txt";
$command .= " > $file";
unless($opt_q) {
print STDERR "GSTAr command is $command\n\n";
}
system "$command";
unless($opt_q) {
print STDERR "\n\nGSTAr run complete\n";
}
return $file;
}
sub get_G_header {
my($file) = @_;
my %out = ();
(open(G, "$file")) || die "ABORT: Failed to open GSTAr file $file in sub-routine get_G_header\n";
my $one = <G>;
chomp $one;
unless($one =~ /^\# GSTAr/) {
print STDERR "FATAL: File $file does not appear to be a GSTAr alignment file\n";
return 0;
}
while (<G>) {
chomp;
unless($_ =~ /^\#/) {
last;
}
if($_ =~ /Queries: (\S+)/) {
$out{'queries'} = $1;
} elsif ($_ =~ /Transcripts: (\S+)/) {
$out{'transcripts'} = $1;
} elsif ($_ =~ /Sorted by: (\S+)/) {
$out{'sort'} = $1;
} elsif ($_ =~ /Output Format: (\S+)/) {
$out{'format'} = $1;
}
}
close G;
if(($out{'queries'}) and
($out{'transcripts'}) and
($out{'sort'}) and
($out{'format'})) {
if($out{'format'} eq "Tabular") {
return %out;
} else {
print STDERR "FATAL: GSTAr file $file is not in tabular format\n";
return 0;
}
} else {
print STDERR "FATAL: File $file does not appear to be a GSTAr alignment file\n";