| 1 |
import sys |
| 2 |
import K3000_common as kc |
| 3 |
|
| 4 |
""" |
| 5 |
Given an input phased variants, select and output only "working zones" |
| 6 |
A working zone is defined by |
| 7 |
1/ Coverage XXX todo |
| 8 |
2/ Topology: conserve only parts defined with X patterns. |
| 9 |
A node is linked to at most two other nodes. |
| 10 |
If a node n1 is linked to two nodes n2 and n3, then n2 and n3 have at most two sons, n4 and n5 |
| 11 |
""" |
| 12 |
|
| 13 |
def get_reverse_fact(x): |
| 14 |
''' reverse of a fact x. Example x = "4l_0;2h_3;-6h_21", reverse(x) = "6h_0;-2h_21;-4l_3"''' |
| 15 |
res ="" |
| 16 |
x=x.strip(";").split(";") |
| 17 |
for i in range (len(x)): |
| 18 |
original_id = x[-i-1] |
| 19 |
if original_id[0]=="-": reversed_id = original_id[1:] |
| 20 |
else: reversed_id = "-"+original_id |
| 21 |
if i==0: |
| 22 |
value=kc.string_allele_value(reversed_id)+"_0" |
| 23 |
else: |
| 24 |
value=kc.string_allele_value(reversed_id)+"_"+kc.distance_string_value(x[-i]) |
| 25 |
res+=value+";" |
| 26 |
return res |
| 27 |
|
| 28 |
|
| 29 |
def store_nodes(fact_file_name): |
| 30 |
""" |
| 31 |
Store nodes with their min, max and mean coverages |
| 32 |
""" |
| 33 |
nodes = {} |
| 34 |
with open(fact_file_name) as fact_file: |
| 35 |
for line in fact_file.readlines(): |
| 36 |
if not line: break |
| 37 |
line=line.strip() |
| 38 |
if line[0]=="#": continue |
| 39 |
" -1145l_0;4780h_-60;-248h_-63;-7035l_-60;4077l_-59;-6265l_-60;-2978h_-69;4958h_-59;-4221h_-60;-1376l_-60;-4664l_-59;-2207h_-60;-6257l_-60;-5529l_-59;-5106h_-59;1310h_-59;-4514h_-59;3933h_-60;-5727h_-60;2325l_-60;-3976l_-59;-3363h_-60;-716l_-59;-3526h_-51;-5819h_-57;2927l_-59;-6146l_-60;-4374h_-53; => 1" |
| 40 |
coverage = int(line.split()[-1]) |
| 41 |
for fact_id in range(len(line.split())-2): |
| 42 |
original_fact = line.split()[fact_id] |
| 43 |
for fact in original_fact, get_reverse_fact(original_fact): |
| 44 |
|
| 45 |
for variant in fact.strip(";").split(";"): |
| 46 |
|
| 47 |
signed_variant_id=variant.split("_")[0] |
| 48 |
if signed_variant_id not in nodes: nodes[signed_variant_id] = 0 |
| 49 |
assert len(signed_variant_id)>0,line+"\n"+str(line.split()[fact_id]) |
| 50 |
nodes[signed_variant_id] += coverage |
| 51 |
return nodes |
| 52 |
|
| 53 |
def f(tuple, el): |
| 54 |
return tuple[0]==el |
| 55 |
|
| 56 |
def isin(list, el): |
| 57 |
for tuple in list: |
| 58 |
if f(tuple, el): return True |
| 59 |
return False |
| 60 |
|
| 61 |
def exists_edge(edges, rev_edges, node_id_1, node_id_2): |
| 62 |
for current_edges in edges, rev_edges: |
| 63 |
if node_id_1 in current_edges: |
| 64 |
if isin(current_edges[node_id_1], node_id_2): return True |
| 65 |
if node_id_2 in current_edges: |
| 66 |
if isin(current_edges[node_id_2], node_id_1): return True |
| 67 |
return False |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
def store_ordered_edges(fact_file_name): |
| 72 |
""" |
| 73 |
Store edges |
| 74 |
Conserve only the closest edges. In case of equalities of minimal distance edges, conserve the closest ones. |
| 75 |
""" |
| 76 |
edges = {} |
| 77 |
|
| 78 |
rev_edges = {} |
| 79 |
with open(fact_file_name) as fact_file: |
| 80 |
for line in fact_file.readlines(): |
| 81 |
if not line: break |
| 82 |
line=line.strip() |
| 83 |
if line[0]=="#": continue |
| 84 |
|
| 85 |
|
| 86 |
for fact_id in range(len(line.split())-2): |
| 87 |
original_fact = line.split()[fact_id] |
| 88 |
for used_fact in original_fact, get_reverse_fact(original_fact): |
| 89 |
comapped_variants = used_fact.strip(";").split(";") |
| 90 |
|
| 91 |
for i in range(len(comapped_variants)-1): |
| 92 |
from_node_id = comapped_variants[i].split("_")[0] |
| 93 |
to_node_id = comapped_variants[i+1].split("_")[0] |
| 94 |
distance = int(comapped_variants[i+1].split("_")[1]) |
| 95 |
|
| 96 |
|
| 97 |
if from_node_id not in edges: edges[from_node_id] = set() |
| 98 |
edges[from_node_id].add((to_node_id, distance)) |
| 99 |
if to_node_id not in rev_edges: rev_edges[to_node_id] = set() |
| 100 |
rev_edges[to_node_id].add((from_node_id, distance)) |
| 101 |
|
| 102 |
|
| 103 |
for current_edges in edges, rev_edges: |
| 104 |
for from_variant_id in current_edges: |
| 105 |
current_edges[from_variant_id] = sorted(current_edges[from_variant_id], key=lambda x : x[1]) |
| 106 |
sys.stderr.write(f"sorted edges {edges}\n") |
| 107 |
|
| 108 |
nb_removed = 0 |
| 109 |
nb_total = 0 |
| 110 |
new_edges = [{},{}] |
| 111 |
i=-1 |
| 112 |
for current_edges in edges, rev_edges: |
| 113 |
i+=1 |
| 114 |
|
| 115 |
for from_variant_id in current_edges: |
| 116 |
|
| 117 |
updated_list = set() |
| 118 |
updated_list.add(current_edges[from_variant_id][0]) |
| 119 |
for id_in_current_edges in range(len(current_edges[from_variant_id])-1): |
| 120 |
nb_total+=1 |
| 121 |
cur_var = current_edges[from_variant_id][id_in_current_edges] |
| 122 |
next_var = current_edges[from_variant_id][id_in_current_edges+1] |
| 123 |
if cur_var[1]==next_var[1]: |
| 124 |
updated_list.add(current_edges[from_variant_id][id_in_current_edges+1]) |
| 125 |
continue |
| 126 |
if not exists_edge(edges, rev_edges, cur_var[0], next_var[0]): |
| 127 |
updated_list.add(current_edges[from_variant_id][id_in_current_edges+1]) |
| 128 |
else: |
| 129 |
sys.stderr.write(f"removed {from_variant_id} -> {current_edges[from_variant_id][id_in_current_edges+1]}\n") |
| 130 |
nb_removed+=1 |
| 131 |
updated_list = sorted(updated_list, key=lambda x : x[1]) |
| 132 |
|
| 133 |
new_edges[i][from_variant_id]=updated_list |
| 134 |
|
| 135 |
sys.stderr.write(f"Removed {nb_removed} among {nb_total}\n") |
| 136 |
edges = new_edges[0] |
| 137 |
rev_edges = new_edges[1] |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
for current_edges in edges, rev_edges: |
| 142 |
for from_variant_id in current_edges: |
| 143 |
id_only_list = set() |
| 144 |
|
| 145 |
for to_variant_id, distance in current_edges[from_variant_id]: |
| 146 |
id_only_list.add(to_variant_id) |
| 147 |
current_edges[from_variant_id] = id_only_list |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
return edges,rev_edges |
| 172 |
|
| 173 |
|
| 174 |
def store_edges(fact_file_name): |
| 175 |
""" |
| 176 |
Store edges |
| 177 |
""" |
| 178 |
edges = {} |
| 179 |
rev_edges = {} |
| 180 |
with open(fact_file_name) as fact_file: |
| 181 |
for line in fact_file.readlines(): |
| 182 |
if not line: break |
| 183 |
line=line.strip() |
| 184 |
if line[0]=="#": continue |
| 185 |
|
| 186 |
|
| 187 |
for fact_id in range(len(line.split())-2): |
| 188 |
comapped_variants = line.split()[fact_id].strip(";").split(";") |
| 189 |
for i in range(len(comapped_variants)-1): |
| 190 |
from_node_id = comapped_variants[i].split("_")[0] |
| 191 |
to_node_id = comapped_variants[i+1].split("_")[0] |
| 192 |
|
| 193 |
|
| 194 |
if from_node_id not in edges: edges[from_node_id] = set() |
| 195 |
edges[from_node_id].add(to_node_id) |
| 196 |
if to_node_id not in rev_edges: rev_edges[to_node_id] = set() |
| 197 |
rev_edges[to_node_id].add(from_node_id) |
| 198 |
|
| 199 |
return edges,rev_edges |
| 200 |
|
| 201 |
def remove_nodes_with_high_io_degree(edges, rev_edges, nodes, max_io=2): |
| 202 |
""" |
| 203 |
remove any node that has two or more childre and remove all the corresponding children |
| 204 |
""" |
| 205 |
for current_edges in edges, rev_edges: |
| 206 |
for from_variant_id in current_edges: |
| 207 |
if len(current_edges[from_variant_id]) > max_io: |
| 208 |
if from_variant_id in nodes: del nodes[from_variant_id] |
| 209 |
for to_variant_id in current_edges[from_variant_id]: |
| 210 |
if to_variant_id in nodes: del nodes[to_variant_id] |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
def print_as_gfa(nodes, edges): |
| 215 |
|
| 216 |
|
| 217 |
node_id_to_variant_id = {} |
| 218 |
variant_id_to_node_id = {} |
| 219 |
node_id=1 |
| 220 |
for node in nodes: |
| 221 |
node_id_to_variant_id[node_id] = node |
| 222 |
variant_id_to_node_id[node] = node_id |
| 223 |
node_id+=1 |
| 224 |
|
| 225 |
|
| 226 |
for node_id in node_id_to_variant_id: |
| 227 |
|
| 228 |
print("S\t"+str(node_id)+"\t"+str(node_id_to_variant_id[node_id])+"\tRC:i:"+str(nodes[node_id_to_variant_id[node_id]])) |
| 229 |
|
| 230 |
|
| 231 |
for from_variant_id in edges: |
| 232 |
if from_variant_id not in nodes: continue |
| 233 |
|
| 234 |
for to_variant_id in edges[from_variant_id]: |
| 235 |
if to_variant_id not in nodes: continue |
| 236 |
|
| 237 |
print("L\t"+str(variant_id_to_node_id[from_variant_id])+"\t+\t"+str(variant_id_to_node_id[to_variant_id])+"\t+\t0M") |
| 238 |
|
| 239 |
def main(): |
| 240 |
nodes = store_nodes(sys.argv[1]) |
| 241 |
|
| 242 |
edges,rev_edges = store_ordered_edges(sys.argv[1]) |
| 243 |
remove_nodes_with_high_io_degree(edges, rev_edges, nodes, 2000) |
| 244 |
print_as_gfa (nodes, edges) |
| 245 |
|
| 246 |
|
| 247 |
if __name__ == '__main__': |
| 248 |
main() |