-
Notifications
You must be signed in to change notification settings - Fork 2
/
Mummer_coord_converter.pm
executable file
·273 lines (218 loc) · 6.37 KB
/
Mummer_coord_converter.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
#!/usr/local/bin/perl
=Description
This module performs coordinate transformations from one assembly to another
based on the results from a Mummer comparison.
#!/usr/local/perl
use Mummer_coord_converter;
my $converter = new Mummer_coord_converter();
$converter->parse_Mummer_outputfiles('mum_forward', 'mum_reverse');
$converter->data_dumper();
my $num = 1;
my $converted_num = $converter->transform_coordinate_1_to_2($num);
print "$num converted to $converted_num\n";
=cut
our $SEE = 0; #set in main.
package Mummer_coord_converter;
use strict;
sub new {
# no input parameters
my $self = {
mum_objs => []
};
bless ($self);
return ($self);
}
sub parse_Mummer_outputfiles {
my $self = shift;
my ($forward_run, $reverse_run) = @_;
## parse forward run file
open (MUM, "$forward_run") or die "Can't open $forward_run\n";
while (<MUM>) {
if (/^\#/) {
next;
}
if (/\s*(\d+)\s+(\d+)\s+(\d+)\s*/) {
print if $main::SEE;
my ($coord_seq1, $coord_seq2, $match_length) = ($1, $2, $3);
my $coord_seq1a = $coord_seq1;
my $coord_seq1b = $coord_seq1 + $match_length - 1;
my $coord_seq2a = $coord_seq2;
my $coord_seq2b = $coord_seq2 + $match_length - 1;
my $orientation = '+';
my $mum_obj = Mum_obj::create_Mum_obj ($coord_seq1a, $coord_seq1b,
$coord_seq2a, $coord_seq2b,
$match_length, $orientation);
$self->add_mum_obj($mum_obj);
}
}
close MUM;
## parse reverse run file
## first pass, gather length of seq 1 (which was reverse complemented)
my $seq1_length;
open (MUM, "$reverse_run") or die "Can't open $reverse_run\n";
while (<MUM>) {
if (/^\#\s(\S+)\s(\d+)\s(\S+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)/) {
my $seq1_name = $1;
$seq1_length = $2;
my $seq2_name = $3;
my $min_mum_length_setting = $4;
my $num_mums = $5;
my $total_mums_length_in_nts = $5;
last;
}
}
close MUM;
unless ($seq1_length) {
die "ERROR: I couldn't parse the seq1_length from the mum_r file\n";
}
## second pass, retrieve the mum info
open (MUM, "$reverse_run") or die "Can't open $reverse_run\n";
while (<MUM>) {
if (/^\#/) {
next;
}
if (/\s*(\d+)\s+(\d+)\s+(\d+)\s*/) {
print if $main::SEE;
my ($coord_seq1, $coord_seq2, $match_length) = ($1, $2, $3);
my $coord_seq1a = $coord_seq1;
my $coord_seq1b = $coord_seq1 + $match_length - 1;
## swap positions after revcomping them
($coord_seq1a, $coord_seq1b) = reverse (&revcomp_coord($coord_seq1a, $seq1_length),
&revcomp_coord($coord_seq1b, $seq1_length));
my $coord_seq2a = $coord_seq2;
my $coord_seq2b = $coord_seq2 + $match_length - 1;
my $orientation = '-';
my $mum_obj = Mum_obj::create_Mum_obj ($coord_seq1a, $coord_seq1b,
$coord_seq2a, $coord_seq2b,
$match_length, $orientation);
$self->add_mum_obj($mum_obj);
}
}
close MUM;
$self->sort_MUMs();
}
sub sort_MUMs {
my $self = shift;
my $mum_objs_list = $self->{mum_objs};
@$mum_objs_list = reverse sort {$a->{match_length}<=>$b->{match_length}} @$mum_objs_list;
}
sub add_mum_obj {
my $self = shift;
my $mum_obj = shift;
my $index = $#{$self->{mum_objs}};
$index++;
#print "Index: $index\n";
$self->{mum_objs}->[$index] = $mum_obj;
}
sub transform_coordinate_1_to_2 {
my $self = shift;
my $coord = shift;
my ($mum_obj, $c1, $c2) = $self->get_spanning_mum_obj($coord);
if ($mum_obj) {
my $strand = $mum_obj->{orientation};
my $rel_coord2 = $mum_obj->{coord_seq2a};
if ($strand eq '+') {
my $delta = $coord - $c1;
return ($delta + $rel_coord2);
} else {
my $delta = $c2 - $coord;
return ($delta + $rel_coord2);
}
}
}
sub get_spanning_mum_obj {
my $self = shift;
my $coord = shift;
my $mum_collection = $self->{mum_objs};
for (my $i=0; $i <=$#{$mum_collection}; $i++) {
my $c1 = $mum_collection->[$i]->{coord_seq1a};
my $c2 = $mum_collection->[$i]->{coord_seq1b};
#print "$i Searching coords: $coord >= $c1 && $coord <= $c2\n";
if ($coord >= $c1 && $coord <= $c2) {
#print "FOUND IT\n";
return ($mum_collection->[$i], $c1, $c2);
}
}
#print "DAMNIT!!!!\n";
return (0);
}
sub data_dumper {
my ($self) = shift;
my @mum_objs = @{$self->{mum_objs}};
foreach my $mum_obj (@mum_objs) {
print $mum_obj->toString();
}
}
sub get_all_mums {
my $self = shift;
return (@{$self->{mum_objs}});
}
sub get_longest_mum {
my $self = shift;
my @mum_objs = @{$self->{mum_objs}};
my $longest_length = 0; #initialize
my $longest_mum = undef(); #initialize
foreach my $mum (@mum_objs) {
print $mum->toString() if $main::SEE;
my $match_length = $mum->get_match_length();
if ($match_length > $longest_length) {
$longest_length = $match_length;
$longest_mum = $mum;
}
}
return ($longest_mum);
}
sub revcomp_coord {
my ($coord, $seq_length) = @_;
return ($seq_length - $coord + 1);
}
##################################
## Mum_obj stores mummer data
package Mum_obj;
use strict;
sub new {
my $self = {
coord_seq1a=>0,
coord_seq1b=>0,
coord_seq2a=>0,
coord_seq2b=>0,
match_length=>0,
orientation=> 0
};
bless ($self);
return ($self);
}
sub create_Mum_obj {
my ($coord_seq1a, $coord_seq1b, $coord_seq2a, $coord_seq2b,
$match_length, $orientation) = @_;
print "INCOMING: ($coord_seq1a, $coord_seq1b, $coord_seq2a, $coord_seq2b, $match_length, $orientation\n" if $SEE;
my $self = new();
$self->{coord_seq1a} = $coord_seq1a;
$self->{coord_seq1b} = $coord_seq1b;
$self->{coord_seq2a} = $coord_seq2a;
$self->{coord_seq2b} = $coord_seq2b;
$self->{match_length} = $match_length;
$self->{orientation} = $orientation;
return ($self);
}
sub get_coords_seq1 {
my $self = shift;
return ($self->{coord_seq1a}, $self->{coord_seq1b});
}
sub get_coords_seq2 {
my $self = shift;
return ($self->{coord_seq2a}, $self->{coord_seq2b});
}
sub get_match_length {
my $self = shift;
return ($self->{match_length});
}
sub get_orientation {
my $self = shift;
return ($self->{orientation});
}
sub toString {
my $self = shift;
return "MUM Data: $self->{coord_seq1a}\t$self->{coord_seq1b}\t$self->{coord_seq2a}\t$self->{coord_seq2b}\t$self->{match_length}\t$self->{orientation}\n";
}
1; #end of module