-
Notifications
You must be signed in to change notification settings - Fork 1
/
ali2profile-pro.pl
187 lines (166 loc) · 3.63 KB
/
ali2profile-pro.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
#!/usr/bin/perl -w
#use strict;
# read mRNA fasta file and alignment file
# for each fully edited mRNA sequence, count how many alignments cover each position
# the score and length distribution
# only allow one edited mRNA file in the alignment
# 12/14: add the read number; output the best alignment for each position
# 12/16: the fasta file name has a changed format; add the number of reads with the highest score
(@ARGV == 4)|| die "Usage: <alignment file> <mRNA file> <gRNA file> <best alignment file>\n";
my ($alifile, $mRNAfile, $gRNAfile, $bestalifile)=@ARGV;
my $name;
my $fasta="";
my @profile;
my @maxS;
my @minS;
my @maxL;
my @minL;
my @readN;
my @maxSindex;
my @maxreadN;
open(IN, $mRNAfile)||die "cannot open $mRNAfile";
while(<IN>)
{
chomp;
if($_=~/^>/)
{
my @fields=split(/\s+/,$_);
$name=substr($fields[0],1);
}
else
{
$fasta.=$_;
}
}
close(IN);
my %gRNA;
my $gname="";
open(IN, $gRNAfile)||die "cannot open $gRNAfile";
while(<IN>)
{
chomp;
if($_=~/^>/)
{
my @fields=split(/\s+/,$_);
$gname=substr($fields[0],1);
}
else
{
$gRNA{$gname}.=$_;
}
}
close(IN);
my $size = length($fasta);
for(my $i=0; $i<$size; $i++)
{
$profile[$i]=0;
$minS[$i]=999;
$maxSindex[$i]=-1;
$maxS[$i]=0;
$minL[$i]=999;
$maxL[$i]=0;
$readN[$i]=0;
$maxreadN[$i]=0;
}
my @aliarray;
my $index=0;
open(IN, $alifile)||die "cannot open $alifile";
while(my $line=<IN>)
{
my $mRNA;
my $record="";
if($line=~/^>/)
{
$record.=$line;
my $totalread=0;
my @items = split(/\s+/,$line);
my @sub_items = split(/:/,$items[2]);
my $alireads=$sub_items[0];
$totalread+=$alireads;
#for(my $k=0; $k<=$#sub_items; $k++)
#{
# my @pairs=split(/\-/,$sub_items[$k]);
#print join(" ",@pairs),"\n";
#($#pairs==1)||die "$line\n";
# if($#pairs==1){
# $totalread+=$pairs[1];}
# else
# {
# print STDERR $line;
# }
#}
#read the next line
my $nextline=<IN>;
$record.=$nextline;
my @fields = split(/\s+/,$nextline);
#print $fields[7]," x ",$fields[9],"\n";
for(my $i=$fields[1]; $i<=$fields[2]; $i++)
{
$profile[$i]++;
$readN[$i]+=$totalread;
if($maxS[$i]<$fields[7])
{
$maxS[$i]=$fields[7];
$maxSindex[$i]=$index;
$maxreadN[$i]=$alireads;
#print "maxS[",$i,"] is ", $maxS[$i],"\n";
}
elsif($maxS[$i] == $fields[7])
{
$maxreadN[$i]+=$alireads;
}
if($maxL[$i]<$fields[9])
{$maxL[$i]=$fields[9];}
if($minS[$i]>$fields[7])
{$minS[$i] = $fields[7];}
if($minL[$i]>$fields[9])
{$minL[$i] = $fields[9];}
}
$index++;
$record.=<IN>;
$record.=<IN>;
$record.=<IN>;
}
push(@aliarray, $record);
}
close(IN);
($index == ($#aliarray+1))||die "number of alignments is not $index";
open(OUT, ">$bestalifile")||die "cannot create $bestalifile";
#print the best alignment
if($maxSindex[0]!=-1)
{
print OUT $aliarray[$maxSindex[0]];
my @namelist=split(/\s+/,$aliarray[$maxSindex[0]]);
print OUT $gRNA{$namelist[2]}, "\n";
}
for(my $i=1; $i<=$#maxSindex; $i++)
{
if($maxSindex[$i]==$maxSindex[$i-1] or
$maxSindex[$i]==-1)
{
}
else
{
print OUT $aliarray[$maxSindex[$i]];
my @namelist=split(/\s+/,$aliarray[$maxSindex[$i]]);
print OUT $gRNA{$namelist[2]},"\n";
}
}
close(OUT);
print "> ",$name,"\n";
my @charlist=split(//,$fasta);
for(my $j=0; $j<=$#charlist; $j++)
{
print $j," ";
print $charlist[$j]," ";
print $readN[$j]," ";
print $maxreadN[$j]," ";
#print $profile[$j]," ";
print $maxS[$j]," ";
print $maxL[$j];
#print $maxSindex[$j]," ";
#print $minS[$j]," ";
#print $minL[$j]," ";
print "\n"
}
print "//";