Beispiel #1
0
    def test_create_temporary_phylip(self, temp_mock):
        temp_file = StringIO()
        temp_mock.return_value = temp_file

        builder = TreeBuilder()

        fasta_file = StringIO("""\
>cluster_A
NNAACAAAANN
>cluster_B
CCAACAAAANN
""")

        expected_output = """\
 2 11
cluster_A  NNAACAAAAN N
cluster_B  CCAACAAAAN N
"""

        builder.load_fasta_sequences(fasta_file)
        output_file = builder._create_temporary_phylip(builder.sequences)

        self.assertEqual(output_file, temp_file)

        output_file.seek(0)
        self.assertEqual(output_file.read(), expected_output)
Beispiel #2
0
    def test_build_tree(self, temp_mock):
        builder = TreeBuilder()

        # I could mock _run_raxml but, because I'm using a regex to parse the
        # results, I want to know straight away if the output of RAxML changes in a
        # way that breaks this.

        random_file = tempfile.NamedTemporaryFile('w', delete=False)
        random_filename = random_file.name
        random_folder = tempfile.mkdtemp()

        temp_mock.NamedTemporaryFile.return_value = random_file
        temp_mock.mkdtemp.return_value = random_folder

        fasta_file = open(os.path.join(test_data(), 'animals.mfa'), 'r')
        builder.load_fasta_sequences(fasta_file)
        fasta_file.close()

        tree = builder.build_tree()

        tree_nodes = sorted([node.name for node in tree.get_terminals()])
        expected_tree_nodes = [
            "Carp", "Chicken", "Cow", "Frog", "Human", "Loach", "Mouse", "Rat",
            "Seal", "Whale"
        ]

        self.assertIsInstance(tree, Bio.Phylo.Newick.Tree)
        self.assertEqual(tree_nodes, expected_tree_nodes)
        self.assertFalse(os.path.isdir(random_folder))
        self.assertFalse(os.path.isfile(random_filename))
  def test_build_tree(self, temp_mock):
    builder = TreeBuilder()

    # I could mock _run_raxml but, because I'm using a regex to parse the
    # results, I want to know straight away if the output of RAxML changes in a
    # way that breaks this.

    random_file = tempfile.NamedTemporaryFile('w', delete=False)
    random_filename = random_file.name
    random_folder = tempfile.mkdtemp()

    temp_mock.NamedTemporaryFile.return_value = random_file
    temp_mock.mkdtemp.return_value = random_folder

    fasta_file = open(os.path.join(test_data(), 'animals.mfa'), 'r')
    builder.load_fasta_sequences(fasta_file)
    fasta_file.close()

    tree = builder.build_tree()

    tree_nodes = sorted([node.name for node in tree.get_terminals()])
    expected_tree_nodes = ["Carp", "Chicken", "Cow", "Frog", "Human", "Loach",
                           "Mouse", "Rat", "Seal", "Whale"]

    self.assertIsInstance(tree, Bio.Phylo.Newick.Tree)
    self.assertEqual(tree_nodes, expected_tree_nodes)
    self.assertFalse(os.path.isdir(random_folder))
    self.assertFalse(os.path.isfile(random_filename))
  def test_create_temporary_phylip(self, temp_mock):
    temp_file = StringIO()
    temp_mock.return_value = temp_file

    builder = TreeBuilder()

    fasta_file = StringIO("""\
>cluster_A
NNAACAAAANN
>cluster_B
CCAACAAAANN
""")

    expected_output = """\
 2 11
cluster_A  NNAACAAAAN N
cluster_B  CCAACAAAAN N
"""

    builder.load_fasta_sequences(fasta_file)
    output_file = builder._create_temporary_phylip(builder.sequences)

    self.assertEqual(output_file, temp_file)

    output_file.seek(0)
    self.assertEqual(output_file.read(), expected_output)
  def test_run_raxml(self):
    builder = TreeBuilder()

    fasta_file = open(os.path.join(test_data(), 'animals.mfa'), 'r')
    builder.load_fasta_sequences(fasta_file)
    fasta_file.close()

    phylip_file = builder._create_temporary_phylip(builder.sequences)
    phylip_file.close()

    output_directory = tempfile.mkdtemp()
    raxml_stdout, raxml_stderr = builder._run_raxml('raxmlHPC', {}, phylip_file.name, output_directory)

    self.assertTrue('Best-scoring ML tree written to' in raxml_stdout)

    os.remove(phylip_file.name)
    shutil.rmtree(output_directory)
Beispiel #6
0
    def test_run_raxml(self):
        builder = TreeBuilder()

        fasta_file = open(os.path.join(test_data(), 'animals.mfa'), 'r')
        builder.load_fasta_sequences(fasta_file)
        fasta_file.close()

        phylip_file = builder._create_temporary_phylip(builder.sequences)
        phylip_file.close()

        output_directory = tempfile.mkdtemp()
        raxml_stdout, raxml_stderr = builder._run_raxml(
            'raxmlHPC', {}, phylip_file.name, output_directory)

        self.assertTrue('Best-scoring ML tree written to' in raxml_stdout)

        os.remove(phylip_file.name)
        shutil.rmtree(output_directory)
  def test_add_hereditary_nodes(self, temp_mock):
    builder = TreeBuilder()

    output_directory = tempfile.mkdtemp()
    temp_mock.mkdtemp.return_value = output_directory
    temp_mock.NamedTemporaryFile.side_effect = \
        (tempfile.NamedTemporaryFile(dir=output_directory, delete=False) for i in xrange(10))

    fasta_filename = os.path.join(test_data(), 'animals.mfa')
    with open(fasta_filename) as fasta_file:
      builder.load_fasta_sequences(fasta_file)

    tree_filename = os.path.join(test_data(), 'animals.terminal_nodes.newick')
    builder.tree = Bio.Phylo.read(tree_filename, 'newick')

    builder.add_hereditary_nodes()

    self.assertIsInstance(builder.tree, Bio.Phylo.Newick.Tree)
    tree_nodes = sorted([node.name for node in builder.tree.find_clades()])

    self.assertItemsEqual(tree_nodes, builder.sequences.keys())
    self.assertEqual(len(tree_nodes), 19)
    self.assertFalse(os.path.isdir(output_directory))
Beispiel #8
0
    def test_add_hereditary_nodes(self, temp_mock):
        builder = TreeBuilder()

        output_directory = tempfile.mkdtemp()
        temp_mock.mkdtemp.return_value = output_directory
        temp_mock.NamedTemporaryFile.side_effect = \
            (tempfile.NamedTemporaryFile(dir=output_directory, delete=False) for i in xrange(10))

        fasta_filename = os.path.join(test_data(), 'animals.mfa')
        with open(fasta_filename) as fasta_file:
            builder.load_fasta_sequences(fasta_file)

        tree_filename = os.path.join(test_data(),
                                     'animals.terminal_nodes.newick')
        builder.tree = Bio.Phylo.read(tree_filename, 'newick')

        builder.add_hereditary_nodes()

        self.assertIsInstance(builder.tree, Bio.Phylo.Newick.Tree)
        tree_nodes = sorted([node.name for node in builder.tree.find_clades()])

        self.assertItemsEqual(tree_nodes, builder.sequences.keys())
        self.assertEqual(len(tree_nodes), 19)
        self.assertFalse(os.path.isdir(output_directory))