diff --git a/tests/context.py b/tests/context.py index 649f009..a4c1e2c 100644 --- a/tests/context.py +++ b/tests/context.py @@ -7,3 +7,4 @@ import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../debruijn'))) import debruijn +#import debruijn_comp \ No newline at end of file diff --git a/tests/kmer_comp.pck b/tests/kmer_comp.pck new file mode 100644 index 0000000..2fc5ac3 Binary files /dev/null and b/tests/kmer_comp.pck differ diff --git a/tests/test.fna b/tests/test.fna new file mode 100644 index 0000000..4692bec --- /dev/null +++ b/tests/test.fna @@ -0,0 +1,8 @@ +>contig_0 len=8 +TCAGCGAT +>contig_1 len=8 +TCAGCGAA +>contig_2 len=8 +ACAGCGAT +>contig_3 len=8 +ACAGCGAA diff --git a/tests/test_characteristics.py b/tests/test_characteristics.py new file mode 100644 index 0000000..2bcfd7f --- /dev/null +++ b/tests/test_characteristics.py @@ -0,0 +1,59 @@ +"""Tests for graph characteristic""" +import pytest +import os +import networkx as nx +import hashlib +from .context import debruijn +#from .context import debruijn_comp +from debruijn import get_starting_nodes +from debruijn import get_sink_nodes +from debruijn import get_contigs +from debruijn import save_contigs + + +def test_get_starting_nodes(): + graph = nx.DiGraph() + graph.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (5, 6), (5, 7)]) + nodes = get_starting_nodes(graph) + assert len(nodes) == 2 + assert 1 in nodes + assert 3 in nodes + +def test_get_sink_nodes(): + graph = nx.DiGraph() + graph.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (5, 6), (5, 7)]) + nodes = get_sink_nodes(graph) + assert len(nodes) == 2 + assert 6 in nodes + assert 7 in nodes + +def test_get_contigs(): + graph = nx.DiGraph() + graph.add_edges_from([("TC", "CA"), ("AC", "CA"), ("CA", "AG"), ("AG", "GC"), ("GC", "CG"), ("CG", "GA"), ("GA", "AT"), ("GA", "AA")]) + contig_list = get_contigs(graph, ["TC", "AC"], ["AT" , "AA"]) + results = ["TCAGCGAT", "TCAGCGAA", "ACAGCGAT", "ACAGCGAA"] + assert len(contig_list) == 4 + for contig in contig_list: + assert contig[0] in results + assert contig[1] == 8 + + +# def test_get_contigs_comp(): +# graph = nx.DiGraph() +# graph.add_edges_from([(("AG", "TC"), ("CA", "GT")), (("AC", "TG"), ("CA", "GT")), (("CA", "GT"), ("AG", "TC")), +# (("AG", "TC"), ("CG", "GC")), (("CG", "GC"), ("CG", "GC")), (("CG", "GC"), ("CT", "GA")), (("CT", "GA"), ("AT", "TC")), +# (("CT", "GA"), ("AA", "TT"))]) +# contig_list = get_contigs(graph, ["TC", "AC"], ["AT" , "AA"]) +# results = ["TCAGCGAT", "TCAGCGAA", "ACAGCGAT", "ACAGCGAA"] +# assert len(contig_list) == 4 +# for contig in contig_list: +# assert contig[0] in results +# assert contig[1] == 8 + + +def test_save_contigs(): + test_file = os.path.abspath(os.path.join(os.path.dirname(__file__), "test.fna")) + contig = [("TCAGCGAT", 8), ("TCAGCGAA",8), ("ACAGCGAT", 8), ("ACAGCGAA", 8)] + save_contigs(contig, test_file) + with open(test_file, 'rb') as contig_test: + assert hashlib.md5(contig_test.read()).hexdigest() == "ca84dfeb5d58eca107e34de09b3cc997" \ No newline at end of file diff --git a/tests/test_construction_debruijn.py b/tests/test_construction_debruijn.py index e30a8ec..290b199 100644 --- a/tests/test_construction_debruijn.py +++ b/tests/test_construction_debruijn.py @@ -4,6 +4,7 @@ import os import networkx as nx import pickle from .context import debruijn +#from .context import debruijn_comp from debruijn import read_fastq from debruijn import cut_kmer from debruijn import build_kmer_dict @@ -48,4 +49,21 @@ def test_build_graph(): assert graph.number_of_edges() == 4 assert "AG" in graph assert "GA" in graph - assert graph.edges["AG", "GA"]['weight'] == 2 \ No newline at end of file + assert graph.edges["AG", "GA"]['weight'] == 2 + +# def test_build_graph_comp(): +# file = open(os.path.abspath(os.path.join(os.path.dirname(__file__), "kmer_comp.pck")),'rb') +# kmer_dict = pickle.load(file) +# graph = build_graph(kmer_dict) +# #TCAGAGA +# #TCA TC CA +# #CAG CA AG +# #AGA AG GA +# #GAG GA AG +# #AGA AG GA +# # ((TC, AG), (CA, GT)), (CA, AG), (AG +# assert graph.number_of_nodes() == 4 +# assert graph.number_of_edges() == 3 +# assert "AG" in graph +# assert "GA" in graph +# assert graph.edges["AG", "GA"]['weight'] == 2 diff --git a/tests/test_decision.py b/tests/test_decision.py new file mode 100644 index 0000000..f469610 --- /dev/null +++ b/tests/test_decision.py @@ -0,0 +1,170 @@ +"""Tests decision""" +import pytest +import os +import networkx as nx +import statistics +from .context import debruijn +#from .context import debruijn_comp +from debruijn import std +from debruijn import path_average_weight +from debruijn import remove_paths +from debruijn import select_best_path +from debruijn import solve_bubble +from debruijn import simplify_bubbles +from debruijn import solve_entry_tips +from debruijn import solve_out_tips + +def test_std(): + assert round(std([9, 5, 15, 20]), 1) == 6.6 + + +def test_path_weight(): + graph = nx.DiGraph() + graph.add_weighted_edges_from([(1, 2, 5), (3, 2, 10), (2, 4, 10), (4, 5, 3), + (5, 6, 10), (5, 7, 10)]) + assert path_average_weight(graph, [1, 2, 4, 5] ) == 6.0 + +def test_remove_paths(): + graph_1 = nx.DiGraph() + graph_2 = nx.DiGraph() + graph_3 = nx.DiGraph() + graph_4 = nx.DiGraph() + graph_1.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (5, 6), (5, 7)]) + graph_2.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (5, 6), (5, 7)]) + graph_3.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (5, 6), (5, 7)]) + graph_4.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (5, 6), (5, 7)]) + graph_1 = remove_paths(graph_1, [(1,2)], True, False) + graph_2 = remove_paths(graph_2, [(5,7)], False, True) + graph_3 = remove_paths(graph_3, [(2,4,5)], False, False) + graph_4 = remove_paths(graph_4, [(2,4,5)], True, True) + assert (1,2) not in graph_1.edges() + assert (3,2) in graph_1.edges() + assert (5,7) not in graph_2.edges() + assert (5,6) in graph_2.edges() + assert 4 not in graph_3.nodes() + assert (2,4) not in graph_4.edges() + assert (4, 5) not in graph_4.edges() + assert 2 not in graph_4.nodes() + assert 4 not in graph_4.nodes() + assert 5 not in graph_4.nodes() + +def test_select_best_path(): + graph_1 = nx.DiGraph() + graph_1.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (5, 6), (5, 7)]) + graph_1 = select_best_path(graph_1, [[1,2], [3,2]], [1, 1], [5, 10], delete_entry_node=True) + assert (1,2) not in graph_1.edges() + assert (3,2) in graph_1.edges() + assert 1 not in graph_1.nodes() + graph_2 = nx.DiGraph() + graph_2.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (5, 6), (5, 7) , (7, 8)]) + graph_2 = select_best_path(graph_1, [[5, 6], [5, 7, 8]], [1, 2], [13, 10], delete_sink_node=True) + assert (5,7) not in graph_2.edges() + assert (7,8) not in graph_2.edges() + assert (5,6) in graph_2.edges() + assert 7 not in graph_2.nodes() + assert 8 not in graph_2.nodes() + #Select heavier + graph_3 = nx.DiGraph() + graph_3.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (2, 8), (8, 9), + (9, 5), (5, 6), (5, 7)]) + graph_3 = select_best_path(graph_3, [[2, 4, 5], [2, 8, 9, 5]], + [1, 4], [13, 10]) + assert (2,8) not in graph_3.edges() + assert (8,9) not in graph_3.edges() + assert (9,5) not in graph_3.edges() + assert (2,4) in graph_3.edges() + assert (4,5) in graph_3.edges() + assert 8 not in graph_3.nodes() + assert 9 not in graph_3.nodes() + assert 2 in graph_3.nodes() + assert 5 in graph_3.nodes() + # Select longest + graph_4 = nx.DiGraph() + graph_4.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (2, 8), (8, 9), + (9, 5), (5, 6), (5, 7)]) + graph_4 = select_best_path(graph_4, [[2, 4, 5], [2, 8, 9, 5]], + [1, 4], [10, 10]) + assert (2,4) not in graph_4.edges() + assert (4,5) not in graph_4.edges() + assert (2,8) in graph_4.edges() + assert (8,9) in graph_4.edges() + assert (9,5) in graph_4.edges() + # Select random + graph_5 = nx.DiGraph() + graph_5.add_edges_from([(1, 2), (3, 2), (2, 4), (4, 5), (2, 8), (8, 9), + (9, 5), (5, 6), (5, 7)]) + graph_5 = select_best_path(graph_5, [[2, 4, 5], [2, 8, 9, 5]], + [1, 4], [10, 10]) + +def test_solve_bubble(): + graph_1 = nx.DiGraph() + graph_1.add_weighted_edges_from([(1, 2, 10), (3, 2, 10), (2, 4, 15), + (4, 5, 15), (2, 10,10), (10, 5,10), + (2, 8, 3), (8, 9, 3), (9, 5, 3), + (5, 6, 10), (5, 7, 10)]) + graph_1 = solve_bubble(graph_1, 2, 5) + assert (2,8) not in graph_1.edges() + assert (8,9) not in graph_1.edges() + assert (9,5) not in graph_1.edges() + assert (2,10) not in graph_1.edges() + assert (10, 5) not in graph_1.edges() + assert (2,4) in graph_1.edges() + assert (4,5) in graph_1.edges() + assert 8 not in graph_1.nodes() + assert 9 not in graph_1.nodes() + assert 10 not in graph_1.nodes() + assert 2 in graph_1.nodes() + assert 5 in graph_1.nodes() + graph_2 = nx.DiGraph() + graph_2.add_weighted_edges_from([(1, 2, 10), (3, 2, 10), (2, 4, 10), + (4, 5, 10), (2, 10,10), (10, 5,10), + (2, 8, 10), (8, 9, 10), (9, 5, 10), + (5, 6, 10), (5, 7, 10)]) + graph_2 = solve_bubble(graph_2, 2, 5) + assert (2,4) not in graph_2.edges() + assert (4,5) not in graph_2.edges() + assert (2,10) not in graph_1.edges() + assert (10, 5) not in graph_1.edges() + assert (2,8) in graph_2.edges() + assert (8,9) in graph_2.edges() + assert (9,5) in graph_2.edges() + + +def test_simplify_bubbles(): + graph_1 = nx.DiGraph() + graph_1.add_weighted_edges_from([(3, 2, 10), (2, 4, 15), (4, 5, 15), + (2, 10,10), (10, 5,10), (2, 8, 3), + (8, 9, 3), (9, 5, 3), (5, 6, 10), + (5, 7, 10)]) + graph_1 = simplify_bubbles(graph_1) + assert (2,8) not in graph_1.edges() + assert (8,9) not in graph_1.edges() + assert (9,5) not in graph_1.edges() + assert (2,10) not in graph_1.edges() + assert (10, 5) not in graph_1.edges() + +def test_solve_entry_tips(): + graph_1 = nx.DiGraph() + graph_1.add_weighted_edges_from([(1, 2, 10), (3, 2, 2), (2, 4, 15), (4, 5, 15)]) + graph_1 = solve_entry_tips(graph_1, [1, 3]) + assert (3, 2) not in graph_1.edges() + assert (1, 2) in graph_1.edges() + graph_2 = nx.DiGraph() + graph_2.add_weighted_edges_from([(1, 2, 2), (6, 3, 2), (3, 2, 2), + (2, 4, 15), (4, 5, 15)]) + graph_2 = solve_entry_tips(graph_2, [1, 6]) + assert (1, 2) not in graph_2.edges() + assert (6, 3) in graph_2.edges() + assert (3, 2) in graph_2.edges() + +def test_solve_out_tips(): + graph_1 = nx.DiGraph() + graph_1.add_weighted_edges_from([(1, 2, 15), (2, 3, 15), (4, 5, 15), (4, 6, 2)]) + graph_1 = solve_out_tips(graph_1, [5, 6]) + assert (4, 6) not in graph_1.edges() + assert (4, 5) in graph_1.edges() + graph_2 = nx.DiGraph() + graph_2.add_weighted_edges_from([(1, 2, 15), (2, 3, 15), (4, 5, 2), (4, 6, 2) , (6, 7, 2)]) + graph_2 = solve_out_tips(graph_2, [5, 7]) + assert (4, 5) not in graph_2.edges() + assert (6, 7) in graph_2.edges()