-
Notifications
You must be signed in to change notification settings - Fork 2
/
Mummer_remap.pm
executable file
·279 lines (203 loc) · 7.35 KB
/
Mummer_remap.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
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
#!/usr/local/bin/perl
package Mummer_remap;
use strict;
use warnings;
use Carp;
use Mummer_delta_parser;
our $VERBOSE = 0;
our $DEBUG = 0;
# constants
our $QUERY_TO_REFERENCE = 1;
our $REFERENCE_TO_QUERY = 2;
sub new {
my $packagename = shift;
my $delta_file = shift;
my $delta_parser = new Mummer_delta_parser($delta_file);
my $self = {
accessions_to_alignments => {},
delta_parser => $delta_parser,
MSP_list => [],
};
bless ($self, $packagename);
$self->_init();
return ($self);
}
####
sub _init {
my $self = shift;
my $delta_parser = $self->get_delta_parser();
my $accessions_to_alignments_href = $self->{accessions_to_alignments};
my @alignments = $delta_parser->get_alignments();
foreach my $alignment (@alignments) {
my $reference_accession = $alignment->get_reference_accession();
my $query_accession = $alignment->get_query_accession();
my $alignment_list_aref = $accessions_to_alignments_href->{$reference_accession}->{$query_accession};
unless (ref $alignment_list_aref) {
$alignment_list_aref = $accessions_to_alignments_href->{$reference_accession}->{$query_accession} = [];
}
push (@$alignment_list_aref, $alignment);
}
}
####
sub get_delta_parser {
my $self = shift;
return ($self->{delta_parser});
}
####
sub get_accession_to_alignments_mappings {
my $self = shift;
return ($self->{accessions_to_alignments});
}
####
sub _clear_MSP_list {
my $self = shift;
@{$self->{MSP_list}} = ();
return;
}
####
sub _store_MSP_list {
my $self = shift;
my @msps = @_;
@{$self->{MSP_list}} = @msps;
return;
}
####
sub get_MSPs {
my $self = shift;
return (@{$self->{MSP_list}});
}
####
sub transform_coordinates {
my $self = shift;
my ($type, $ids_href, @coordinates) = @_;
unless ($type == $REFERENCE_TO_QUERY || $type == $QUERY_TO_REFERENCE) {
confess "fatal, don't understand 'type' function argument. ";
}
my @alignments = $self->_get_alignments_from_ids($ids_href);
my @converted_coordinates;
my %msp_tracker; # track those msps used for coordinate conversions, and store them for later access
## clear the current msp list
$self->_clear_MSP_list();
COORDINATES:
foreach my $coordinate (@coordinates) {
my $alignment = $self->_find_longest_overlapping_coordinate_set($type, $coordinate, \@alignments);
unless (ref $alignment) {
push (@converted_coordinates, -1); # no alignment contains this coordinate in the span
next COORDINATES;
}
## got overlapping alignment
## find the msp containing the coordinate;
my @msps = $alignment->get_MSPs();
my $msp = $self->_find_longest_overlapping_coordinate_set($type, $coordinate, \@msps);
unless (ref $msp) {
# no msp contains this coordinate
push (@converted_coordinates, -1);
next COORDINATES;
}
$msp_tracker{$msp} = $msp;
if ($Mummer_remap::VERBOSE) {
print "Longest alignment MSP found spanning coordinate $coordinate:\n"
. $msp->toString() . "\n";
}
my ($reference_lend, $reference_rend) = $msp->get_reference_coords();
my ($query_lend, $query_rend) = $msp->get_query_coords();
my $orientation = $msp->get_orientation();
my $adj_coordinate;
if ($type == $REFERENCE_TO_QUERY) {
my $delta = $coordinate - $reference_lend;
if ($orientation eq '+') {
$adj_coordinate = $query_lend + $delta;
print "CoordConvert: case A\n" if $DEBUG;
}
else {
# revcomp match
$adj_coordinate = $query_rend - $delta;
print "CoordConvert: case B\n" if $DEBUG;
}
}
else {
## QUERY_TO_REFERENCE
if ($orientation eq '+') {
my $delta = $coordinate - $query_lend;
$adj_coordinate = $reference_lend + $delta;
print "CoordConvert: case C\n" if $DEBUG;
}
else {
# revcomp'd
my $delta = $query_rend - $coordinate;
$adj_coordinate = $reference_lend + $delta;
print "CoordConvert: case D\n" if $DEBUG;
}
}
push (@converted_coordinates, $adj_coordinate);
}
$self->_store_MSP_list(values %msp_tracker); #trick to get unique entries (using keys doesn't work due to string conversion)
## check calling context to make sure we do the right thing in our return value(s)
if (scalar (@converted_coordinates) > 1) {
if (wantarray) {
return (@converted_coordinates);
}
else {
confess "fatal, have list of converted coordinates but function called in scalar context ";
}
}
else {
if (wantarray) {
return (@converted_coordinates);
}
else {
return (scalar $converted_coordinates[0]);
}
}
}
###
sub _find_longest_overlapping_coordinate_set {
my $self = shift;
my ($type, $coordinate, $coord_sets_aref) = @_;
my $longest_coordset = undef;
my $longest_length = 0;
foreach my $coordset (@$coord_sets_aref) {
my $got_overlap_flag = 0;
my $length;
if ($type == $REFERENCE_TO_QUERY) {
my ($reference_lend, $reference_rend) = $coordset->get_reference_coords();
$length = $coordset->get_reference_coord_span_length();
if ($reference_lend <= $coordinate && $coordinate <= $reference_rend) {
## overlap found
$got_overlap_flag = 1;
}
}
elsif ($type == $QUERY_TO_REFERENCE) {
my ($query_lend, $query_rend) = $coordset->get_query_coords();
$length = $coordset->get_query_coord_span_length();
if ($query_lend <= $coordinate && $coordinate <= $query_rend) {
## overlap found
$got_overlap_flag = 1;
}
}
else {
confess "fatal bug, type not recognized.";
}
if ($got_overlap_flag) {
if ($length > $longest_length) {
$longest_length = $length;
$longest_coordset = $coordset;
}
}
}
return ($longest_coordset);
}
####
sub _get_alignments_from_ids {
my $self = shift;
my ($ids_href) = @_;
my $reference_accession = $ids_href->{reference_accession} or confess "must specify reference_accession as key in hash_ref ";
my $query_accession = $ids_href->{query_accession} or confess "must specify query_accession as key in hash_ref ";
my $accessions_to_alignments_href = $self->get_accession_to_alignments_mappings();
my $alignment_list_aref = $accessions_to_alignments_href->{$reference_accession}->{$query_accession};
unless (ref $alignment_list_aref) {
die "Error, couldn't find alignment based on ref: $reference_accession, query: $query_accession ";
}
return (@$alignment_list_aref);
}
1; #EOM