Debian Codegraph @

scripts/k3000/K3000_gfa_post_treatment.py

raw symbols SCIP
1 import warnings # deprecated
2
3 import networkx as nx
4
5 import sys
6 # import getopt
7 import K3000_common as kc
8 # import sorted_list
9 import argparse
10
11 import negative_binomial as nb
12
13 def store_graph(gfa_file_name):
14 DG = nx.DiGraph()
15 gfa_file = open(gfa_file_name)
16
17 while(True):
18 gfa_line=gfa_file.readline()
19 if not gfa_line: break
20 if gfa_line[0]=='H': continue #Header
21 if gfa_line[0]=='S': #node
22 split_gfa_line=gfa_line.strip().split()
23 # print("node", split_gfa_line)
24 node_id = split_gfa_line[1]
25 node_coverage = split_gfa_line[-1].split(':')[-1] # RC:i:24 -> 24
26 DG.add_node(node_id, coverage=node_coverage)
27 # print("added", node_id, node_coverage)
28
29 if gfa_line[0]=='L': #edge
30 split_gfa_line=gfa_line.strip().split()
31 # print("edge", split_gfa_line)
32 source = split_gfa_line[1]
33 target = split_gfa_line[3]
34 overlap = split_gfa_line[5]
35 if overlap == "0M" or overlap == "-2M": continue #pairend and forbiden link are not considered
36 # if overlap == "-2M": continue #forbiden link
37 DG.add_edge(source, target,type=overlap)
38 gfa_file.close()
39 return DG
40
41
42 def remove_outsider_nodes_from_cc(DG,cc):
43 """ for each CC compute the r,p values of the coverage distributions of the nodes
44 Remove nodes that are too far from the mean distribution
45 This creates new CC
46 """
47 return # does nothing for now, waiting for amin.
48 if len(cc)<8: return # TODO: parameter
49 coverages = []
50 for node_id in cc:
51 print(DG.nodes[node_id]['coverage'])
52 coverages.append(int(DG.nodes[node_id]['coverage']))
53 print("for",coverages)
54 n,p=nb.neg_bin_fit(coverages)
55 E=n/p
56 V=n*(1-p)/(p*p)
57 print("n",n,"p",p,"E",E,"V",V)
58 #TODO remove outsider nodes from the graph
59
60 def assign_cc(DG,max_cc_size=sys.maxsize):
61 #CC=list(nx.connected_components(nx.Graph(DG)))
62 #for cc in CC:
63 # remove_outsider_nodes_from_cc(DG,cc)
64
65 # recompute CC after having removed outsiders from original CCs
66 # assign each node to its cc_id
67 CC=list(nx.connected_components(nx.Graph(DG)))
68 cc_id=0
69 for cc in CC:
70 cc_id +=1
71 if len(cc) > max_cc_size:
72 # print ("remove nodes from too large", len(cc) )
73 for node_id in cc:
74 DG.remove_node(node_id)
75 continue
76 for node_id in cc:
77 DG.nodes[node_id]['cc_id'] = cc_id
78
79
80 def remove_cc_with_cycles(DG):
81 # remove pairend links and unitig links (unoriented)
82 edges_to_remove = []
83 for edge in DG.edges.data():
84 if edge[2]['type'] == '-1M':
85 edges_to_remove.append(edge)
86
87 for edge in edges_to_remove:
88 DG.remove_edge(edge[0],edge[1])
89 cycles = list(nx.simple_cycles(DG))
90 # sys.stderr.write(f" removed {len(cycles)} cycles\n") #DEB
91
92 # tmpnb=0 #DEB
93 G=nx.Graph(DG)
94 for nodes in cycles:
95 first_node_in_cycle = nodes[0] # get the first node, the other ones in the cycle are in the same CC
96 # remove the whole CC:
97 CC_with_cycle = nx.node_connected_component(G, first_node_in_cycle)
98 for node_id in CC_with_cycle:
99 if node_id in DG.nodes():
100 # tmpnb+=1 #DEB
101 DG.remove_node(node_id)
102 # sys.stderr.write(f" removed {tmpnb} nodes\n") #DEB
103
104 def main():
105 '''
106 Compaction of set of facts coded as set of ids of unitigs
107 '''
108 warnings.warn(f"{sys.argv[0]} is not maintenained anymore (May 2020).", DeprecationWarning)
109 parser = argparse.ArgumentParser(description='Compaction of set of facts coded as set of ids of unitigs.')
110 parser.add_argument("input_file", type=str,
111 help="gfa file " )
112
113
114 max_cc_size = 1000
115 args = parser.parse_args()
116 gfa_file_name = str(args.input_file)
117 DG = store_graph(gfa_file_name)
118 assign_cc(DG,max_cc_size)
119 # print(DG.nodes.data())
120 remove_cc_with_cycles(DG)
121
122 if '1' in DG.nodes():
123 print("is in DG", DG.nodes['1']['cc_id'])
124
125 if __name__ == "__main__":
126 main()