| 1 |
|
| 2 |
|
| 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] |
| 28 |
if not kc.is_canonical(x): return |
| 29 |
n=len(x) |
| 30 |
|
| 31 |
|
| 32 |
strandx='+' |
| 33 |
|
| 34 |
for len_u in range(1,n): |
| 35 |
u=x[-len_u:] |
| 36 |
|
| 37 |
Y=facts.get_lists_starting_with_given_prefix(u) |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
if len(Y)==0: continue |
| 42 |
for y in Y: |
| 43 |
|
| 44 |
|
| 45 |
if kc.is_canonical(y[:-1]): |
| 46 |
strandy ='+' |
| 47 |
|
| 48 |
id_y=kc.get_fact_id(y) |
| 49 |
|
| 50 |
else: |
| 51 |
strandy='-' |
| 52 |
|
| 53 |
id_y=kc.get_reverse_fact_id(y,facts) |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
if id_x>id_y: continue |
| 58 |
|
| 59 |
print ("L\t"+str(id_x)+"\t"+strandx+"\t"+str(id_y)+"\t"+strandy+"\t"+str(len_u)+"M") |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
strandx='-' |
| 64 |
x_=kc.get_reverse_fact(x) |
| 65 |
for len_u in range(1,n): |
| 66 |
u=x_[-len_u:] |
| 67 |
Y=facts.get_lists_starting_with_given_prefix(u) |
| 68 |
|
| 69 |
if len(Y)==0: continue |
| 70 |
for y in Y: |
| 71 |
if kc.is_canonical(y[:-1]): |
| 72 |
strandy ='+' |
| 73 |
|
| 74 |
id_y=kc.get_fact_id(y) |
| 75 |
|
| 76 |
if id_x>id_y: continue |
| 77 |
print ("L\t"+str(id_x)+"\t"+strandx+"\t"+str(id_y)+"\t"+strandy+"\t"+str(len_u)+"M") |
| 78 |
|
| 79 |
|
| 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) |
| 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 |
|
| 97 |
|
| 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 |
|
| 112 |
s_fact_line = fact_line.strip().split() |
| 113 |
node_as_list = [node for node in s_fact_line[0].split(";")[:-1]] |
| 114 |
|
| 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 |
|
| 139 |
for fact in facts.traverse(): |
| 140 |
|
| 141 |
node_id = kc.get_fact_id(fact) |
| 142 |
fact = fact[:-1] |
| 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 |
|
| 148 |
|
| 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 |
|
| 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() |
| 183 |
|
| 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() |