Coverage for PanACoTA/tree_module/fasttree_func.py: 100%
27 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-20 14:37 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-20 14:37 +0000
1#!/usr/bin/env python3
2# coding: utf-8
4# ###############################################################################
5# This file is part of PanACOTA. #
6# #
7# Authors: Amandine Perrin #
8# Copyright © 2018-2020 Institut Pasteur (Paris). #
9# See the COPYRIGHT file for details. #
10# #
11# PanACOTA is a software providing tools for large scale bacterial comparative #
12# genomics. From a set of complete and/or draft genomes, you can: #
13# - Do a quality control of your strains, to eliminate poor quality #
14# genomes, which would not give any information for the comparative study #
15# - Uniformly annotate all genomes #
16# - Do a Pan-genome #
17# - Do a Core or Persistent genome #
18# - Align all Core/Persistent families #
19# - Infer a phylogenetic tree from the Core/Persistent families #
20# #
21# PanACOTA is free software: you can redistribute it and/or modify it under the #
22# terms of the Affero GNU General Public License as published by the Free #
23# Software Foundation, either version 3 of the License, or (at your option) #
24# any later version. #
25# #
26# PanACOTA is distributed in the hope that it will be useful, but WITHOUT ANY #
27# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
28# FOR A PARTICULAR PURPOSE. See the Affero GNU General Public License #
29# for more details. #
30# #
31# You should have received a copy of the Affero GNU General Public License #
32# along with PanACOTA (COPYING file). #
33# If not, see <https://www.gnu.org/licenses/>. #
34# ###############################################################################
36"""
37Function to infer a phylogenetic tree from a persistent genome, using FastTree
39@author gem
40June 2017
41"""
43import os
44import logging
46from PanACoTA import utils
48logger = logging.getLogger("tree.fasttree")
50def run_tree(alignfile, boot, outdir, quiet, threads, **kwargs):
51 """
52 Run FastTree for the given alignment file and options
54 Parameters
55 ----------
56 alignfile: str
57 path to file containing all persistent families aligned, and grouped by genome
58 boot: int or None
59 number of bootstraps to calculate, None if no bootstrap asked
60 outdir: str or None
61 Path to the tree file that must be created
62 quiet: bool
63 True if nothing must be printed to stderr/stdout, False otherwise
64 threads: int
65 Maximum number of threads to use
66 model: str
67 DNA substitution model chosen by user
68 kwargs: Object
69 Used to be compatible with the 'run_tree' function of other softs like fastME and
70 quicktree, which require more arguments
71 """
72 model = kwargs["model"]
73 define_nb_threads(threads)
74 run_fasttree(alignfile, boot, outdir, model, quiet)
77def define_nb_threads(threads):
78 """
79 With FastTree, number of threads to use must be defined before running the
80 script, by changing an environment variable.
82 Parameters
83 ----------
84 threads: int
85 Maximal number of threads to use
86 """
87 os.environ["OMP_NUM_THREADS"] = str(threads)
90def run_fasttree(alignfile, boot, outdir, model, quiet):
91 """
92 Run FastTree on given alignment
94 Parameters
95 ----------
96 alignfile: str
97 Path to file containing all families aligned, grouped by genome
98 boot: int or None
99 Number of bootstraps to calculate (None if no bootstrap asked)
100 treefile: str or None
101 Path to the tree file that must be created
102 model: str
103 DNA substitution model
104 quiet: bool
105 True if nothing must be printed to stderr/stdout, False otherwise
106 """
107 logger.info("Running FasttreeMP...")
108 if not boot:
109 bootinfo = "-nosupport"
110 else:
111 bootinfo = "-boot {}".format(boot)
112 align_name = os.path.basename(alignfile)
113 logfile = os.path.join(outdir, align_name + ".fasttree.log")
114 treefile = os.path.join(outdir, align_name + ".fasttree_tree.nwk")
115 cmd = f"FastTreeMP -nt {model} -noml -nocat {bootinfo} -log {logfile} {alignfile}"
116 logger.details("Fasttree command: " + cmd)
117 if quiet:
118 fnull = open(os.devnull, 'w')
119 else:
120 fnull = None
121 stdout = open(treefile, "w")
122 error = ("Problem while running Fasttree. See log file ({}) for "
123 "more information.").format(logfile)
124 utils.run_cmd(cmd, error, stdout=stdout, eof=True, logger=logger, stderr=fnull)