-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtag_gfa_copy_numbers.pl
More file actions
executable file
·294 lines (255 loc) · 7.54 KB
/
tag_gfa_copy_numbers.pl
File metadata and controls
executable file
·294 lines (255 loc) · 7.54 KB
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
#!/usr/bin/perl -w
# Analyses a GFA file to estimate the coverage depth.
# From here we turn it to a node coverage estimation. (Floating point)
use strict;
use Getopt::Long;
my $verbose = 0;
my $offset = 0.4;
my $float_copy = 1;
my $min_depth = 0.1;
my $depth_div = 4;
my $max_copy = 10;
my $mode = "float";
my $min_copy = 0.45;
GetOptions("v|verbose" => \$verbose,
"offset=f" => \$offset,
"mode=s" => \$mode,
"m|min-depth=f" => \$min_depth,
"d|depth-div=f" => \$depth_div,
"C|max-copy=i" => \$max_copy,
"c|min-copy=i" => \$min_copy);
if ($mode =~ /^f.*/) {
$float_copy = 1;
} elsif ($mode =~ /^i.*/) {
$float_copy = 0;
} else {
print STDERR "Illegal mode: use 'f' or 'i'\n";
exit(1);
}
# Parse GFA
my @node_order;
my %seq; # node sequence
my %node; # node GFA line (S)
my @edge; # edge GFA line (L)
my $edge_num=0;
my %edge_in; # index into @edge above
my %edge_out; # index into @edge above
my %self_loop;
local $"="\t";
while (<>) {
chomp();
if (/^S\s+(\S+)/) {
push(@node_order, $1);
$node{$1} = $_;
my @N = split("\t", $_);
$seq{$N[1]} = $N[2];
my ($SC) = $node{$N[1]} =~ m/SC:f:(-?\d+(\.\d+)?)/;
if (!defined($SC)) {
my ($KC) = $node{$N[1]} =~ m/KC:i:(\d+)/;
if (!defined($KC)) {
print STDERR "No SC or KC present\n";
exit(1);
}
$SC = $KC / length($seq{$N[1]});
$node{$N[1]} .= "\tSC:f:$SC";
}
} elsif (/^L\s+(\S+)\s+(.)\s+(\S+)\s+(.)/) {
# ASSUMPTION: nodes before edges
if (!exists($node{$1}) || !exists($node{$3})) {
print STDERR "Removing edge $_\n";
next;
}
$edge[$edge_num] = $_;
$self_loop{$1} = 1 if $1 eq $3;
if ($2 eq "+") {
push(@{$edge_out{$1}}, $edge_num);
} else {
push(@{$edge_in{$1}}, $edge_num);
}
if ($4 eq "+") {
push(@{$edge_in{$3}}, $edge_num);
} else {
push(@{$edge_out{$3}}, $edge_num);
}
$edge_num++;
}
}
# Find simple cycles. This isn't particularly fast on large graphs.
# TODO: look at Johnson's algorithm, or rewrite-in / callout to Python which
# has an implementation of this in networkx:
#
# my $code = "import networkx as nx\nG=nx.DiGraph()\n";
# $code .= "G.add_nodes_from([\"" . join("\",\"",@node_order) . "\"])\n";
# my @edge_str;
# foreach (@edge) {
# m/^L\s+(\S+)\s+(.)\s+(\S+)\s+(.)/;
# push(@edge_str, "(\"".$1."\",\"".$3."\")");
# }
# $code .= "G.add_edges_from([" . join(",", @edge_str) . "])\n";
# $code .= "print(list(nx.simple_cycles(G)))\n";
# my $simple_cycles = `python -c '$code'`;
# $simple_cycles =~ tr/[],'/ /;
# $simple_cycles =~ s/^\s+//;
# foreach (split(/\s+/,$simple_cycles)) {
# $loop{$_}=1;
# }
sub find_all_cycles {
my @all_cycles;
for my $start (@node_order) {
my @stack = ([$start, [$start]]);
while (@stack) {
my ($node, $path) = @{pop @stack};
my @next;
# Do we also need to do edge_in here, or take into account orientation?
foreach (@{$edge_out{$node}}) {
$edge[$_] =~ m/^L\s+(\S+)\s+(.)\s+(\S+)\s+(.)/;
if ($1 eq $node) {
push(@next, $3);
} else {
push(@next, $1);
}
}
for my $neighbour (sort @next) {
if ($neighbour eq $path->[0] && @$path > 1) {
push @all_cycles, [@$path];
} elsif (!grep { $_ eq $neighbour } @$path[1..$#$path]) {
push @stack, [$neighbour, [@$path, $neighbour]];
}
}
}
}
return @all_cycles;
}
my @all_cycles = find_all_cycles();
my %loop;
foreach my $cycle (@all_cycles) {
foreach (@$cycle) {
$loop{$_}=1;
}
}
if ($verbose) {
print "In loops:";
foreach (sort keys %loop) {
print " $_";
}
print "\n";
}
# An array of raw depths. Filter very low figures (unused nodes), compute
# average, and then cycle again with a revised estimate of what low-depth
# means.
my $avg_depth = 0;
my $total;
my $tlen;
my $last_avg_depth;
my $ncycles = 0;
do {
$last_avg_depth = $avg_depth;
$avg_depth = 0;
$total = 0;
$tlen = 0;
foreach my $n (sort keys %node) {
my ($d) = $node{$n} =~ m/SC:f:(-?\d+(\.\d+)?)/;
#if ($verbose) {
# print "Node $n\tdepth ",int(100*$d)/100,"\tlen ",length($seq{$n}),"\tin-loop ",exists($loop{$n}) ? 1 : 0, "\n";
#}
if ($d > $min_depth) {
# For initial average, exclude self loops
next if exists($self_loop{$n});
next if $loop{$n};
#if ($verbose) {
# print "Using $n len ",length($seq{$n}), " depth $d\n";
#}
$total += $d*(length($seq{$n}));
$tlen += (length($seq{$n}));
}
}
$avg_depth = $total / $tlen;
if ($verbose) {
print "Avg depth $avg_depth\n";
}
$min_depth = $avg_depth/$depth_div;
$ncycles++;
} while ($avg_depth != $last_avg_depth && $ncycles < 10);
# When running on an trimmed sub-graph our stats can be skewed.
# Rerun on the entire file to get a better estimate of the starting average
# depth.
#
# Detrimental to minigraph based GFAs.
# Marginally helpful to alfapang graphs, but maybe not worth it.
# Relies on the print "Avg depth" above, but we're not using this now
# anyway.
#
#if ($ARGV =~ /\.edited\./) {
# my ($base) = $ARGV =~ m/(.*)\.edited\..*/;
# $_=`tag_gfa_copy_numbers.pl -v $base.gfa | grep "1-depth"`;
# my @F = split(/\s+/, $_);
# #print "Avg $F[-1]\n";
# $avg_depth = $F[-1];
#}
# Alternative; try fitting depth to D, 1*D, 2*D, etc.
my $best_try=$avg_depth;
my $best_delta=1e10;
for (my $try=$avg_depth/1.3; $try<$avg_depth*1.5; $try+=0.1) {
my $delta = 0;
foreach my $n (sort keys %node) {
my ($d) = $node{$n} =~ m/SC:f:(-?\d+(\.\d+)?)/;
if ($d > $avg_depth/4) {
my $mult = int(0.5+$d/$try);
my $diff = abs($d-$mult*$try)/$try;
#my $diff = $d % int($try+0.5)/$try; # better with minigraph?
$delta += $diff*(length($seq{$n}));
#printf(" %s\t%.2f\t%d\t%d\t%.2f\t%8.2f\n",
# $n,$d,$mult,length($seq{$n}),$diff,
# $diff*(length($seq{$n})));
}
}
# Normalise by try itself? Smaller values inherently have smaller
# remainders.
#$delta /= $try;
if ($best_delta > $delta) {
$best_delta = $delta;
$best_try = $try;
}
print "try $try, delta=$delta\n" if $verbose;
}
if ($verbose) {
print "Possibly 1-depth = $best_try\n";
}
$avg_depth = $best_try;
# Another possible test is if our copy number estimations are now mostly even
# and rarely odd, then we have half the depth. Similarly if they go in
# multiples of 3. There's likely too much noise for this to work however,
# and if it's not noisy then the chances are we found the correct depth.
# Report
# TODO: maybe output a new GFA with CN:i: or CN:f: values?
if ($verbose) {
foreach my $n (@node_order) {
my ($d) = $node{$n} =~ m/SC:f:(-?\d+(\.\d+)?)/;
printf("%s\t%.2f\t%.2f\t%d\n", $n, $d, $d/$avg_depth,
int($d/$avg_depth+0.5));
}
} else {
my @copy;
foreach my $n (@node_order) {
my ($d) = $node{$n} =~ m/SC:f:(-?\d+(\.\d+)?)/;
my $copy;
if ($float_copy) {
$copy = $d/$avg_depth+$offset;
} else {
$copy = int($d/$avg_depth+0.5+$offset);
}
$copy = $min_copy if ($copy < $min_copy);
$copy = $max_copy if ($copy > $max_copy);
push(@copy, $copy);
}
local $"=",";
print "@copy\n";
}
__END__
compare:
~/lustre/tmp/_ga_pf2k-t2.25_00100/seq_1050-0003-#1#1.edited.gfa (27.9)
~/lustre/tmp/_ga_pf2k-t2.25_00100/seq_1089-0054-#1#1.edited.gfa (113.5)
[NB: works better on the unedited.gfa to get initial depth]
[But pathfinder picks the wrong values too]
See also ../_km_pf2k-t1.25_00102/seq_1055-0054-#1#1.edited.gfa for
demonstration of graph simplification which hasn't worked.