Exemplo n.º 1
0
 def test_iqtree_outfile(self):
     out = os.path.join(PATH, 'tests', 'data', 'mlt',
                        'iqtree.ml.tree.newick')
     outfile = mlt(IQTREE, self.msa, outfile=out)
     self.assertTrue(os.path.isfile(outfile))
     self.assertEqual(out, outfile)
     self.assertTrue(_newick(out))
     self.rm = out
Exemplo n.º 2
0
 def test_phyml_outfile(self):
     out = os.path.join(PATH, 'tests', 'data', 'mlt',
                        'phyml.ml.tree.newick')
     outfile = mlt(PHYML, self.msa, outfile=out)
     self.assertTrue(os.path.isfile(outfile))
     self.assertEqual(out, outfile)
     self.assertTrue(_newick(out))
     self.rm = out
Exemplo n.º 3
0
def _iqtree(exe, msa, tree, model, seed):
    """
    Perform topology test (AU test) using IQ-TREE_.
    
    :param msa: str, path to a FASTA format MSA file.
    :param tree: str, path to a NEWICK format tree file.
    :param exe: str, path to a topology test program's executable.
    :param model: str, name of the model or path of the model file.
    :param seed: int, the seed used to initiate the random number generator.
    :return: tuple, p-value of the AU test (float), first tree (string), and
        second tree (string).
    
    .. _IQ-TREE: www.iqtree.org/

    """

    trees = Phylo.parse(tree, 'newick')
    number = sum(1 for t in trees)

    wd = tempfile.mkdtemp(dir=os.path.dirname(os.path.abspath(msa)))
    args = [
        exe, '-s', 'msa.fasta', '-zb', '10000', '-au', '-nt', '1', '-pre',
        'autest', '-n', '0'
    ]
    if model:
        args.extend(['-m', model])
    else:
        args.extend(['-m', 'TEST'])

    shutil.copy(msa, os.path.join(wd, 'msa.fasta'))

    t1, t2 = '', ''
    if number == 1:
        info('One tree found in tree file, inferring ML tree.')
        mltree = mlt(exe, msa, model=model)
        if mltree:
            trees = os.path.join(wd, 'trees.newick')
            if isinstance(tree, str):
                with open(tree) as f1:
                    t1 = f1.readline().rstrip()
            else:
                t1 = tree.format('newick').rstrip()
            with open(trees, 'w') as o, open(mltree) as f2:
                t2 = f2.read().strip()
                o.write('{}\n{}\n'.format(t1, t2))
                args.extend(['-z', 'trees.newick'])
        else:
            error('Infer ML tree failed, AU TEST aborted.')
            sys.exit(1)
    else:
        with open(tree) as f:
            t1, t2 = f.readline().strip(), f.readline().strip()
        shutil.copy(tree, os.path.join(wd, 'trees.newick'))
        args.extend(['-z', 'trees.newick'])

    try:
        info('Running AU TEST (IQ-TREE) using the following command:\n\t'
             '{}'.format(' '.join(args)))
        process = Popen(args,
                        cwd=wd,
                        stdout=PIPE,
                        stderr=PIPE,
                        universal_newlines=True)
        code = process.wait()
        if code:
            msg = indent(process.stderr.read(), prefix='\t')
            error('Topology test (AU TEST) failed for {}\n{}'.format(msa, msg))
            sys.exit(1)
        else:
            info('Parsing tree topology test (AU TEST) results.')
            with open(os.path.join(wd, 'autest.iqtree')) as f:
                for line in f:
                    if 'USER TREES' in line:
                        break

                for line in f:
                    if line.strip().startswith('1'):
                        aup = float(line.split()[-2])
                        return aup, t1, t2
                error('Parsing tree topology test (AU TEST) failed, no test'
                      'result found.')
                sys.exit(1)
    except OSError:
        error('Invalid IQ-TREE executable (exe) {}, topology test (AU TEST) '
              'failed for {}.'.format(exe, msa))
        sys.exit(1)
    finally:
        shutil.rmtree(wd)
Exemplo n.º 4
0
 def test_raxml_dfault(self):
     out = mlt(RAXML, self.msa)
     self.assertTrue(os.path.isfile(out))
     self.assertTrue(_newick(out))
     self.rm = out
Exemplo n.º 5
0
 def test_iqtree_dfault(self):
     out = mlt(IQTREE, self.msa)
     self.assertTrue(os.path.isfile(out))
     self.assertTrue(_newick(out))
     self.rm = out