99from glob import glob
1010from subprocess import Popen
1111from shlex import split
12+ from shutil import rmtree
1213import math
1314import shutil
1415import os
@@ -21,40 +22,39 @@ def makeTree(ap, gene, wd, treefile, forward):
2122 # Call PhyML to make gene tree
2223 phy = Popen (split (ap + "PhyML/PhyML -q -i " + gene ), stdout = DEVNULL )
2324 phy .wait ()
24- if phy .returncode == 0 :
25- # Move PhyML output to temp directory
26- output = glob (gene + "_phyml_*" )
27- for i in output :
28- shutil .copy (i , wd )
29- os .remove (i )
30- # Read in PhyML tree
31- with open (treefile , "r" ) as genetree :
32- try :
33- tree = genetree .readlines ()[0 ]
34- except IndexError :
35- pass
36- # Remove branch lables introduced by PhyML
37- tree = re .sub (r"\d+\.\d+:" , ":" , tree )
38- # Add forward node to tree if specified
39- if forward :
40- if forward in tree :
41- # Determine location and length of species name
42- i = tree .index (forward ) + len (forward )
43- if ":" in tree :
44- # Find end of branch length
45- comma = tree .find ("," , i )
46- paren = tree .find (")" , i )
47- i = min ([comma , paren ])
48- # Insert space and node symbol after species name
49- tree = (tree [:i ] + " #1" + tree [i :])
50- elif forward not in tree :
25+ # Move PhyML output to temp directory
26+ output = glob (gene + "_phyml_*" )
27+ for i in output :
28+ shutil .copy (i , wd )
29+ os .remove (i )
30+ # Read in PhyML tree
31+ with open (treefile , "r" ) as genetree :
32+ try :
33+ tree = genetree .readlines ()[0 ]
34+ except IndexError :
5135 pass
52- with open (treefile , "w" ) as outtree :
53- # Overwrite treefile
54- string = ""
55- for i in tree :
56- string += i
57- outtree .write (string )
36+ # Remove branch lables introduced by PhyML
37+ tree = re .sub (r"\d+\.\d+:" , ":" , tree )
38+ # Add forward node to tree if specified
39+ if forward :
40+ if forward in tree :
41+ # Determine location and length of species name
42+ i = tree .index (forward ) + len (forward )
43+ if ":" in tree :
44+ # Find end of branch length
45+ comma = tree .find ("," , i )
46+ paren = tree .find (")" , i )
47+ i = min ([comma , paren ])
48+ # Insert space and node symbol after species name
49+ tree = (tree [:i ] + " #1" + tree [i :])
50+ elif forward not in tree :
51+ pass
52+ with open (treefile , "w" ) as outtree :
53+ # Overwrite treefile
54+ string = ""
55+ for i in tree :
56+ string += i
57+ outtree .write (string )
5858
5959def makeCtl (gene , outfile , tempctl , treefile , ctl ):
6060 '''Creates unique control file'''
@@ -72,7 +72,7 @@ def makeCtl(gene, outfile, tempctl, treefile, ctl):
7272#-----------------------------------------------------------------------------
7373
7474def parallelize (ap , outdir , finished , completed , multiple , cpu , ctl ,
75- forward , gene ):
75+ forward , treefile , gene ):
7676 filename = gene .split ("/" )[- 1 ]
7777 geneid = filename .split ("." )[0 ]
7878 if (geneid + "\n " ) in completed :
@@ -90,27 +90,31 @@ def parallelize(ap, outdir, finished, completed, multiple, cpu, ctl,
9090 if multiple == True :
9191 if filename .split ("." )[1 ] == "2" :
9292 # Skip pairwise genes and remove directory
93- os . rmdir (wd )
93+ rmtree (wd )
9494 pass
9595 else :
96- treefile = wd + filename + "_phyml_tree.txt"
97- # Make control file and run PhyML
96+ if not treefile :
97+ # Run Phyml
98+ treefile = wd + filename + "_phyml_tree.txt"
99+ makeTree (ap , gene , wd , treefile , forward )
100+ # Make control file
98101 makeCtl (gene , outfile , tempctl , treefile , ctl )
99- makeTree (ap , gene , wd , treefile , forward )
100102 os .chdir (wd )
101103 # Call CodeML
102- cm = Popen (split (ap + "paml/bin/codeml " + tempctl ),
103- stdout = DEVNULL )
104- cm .wait ()
104+ with open (wd + "codemlLog.txt" , "w" ) as tmpout :
105+ cm = Popen (split (ap + "paml/bin/codeml " + tempctl ),
106+ shell = True , stdout = tmpout )
107+ cm .wait ()
105108 elif multiple == False :
106109 # Make control file
107110 treefile = wd + filename + ""
108111 makeCtl (gene , outfile , tempctl , treefile , ctl )
109112 # Call CodeML for all files
110113 os .chdir (wd )
111- cm = Popen (split (ap + "paml/bin/codeml " + tempctl ),
112- stdout = DEVNULL )
113- cm .wait ()
114+ with open (wd + "codemlLog.txt" , "w" ) as tmpout :
115+ cm = Popen (split (ap + "paml/bin/codeml " + tempctl ),
116+ shell = True , stdout = tmpout )
117+ cm .wait ()
114118 with open (finished , "a" ) as fin :
115119 # Append gene id to list when done
116120 fin .write (geneid + "\n " )
0 commit comments