Debian Codegraph @

scripts/phasing/phased_variants_to_graph_deprecated.py

raw symbols SCIP
1 # cf https://www.evernote.com/l/ARWYfQTvvoVFL4sOHvjcJ-AUksvCNp1zz-8
2
3 import sys
4
5 def printid(anid):
6 if anid[0]=='-': print(anid[1:],end='')
7 else: print(anid,end='')
8
9 def formatid(anid):
10 return anid
11 if anid[0]=='-': return anid[1:]
12 else: return anid
13
14
15 node2nodes={}
16 node2paired_nodes={}
17 nodes={}
18
19 def id1id2 (id1,id2,coverage,pairend):
20 if pairend: struct=node2paired_nodes
21 else: struct=node2nodes
22 # if id2<id1:
23 # tmp=id1
24 # id1=id2
25 # id2=tmp
26 if id1 not in struct:
27 struct[id1]={}
28 if id2 not in struct[id1]:
29 struct[id1][id2]=0
30 struct[id1][id2]+=coverage
31 if id1 not in nodes: nodes[id1]=0
32 if id2 not in nodes: nodes[id2]=0
33
34 def get_phasing_edges(file):
35
36 # Print phasing edges
37 for line in file: #-1064h;-917l;1880l; => 2
38 if line[0]=='#':
39 continue
40 line=line.replace(" ", " ") # in pairend files, it happens that a line contains only left or right nodes. In this case, a double space occurs before the '=>' symbol or at the begining of the line. In the first case this is problematic (else it creates empty nodes), thus we consider such lines as non pairend.
41 line=line.strip().lstrip().rstrip().split(' ')
42 pairend=False
43 if len(line)==3:
44 coverage=int(line[2])
45 else:
46 coverage=int(line[3])
47 pairend=True
48
49 if coverage < 1: continue
50
51 ids=line[0].split(';')[:-1]
52 for i in range(len(ids)-1):
53 id1=formatid(ids[i])
54 id2=formatid(ids[i+1])
55 id1id2 (id1,id2,coverage,False)
56
57 if pairend:
58 id1=line[0].split(';')[-2]
59 id2=line[1].split(';')[0]
60 id1id2 (id1,id2,coverage,True)
61 ids=line[1].split(';')[:-1]
62 for i in range(len(ids)-1):
63 id1=formatid(ids[i])
64 id2=formatid(ids[i+1])
65 id1id2 (id1,id2,coverage,False)
66
67
68
69
70 def print_phasing_edges():
71 for id1 in node2nodes:
72 for id2 in node2nodes[id1]:
73 print(str(id1)+"\t"+str(id2)+"\t"+str(node2nodes[id1][id2])+"\th")
74
75 def print_pairing_edges():
76 for id1 in node2paired_nodes:
77 for id2 in node2paired_nodes[id1]:
78 print(str(id1)+"\t"+str(id2)+"\t"+str(node2paired_nodes[id1][id2])+"\tp")
79
80 def print_allele_edges():
81 for id1 in nodes:
82 if id1[-1]=='l': continue
83 int_id=id1[:-1]
84 id2=int_id+'l'
85 if id2 in nodes:
86 print(id1+"\t"+id2+"\t0\ta")
87
88 if len(sys.argv)>1:
89 file = open(sys.argv[1])
90 else:
91 file=sys.stdin
92
93 get_phasing_edges(file)
94 print("source\ttarget\tcoverage\ttype")
95 print_phasing_edges()
96 print_pairing_edges()
97 print_allele_edges()
98
99 #
100 # # Print phasing edges
101 # for line in file: #-1064h;-917l;1880l; => 2
102 # if line[0]=='#':
103 # continue
104 # line=line.strip().rstrip().split(' ')
105 # ids=line[0].split(';')[:-1]
106 # coverage=line[2]
107 # for i in range(len(ids)-1):
108 # printid(ids[i])
109 # print("\t",end='')
110 # printid(ids[i+1])
111 # print("\t",coverage, "\t p")
112 #
113
114
115
116
117