File: iqtree.py

package info (click to toggle)
python-ete3 3.1.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 14,152 kB
  • sloc: python: 52,375; javascript: 12,959; xml: 4,903; ansic: 69; sql: 65; makefile: 26; sh: 7
file content (83 lines) | stat: -rw-r--r-- 2,908 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from __future__ import absolute_import
import os
import shutil
import sys
import re

import logging
log = logging.getLogger("main")

from ..master_task import TreeTask
from ..errors import TaskError,ConfigError
from ..master_job import Job
from ..utils import basename, PhyloTree, OrderedDict, GLOBALS,  DATATYPES
from .. import db

__all__ = ["IQTree"]

class IQTree(TreeTask):
    def __init__(self, nodeid, alg_phylip_file, constrain_id, model,
                 seqtype, conf, confname, parts_id=None):

        GLOBALS["citator"].add('iqtree')

        if constrain_id:
            raise ConfigError("IQTree does not support topology constraints")
        
        base_args = {}
        if "-st" not in conf[confname]:
            base_args["-st"] = "AA" if seqtype == "aa" else "DNA"            
        else:
            if conf[confname]["-st"].startswith("CODON"):
                if seqtype == "aa" or "aa" not in GLOBALS["seqtypes"]: 
                    raise ConfigError("IQTREE CODON models require a codon alignment.\nProvide nucleotide sequences with '-n' and set '--nt-switch-thr 0.0' to ensure codon alignments.")
            
        if model:
            raise TaskError('External model selection not yet supported for IQTree')
        else:
            if "-m" in conf[confname]:
                model = conf[confname]["-m"]
            elif conf[confname].get("-st", "").startswith("CODON"):
                model = "defaultCODON"
            elif seqtype == "aa":
                model = "WAG"
            elif seqtype == "nt":
                model = "HKY"
                
        self.model = model
        self.confname = confname
        self.conf = conf
        self.constrain_tree = None
        self.alg_phylip_file = alg_phylip_file
        self.seqtype = seqtype
        self.lk = None
        
        TreeTask.__init__(self, nodeid, "tree", "IQTree",
                          base_args, conf[confname])  
                  
        self.init()

    def load_jobs(self):
        appname = self.conf[self.confname]["_app"]
        args = OrderedDict(self.args)
        args['-s'] = self.alg_phylip_file            
        job = Job(self.conf["app"][appname], args, parent_ids=[self.nodeid])
        job.add_input_file(self.alg_phylip_file, job.jobdir)
        
        job.jobname += "-"+self.model.replace(' ', '')
        self.jobs.append(job)

    def finish(self):
        j = self.jobs[0]
        tree_file = os.path.join(j.jobdir,
                                 self.alg_phylip_file+".treefile")
        stats_file = os.path.join(j.jobdir,
                                  self.alg_phylip_file+".iqtree")

        m = re.search('Log-likelihood of the tree:\s+(-?\d+\.\d+)',
                      open(stats_file).read())
        lk = float(m.groups()[0])
        stats = {"lk": lk}
        tree = PhyloTree(tree_file)
        self.store_data(tree.write(), stats)