last version still buggy
parent
c978b20705
commit
e6337ad43e
|
|
@ -45,7 +45,7 @@ def build_kmer_dict(fichier, k):
|
|||
hash_table, dict: dictionnary with key = k-mer as str
|
||||
and value count of k-mer occurence
|
||||
"""
|
||||
hash_table = {} # initialise empty hash table
|
||||
hash_table = {}# initialise empty hash table
|
||||
it_fastq = read_fastq(fichier)
|
||||
for seq in it_fastq: # for each sequence
|
||||
it_kmer = cut_kmer(seq, k) # count each occurence of k-mer
|
||||
|
|
@ -132,11 +132,11 @@ def path_average_weight(graph, path):
|
|||
"""
|
||||
weight = 0
|
||||
for i in range(len(path)-1):
|
||||
weight += graph[path[i][i+i][weight]
|
||||
|
||||
return weight/(len(path)-1)
|
||||
weight += graph.edges[path[i], path[i+1]]["weight"]
|
||||
|
||||
weight = weight/(len(path) - 1)
|
||||
|
||||
return weight
|
||||
|
||||
def remove_paths(graph, paths, delete_entry_node=False, delete_sink_node=False):
|
||||
"""
|
||||
|
|
@ -164,16 +164,30 @@ def remove_paths(graph, paths, delete_entry_node=False, delete_sink_node=False):
|
|||
return graph
|
||||
|
||||
|
||||
def select_best_path(graph, paths, path_len, mean_weights,
|
||||
def select_best_path(graph, paths, path_lens, mean_weights,
|
||||
delete_entry_node=False, delete_sink_node=False):
|
||||
"""
|
||||
Given path list, their length and weight, keeps only the best path among
|
||||
them considering the following priority : weight, length and randomly
|
||||
|
||||
Arguments:
|
||||
graph, nx.DiGraph: a de bruijn graph
|
||||
paths, list of str: list of paths
|
||||
path_lens: lengths of the paths
|
||||
mean_weights: mean weights of the paths
|
||||
delete_entry_node, boolean: either or not if the entry node
|
||||
should be deleted
|
||||
delete_sink_node, boolean: either or not if the sink node
|
||||
should be deleted
|
||||
|
||||
Returns:
|
||||
graph, nx.DiGraph: graph with deleted paths
|
||||
"""
|
||||
max_weight = max(mean_weights)
|
||||
heaviest = [i for i, j in enumerate(mean_weights) if j == mean_weights]
|
||||
heaviest = [i for i, j in enumerate(mean_weights) if j == max_weight]
|
||||
if len(heaviest) > 1:
|
||||
max_len = max(path_lengths)
|
||||
longest = [i for i in heaviest if path_len[i] == max_len]
|
||||
max_len = max(path_lens)
|
||||
longest = [i for i in heaviest if path_lens[i] == max_len]
|
||||
if len(longest) > 1:
|
||||
Random.seed(9001)
|
||||
best = random.choice[longest]
|
||||
|
|
@ -181,9 +195,14 @@ def select_best_path(graph, paths, path_len, mean_weights,
|
|||
best = longest[0]
|
||||
else:
|
||||
best = heaviest[0]
|
||||
paths.pop(best)
|
||||
|
||||
return remove_paths(graph, paths, delete_entry_node, delete_sink_node)
|
||||
for p in paths:
|
||||
print(p)
|
||||
|
||||
paths2 = [p for p in paths]
|
||||
paths2.pop(best)
|
||||
|
||||
return remove_paths(graph, paths2, delete_entry_node, delete_sink_node)
|
||||
|
||||
|
||||
def fill(text, width=80):
|
||||
|
|
@ -227,13 +246,47 @@ def get_contigs(graph, starting_nodes, sink_nodes):
|
|||
|
||||
return contigs
|
||||
|
||||
def solve_bubble():
|
||||
pass
|
||||
def solve_bubble(graph, ancestor_node, descendent_node):
|
||||
"""
|
||||
solve a bubble
|
||||
|
||||
Arguments:
|
||||
graph, nx.DiGraph: a de bruijn graph
|
||||
ancestor_node, str: a node
|
||||
descendent_node, str: a node
|
||||
|
||||
Returns:
|
||||
graph, nx.DiGraph: the same graph without the bubble
|
||||
"""
|
||||
paths = algorithms.all_simple_paths(graph, ancestor_node, descendent_node)
|
||||
|
||||
weights = []
|
||||
path_lens = []
|
||||
for path in paths:# constituting weights and length lists
|
||||
weights.append(path_average_weight(graph, path))
|
||||
path_lens.append(len(path))
|
||||
|
||||
return select_best_path(graph, paths, weights, path_lens) # keep best path
|
||||
|
||||
|
||||
def simplify_bubbles():
|
||||
pass
|
||||
def simplify_bubbles(graph):
|
||||
"""
|
||||
Returns a bubble-less graph
|
||||
|
||||
Arguments:
|
||||
graph, nx.DiGraph: a de bruijn graph
|
||||
|
||||
Returns:
|
||||
graph, nx.DiGraph: a bubble-less de bruijn graph
|
||||
"""
|
||||
fork_nodes = []# empty list containing nodes with 2 or more ancestors
|
||||
for node in graph:
|
||||
while graph.in_degree(node) >= 2: # if 2 or more ancestor add node
|
||||
pred = [n for n in graph.predecessors(node)]
|
||||
ancestor = algorithms.lowest_common_ancestor(graph,pred[0], pred[1])
|
||||
graph = solve_bubble(graph, ancestor, node)
|
||||
|
||||
return graph
|
||||
|
||||
def solve_entry_tips():
|
||||
pass
|
||||
|
|
|
|||
Loading…
Reference in New Issue