-
Notifications
You must be signed in to change notification settings - Fork 0
/
mrna_analysis.pl
139 lines (130 loc) · 5.92 KB
/
mrna_analysis.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
#!/usr/bin/perl
$opt = shift(@ARGV);
$refseq = 'GGGACTAGTATTTCAGTTGCAGTATAATATAGTTAAGTAGTTAGAAGTATTGTAATATTCCCATGGGTGACTACAAAGACCATGACGGTGATTATAAAGATCATGACATCGATTACAAGGATGACGATGACAAGCTCgaagacgccaaaaacataaagaaaggcccggcgccattctatccgctggaagatggaaccgctggagagcaactgcataaggctatgaagagatacgccctggttcctggaacaattgcttttacagatgcacatatcgaggtggacatcacttacgctgagtacttcgaaatgtccgttcggttggcagaagctatgaaacgatatgggctgaatacaaatcacagaatcgtcgtatgcagtgaaaactctcttcaattctttatgccggtgttgggcgcgttatttatcggagttgcagttgcgcccgcgaacgacatttataatgaacgtgaattgctcaacagtatgggcatttcgcagcctaccgtggtgttcgtttccaaaaaggggttgcaaaaaattttgaacgtgcaaaaaaagctcccaatcatccaaaaaattattatcatggattctaaaacggattaccagggatttcagtcgatgtacacgttcgtcacatctcatctacctcccggttttaatgaatacgattttgtgccagagtccttcgatagggacaagacaattgcactgatcatgaactcctctggatctactggtctgcctaaaggtgtcgctctgcctcatagaactgcctgcgtgagattctcgcatgccagagatcctatttttcggcaatcaaatcattccggatactgcgattttaagtgttgttccattccatcacggttttggaatgtttactacactcggatatttgatatgtggatttcgagtcgtcttaatgtatagatttgaagaagagctgtttctgaggagccttcaggattacaagattcaaagtgcgctgctggtgccaaccctattctccttcttcgccaaaagcactctgattgacaaatacgatttatctaatttacacgaaattgcttctggtggcgctcccctctctaaggaagtcggggaagcggttgccaagaggttccatctgccaggtatcaggcaaggatatgggctcactgagactacatcagctattctgattacacccgagggggatgataaaccgggcgcggtcggtaaagttgttccattttttgaagcgaaggttgtggatctggataccgggaaaacgctgggcgttaatcaaagaggcgaactgtgtgtgagaggtcctatgattatgtccggttatgtaaacaatccggaagcgaccaacgccttgattgacaaggatggatggctacattctggagacatagcttactgggacgaagacgaacacttcttcatcgttgaccgcctgaagtctctgattaagtacaaaggctatcaggtggctcccgctgaattggaatccatcttgctccaacaccccaacatcttcgacgcaggtgtcgcaggtcttcccgacgatgacgccggtgaacttcccgccgccgttgttgttttggagcacggaaagacgatgacggaaaaagagatcgtggattacgtcgccagtcaagtaacaaccgcgaaaaagttgcgcggaggagttgtgtttgtggacgaagtaccgaaaggtcttaccggaaaactcgacgcaagaaaaatcagagagatcctcataaaggccaagaagggcggaaagatcgccgtgTAATTAACATAAGCTAGCTACCCATACGATGTTCCAGATTACGCTCTCGAGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
$refseq = uc($refseq);
if ($opt eq "help" || $opt eq ''){
print STDERR "\nUsage: \"mod_mrna_analysis.pl <running option> [additional arguments]\"\n\n";
print STDERR "Running options:\n";
print STDERR " help - prints out this help message\n";
print STDERR " split - splits the SAM alignment file to a collection full-matching and a collection of non-full matching aligned reads SAM file. Additional argument is the input SAM file.\n";
print STDERR " simpleQChist - calculate average QC score distribution of a sam file. Additional argument is the input SAM file.\n";
print STDERR " qcfilter - filter reads in the sam file based on average QC score. Additional arguments: 1. input SAM file, 2. QC score threshold\n";
print STDERR " mapmutations - maps insertions and deletions on the sequence and will create datafiles for plotting these in R. Additional argument is the input SAM file.\n\n";
}
if ($opt eq "split"){
$infile = shift(@ARGV);
$outfile1 = $outfile2 = $infile;
$outfile1 =~ s/\.sam/_fullmatch\.sam/;
$outfile2 =~ s/\.sam/_nofullmatch\.sam/;
open(IN, $infile);
open(O1, ">".$outfile1);
open(O2, ">".$outfile2);
$cnt1 = $cnt2 = 0;
while(<IN>){
if ($_ =~ /^@/){
print O1 $_;
print O2 $_;
}
else {
@fl = split(/\t/, $_);
if ($fl[5] =~ /^\d+M$/ || $fl[5] =~ /^\d+S\d+M$/ || $fl[5] =~ /^\d+M\d+S$/ || $fl[5] =~ /^\d+S\d+M\d+S$/){
$cnt1++;
print O1 $_;
}
else {
$cnt2++;
print O2 $_;
}
}
}
close(IN);
close(O1);
close(O2);
print STDERR "Number of matching reads: ".$cnt1.", number of non-matching reads: ".$cnt2."\n";
}
if ($opt eq "simpleQChist"){
open(IN, shift(@ARGV));
for $i (0..40){
$qscores[$i] = 0;
}
while(<IN>){
if (!/^@/){
@fl = split(/\t/, $_);
$avg_qscore = qcScore($fl[10]);
$avg_qscore = int($avg_qscore+0.5);
$qscores[$avg_qscore]++;
}
}
close(IN);
for $i (0..40){
print $i."\t".$qscores[$i]."\n";
}
}
if ($opt eq "qcfilter"){
open(IN, shift(@ARGV));
$tr = shift(@ARGV);
while(<IN>){
if (/^@/){
print $_;
}
else {
@fl = split(/\t/, $_);
$avg_qscore = qcScore($fl[10]);
if ($avg_qscore >= $tr){
print $_;
}
}
}
close(IN);
}
if ($opt eq "mapmutations"){
@seqarr = split(/|/, $refseq);
for $pos (1..scalar(@seqarr)){
$data{$pos}{'seq'} = $seqarr[$pos-1];
$data{$pos}{'N'} = 0;
$data{$pos}{'I'} = 0;
$data{$pos}{'D'} = 0;
}
open(IN, shift(@ARGV));
while(<IN>){
if (!/^@/){
@fl = split(/\t/, $_);
if ($fl[1] == 16){
@regs = $fl[5] =~ /(\d+[SMIDN])/g; #
$current_pos = $fl[3];
for $r (@regs){
if ($r =~ /[SM]/){
$r =~ s/[SM]//;
$current_pos = $current_pos + $r;
}
else{
($type) = $r =~ /([IDN])/;
$r =~ s/[IDN]//;
for $i (1..$r){
$data{($current_pos+$i-1)}{$type}++;
}
$current_pos = $current_pos + $r;
}
}
}
}
}
print "POS\tREF\tNUM_DEL\tNUM_INS\n";
for $i (1..scalar(@seqarr)){
print $i."\t".$data{$i}{'seq'}."\t".$data{$i}{'D'}."\t".$data{$i}{'I'}."\n";
}
close(IN);
}
sub qcScore {
if (length($_[0]) == 1){
return (ord($_[0])-33);
}
if (length($_[0]) > 1){
my @chr = split(/|/, $_[0]);
my $sum = 0;
for my $c (@chr){
$sum += ord($c)-33;
}
return ($sum / length($_[0]));
}
}