def constructTree(alignmentFile, treeFile, logFile, nucleotide=False): """ Constructs a phylogenetic tree using FastTree """ if nucleotide: fastTreeCline = FastTreeCommandline(input=alignmentFile, log=logFile, out=treeFile, nt=True, gtr=True) else: fastTreeCline = FastTreeCommandline(input=alignmentFile, log=logFile, out=treeFile) fastTreeCline()
def test_single(self): path = "Fasta/f001" records = list(SeqIO.parse(path, "fasta")) self.assertEqual(len(records), 1) cline = FastTreeCommandline(fasttree_exe, input=path) stdout, stderr = cline() self.assertIn("Unique: 1/1", stderr)
def fast_tree(file): try: FastTreeCommandline( "fasttreemp", nt=True, gtr=True, input=file, out=f"{file}.tre", fastest=True, )() except Bio.Application.ApplicationError: FastTreeCommandline( "fasttree", nt=True, gtr=True, input=file, out=f"{file}.tre", fastest=True, )()
def test_empty(self): path = "does_not_exist.fasta" cline = FastTreeCommandline(fasttree_exe, input=path) with self.assertRaises(ApplicationError) as cm: stdout, stderr = cline() message = str(cm.exception) self.assertTrue("Cannot open sequence file" in message or "Cannot open sequence file" in message or "Cannot read %s" % path in message or "Non-zero return code " in message, msg="Unknown ApplicationError raised: %s" % message)
def test_invalid(self): path = "Medline/pubmed_result1.txt" cline = FastTreeCommandline(fasttree_exe, input=path) with self.assertRaises(ApplicationError) as cm: stdout, stderr = cline() message = str(cm.exception) self.assertTrue("invalid format" in message or "not produced" in message or "No sequences in file" in message or "Error parsing header line:" in message or "Non-zero return code " in message, msg="Unknown ApplicationError raised: %s" % message)
break if not fasttree_exe: raise MissingExternalDependencyError( "Install FastTree and correctly set the file path to the program " "if you want to use it from Biopython.") ################################################################# print("Checking error conditions") print("=========================") print("Empty file") input_file = "does_not_exist.fasta" assert not os.path.isfile(input_file) cline = FastTreeCommandline(fasttree_exe, input=input_file) try: stdout, stderr = cline() assert False, "Should have failed, returned:\n%s\n%s" % (stdout, stderr) except ApplicationError as err: print("Failed (good)") #Python 2.3 on Windows gave (0, 'Error') #Python 2.5 on Windows gives [Errno 0] Error assert "Cannot open sequence file" in str(err) or \ "Cannot open input file" in str(err) or \ "non-zero exit status" in str(err), str(err) print("") print("Single sequence") input_file = "Fasta/f001" assert os.path.isfile(input_file)
from Bio import AlignIO align = AlignIO.read(child.stdout, "clustal") print(align) # convert into PHYLIP format phylip = "seqs.phy" with open(phylip, 'w') as out: AlignIO.write(align, out, 'phylip') ### # reconstruct phylogenetic tree from Bio import Phylo from Bio.Phylo.Applications import PhymlCommandline, FastTreeCommandline #cmd = PhymlCommandline(input=phylip, datatype='aa') cmd = FastTreeCommandline(input=phylip, out=phylip + ".nw") out_log, err_log = cmd() tree = Phylo.read(phylip + ".nw", 'newick') # '_phyml_tree.txt' Phylo.draw_ascii(tree) # ete import ete3 t = ete3.PhyloTree(phylip + ".nw") # root by mid-point t.set_outgroup(t.get_midpoint_outgroup()) print(t) t.show()