| 1 |
import warnings |
| 2 |
|
| 3 |
import networkx as nx |
| 4 |
|
| 5 |
import sys |
| 6 |
|
| 7 |
import K3000_common as kc |
| 8 |
|
| 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 |
| 21 |
if gfa_line[0]=='S': |
| 22 |
split_gfa_line=gfa_line.strip().split() |
| 23 |
|
| 24 |
node_id = split_gfa_line[1] |
| 25 |
node_coverage = split_gfa_line[-1].split(':')[-1] |
| 26 |
DG.add_node(node_id, coverage=node_coverage) |
| 27 |
|
| 28 |
|
| 29 |
if gfa_line[0]=='L': |
| 30 |
split_gfa_line=gfa_line.strip().split() |
| 31 |
|
| 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 |
| 36 |
|
| 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 |
| 48 |
if len(cc)<8: return |
| 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 |
|
| 59 |
|
| 60 |
def assign_cc(DG,max_cc_size=sys.maxsize): |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 91 |
|
| 92 |
|
| 93 |
G=nx.Graph(DG) |
| 94 |
for nodes in cycles: |
| 95 |
first_node_in_cycle = nodes[0] |
| 96 |
|
| 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 |
|
| 101 |
DG.remove_node(node_id) |
| 102 |
|
| 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 |
|
| 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() |