-
Notifications
You must be signed in to change notification settings - Fork 3
/
chains2inserts.pl
298 lines (233 loc) · 7.5 KB
/
chains2inserts.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#! /usr/bin/perl
if ($#ARGV < 0)
{
print "Useage: $0 file1.chains [file2.chains ...]\n";
print " Converts a sorted chains file into a file of insert lengths.\n";
print " The read-ids are assumed to be those generated by\n";
print " MakeRealInserts.cc or MakeFakeInserts.cc\n";
exit(0);
}
# the mean fosmid insert length;
# this should be constant so long as
# the fosmid library construction technology does not change.
$mean_length = 39400; # not used except for experimental purposes
####################
# Iterate through the given list of files
$num_files = $#ARGV + 1;
for ($i = 0; $i < $num_files; $i++)
{
$sorted_chains_file = $ARGV[$i];
$sorted_chains_file =~ /(.+)\.chains/;
$inserts_file = "$1.inserts";
####################
# Load the chains file and parse it
# into the data about the alignments
$now_time = localtime;
print "$now_time: Reading $sorted_chains_file...\n";
$num_aligns = 0;
@align_score = ();
@align_chromosome = ();
@align_start_coord = ();
@align_stop_coord = ();
@align_read_num = ();
@align_read_AB = ();
@align_orientation = ();
open(SORTED_CHAINS_FILE, $sorted_chains_file) ||
die "Can't open $sorted_chains_file\n";
while ($line = <SORTED_CHAINS_FILE>)
{
# if ($line =~ /^chain \d+ (.+)_chr(.+)_\d+_\d+ \d+ . \d+ \d+ r(\d+)(A|B) \d+ . \d+ \d+/)
if ( $line =~ /^chain (\d+) chr(.+)\.(\d+)-(\d+) (\d+) (.) (\d+) (\d+) (.+) (\d+) (.) (\d+) (\d+) /)
{
$score = $1;
$chromosome = $2;
$chr_min = $3;
$chr_max = $4;
$chr_length = $5;
$chr_orien = $6;
$chr_start = $7;
$chr_stop = $8;
$read_id = $9;
$read_length = $10;
$read_orien = $11;
$read_start = $12;
$read_stop = $13;
if ($chr_orien eq "-")
{
print "REVERSED: $line";
}
$read_id =~ /r(\d+)(A|B)/;
$read_num = $1;
$read_AB = $2;
push @align_score, $score;
push @align_chromosome, $chromosome;
push @align_start_coord, $chr_min+$chr_start;
push @align_stop_coord, $chr_min+$chr_stop;
push @align_read_num, $read_num;
push @align_read_AB, $read_AB;
if ( ($chr_orien eq "+" && $read_orien eq "+") ||
($chr_orien eq "-" && $read_orien eq "-") )
{
push @align_orientation, "+";
}
else
{
push @align_orientation, "-";
}
$num_aligns++;
}
else
{
print "UNMATCHED: $line";
}
} # while ($line = <SORTED_CHAINS_FILE>)
close(SORTED_CHAINS_FILE);
$now_time = localtime;
print "$now_time: Reading $sorted_chains_file...done.\n";
print "$num_aligns many alignments\n";
if ($num_aligns == 0)
{
die "No alignments found in $sorted_chains_file\n";
}
####################
# Screen the alignments of pairs of reads
# and select those for which both reads align
# to the same chromosome and in the correct orientation
$now_time = localtime;
print "$now_time: Placing inserts...\n";
@placement_nums = ();
@placement_lengths = ();
@placement_scores = ();
$num_placements = 0;
# array index for a block of consecutive entries with the same read_num
$blk_index = 0;
while ($blk_index < $num_aligns)
{
$curr_blk_index = $blk_index;
$curr_placement_num = $align_read_num[$blk_index];
while ($blk_index < $num_aligns &&
$align_read_num[$blk_index] == $curr_placement_num)
{
$blk_index++;
}
# now the indices in the interval [$curr_blk_index, $blk_index)
# all have the same $align_read_num[..] value, equal to $curr_placement_num
$blkA_start = $curr_blk_index;
$blkA_stop = $curr_blk_index;
while ($blkA_stop < $blk_index &&
$align_read_AB[$blkA_stop] eq "A")
{
$blkA_stop++;
}
if ($blkA_start == $blkA_stop)
{
# there are no A-reads in this block
next;
}
$blkB_start = $blkA_stop;
$blkB_stop = $blk_index;
if ($blkB_start == $blkB_stop)
{
# there are no B-reads in this block
next;
}
# now the indices in the interval [$blkA_start, $blkA_stop)
# all have the same $align_read_AB[..] value, equal to "A",
# while the indices in the interval [$blkB_start, $blkB_stop)
# all have the same $align_read_AB[..] value, equal to "B"
for ($a = $blkA_start; $a < $blkA_stop; $a++)
{
for ($b = $blkB_start; $b < $blkB_stop; $b++)
{
if ($align_chromosome[$a] != $align_chromosome[$b])
{
# the reads lie on different chromosomes
next;
}
$chromosome = $align_chromosome[$a];
if ($align_orientation[$a] != $align_orientation[$b])
{
# the reads are oriented differently
next;
}
$orientation = $align_orientation[$a];
if ($orientation eq "+")
{
$start_coord = $align_start_coord[$a];
$stop_coord = $align_stop_coord[$b];
$start_score = $align_score[$a];
$stop_score = $align_score[$b];
}
else
{
$start_coord = $align_start_coord[$b];
$stop_coord = $align_stop_coord[$a];
$start_score = $align_score[$b];
$stop_score = $align_score[$a];
}
# NOTE: Blastz somehow returns coordinates such that
# the resulting insert lengths are 4 bases short;
# so we compensate for that by adding 4 here.
push @placement_lengths, $stop_coord - $start_coord + 4;
push @placement_nums, $curr_placement_num;
push @placement_scores, $start_score + $stop_score;
$num_placements++;
} # for ($b = $blkB_start; $b < $blkB_stop; $b++)
} # for ($a = $blkA_start; $a < $blkA_stop; $a++)
} # while ($blk_index < $num_aligns)
$now_time = localtime;
print "$now_time: Placing inserts...done.\n";
print "$num_placements many placements found in total\n";
if ($num_placements == 0)
{
die "No placement found\n";
}
####################
# Filter the placements of read-pairs generated above
# and pick those which have good scores and reasonable insert lengths
# and write them to the output file.
$now_time = localtime;
print "$now_time: Writing to $inserts_file...\n";
open(INSERTS_FILE, "> $inserts_file") ||
die "Can't open $inserts_file for output\n";
$num_ok_inserts = 0;
$index = 0;
while ($index < $num_placements)
{
$curr_index = $index;
$num = $placement_nums[$index];
while ($index < $num_placements &&
$placement_nums[$index] eq $num)
{
$index++;
}
# now the indices in the interval [$curr_index, $index)
# all have the same $placement_nums[..] value, equal to $num
$best_line = "";
$best_score = 0;
for ($i = $curr_index; $i < $index; $i++)
{
if ($placement_scores[$i] > $best_score &&
$placement_lengths[$i] > 0 &&
$placement_lengths[$i] < 200000)
# $placement_lengths[$i] > $mean_length/2 &&
# $placement_lengths[$i] < 2*$mean_length)
# $placement_lengths[$i] > $mean_length - 2.5 * 1000 &&
# $placement_lengths[$i] < $mean_length + 2.5 * 1000)
{
$best_score = $placement_scores[$i];
$best_line = sprintf "%6s %6s\n",
( $placement_nums[$i], $placement_lengths[$i] );
}
}
if ($best_score > 1)
{
print INSERTS_FILE $best_line;
$num_ok_inserts++;
}
}
close(INSERTS_FILE);
$now_time = localtime;
print "$now_time: Writing to $inserts_file...done.\n";
print "$num_ok_inserts many inserts accepted\n";
} # for ($i = 0; $i < $num_files; $i++)