-
Notifications
You must be signed in to change notification settings - Fork 12
/
ACF_Step5m2.pl
270 lines (256 loc) · 6.92 KB
/
ACF_Step5m2.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
#!/usr/bin/perl -w
use strict;
die "Usage: $0 \"parsed.tmp\" \"MEA\" \"CBR\" \"expr\" \"\(optional\) min_AS\"" if (@ARGV < 4);
# to estimate the expresison of circs after parsed remap
my $filetmp=$ARGV[0]; # parsed.tmp
my $filein1=$ARGV[1]; # circle_candidates_MEA
my $filein3=$ARGV[2]; # circle_candidates_CBR
my $filein4=$ARGV[3]; # UNMAP_expr # if this argument is set to "no", then assume all read have read_count == 1 for the only one sample; so that merging reads is not needed.
my $MAS=0;
if (scalar(@ARGV) > 4) {$MAS=$ARGV[4];}
my %OK;
my %OKinfo;
open IN0, $filetmp;
while(<IN0>) {
chomp;
my @a=split("\t",$_);
my $Nr=scalar(@a);
my $info="";
my $ASsum=0;
my $ASc=0;
for(my $i=2; $i<$Nr; $i++){
my @b=split(/\,/,$a[$i]);
#if ($b[3] > $MAS){ # the AS is useless here
if($info ne ""){$info=$info."\t".join(",",@b);}
else{$info=join(",",@b)}
$ASsum+=$b[3];
$ASc++;
#}
}
if ($info ne "") {
$OK{$a[0]}=0;
$OKinfo{$a[0]}=$a[0]."\t".int($ASsum/$ASc)."\t".$info;
}
else { $OK{$a[0]}=0; }
}
close IN0;
my %Gname;
# circle_candidates_MEA
my %uniq1;
open IN1,$filein1.".p1.2";
while(<IN1>) {
chomp;
my @a=split("\t",$_);
my @b=split(/\_\_\_/,$a[2]);
#if (exists $OK{$a[0]}) {
if (exists $uniq1{$b[0]}) { $uniq1{$b[0]}=$uniq1{$b[0]}."\t".$a[0]; }
else { $uniq1{$b[0]}=$a[0]; }
$OK{$a[0]}++;
#}
}
close IN1;
open IN11,$filein1.".refFlat";
while(<IN11>) {
chomp;
my @a=split("\t",$_);
$Gname{$a[1]}=$a[0];
}
close IN11;
# circle_candidates_CBR
my %uniq3;
open IN3,$filein3.".p1.2";
while(<IN3>) {
chomp;
my @a=split("\t",$_);
my @b=split(/\_\_\_/,$a[2]);
if ($OK{$a[0]} eq 0) {
if (exists $uniq3{$b[0]}) { $uniq3{$b[0]}=$uniq3{$b[0]}."\t".$a[0]; }
else { $uniq3{$b[0]}=$a[0]; }
$OK{$a[0]}++;
}
}
close IN3;
open IN31,$filein3.".refFlat";
while(<IN31>) {
chomp;
my @a=split("\t",$_);
$Gname{$a[1]}=$a[0];
}
close IN31;
my %Anno;
my $header="";
my @Header;
if (($filein4 ne "no") and (-e $filein4)) {
open IN4, $filein4;
$header=<IN4>;
chomp $header;
@Header=split("\t",$header);
my $tmpid=$Header[0];
$Header[0]=$Header[0]."\tGname";
$header=join("\t",@Header);
while(<IN4>) {
chomp;
my @a=split("\t",$_);
$Anno{$a[0]}=join("\t",@a);
}
close IN4;
}
else {
$header=join("\t","newid","Sample");
@Header=split("\t",$header);
$Header[0]=$Header[0]."\tGname";
$header=join("\t",@Header);
foreach my $id(keys %OK) { $Anno{$id}=join("\t",1,1); }
}
my $Nr=scalar(@Header);
my $template=0;
for(my $i=2; $i<=$Nr; $i++) {$template=$template."\t0";}
# report the minimal number of read from prediction, in case that the second-step alignment missed due to heuristics
my %total_MEA;
my %info_MEA;
open(INtmp1, $filein1.".refFlat");
while (<INtmp1>) {
chomp;
if (m/^#/) { next;}
my @a=split("\t",$_);
$total_MEA{$a[1]}=1;
$info_MEA{$a[1]}=join("\t",@a);
}
close INtmp1;
open(INtmp11, $filein1);
while (<INtmp11>) {
chomp;
my @a=split("\t",$_);
if (exists $total_MEA{$a[0]}) {
my @c=split(/\,/,$a[-1]);
my @info=split("\t",$a[0]."\t".$template);
$info[1]=$a[6];
for(@c){
my $read=$_;
if (exists $Anno{$read}) {
my @b=split("\t",$Anno{$read});
for(my $i=2; $i<=$Nr; $i++) { $info[$i]+=$b[$i-1]; }
}
}
$total_MEA{$a[0]}=join("\t",@info);
}
}
close INtmp11;
my %total_CBR;
my %info_CBR;
open(INtmp3, $filein3.".refFlat");
while (<INtmp3>) {
chomp;
if (m/^#/) { next;}
my @a=split("\t",$_);
$total_CBR{$a[1]}=1;
$info_CBR{$a[1]}=join("\t",@a);
}
close INtmp3;
open(INtmp31, $filein3);
while (<INtmp31>) {
chomp;
my @a=split("\t",$_);
if (exists $total_CBR{$a[0]}) {
my @c=split(/\,/,$a[-1]);
my @info=split("\t",$a[0]."\t".$template);
$info[1]=$a[6];
if (($a[6] eq "na") and ($a[12] ne "na")) { $info[1]=$a[12]; }
for(@c){
my $read=$_;
if (exists $Anno{$read}) {
my @b=split("\t",$Anno{$read});
for(my $i=2; $i<=$Nr; $i++) { $info[$i]+=$b[$i-1]; }
}
}
$total_CBR{$a[0]}=join("\t",@info);
}
}
close INtmp31;
#my %expP;
#my $cntP="unmap.parsed.2pp.S4";
#if (-e $cntP) {
# my $tmpcnt=0;
# open(INP, $cntP);
# while (<INP>) {
# chomp;
# my @a=split("\t",$_);
# if ((!exists $total_MEA{$a[0]}) and (!exists $total_CBR{$a[0]})) { next; }
# my @c=split(/\,/,$a[14]);
# my @info=split("\t",$a[0]."\t".$template);
# for(@c){
# my $read=$_;
# if (exists $Anno{$read}) {
# my @b=split("\t",$Anno{$read});
# for(my $i=2; $i<=$Nr; $i++) { $info[$i]+=$b[$i-1]; }
# }
# }
# $expP{$a[0]}=join("\t",@info);
# $tmpcnt++;
# }
# close INP;
# print "reading $tmpcnt lines in unmap.parsed.2pp.S4 finished.\n";
#}
# report the minimal number of read from prediction, in case that the second-step alignment missed due to heuristics
open OUT1,">".$filein1.".expr";
open OUT11,">".$filein1.".newid";
print OUT1 $header,"\n";
foreach my $id (sort keys %total_MEA) {
if (exists $uniq1{$id}) {
my @a=split("\t",$uniq1{$id});
my @info=split("\t",$id."\t".$template);
for(@a) {
my $read=$_;
if (exists $OKinfo{$read}) { print OUT11 $OKinfo{$read},"\n"; }
else {print OUT11 $read,"\n"; }
my @b=split("\t",$Anno{$read});
$info[1]=$Gname{$id};
for(my $i=2; $i<=$Nr; $i++) {
$info[$i]+=$b[$i-1];
}
}
if (exists $total_MEA{$id}) {
my @org=split("\t",$total_MEA{$id});
for(my $i=2; $i<=$Nr; $i++) {
if ($info[$i] < $org[$i]) { $info[$i]=$org[$i];}
}
}
print OUT1 join("\t",@info),"\n";
}
else{
print OUT1 $total_MEA{$id},"\n";
}
}
close OUT1;
close OUT11;
open OUT3,">".$filein3.".expr";
open OUT31,">".$filein3.".newid";
print OUT3 $header,"\n";
foreach my $id (sort keys %total_CBR) {
if (exists $uniq3{$id}) {
my @a=split("\t",$uniq3{$id});
my @info=split("\t",$id."\t".$template);
for(@a) {
my $read=$_;
if (exists $OKinfo{$read}) { print OUT31 $OKinfo{$read},"\n"; }
else {print OUT31 $read,"\n"; }
my @b=split("\t",$Anno{$read});
$info[1]=$Gname{$id};
for(my $i=2; $i<=$Nr; $i++) {
$info[$i]+=$b[$i-1];
}
}
if (exists $total_CBR{$id}) {
my @org=split("\t",$total_CBR{$id});
for(my $i=2; $i<=$Nr; $i++) {
if ($info[$i] < $org[$i]) { $info[$i]=$org[$i];}
}
}
print OUT3 join("\t",@info),"\n";
}
else{
print OUT3 $total_CBR{$id},"\n";
}
}
close OUT3;
close OUT31;