-
Notifications
You must be signed in to change notification settings - Fork 12
/
ACF_Step1.pl
299 lines (296 loc) · 11.3 KB
/
ACF_Step1.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
#!/usr/bin/perl -w
use strict;
die "Usage: $0 \"BWA_sam\" \"output\" \"\(optional\)min_AS\" \"\(optional\)cutoff\"" if (@ARGV < 2);
my $filein=$ARGV[0];
my $fileout=$ARGV[1];
my $MAS=30;
if (scalar(@ARGV) > 2) {$MAS=$ARGV[2];}
my $cutoff=0.9;
if (scalar(@ARGV) > 3) {$cutoff=$ARGV[3];}
if (($cutoff <= 0) or ($cutoff >=1 )) { die "cutoff must be within (0,1) !" }
my %uniq; # store processed ID
open IN, $filein;
open OUT,">".$fileout.".tmp";
open OUT1,">".$fileout.".1"; # single hit
open OUT2,">".$fileout.".multi";
open OUT4,">".$fileout.".segs";
open OUT21,">".$fileout.".2pp"; # two hits on the same chr and same strand
open OUT22,">".$fileout.".2pm"; # two hits on the same chr BUT Different strand
open OUT3,">".$fileout.".unmap";
open OUTMT,">".$fileout.".MT";
open OUT2S,">".$fileout.".2pp.S1"; # selected candidates from two hits on the same chr and same strand
open OUTUID,">".$fileout.".UID";
my $command="rm -f Step1_finished";
system($command);
while(<IN>) {
chomp;
if ((m/^#/) or (m/^@/)) {next;}
my @a=split("\t",$_);
my $Len=length($a[9]);
if ($a[1] eq 4) {
print OUT3 $a[0],"\t",$a[9],"\n";
print OUTUID $a[0],"\n";
next;
}
if (exists $uniq{$a[0]}) {
next;
}
else {
$uniq{$a[0]}=1;
my $strand="+";
if ($a[1] eq 16) {$strand="-";}
my $info="";
my @d=split(/\:/,$a[11]);
if (scalar(@a) > 14) {
my @b=split(/\:/,$a[14]);
my @c=split(/\;/,$b[2]);
$info=join("\t",join("\,",$a[2],$strand.$a[3],$a[5],$a[4],$d[2]),@c);
}
else {
$info=join("\t",join("\,",$a[2],$strand.$a[3],$a[5],$a[4],$d[2]));
}
if ($info =~/MT/) {
print OUTMT join("\t",$a[0],$Len,$info),"\n";
next;
}
if(scalar(@a) <= 14){
my @CIGAR_op=($a[5]=~m/[MSID]/g);
my @CIGAR_va=($a[5]=~m/\d+/g);
my $start=0;
my $length=0;
my $glength=0;
my $Nr=scalar(@CIGAR_op);
if ($strand eq "+") {
for(my $i=0; $i<$Nr; $i++){
if ($CIGAR_op[$i] eq "M") {$length+=$CIGAR_va[$i]; $glength+=$CIGAR_va[$i];}
elsif ($CIGAR_op[$i] eq "D") {$glength=$CIGAR_va[$i]; }
elsif ($CIGAR_op[$i] eq "I") {$length+=$CIGAR_va[$i]; }
elsif ($CIGAR_op[$i] eq "S") {
if (($start > 0) or ($length > 0)) {last;}
$start=$CIGAR_va[$i];
}
}
}
else {
for(my $i=$Nr-1; $i>=0; $i--){
if ($CIGAR_op[$i] eq "M") {$length+=$CIGAR_va[$i]; $glength+=$CIGAR_va[$i];}
elsif ($CIGAR_op[$i] eq "D") {$glength+=$CIGAR_va[$i];}
elsif ($CIGAR_op[$i] eq "I") {$length+=$CIGAR_va[$i]; }
elsif ($CIGAR_op[$i] eq "S") {
if (($start > 0) or ($length > 0)) {last;}
$start=$CIGAR_va[$i];
}
}
}
print OUT1 join("\t",$a[0],$Len,$start,$length,$d[2],$a[2],$strand,$a[3],$a[4]),"\n";
print OUT join("\t",$a[0],$Len,$info),"\n";
next;
}
print OUT join("\t",$a[0],$Len,$info),"\n";
# process $info
my %anno;
my %Chr;
my @p=split("\t",$info);
for(@p) {
my @tmp=split(/\,/,$_);
# ignore bad-quality alignments
if ($tmp[3] < $MAS) {next;}
my $strandy="+";
if ($tmp[1]=~m/^-/){
$strandy="-";
$tmp[1]=~s/^\-//;
}
else {
$tmp[1]=~s/^\+//;
}
my @CIGAR_op=($tmp[2]=~m/[MSID]/g);
my @CIGAR_va=($tmp[2]=~m/\d+/g);
my $start=0;
my $length=0;
my $glength=0;
my $Nr=scalar(@CIGAR_op);
if ($strandy eq "+") {
for(my $i=0; $i<$Nr; $i++){
if ($CIGAR_op[$i] eq "M") {$length+=$CIGAR_va[$i]; $glength+=$CIGAR_va[$i]; }
elsif ($CIGAR_op[$i] eq "D") {$glength+=$CIGAR_va[$i]; }
elsif ($CIGAR_op[$i] eq "I") {$length+=$CIGAR_va[$i]; }
elsif ($CIGAR_op[$i] eq "S") {
if (($start > 0) or ($length > 0)) {last;}
$start=$CIGAR_va[$i];
}
}
}
else {
for(my $i=$Nr-1; $i>=0; $i--){
if ($CIGAR_op[$i] eq "M") {$length+=$CIGAR_va[$i]; $glength+=$CIGAR_va[$i];}
elsif ($CIGAR_op[$i] eq "D") {$glength+=$CIGAR_va[$i];}
elsif ($CIGAR_op[$i] eq "I") {$length+=$CIGAR_va[$i]; }
elsif ($CIGAR_op[$i] eq "S") {
if (($start > 0) or ($length > 0)) {last;}
$start=$CIGAR_va[$i];
}
}
}
if (exists $anno{$tmp[0]}{$start}) {}
else {
$Chr{$tmp[0]}++;
$anno{$tmp[0]}{$start}=join("\t",$length,$tmp[1],($tmp[1]+$glength),$strandy);
}
}
# pick the most-hit chromosome
my $chromo="";
foreach my $id(sort{$Chr{$b} <=> $Chr{$a}} keys %Chr) {
if ($Chr{$id} > 1) {$chromo=$id; last;}
}
# each part hit once
if ($chromo eq "") {
my $result=$Len;
my $cov1=0;
my $cov2=0;
my @Cov;
for(my $i=0; $i<$Len; $i++) { $Cov[$i]=0; }
foreach my $id(sort keys %anno) {
foreach my $pos (sort{$a <=> $b} keys %{$anno{$id}}) {
$result=$result."\t".$id."\t".$pos."\t".$anno{$id}{$pos};
my @t=split("\t",$anno{$id}{$pos});
for(my $i=0; $i<$t[0]; $i++) {$Cov[$i+$pos]=1;}
}
}
my $first=-1;
my $last=-1;
for(my $i=0; $i<$Len; $i++) {
if ($Cov[$i] eq 1) {
$cov1++;
if ($first eq -1) {$first = $i;}
$last=$i;
}
}
$cov2=$last - $first + 1;
if ($cov1 > 0) { print OUT2 $a[0],"\t",$cov2,"\t",$cov1,"\t",$result,"\n"; }
}
# exactly two-hitter, interesting !!!
elsif ($Chr{$chromo} eq 2) {
my $flag=1;
my $gap=0;
foreach my $pos (sort{$a <=> $b} keys %{$anno{$chromo}}) {
my @tmp=split("\t",$anno{$chromo}{$pos});
if ($tmp[3] eq "-") {$flag+=2;}
}
if ($flag eq 1) {
# both segments on the plus strand
my $result=$Len."\t".$chromo;
my $cov1=0;
my $cov2=0;
my @Cov;
for(my $i=0; $i<$Len; $i++) { $Cov[$i]=0; }
foreach my $pos (sort{$a <=> $b} keys %{$anno{$chromo}}) {
$result=$result."\t".$pos."\t".$anno{$chromo}{$pos};
my @tmp=split("\t",$anno{$chromo}{$pos});
for(my $i=0; $i<$tmp[0]; $i++) {$Cov[$i+$pos]=1;}
if ($gap eq 0) {$gap = $tmp[2];}
else {$gap=$tmp[1] - $gap;}
}
my $first=-1;
my $last=-1;
for(my $i=0; $i<$Len; $i++) {
if ($Cov[$i] eq 1) {
$cov1++;
if ($first eq -1) {$first = $i;}
$last=$i;
}
}
$cov2=$last - $first + 1;
print OUT21 $a[0],"\t",$cov2,"\t",$cov1,"\t",$result,"\t",$gap,"\n";
print OUTUID $a[0],"\n";
if (($cov2 eq $cov1) and ($cov2 > $cutoff*$Len)) {
print OUT2S $a[0],"\t",$result,"\t",$gap,"\n";
}
}
elsif ($flag eq 5) {
# both segments on the minus strand
my $result=$Len."\t".$chromo;
my $cov1=0;
my $cov2=0;
my @Cov;
for(my $i=0; $i<$Len; $i++) { $Cov[$i]=0; }
foreach my $pos (sort{$b <=> $a} keys %{$anno{$chromo}}) {
$result=$result."\t".$pos."\t".$anno{$chromo}{$pos};
my @tmp=split("\t",$anno{$chromo}{$pos});
for(my $i=0; $i<$tmp[0]; $i++) {$Cov[$i+$pos]=1;}
if ($gap eq 0) {$gap = $tmp[2];}
else {$gap=$tmp[1] - $gap;}
}
my $first=-1;
my $last=-1;
for(my $i=0; $i<$Len; $i++) {
if ($Cov[$i] eq 1) {
$cov1++;
if ($first eq -1) {$first = $i;}
$last=$i;
}
}
$cov2=$last - $first + 1;
print OUT21 $a[0],"\t",$cov2,"\t",$cov1,"\t",$result,"\t",$gap,"\n";
print OUTUID $a[0],"\n";
if (($cov2 eq $cov1) and ($cov2 > $cutoff*$Len)) {
print OUT2S $a[0],"\t",$result,"\t",$gap,"\n";
}
}
else {
# two segments on different strands
my $result=$Len."\t".$chromo;
my $cov1=0;
my $cov2=0;
my @Cov;
for(my $i=0; $i<$Len; $i++) { $Cov[$i]=0; }
foreach my $pos (sort{$a <=> $b} keys %{$anno{$chromo}}) {
$result=$result."\t".$pos."\t".$anno{$chromo}{$pos};
my @tmp=split("\t",$anno{$chromo}{$pos});
for(my $i=0; $i<$tmp[0]; $i++) {$Cov[$i+$pos]=1;}
if ($gap eq 0) {$gap = $tmp[1];}
else {$gap=$tmp[1] - $gap;}
}
my $first=-1;
my $last=-1;
for(my $i=0; $i<$Len; $i++) {
if ($Cov[$i] eq 1) {
$cov1++;
if ($first eq -1) {$first = $i;}
$last=$i;
}
}
$cov2=$last - $first + 1;
print OUT22 $a[0],"\t",$cov2,"\t",$cov1,"\t",$result,"\t",$gap,"\n";
}
}
# more than two-hitter, whassap ??
elsif ($Chr{$chromo} > 2) {
my $result=$Len."\t".$chromo;
my $cov1=0;
my $cov2=0;
my @Cov;
for(my $i=0; $i<$Len; $i++) { $Cov[$i]=0; }
foreach my $pos (sort{$a <=> $b} keys %{$anno{$chromo}}) {
$result=$result."\t".$pos."\t".$anno{$chromo}{$pos};
my @tmp=split("\t",$anno{$chromo}{$pos});
for(my $i=0; $i<$tmp[0]; $i++) {$Cov[$i+$pos]=1;}
}
my $first=-1;
my $last=-1;
for(my $i=0; $i<$Len; $i++) {
if ($Cov[$i] eq 1) {
$cov1++;
if ($first eq -1) {$first = $i;}
$last=$i;
}
}
$cov2=$last - $first + 1;
print OUT4 $a[0],"\t",$cov2,"\t",$cov1,"\t",$result,"\n";
print OUTUID $a[0],"\n";
}
}
}
close IN;
open(OUTFLAG,">Step1_finished");
print OUTFLAG "Step1_finished\n";
close OUTFLAG;