forked from xflicsu/tsRFinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsRFinder.pl
executable file
·2468 lines (2036 loc) · 54 KB
/
tsRFinder.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/env perl
#===============================================================================
#
# File: tsRFinder.pl
#
# Description: A tool for tsRNA analysis with NGS data
#
# Author: Qinhu Wang
# Northwest A&F University
# wangqinhu@nwafu.edu.cn
# http://wangqinhu.com
# https://github.com/wangqinhu/tsRFinder
#
#===============================================================================
use strict;
use warnings;
use Env;
use Getopt::Std;
no if $] >= 5.017011, warnings => 'experimental::smartmatch';
# Environment
my $version = '1.0.0';
my $status = "development";
my $tsR_dir = $ENV{"tsR_dir"};
my %option;
my %config;
my @attention_msg;
#-------------------------------------------------------------------------------
# Main
#-------------------------------------------------------------------------------
init();
# Global variables
# Priority: option > config > default
# If zero is specified, using the default value of variable when available
my $mode = $option{m};
my $label = $option{l} || $config{"label"};
my $refseq = $option{g} || $config{"reference_genome"};
my $trna = $option{t} || $config{"reference_tRNA"};
my $srna = $option{s} || $config{"sRNA"};
my $adaptor = $option{a} || $config{"adaptor"};
my $minrl = $option{n} || $config{"min_read_length"} || "18";
my $maxrl = $option{x} || $config{"max_read_length"} || "45";
my $minexp = $option{e} || $config{"min_expression_level"} || "10";
my $mat_cut = $option{u} || $config{"mature_cut_off"} || "10";
my $fam_thr = $option{f} || $config{"family_threshold"} || "72";
my $norm = $option{d} || $config{"method_normalization"} || "rptm.r";
my $lab_trna = $option{w} || $config{"tRNA_with_label"} || "no";
my $tgz = $option{o} || $config{"output_compressed"} || "no";
my $inter = $option{i} || $config{"interactive"} || "yes";
check_config();
find_tsRNA();
stop();
#-------------------------------------------------------------------------------
# Subroutine
#-------------------------------------------------------------------------------
# Main process for tsRNA prediction and annotation
sub find_tsRNA {
# Create output directory
create_directory();
# Build reference tRNA dataset
tRNA_scan();
# Processing the raw sequencing data
format_reads();
# Mapping
map_srna();
# tsRNA identification
define_tsRNA();
# sRNA/tRNA distribution
draw_distribution();
# Cleavage position analysis
find_cleavage_site();
# Draw cleavage sites
draw_cleavage_site();
# Family analysis
srna_family();
# Write report file
write_report();
}
# Init tsRFinder
sub init {
# Check the number of arguments
if ($#ARGV == -1) {
usage();
}
# Options
getopts("m:c:l:g:t:s:a:n:x:e:u:f:d:w:o:i:hv", \%option) or die "$!\n" . usage();
# Set defualt mode as run
unless ($option{m}) {
$option{m} = "run";
}
# Check help
if ($option{h}) {
usage();
}
# Check version
if ($option{v}) {
version();
}
# Check environment
if (!defined($tsR_dir)) {
set_env_tmp();
}
chomp $tsR_dir;
$tsR_dir =~ s/\/\s{0,}$//; # remove extra "/" (and spaces) someone may include in the environment
unless ( -d $tsR_dir ) {
print "Exit: environment tsR_dir is set, but the directory is not exist!\n";
exit;
}
# Check inputs logic
if ( defined($option{c}) ) {
if ( -e $option{c} ) {
unless ( -d $option{c} ) {
start_log();
print_log("Parsing configuration file $option{c}");
parse_config($option{c});
} else {
print_log("$option{c} is a directory!");
exit;
}
} else {
print_log("Configuration file $option{c} does not exist!");
exit;
}
} else {
if ( defined($option{s}) ) {
if ( defined($option{t}) or defined($option{g})) {
if ( defined($option{l}) ) {
if ( defined($option{a}) ) {
start_log();
} else {
print "No adaptor sequence specified!";
usage();
}
} else {
print "No label specified!\n";
usage();
}
} else {
print "No reference tRNA or genome sequence file specified!\n";
usage();
}
} else {
print "No sRNA file specified!\n";
usage();
}
}
# Check critical dependencies
# Mapping dependency: bowtie
check_install("bowtie-build");
check_install("bowtie");
# Graphics dependency: R
check_install("R");
# Check mode
if ($option{m} eq "debug") {
print_log("tsRFinder init success: Running in debug mode");
} else {
if ($option{m} eq "run") {
print_log("tsRFinder init success!");
} else {
die "Unknown mode: $option{m}!\n";
}
}
# Flag unstable development version
unless ($status eq "release") {
attention_msg("You are using a $status version of tsRFinder");
}
}
# Check arguments
sub check_config {
# Conflict detecting: trna (-t) and refseq (-g)
if (defined($trna) and defined($refseq)) {
my $info = "Both genome and tRNA sequence are specified\n";
my $head = "User configuration:\n";
my $tline = "tRNA (-t): $trna\n";
my $rline = "Reference (-g): $refseq\n";
my $msg = $info . $head . $tline . $rline;
attention_msg($msg);
}
# Check adaptor
if (defined($adaptor)) {
if ($adaptor =~ /[^ATCG]+/i) {
print "Adaptor should be nucleotide sequence!\n";
print "But you have input a non-ATCG string:\n";
print "$adaptor\n";
exit;
} else {
my $len = length($adaptor);
if ($len < 6) {
print "Adaptor should longer than 6!\n";
print "But you have input an adaptor with $len base(s).\n";
exit;
}
}
}
# Check numbers
# min read length and max read length
unless ($minrl =~ /^\d+$/) {
print "Min read length (-n): expect postive integer!\n";
print "Input value: $minrl\n";
exit;
}
unless ($maxrl =~ /^\d+$/) {
print "Max read length (-x): expect postive integer!\n";
print "Input value: $maxrl\n";
exit;
}
# 15 <= min <= max <= 50
if ( $minrl >= 15 && $minrl <=50 ) {
if ( $maxrl >= 15 or $maxrl <=50 ) {
if ( $minrl >= $maxrl ) {
print "min_read_length larger than max_read_length:\n";
print "min: $minrl\n";
print "max: $maxrl\n";
exit;
}
} else {
print "max_read_length less than 15 or more than 50!\n";
}
} else {
print "min_read_length less than 15 or more than 50!\n";
exit;
}
# min expression level
unless ($minexp =~ /^\d+$/) {
print "Min expression level (-e): expect postive integer!\n";
print "Input value: $minexp\n";
exit;
}
# mature tsRNA level threshold
unless ($mat_cut =~ /^\d+$/) {
print "Mature tsRNA level cut-off (-u): expect postive integer!\n";
print "Input value: $mat_cut\n";
exit;
}
# family threshold
# above 50 take effects, below 50 will disable classification function
unless ($fam_thr =~ /^\d+$/) {
print "Small RNA family threshold (-f): expect postive integer!\n";
print "Input value: $fam_thr\n";
exit;
} else {
if ($fam_thr < 50 ) {
print "Small RNA family threshold is too low!\n";
print "Input value: $fam_thr\n";
print "Small RNA classification function OFF\n";
$fam_thr = "off";
}
}
# Check normalization method
$norm = lc($norm);
unless ($norm ~~ ["rptm.r", "rpm.r", "rptm.m", "rpm.m", "no"]) {
print "WARNING: Unknow normalization method found!\n";
print "Configuration: method_normalization or option -d should be:\n";
print '"rptm.r", "rpm.r" , "rptm.m", "rpm.m", or "no"', "\n";
print "Input value: $norm\n";
exit;
}
# Check switches
$lab_trna = lc($lab_trna);
$tgz = lc($tgz);
$inter = lc($inter);
# tRNA_with_label
unless ($lab_trna ~~ ["yes", "no"]) {
print "WARNING: Incorrect value for tRNA_with_label found!\n";
print "Configuration: tRNA_with_label or option -w should be:\n";
print '"yes" or "no"', "\n";
print "Input value: $lab_trna\n";
exit;
}
# output_compressed
unless ($tgz ~~ ["yes", "no"]) {
print "WARNING: Incorrect value for output_compressed found!\n";
print "Configuration: output_compressed or option -o should be:\n";
print '"yes" or "no"', "\n";
print "Input value: $tgz\n";
exit;
}
# interactive
unless ($inter ~~ ["yes", "no"]) {
print "WARNING: Incorrect value for interactive found!\n";
print "Configuration: interactive or option -i should be:\n";
print '"yes" or "no"', "\n";
print "Input value: $inter\n";
exit;
}
print_log("Valid input, perform analyzing ...");
}
# Stop tsRFinder
sub stop {
stop_log();
if ($mode ne "debug") {
clean_data();
} else {
attention_msg("You enabled debug mode, temporary files were keeped");
}
# Compress output files
compress_output();
# print attention_msg if it is not empty
if (@attention_msg > 0) {
print "\nATTENTION\n";
foreach my $msg (@attention_msg) {
print "$msg\n";
}
}
exit;
}
# Clean the temporary files
sub clean_data {
# Do this for unzipped file
system("rm -rf $label/sRNA.fq 1>/dev/null 2>&1");
system("rm -rf $label/refseq.fa 1>/dev/null 2>&1");
# Clean temporary files
system("rm -rf $label/_trna*");
system("rm -rf $label/_raw");
system("rm -rf $label/_srna_map");
system("rm -rf $label/_genome");
system("rm -rf $label/sRNA.fn");
unless ($trna) {
system("rm $label/trna.ss");
}
system("rm $label/tRNA.fas");
system("rm *.Rout");
system("rm BDI.txt infile.txt");
system("rm *.len");
system("rm tscs.*");
system("mv tsRFinder.log $label/");
}
# Parse config
sub parse_config {
my ($file) = @_;
open (FILE, $file) or die "Cannot open file $file: $!\n";
while (<FILE>) {
next if /^\s{0,}\#/;
if (/\s{0,}(\S+)\s{0,}\:\s{0,}(\S+)\s{0,}\#{0,}/) {
$config{$1} = $2;
} elsif (/\s{0,}(\S+)\s{0,}\:\s{0,}\#{0,}/) {
$config{$1} = undef;
} else {
if ($option{m} eq "debug") {
print_log("Unknown configuration line: $_");
} else {
next;
}
}
}
close FILE;
}
# Create the output directory
sub create_directory {
# Deal with build-in directory conflict
if ( $label ~~ ["tsRFinder", "demo", "lib", "doc"] ) {
print "Your label setting conflict with tsRFinder build-in directory, please change to another label!\n";
print "Label tsRFinder, demo, lib and doc are not allowed!\n";
exit;
}
# In case people mistyped /usr /bin /local /disk /www /var and (/) etc.
if ( $label =~ /^\S{0,}\s{0,}\// ) {
print "Your label $label is not allowed!\n";
print "Please DO NOT use '/' in your label for security\n";
exit;
}
# If specified directory exist
if ( -e "$label" ) {
if ($inter eq "yes") {
print "WARNING: $label exist, remove it? [N/y] ";
my $answer = <>;
chomp $answer;
if (lc($answer) ~~ ["y", "yes"] ) {
print_log("Directory $label will be removed now!");
if ($mode eq "debug") {
system("rm -rf $label");
} else {
system("rm -rf $label 1>/dev/null 2>&1");
}
} else {
print_log("Exit: directory $label exists");
exit;
}
} else {
print_log("Non-interactive $mode mode enabled");
my $ctime = `date +%Y%m%d.%H%M%S`;
chomp $ctime;
my $backup = $label . "." . $ctime;
if ($mode eq "debug") {
system("mv $label $backup");
} else {
system("mv $label $backup 1>/dev/null 2>&1");
}
attention_msg("Old directory $label was moved to $backup");
}
}
# Create directory
system("mkdir $label");
}
# Set temporary environment
sub set_env_tmp {
print_log("WARNING: Environment tsR_dir is not correctly set up!");
my $dir = `dirname $0`;
my $app = `basename $0`;
my $pwd = `pwd`;
chomp $dir;
chomp $app;
chomp $pwd;
# If tsRFinder.pl is a link file
my $fapp = $dir . "/" . "$app";
if ( -l $fapp) {
# read the real file's path
my $full = `readlink $fapp`;
chomp $full;
print_log("$fapp -> $full");
print_log("Using $full to determine environment tsR_dir");
# read the real file's directory name
$dir = `dirname $full`;
}
# For abs path
if ($dir =~ /^\//) {
$tsR_dir = $dir;
print_log("Set temporary tsR_dir = $tsR_dir");
# For rel path
} elsif ($dir =~ /\./) {
$tsR_dir = "$pwd/$dir";
print_log("Set temporary tsR_dir = $tsR_dir");
} else {
print_log("Check doc/manual.pdf to see how to do this.");
exit;
}
}
# Fetech tRNA from genome sequence or user input
sub tRNA_scan {
unless ( defined($trna) ) {
print_log("No reference tRNA supplied, predicting by tRNAscan-SE ...");
if ( -e $refseq ) {
predict_tRNA();
} else {
print_log("Exit: No reference genome specified!");
exit;
}
} else {
print_log("Using tRNA $trna");
my $status = check_valid_tRNA($trna);
if ($status == 1) {
print_log("Valid tRNA file found!");
} else {
print_log("The tRNA file supplied is NOT valid");
exit;
}
}
}
# Check if the tRNA sequence supplied is valid
sub check_valid_tRNA {
my ($file) = @_;
open (FILE, "$file") or die "Cannot open file $file: $!\n";
# A typical tRNA file should contain at least three lines:
while (<FILE>) {
# First, begin with ">" and followed by accession number
if (/^>\S+/) {
my $seq = <FILE>;
# Second, have nucleotide sequences
if (/[^ATCGatcg]+/) {
my $str = <FILE>;
# Third, have structure information
# Please do NOT use ">" for matching becuase all fasta file have it
if ($str =~ /[\<|\)]+/) {
format_tRNA($trna);
return 1; # valid;
} else {
return 0; # non-valid
}
} else {
return 0; # non-valid
}
} else {
return 0; # non-valid
}
}
}
# Format tRNA SS file if a valid tRNA file is supplied
sub format_tRNA {
my ($file) = @_;
unless ( -e $file ) {
print_log("file $file does not exist!");
exit;
}
print_log("Formating tRNA ...");
open (IN, "$file") or die "Cannot open file $file: $!\n";
open (TSQ, ">$label/tRNA.fa") or die "Cannot create file tRNA.fa: $!\n";
open (TSS, ">$label/tRNA.fas") or die "Cannot create file tRNA.fas: $!\n";
while (<IN>) {
if (/\(+/) {
s/\(/\>/g;
s/\)/\</g;
print TSS $_;
} elsif (/\<+/) {
print TSS $_;
} else {
print TSQ $_;
print TSS $_;
}
}
close TSQ;
close TSS;
close IN;
}
# A protocal for tRNA prediction
sub predict_tRNA {
# To build a reference tRNA sequence and its sencondary structure file
run_tRNAscanSE();
# Extract tRNA sequence and structure information
extract_tRNA();
# To build a unique tRNA sequence and structure reference
unique_tRNA();
}
# tRNAscanSE control
sub run_tRNAscanSE {
my $ss_file = "trna.ss";
print_log("Predicting tRNA ...");
if ( check_install("tRNAscan-SE") ) {
my $correct_install = `tRNAscan-SE --version 2>&1`;
if ($correct_install !~ /^Option/) {
print_log("tRNAscan-SE is not correctly installed, e.g. PERL5LIB is not correctly set.");
exit;
} else {
# Decompress gzipped reference genome sequence
if ($refseq =~ /.gz$/) {
system("gzip -dc $refseq > $label/refseq.fa");
$refseq = "$label/refseq.fa";
}
if ($mode eq "debug") {
system("tRNAscan-SE -f $label/$ss_file $refseq");
} else {
system("tRNAscan-SE -f $label/$ss_file $refseq 1>/dev/null 2>&1");
}
}
}
}
# Extract reliable tRNA sequence and structure
sub extract_tRNA {
print_log("Extracting tRNA sequences ... ");
my $ss_file = "trna.ss";
my $trna_file = "_trna.sq";
print_log("Processing file $ss_file ...");
# Hold the ss_file content in $trna_ss
open (SS, "$label/$ss_file") or die "Cannot open file $ss_file: $!\n";
my $trna_ss = undef;
while (<SS>) {
$trna_ss .= $_;
}
close SS;
# Split the sequence and secondary structure of each tRNA
open (OUT, ">$label/$trna_file") or die "Cannot open file $trna_file: $!\n";
my @trna = split /\n\n/, $trna_ss;
my @trnas = ();
foreach (@trna) {
# Skip undet and pseudogene tRNA
next if /Undet|pseudogene/;
# Parsing SS to extract id, seq, and str
if (/Type\:\s+(\S+)\s+Anticodon\:\s+(\S+)/) {
my $id = $1 . $2;
my $seq = undef;
my $str = undef;
if (/Seq\:\s+(\S+)\nStr\:\s+(\S+)/) {
$seq = $1;
$str = $2;
# Discard the sequence with more than 5 'N'
next if ($seq =~ /N{5,}/i);
# Output tRNA sequence and its sencondary structure
print OUT $id, "\t", $seq, "\n";
print OUT $str, "\n";
}
}
}
close OUT;
}
# Generate a non-redundant tRNA sequence and structure reference
sub unique_tRNA {
print_log("Generating non-redundant tRNA ...");
system("mkdir $label/_trna");
# Create triplet files
my @base = ("A", "T", "C", "G");
my @code = ();
my $i = 0;
for my $a (@base) {
for my $b (@base) {
for my $c (@base) {
$code[$i] = $a.$b.$c;
system("touch $label/_trna/$code[$i]");
$i++;
}
}
}
# Generate summary file without redundancy
my %count = ();
print_log("The number of unique triplet:");
my $trna_file = "_trna.sq";
# Foreach triplet
foreach my $ac (@code) {
# Prepare to write the sencondary sequences
open (UNI, ">$label/_trna/$ac") or die "Cannot open file $ac: $!\n";
# For counting
$count{$ac} = 0;
# Read the tRNA file
open (TRNA, "$label/$trna_file") or die "Cannot open file $trna_file: $!\n";
# Store the unique sequences
my %unique = ();
# id index of each triplet (ac)
my $idi = 0;
# Parse the tRNA file
while (<TRNA>) {
# Split id and sequence
my ($id, $seq) = split /\t/;
# The next line hold the secondary structure of tRNA
my $str = <TRNA>;
# If the id, seq, and str belongs to the current triplet
if ($ac eq substr($id,3,3)) {
# Use hash to remove redundancy
if (!exists $unique{$seq}) {
$unique{$seq} = 1;
$count{$ac}++;
$idi++;
print UNI ">";
if ($lab_trna eq "yes") {
print UNI $label;
}
print UNI "tRNA-", $id, $idi, "\n", $seq;
print UNI "$str";
}
}
}
close TRNA;
close UNI;
my $aa = anticodon_to_aa($ac);
print_log("$aa\t$count{$ac}");
}
# Write tRNA sequence file with sencondary structure
system("cat $label/_trna/* > $label/tRNA.fas");
# Write tRNA sequence file
system("awk 'NR%3' $label/tRNA.fas > $label/tRNA.fa");
}
# Convert anti-codon to AA
sub anticodon_to_aa {
my ($ac) = @_;
my %aa = (
"TTT" => "Phe",
"TTC" => "Phe",
"TTA" => "Leu",
"TTG" => "Leu",
"TCT" => "Ser",
"TCC" => "Ser",
"TCA" => "Ser",
"TCG" => "Ser",
"TAT" => "Tyr",
"TAC" => "Tyr",
"TAA" => "Stp",
"TAG" => "Stp",
"TGT" => "Cys",
"TGC" => "Cys",
"TGA" => "Stp",
"TGG" => "Trp",
"CTT" => "Leu",
"CTC" => "Leu",
"CTA" => "Leu",
"CTG" => "Leu",
"CCT" => "Pro",
"CCC" => "Pro",
"CCA" => "Pro",
"CCG" => "Pro",
"CAT" => "His",
"CAC" => "His",
"CAA" => "Gln",
"CAG" => "Gln",
"CGT" => "Arg",
"CGC" => "Arg",
"CGA" => "Arg",
"CGG" => "Arg",
"ATT" => "Ile",
"ATC" => "Ile",
"ATA" => "Ile",
"ATG" => "Met",
"ACT" => "Thr",
"ACC" => "Thr",
"ACA" => "Thr",
"ACG" => "Thr",
"AAT" => "Asn",
"AAC" => "Asn",
"AAA" => "Lys",
"AAG" => "Lys",
"AGT" => "Ser",
"AGC" => "Ser",
"AGA" => "Arg",
"AGG" => "Arg",
"GTT" => "Val",
"GTC" => "Val",
"GTA" => "Val",
"GTG" => "Val",
"GCT" => "Ala",
"GCC" => "Ala",
"GCA" => "Ala",
"GCG" => "Ala",
"GAT" => "Asp",
"GAC" => "Asp",
"GAA" => "Glu",
"GAG" => "Glu",
"GGT" => "Gly",
"GGC" => "Gly",
"GGA" => "Gly",
"GGG" => "Gly"
);
my $codon = reverse $ac;
$codon =~ tr/ATCG/TAGC/;
my $aa = "tRNA-" . $aa{$codon} . "$ac";
return $aa;
}
# Format the input sRNA file to adapt tsRFinder
sub format_reads {
my ( $file ) = ( $srna );
unless ( -e $file ) {
print_log("No such file: $file");
exit;
}
# Decompress gzip file
if ($file =~ /.gz$/) {
system("gzip -dc $file > $label/sRNA.fq");
$file = "$label/sRNA.fq";
}
# Detect the file type of sRNA supplied
my $file_type = fastq_or_fasta($file);
# Formating the raw data or clean data
# For fastq format
if ($file_type eq "fq") {
print_log("Processing the raw fastq file ...");
# Build a clean sRNA reads from raw data
process_raw($file);
# Format fasta file required by tsRFinder
format_fasta("$label/_raw/sRNA.fa", "$label/sRNA.raw.fa");
# For fasta format
} elsif ($file_type eq "fa") {
my $fasta = check_fasta($file);
if ($fasta eq "valid") {
system("cp $file $label/sRNA.raw.fa");
} else {
# Format fasta file required by tsRFinder
format_fasta("$file", "$label/sRNA.raw.fa");
}
# If not a fastq or fasta file
} else {
print_log("Exit: unknown filetype detected! $file ");
exit;
}
# Do small RNA normalization
if ($norm eq "no") {
system("cp $label/sRNA.raw.fa $label/sRNA.fn");
} else {
normalization("$label/sRNA.raw.fa","$label/sRNA.fn");
}
# Filter by min_expression_level
min_exp_filter("$label/sRNA.fn", "$label/sRNA.fa");
}
# Detect file type: fastq or fasta
sub fastq_or_fasta {
my ($file) = @_;
my $head = `head -1 $file`;
# fastq file begin with "@"
if ($head =~ /^@/) {
return "fq";
# fasta file begin with ">"
} elsif ($head =~ /^>/) {
return "fa";
} else {
return "unkown";
}
}
# Check fasta
sub check_fasta {
my ($file) = @_;
my $head = `head -1 $file`;
# Typically, a fasta head carrying both id and num sections is required
unless ($head =~ /^\>(\S+)[\-|\||\_|\t|\s+](\d+)/) {
print_log("Unsupport fasta format dectected, see the demo file or using your raw data instead");
exit;
}
if ($head =~ /^\>[A-Z]{1,20}\d{1,20}\_\d+/i) {
print_log("Valid sRNA file found!");
return "valid";
}
return "unknown";
}
# Define a special fasta file required by tsRFinder
sub format_fasta {
my ($input, $output) = @_;
print_log("Formatting fasta reads ...");
my $head = `head -1 $input`;
open (IN, $input) or die "Cannot open file $input: $!\n";
open (OUT, ">$output") or die "Cannot open file $output: $!\n";
if ($head =~ /^\>[A-Z]{1,20}\d{1,20}[\||\t|\-]\d+/i) {
while (<IN>) {
s/[\||\t|\-]/\_/;
print OUT $_;
}
} else {
while (<IN>){
# Detect, split id and num by [-, |, _, \s, \t]
if (/^\>(\S+)[\-|\||\_|\t|\s+](\d+)/) {
my $id = $1;
my $num = $2;
# Usually, small RNA sequencing will generate ~ 10,000,000 reads
# and 7 characters are enough to represent the unique reads
my $zlen = 7 - length($id);
my $str = '0' x $zlen;
# The head is formated as follow:
# lab0000001
# ...
# lab9999999
$id = $label . $str . $id . "_" . $num;
print OUT ">$id\n";
} else {
print OUT $_;
}
}
}
close IN;
close OUT;
}
# Normalization of small RNA reads
# by rptm.r : reads per ten million of raw reads
# or rpm.r : reads per million of raw reads
# by rptm.m : reads per ten million of mapped reads
# or rpm.m : reads per million of mapped reads
# or no : disable
sub normalization {
my ($raw, $normed) = @_;
my $factor = undef;
if ($norm eq "rptm.r") {
$factor = 10000000;
} elsif ($norm eq "rpm.r") {
$factor = 1000000;
} elsif ($norm eq "rptm.m") {
$factor = 10000000;
$raw = unmapped_srna_filter($raw);
} elsif ($norm eq "rpm.m") {
$factor = 1000000;
$raw = unmapped_srna_filter($raw);
} else {
print_log("Unknow method for small RNA normalization found!");
print_log("Using default method: rptm");
attention_msg("Method for small RNA normalization: rptm");
$factor = 10000000;
}
my ($total, $unique) = read_stat($raw);
open (IN, $raw) or die "Cannot open file $raw: $!\n";
open (OUT, ">$normed") or die "Cannot open file $normed: $!\n";
while (<IN>) {
if (/^(\>\S+\_)(\d+)/) {
my $leader = $1;
my $num = $2;
my $read = <IN>;