-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBP_cluster_filter.v3.pl
executable file
·188 lines (151 loc) · 5.4 KB
/
BP_cluster_filter.v3.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
#!/usr/bin/perl -w
# Author: Jay Mashl <rmashl@genome.wustl.edu>
# Date: 2015-02-23
# This file is part of BreakRUsT.
#
# version 1: empirical filter for removal of duplicate or near-duplicate Tigra calls. Subsequent calls that are within $tol base pairs of the first call are rejected. Confidence intervals removed.
# version 2: Check SV type and select best quality score.
# version 3: convert TRA translocations to mates
# Tigra scores can be revised to a hard-coded value (e.g., 60). See code.
use strict;
use warnings;
my $tol=10000;
my $hadFirst=0;
my ($lastChr, $lastPos);
my ($lastChr2, $lastEnd);
my @pass=();
my $pass_cnt=0;
open (PASS, ">", "BP_cluster.v3.PASS" );
open (FAIL, ">", "BP_cluster.v3.FAIL" );
while (my $line=<>) {
if($line =~ /^#/) {
print PASS $line unless ($line =~ /CIPOS/ || $line =~ /ID=GT/);
next;
}
chomp $line;
my($this_chr,$this_pos,$id,$ref,$alt,$qual,$filter,$info,$gt,$gt_dat,$tigra_contig) = split /\t/,$line;
my($this_chr2, $this_pos2);
# Get chr2 from INFO field
my @a=split /\;/,$info;
foreach (@a) {
if ($_=~ /^CHR2=/) {
my @b=split /=/,$_;
$this_chr2=$b[1];
} elsif ($_=~ /^END=/) {
my @b=split /=/,$_;
$this_pos2=$b[1];
}
}
# print "Read: $this_chr $this_pos $this_chr2 $this_pos2\n";
if ($hadFirst==0) { # first line always passes
$pass[$pass_cnt]{line} = $line;
$pass[$pass_cnt]{chr1} = $this_chr;
$pass[$pass_cnt]{pos1} = $this_pos;
$pass[$pass_cnt]{chr2} = $this_chr2;
$pass[$pass_cnt]{pos2} = $this_pos2;
$pass[$pass_cnt]{svtype} = $alt;
$pass[$pass_cnt]{qual} = $qual;
++$pass_cnt;
$hadFirst=1;
# print "Default retain: $this_chr $this_pos $this_chr2 $this_pos2\n";
}
else {
my $reject=0;
for (my $k=0; $k < $pass_cnt; ++$k) {
my ($d1,$d2);
# print "Test: ( $k ) $pass[$k]{chr1} $pass[$k]{pos1} $pass[$k]{chr2} $pass[$k]{pos2} \n";
# Note: Seems like read pairs of a tandem duplication could appear to correspond to a small INS. Allow Tigra to override BreakDancer.
# A DUP that looks like an INS would be called INS (per Kai).
# ( $alt eq $pass[$k]{svtype} || ($alt=~/(INS|DUP)/ && $pass[$k]{svtype}=~/(INS|DUP)/)) ) {
if( ($this_chr eq $pass[$k]{chr1}) && ($this_chr2 eq $pass[$k]{chr2}) &&
( $alt eq $pass[$k]{svtype} ) ) {
$d1=abs(($this_pos+0) - ($pass[$k]{pos1}+0));
$d2=abs(($this_pos2+0) - ($pass[$k]{pos2}+0));
if( $d1 <= $tol && $d2 <= $tol) { # not too different
if ($qual > $pass[$k]{qual}) {
# Reject old, keep new
print FAIL $pass[$k]{line}."\n";
$pass[$k]{line} = $line; $pass[$k]{chr1} = $this_chr;
$pass[$k]{pos1} = $this_pos; $pass[$k]{chr2} = $this_chr2;
$pass[$k]{pos2} = $this_pos2; $pass[$k]{svtype} = $alt;
$pass[$k]{qual} = $qual;
} else {
# Reject
print FAIL $line."\n";
}
$reject=1;
last;
}
} elsif( ($this_chr eq $pass[$k]{chr2}) && ($this_chr2 eq $pass[$k]{chr1}) &&
( $alt eq $pass[$k]{svtype} ) ) {
$d1=abs(($this_pos+0) - ($pass[$k]{pos2}+0));
$d2=abs(($this_pos2+0) - ($pass[$k]{pos1}+0));
if( $d1<=$tol && $d2<=$tol) {
if ($qual > $pass[$k]{qual}) {
# Reject old, keep new
print FAIL $pass[$k]{line}."\n";
$pass[$k]{line} = $line; $pass[$k]{chr1} = $this_chr;
$pass[$k]{pos1} = $this_pos; $pass[$k]{chr2} = $this_chr2;
$pass[$k]{pos2} = $this_pos2; $pass[$k]{svtype} = $alt;
$pass[$k]{qual} = $qual;
} else {
# Reject
print FAIL $line."\n";
}
$reject=1;
last;
}
}
} # for
if( !$reject ) {
$pass[$pass_cnt]{line} = $line; $pass[$pass_cnt]{chr1} = $this_chr;
$pass[$pass_cnt]{pos1} = $this_pos; $pass[$pass_cnt]{chr2} = $this_chr2;
$pass[$pass_cnt]{pos2} = $this_pos2; $pass[$pass_cnt]{svtype} = $alt;
$pass[$pass_cnt]{qual} = $qual;
++$pass_cnt;
}
}
} #while
my $mate_id=1;
for (my $k=0; $k < $pass_cnt; ++$k) {
my($this_chr,$this_pos,$id,$ref,$alt,$qual,$filter,$info,$gt,$gt_dat,$tigra_contig) = split /\t/, $pass[$k]{line};
my($this_chr2, $this_pos2);
# Get chr2 from INFO field
my @a=split /\;/,$info;
foreach (@a) {
if ($_=~ /^CHR2=/) {
my @b=split /=/,$_;
$this_chr2=$b[1];
} elsif ($_=~ /^END=/) {
my @b=split /=/,$_;
$this_pos2=$b[1];
}
}
# Expand translocations to mate pairs
if( $info =~ /SVTYPE=TRA/) {
# Tigra-EXT is hard-coded with +/-10 bp. Increasing by 100bp for read length.
# Reconstruct info string
$info = "SVTYPE=BND;MATEID=".$mate_id."_2;IMPRECISE;CIPOS=-110,110;CIEND=-110,110;SOMATIC";
$id=$mate_id."_1";
$ref=".";
$alt=$this_chr2.":".$this_pos2;
print PASS join("\t", $this_chr,$this_pos,$id,$ref,$alt,$qual,$filter,$info)."\n";
$info = "SVTYPE=BND;MATEID=".$mate_id."_1;IMPRECISE;CIPOS=-110,110;CIEND=-110,110;SOMATIC";
$id=$mate_id."_2";
$ref=".";
$alt=$this_chr.":".$this_pos;
print PASS join("\t", $this_chr2,$this_pos2,$id,$ref,$alt,$qual,$filter,$info)."\n";
$mate_id++;
}
else {
# Tigra-EXT is hard-coded with +/-10 bp. Increasing by 100bp for read length.
$info =~ s/CIPOS=\-10,10\;CIEND=\-10,10\;/CIPOS=\-110,110\;CIEND=\-110,110\;/;
$id=".";
$alt=".";
# Optional: hard code value
# $qual="60";
print PASS join("\t", $this_chr,$this_pos,$id,$ref,$alt,$qual,$filter,$info)."\n";
}
}
close(FAIL);
close(PASS);