EnsEMBL/Utils/CigarString.pm untested

filter: Statementstmt 0.0% 0 / 146 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 27 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 249 CC 31 SCAR 51.1
CC
31
Cov
0%
split_hsp
342.0
generate_cigar_string
30.0
_findStrands
12.0
CRAP 165.8
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5  
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9  
10 http://www.apache.org/licenses/LICENSE-2.0
11  
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17  
18 =cut
19  
20  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =head1 AUTHOR
30  
31 Juguang Xiao <juguang@tll.org.sg>
32  
33 =cut
34  
35 =head1 NAME
36  
37 Bio::EnsEMBL::Utils::CigarString, a utilites module to generate cigar
38 strings
39  
40 =head1 DESCRIPTION
41  
42 Sequence alignment hits were previously stored within the core database
43 as ungapped alignments. This imposed 2 major constraints on alignments:
44  
45 a) alignments for a single hit record would require multiple rows in the
46 database, and
47 b) it was not possible to accurately retrieve the exact original
48 alignment.
49  
50 Therefore, in the new branch sequence alignments are now stored as
51 ungapped alignments in the cigar line format (where CIGAR stands for
52 Concise Idiosyncratic Gapped Alignment Report).
53  
54 In the cigar line format alignments are stored as follows:
55  
56 M: Match
57 D: Deletion
58 I: Insertion
59  
60 An example of an alignment for a hypthetical protein match is shown
61 below:
62  
63  
64 Query: 42 PGPAGLP----GSVGLQGPRGLRGPLP-GPLGPPL...
65 PG P G GP R PLGP
66 Sbjct: 1672 PGTP*TPLVPLGPWVPLGPSSPR--LPSGPLGPTD...
67  
68 This would be stored in the protein_align_feature table as the following
69 cigar line:
70  
71 7M4D12M2I2MD7M
72  
73 =cut
74  
75 package Bio::EnsEMBL::Utils::CigarString;
76 $Bio::EnsEMBL::Utils::CigarString::VERSION = '114.0.0_50'; # TRIAL
77 $Bio::EnsEMBL::Utils::CigarString::VERSION = '114.0.050';
78 use strict;
79 use vars qw(@ISA);
80  
81 =head2 split_hsp
82  
83 Name : split_hsp (this name is derived from the original sub in BlastWorn)
84 Usage : my $hsp; # a ready Bio::Search::HSP::GenericHSP object.
85 my $factory = new Bio::EnsEMBL::Utils::CigarString;
86 my $cigar_string = $factory->split_hsp($hsp);
87
88 Function: generate cigar string.
89 Argument: a HSP object.
90 Returns : a text string.
91
92 =cut
93  
94 sub split_hsp {
95 my ($self, $hsp) = @_;
96  
97 $self->throw("a defined object needed") unless($hsp && defined($hsp));
98 unless(ref($hsp) && $hsp->isa('Bio::Search::HSP::GenericHSP')){
99 $self->throw("a HSP object needed");
100 }
101  
102 my ($qtype, $htype) = $self->_findTypes($hsp);
103 my ($qstrand, $hstrand) = $self->_findStrands($hsp);
104 my ($qinc, $hinc) = $self->_findIncrements($qstrand,$hstrand,$qtype,$htype);
105  
106 my @gaps = ();
107 my @qchars = split(//, $hsp->query_string);
108 my @hchars = split(//, $hsp->hit_string);
109 my $qstart;
110 if($qstrand == 1){
111 $qstart = $hsp->query->start;
112 }elsif($qstart == -1){
113 $qstart = $hsp->query->end;
114 }else{
115 $self->warn("[$qstart], invalid strand value on query");
116 $qstart = $hsp->query->start;
117 # Is this a SearchIO's bug???
118 }
119
120 my $hstart;
121 if($hstrand == 1){
122 $hstart = $hsp->subject->start;
123 }elsif($hstrand != -1){
124 $hstart = $hsp->subject->end;
125 }else{
126 $self->throw("[$hstart], invalid strand value on subject");
127 }
128  
129 my $qend = $qstart;
130 my $hend = $hstart;
131 my $count = 0;
132 my $found = 0;
133  
134 my @align_coordinates = ();
135 while($count <= $#qchars){
136 if($qchars[$count] ne '-' && $hchars[$count] ne '-') {
137 $qend += $qinc;
138 $hend += $hinc;
139 $found = 1;
140 }else{ # gapped region
141 push(@align_coordinates, [$qstart, $hstart]) if($found == 1);
142  
143 $qstart = $qend;
144 $qstart += $qinc if($qchars[$count] ne '-');
145  
146 $hstart = $hend;
147 $hstart += $hinc if($hchars[$count] ne '-');
148  
149 $qend = $qstart;
150 $hend = $hstart;
151 $found = 0;
152 }
153 $count++;
154 }
155  
156 if($found){
157 push(@align_coordinates, [$qstart, $hstart]);
158 }
159  
160 my $cigar_string = "";
161 my $last = $#align_coordinates;
162 if($last >= 0){
163 for(my $i=0; $i<$last; $i++){
164 my $q_this_start = $align_coordinates[$i]->[0];
165 my $q_next_start = $align_coordinates[$i+1]->[0];
166 my $q_length = ($q_next_start-$q_this_start-1)*$qinc;
167 $q_length = abs($q_length);
168 my $h_this_start = $align_coordinates[$i]->[1];
169 my $h_next_start = $align_coordinates[$i+1]->[1];
170 my $h_length = ($h_next_start-$h_this_start-1)*$hinc;
171 $h_length = abs($h_length);
172  
173 my $diff = $q_length - $h_length;
174 if($diff > 0){ # Insertion
175 $cigar_string .= $diff unless($diff == 1);
176 $cigar_string .= 'I';
177 }elsif($diff < 0){ # Deletion
178 $cigar_string .= -$diff unless($diff == -1);
179 $cigar_string .= 'D';
180 }else{ # e.g $diff == 0, Match
181 $cigar_string .= $q_length unless($q_length == 1);
182 $cigar_string .= 'M';
183 }
184
185 } # for
186 } # if
187
188 return $cigar_string;
189 }
190  
191  
192 sub _findStrands {
193 my ($self,$hsp) = @_;
194
195 my $qstrand = $hsp->query->strand;
196 unless($qstrand == 1 || $qstrand == -1){
197 $self->warn("query's strand value is neither 1 or -1");
198 $qstrand = 1;
199 }
200
201 my $hstrand = $hsp->subject->strand;
202 unless($hstrand == 1 || $hstrand == -1){
203 $self->warn("subject's strand value is neither 1 or -1");
204 $hstrand = 1;
205 }
206
207 return ( $qstrand, $hstrand);
208 }
209  
210 sub _findTypes {
211 my ($self,$hsp) = @_;
212  
213 my $type1;
214 my $type2;
215 my $len1 = $hsp->query->length();
216 my $len2 = $hsp->subject->length();
217  
218 if ($len1/$len2 > 2) {
219 $type1 = 'dna';
220 $type2 = 'pep';
221 } elsif ($len2/$len1 > 2) {
222 $type1 = 'pep';
223 $type2 = 'dna';
224 } else {
225 $type1 = 'dna';
226 $type2 = 'dna';
227 }
228  
229 return ($type1,$type2);
230 }
231  
232 sub _findIncrements {
233 my ($self,$qstrand,$hstrand,$qtype,$htype) = @_;
234  
235 my $qinc = 1 * $qstrand;
236 my $hinc = 1 * $hstrand;
237  
238 if ($qtype eq 'dna' && $htype eq 'pep') {
239 $qinc = 3 * $qinc;
240 }
241 if ($qtype eq 'pep' && $htype eq 'dna') {
242 $hinc = 3 * $hinc;
243 }
244  
245 return ($qinc,$hinc);
246 }
247  
248 # This is a core logic of cigar string. The finite state machine theory is
249 # apply. See the below table, x-axis represents the input, with 3 options:
250 # (+/+) -- Both current query and subject bases are non-gap. Match
251 # (-/+) -- The current query base is gap, but subject not. Deletion
252 # (+/-) -- The current subject base is gap, but query not. Insertion
253 # While the y-axis means the current state with letter 'M', 'D', 'I'
254 #
255 # The content of this table is the action taken in response of the input and
256 # the current state.
257 # R remain the state, counter increment.
258 # G;X generate the cigar line based on the current state and counter;
259 # clear the counter to zero and change to the state X
260 #
261 # || +/+ | -/+ | +/- |
262 # -------+----------------------+
263 # M || R | G;D | G;I |
264 # ------------------------------+
265 # D || G;M | R | G;I |
266 # ------------------------------+
267 # I || G;M | G;D | R |
268 #
269  
270 =head2 generate_cigar_string
271  
272 Name : generate_cigar_string
273 Usage: $cigar_string = $self->generate_cigar_string(\@qchars, \@hchars);
274 Function: generate the cigar string for a piece of alignment.
275 Args: 2 array references. The lengths of 2 arrays are the same
276 Return: a cigar string
277  
278 =cut
279  
280 # Developer's Note: The method is originally abstracted from the concept of
281 # cigar string. It only asks the essential information of 2 sequence characters
282 # of the alignment, while the BlastWorn::split_HSP asks more unused information
283 # for cigar string, which is useful to form align_coordinates. - Juguang
284  
285 my ($count, $state); # strictly only used in the following 2 subs
286  
287 sub generate_cigar_string {
288  
289 # my ($self, $qstart, $hstart, $qinc, $hinc, $qchars_ref, $hchars_ref) = @_;
290  
291 my ($self, $qchars_ref, $hchars_ref) = @_;
292
293 my @qchars = @{$qchars_ref};
294 my @hchars = @{$hchars_ref};
295  
296 unless(scalar(@qchars) == scalar(@hchars)){
297 $self->throw("two sequences are not equal in lengths");
298 }
299
300 $count = 0;
301 $state = 'M'; # the current state of gaps, (M, D, I)
302
303 my $cigar_string = '';
304 for(my $i=0; $i <= $#qchars; $i++){
305 my $qchar = $qchars[$i];
306 my $hchar = $hchars[$i];
307 if($qchar ne '-' && $hchar ne '-'){ # Match
308 $cigar_string .= $self->_sub_cigar_string('M');
309 }elsif($qchar eq '-'){ # Deletion
310 $cigar_string .= $self->_sub_cigar_string('D');
311 }elsif($hchar eq '-'){ # Insertion
312 $cigar_string .= $self->_sub_cigar_string('I');
313 }else{
314 $self->throw("Impossible state that 2 gaps on each seq aligned");
315 }
316 }
317 $cigar_string .= $self->_sub_cigar_string('X'); # not forget the tail.
318 return $cigar_string;
319 }
320  
321 sub _sub_cigar_string {
322 my ($self, $new_state) = @_;
323 my $sub_cigar_string = '';
324 if($state eq $new_state){
325 $count++; # Remain the state and increase the counter
326 }else{
327 $sub_cigar_string .= $count unless $count == 1;
328 $sub_cigar_string .= $state;
329 $count = 1;
330 $state = $new_state;
331 }
332 return $sub_cigar_string;
333 }
334  
335 =head2 generate_cigar_string_by_hsp
336
337 Name : generate_cigar_string_by_hsp
338 Usage : my $hsp; # a ready GenericHSP object
339 my $cigar_string = $self->generate_cigar_string_by_hsp($hsp);
340 Function: generate a cigar string by given HSP object.
341 Args : a GenericHSP object
342 Returns: a text string of cigar string
343  
344 =cut
345  
346 sub generate_cigar_string_by_hsp {
347 my ($self, $hsp) = @_;
348  
349 unless(ref($hsp) && $hsp->isa('Bio::Search::HSP::GenericHSP')){
350 $self->throw("A GenericHSP object needed");
351 }
352  
353 my @qchars = split(//, $hsp->query_string);
354 my @hchars = split(//, $hsp->hit_string);
355  
356 return $self->generate_cigar_string(\@qchars, \@hchars);
357 }
358  
359 1;