forked from genome/pindel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adaptor.pm
188 lines (158 loc) · 5.58 KB
/
Adaptor.pm
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
package Pindel::Bam::Adaptor;
########################################################################################################
# #
# CGP Software License #
# #
# Copyright (c) 2010 Genome Research Ltd. #
# Author: Cancer Genome Project, [email protected] #
# #
# THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT #
# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND #
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES #
# OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN #
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #
# #
# This code is free software; you can redistribute it and/or modify it under the terms of the BSD #
# License. #
# #
# Any redistribution or derivation in whole or in part including any substantial portion of this code #
# must include this copyright and permission notice. #
# #
########################################################################################################
use strict;
use warnings FATAL => 'all';
use Carp;
use English qw( -no_match_vars );
my $q_unmap_mask = 4; #0x0004 (set when not mapped)
my $strand_mask = 16; #0x0010 (set when reverse)
our $regexps;
sub new {
my ($class, $read_ref) = @_;
my $self = {
'edits' => -1, # must start at -1 as 0 has meaning
'mapped' => 0,
'unique' => 0,
'repeat' => 0,
#'insert' => undef,
'sw' => 0,
'suboptimal' => 1,
'rg' => undef,
'strand' => 1, # as used in tests set 1 for + and 0 for -
};
bless $self, $class;
if(!$read_ref) {
croak 'Adaptor should be instantiated with array reference of sam read split on tabs';
}
if(!$regexps) {
# pre-compiles regular expressions for _tag_finder
$regexps = {'RG' => qr/^RG\:/,
'NM' => qr/^NM\:/,
'XT' => qr/^XT\:/,
'X0' => qr/^X0\:/,
'X1' => qr/^X1\:/,
};
}
$self->_populate($read_ref);
return $self;
}
sub _populate {
my ($self, $read_ref) = @_;
$self->{'sam'} = $read_ref;
$self->{'name'} = $read_ref->[0];
$self->{'rname'} = $read_ref->[2];
$self->{'cigar'} = $read_ref->[5];
$self->{'read_length'} = length $read_ref->[9];
if(($read_ref->[1] | $strand_mask) == $read_ref->[1]) {
$self->{'strand'} = 0;
}
my $rg_pos = _tag_finder($regexps->{'RG'}, $read_ref, 11); # fixed tags are first 11 items so skip
if($rg_pos) {
my (undef, undef, $rg) = split /:/, $read_ref->[$rg_pos];
$self->{'rg'} = $rg;
}
if(($read_ref->[1] | $q_unmap_mask) != $read_ref->[1]) {
$self->{'mapped'} = 1;
my ($xt, $nm);
my ($best, $subopt) = (0,0);
(undef, undef, $nm) = split /:/, $read_ref->[_tag_finder($regexps->{'NM'}, $read_ref, 11)];
$self->{'edits'} = 0 + $nm;
(undef, undef, $xt) = split /:/, $read_ref->[_tag_finder($regexps->{'XT'}, $read_ref, 11)];
if($xt eq 'U') {
$self->{'unique'} = 1;
}
elsif($xt eq 'M') {
$self->{'sw'} = 1;
if($self->{'edits'} <= 2) {
# don't think it is realistically possible for a SW alignment to be considered
# optimal however this will allow those that only have soft-clipping and minimal
# edits to be used (Kai recommended 2)
$self->{'suboptimal'} = 0;
}
}
if($xt ne 'M' && $xt ne 'N') {
(undef, undef, $best) = split /:/, $read_ref->[_tag_finder($regexps->{'X0'}, $read_ref, 11)];
#my $x1_pos = _tag_finder($regexps->{'X1'}, $read_ref, 11);
#if($x1_pos) {
# (undef, undef, $subopt) = split /:/, $read_ref->[$x1_pos];
#}
if($self->{'unique'} && $best == 1) {
# this deals with reads where a 100% match and several <100% matches were found
$self->{'suboptimal'} = 0;
}
}
}
return;
}
#sub repeat {
# return shift->{'repeat'};
#}
#sub insert {
# return shift->{'insert'};
#}
sub cigar {
return shift->{'cigar'};
}
sub rname {
return shift->{'rname'};
}
sub name {
return shift->{'name'};
}
# 1 if positive, 0 if neg
sub strand {
return shift->{'strand'};
}
sub read_group {
return shift->{'rg'};
}
sub read_length {
return shift->{'read_length'};
}
sub mapped {
return shift->{'mapped'};
}
sub unique {
return shift->{'unique'};
}
sub sw {
return shift->{'sw'};
}
sub edits {
return shift->{'edits'};
}
sub suboptimal {
return shift->{'suboptimal'};
}
sub sam {
return shift->{'sam'};
}
sub _tag_finder {
my ($reg_ex, $comp_ref, $start_at) = @_;
for my $i($start_at..@{$comp_ref}-1) {
if($comp_ref->[$i] =~ $reg_ex) {
return $i;
}
}
return undef;
}
1;