Debian Codegraph @

scripts/k3000/K3000_msr_to_gfa.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 #
4 '''
5 Creation of a GFA file from a set of compacted maximal facts
6 '''
7
8 __author__ = "Pierre Peterlongo"
9 __email__ = "[email protected]"
10
11
12 import sys
13 import K3000_common as kc
14
15
16 def show_right_edges (facts,x,id_x):
17 ''' Main function. For a given fact x, we find y that overlap x, and we print the links in a GFA style:
18 L 11 + 12 - overlap size
19 Note that one treat x only if its canonical.
20 Four cases :
21 1/ x overlaps y, with y canonical. One prints x + y + blabla
22 2/ x overlaps y_, with y_ non canonical. In this case, y_ overlaps x. One of the two solutions has to be chosen. We chose min(idx,idy) (with idx,idy being the ids of the facts x,y in SR) One searches the id of y, and one prints x + y - blabla.
23 3/ x_ overlaps y. same as 2.
24 4/ x_ overlaps y_. We do nothing, this case is treated when the entry of the function is y that thus overlaps x.
25 WARNING: here x and each fact in facts contain as last value its unique id.
26 '''
27 x=x[:-1] # remove the x_id from the x fact
28 if not kc.is_canonical(x): return
29 n=len(x)
30
31 # CASES 1 AND 2
32 strandx='+'
33 # print ("x is", x)
34 for len_u in range(1,n): # for each possible x suffix
35 u=x[-len_u:]
36 # print ("u is", u)
37 Y=facts.get_lists_starting_with_given_prefix(u)
38 # if x in Y: Y.remove(x) # we remove x itself from the list of y : note that it should not occur.
39 # print (x)
40 # assert(x not in Y)
41 if len(Y)==0: continue # No y starting with u
42 for y in Y:
43 # detect the y strand
44 # CASE 1/
45 if kc.is_canonical(y[:-1]): # remove the last value that corresponds to the node id
46 strandy ='+'
47 # id_y=indexed_nodes.index(y) # get the id of the target node
48 id_y=kc.get_fact_id(y) # last value is the node id, here the id of the target node
49 # CASE 2/
50 else:
51 strandy='-'
52 # id_y = indexed_nodes.index(kc.get_reverse_fact(y))
53 id_y=kc.get_reverse_fact_id(y,facts) # find the reverse of list y in facts to grab its id.
54 # assert kc.is_canonical(full_y[:-1]), "not canonical "+str(full_y[-1])+" full = "+str(full_y)
55 # print("y is ", y)
56 # print("id_y is", id_y)
57 if id_x>id_y: continue # x_.y is the same as y_.x. Thus we chose one of them. By convention, we print x_.y if x<y.
58 # print the edges
59 print ("L\t"+str(id_x)+"\t"+strandx+"\t"+str(id_y)+"\t"+strandy+"\t"+str(len_u)+"M")
60
61
62 # CASES 3 AND 4
63 strandx='-'
64 x_=kc.get_reverse_fact(x)
65 for len_u in range(1,n): # for each possible x suffix
66 u=x_[-len_u:]
67 Y=facts.get_lists_starting_with_given_prefix(u)
68 # assert(x_ not in Y)
69 if len(Y)==0: continue # No y starting with u
70 for y in Y:
71 if kc.is_canonical(y[:-1]): # CASE 3
72 strandy ='+'
73 # id_y=indexed_nodes.index(y) # get the id of the target node
74 id_y=kc.get_fact_id(y) # last value is the node id, here the id of the target node
75 # we determine min(id_x,id_y)
76 if id_x>id_y: continue # x_.y is the same as y_.x. Thus we chose one of them. By convention, we print x_.y if x<y.
77 print ("L\t"+str(id_x)+"\t"+strandx+"\t"+str(id_y)+"\t"+strandy+"\t"+str(len_u)+"M") # note that strand x is always '-' and strandy is always '+' in this case.
78
79 # else: continue # CASE 4, nothing to do.
80
81
82
83
84 def print_GFA_edges(facts):
85 '''print each potiential edge in GFA format. Note that each edge is printed in one unique direction, the other is implicit
86 WARNING: here each fact in facts contains as last value its unique id.
87 '''
88 for fact in facts.traverse():
89 x_id = kc.get_fact_id(fact) # last value is the node id
90 if x_id%100==0: sys.stderr.write("\t%.2f"%(100*x_id/len(facts))+"%\r")
91 show_right_edges(facts,fact,x_id)
92 sys.stderr.write("\t100.00%\n")
93
94
95 def check_fact(fact, fact_int):
96 # fact ['49648_0', '67994_-20', '20000_23']
97 # fact_int 49648_0;67994_-20;20000_23; SP:0_166;126_261;178_444; BP:0_83;-20_72;23_61;
98 for i,allele_id in enumerate(fact_int.split()[0].split(";")[:-1]):
99 if fact[i] != allele_id:
100 sys.stderr.write("Not corresponding fact and fact_int:\n")
101 sys.stderr.write(str(fact)+"\n")
102 sys.stderr.write(fact_int+"\n")
103 sys.exit(0)
104
105 def index_nodeid_to_distance(facts, compacted_fact_int_file_name):
106 """ returns an index nodeid -> distances
107 """
108 compacted_fact_int_file=open(compacted_fact_int_file_name)
109 nodeid_to_distance = {}
110 for fact_line in compacted_fact_int_file.readlines():
111 # 49648_0;67994_-20;20000_23; SP:0_166;126_261;178_444; BP:0_83;-20_72;23_61;
112 s_fact_line = fact_line.strip().split()
113 node_as_list = [node for node in s_fact_line[0].split(";")[:-1]]
114 # print(s_fact_line[0], node_as_list)
115 if not kc.is_canonical(node_as_list): continue
116 node_id = facts.get_node_id(node_as_list)
117 assert node_id not in nodeid_to_distance, "node "+str(node_id)+" already in truc, with value "+ nodeid_to_distance[node_id]
118 nodeid_to_distance[node_id]=s_fact_line[1]+"\t"+s_fact_line[2]
119 compacted_fact_int_file.close()
120 return nodeid_to_distance
121
122
123 def contains_extreme_variant(fact, has_input_edge, has_output_edge):
124 '''
125 Returns True if a fact contains at least one extreme variant
126 Else returns false
127 '''
128 for variant_id in kc.allele_values(fact):
129 if variant_id not in has_input_edge or variant_id not in has_output_edge:
130 return True
131 return False
132
133 def print_GFA_nodes_as_ids(facts, compacted_fact_int_file_name, has_input_edge, has_output_edge):
134 '''print canonical unitigs ids
135 WARNING: here each fact in facts contains as last value its unique id.
136 '''
137 nodeid_to_distance = index_nodeid_to_distance(facts, compacted_fact_int_file_name)
138 # compacted_fact_int_file=open(compacted_fact_int_file_name)
139 for fact in facts.traverse():
140 # fact_int=compacted_fact_int_file.readline().strip() # 49648_0;67994_-20;20000_23; SP:0_166;126_261;178_444; BP:0_83;-20_72;23_61;
141 node_id = kc.get_fact_id(fact) # last value is the node id
142 fact = fact[:-1] # remove the last value that corresponds to the node id
143 if not kc.is_canonical(fact): continue
144 print ("S\t"+str(node_id)+"\t", end="")
145 for unitig_id in fact:
146 print (kc.unitig_id2snp_id(kc.allele_value(unitig_id))+";", end="")
147 # check_fact(fact, fact_int)
148 # assert str(node_id) in nodeid_to_distance, nodeid_to_distance
149 print ("\t"+nodeid_to_distance[str(node_id)], end="")
150 if contains_extreme_variant(fact, has_input_edge, has_output_edge):
151 print("\tEV:1")
152 else:
153 print("\tEV:0")
154
155 def union(a, b):
156 """ return the union of two lists """
157 return list(set(a) | set(b))
158
159
160 def check(facts):
161 """ debug purpose """
162 for fact in facts.traverse():
163 assert kc.get_reverse_fact_id(fact,facts) != None, fact
164
165
166 def main():
167 '''
168 Creation of a GFA file from a set of facts
169 For eahc fact: indicates if it contains at least an extreme variant
170 Field EV
171 * EV:1 if it contains at least an extreme variant
172 * EV:0 else
173 '''
174 # SR=[[1,3,4],[14],[4,6],[-6,-1,-2],[4,6,7,9],[9,10,11,12],[4,6],[-13, -12, -11]]
175
176 facts=kc.generate_facts(sys.argv[1])
177
178
179 kc.add_reverse_facts(facts)
180 has_input_edge, has_output_edge = kc.detect_input_output_edges(facts)
181 facts.sort()
182 facts.index_nodes() # This adds a final value to each sr, providing its node id.
183 # check(facts)
184 sys.stderr.write("Print GFA Nodes\n")
185 print_GFA_nodes_as_ids(facts, sys.argv[1], has_input_edge, has_output_edge)
186 sys.stderr.write("Print GFA Edges\n")
187 print_GFA_edges(facts)
188
189
190 if __name__ == "__main__":
191 main()