1717import math
1818import os
1919import re
20+ from callPhyML import phyml
2021
2122# Define max number of threads and devnull for capturing stdout
2223MAXCPU = cpu_count ()
@@ -49,39 +50,34 @@ def controlFiles(indir, outdir, forward, cpu):
4950 os .mkdir (tmp )
5051 except FileExistsError :
5152 pass
52- cmd = ("python bin/04_callPhyML.py -i" + indir + " -o " + tmp +
53- " -t " + str (cpu ))
54- if forward :
55- cmd += " -f " + forward
56- phyml = Popen (split (cmd ))
57- phyml .wait ()
58- #if phyml.returncode() == 0:
59- return True
53+ multiple = phyml (indir , tmp , cpu , forward )
54+ return True , multiple
6055
6156#-----------------------------------------------------------------------------
6257
63- def runCodeml (ap , outdir , finished , completed , gene ):
58+ def runCodeml (ap , outdir , finished , completed , multiple , gene ):
6459 '''Creates temporary control and tree files and runs CodeML.'''
6560 filename = gene .split ("/" )[- 1 ]
6661 geneid = filename .split ("." )[0 ]
6762 wd = outdir + "tmp/" + geneid + "/"
68- if filename .split ("." )[1 ] == "2" :
69- if len (glob (wd + "*" )) > 1 :
70- # Skip pairwise genes if tree files are present
71- pass
72- elif (geneid + "\n " ) in completed :
63+ if (geneid + "\n " ) in completed :
7364 pass
7465 else :
75- tempctl = wd + geneid + ".ctl"
76- # Calls CodeML
66+ tempctl = wd + "codeml.ctl"
7767 os .chdir (wd )
78- cm = Popen (split (ap + "/paml/bin/codeml " + tempctl ),
79- stdout = DEVNULL )
80- cm .wait ()
81- if cm .returncode == 0 :
82- # Append gene ID to list of finishedCodeML.txt
83- with open (finished , "a" ) as fin :
84- fin .write (geneid + "\n " )
68+ if multiple == True :
69+ if filename .split ("." )[1 ] == "2" :
70+ pass
71+ else :
72+ # Calls CodeML if 3 or more sequences are present
73+ cm = Popen (split (ap + "paml/bin/codeml " + tempctl ),
74+ stdout = DEVNULL )
75+ elif multiple == False :
76+ # Call CodeML for all files
77+ cm = Popen (split (ap + "paml/bin/codeml " + tempctl ),
78+ stdout = DEVNULL )
79+ with open (finished , "a" ) as fin :
80+ fin .write (geneid + "\n " )
8581
8682#-----------------------------------------------------------------------------
8783
@@ -115,7 +111,7 @@ def main():
115111 cleanup = args .cleanUp
116112 # Reads in required data
117113 finished , completed = outputFiles (outdir )
118- run = controlFiles (indir , outdir , forward , cpu )
114+ run , multiple = controlFiles (indir , outdir , forward , cpu )
119115 if run == True :
120116 # Call CodeML after PhyML completes.
121117 genes = glob (indir + "*.phylip" )
@@ -126,15 +122,15 @@ def main():
126122 elif l > cpu :
127123 chunk = int (math .ceil (l / cpu ))
128124 pool = Pool (processes = cpu )
129- func = partial (runCodeml , ap , outdir , finished , completed )
130- print ("\t Running CodeML with" , str ( cpu ), " threads...." )
131- rcml = pool .imap (func , genes , chunksize = chunk )
125+ func = partial (runCodeml , ap , outdir , finished , completed , multiple )
126+ print (( "\t Running CodeML with {0!s} threads...." ). format ( cpu ) )
127+ rcml = pool .imap_unordered (func , genes , chunksize = chunk )
132128 pool .close ()
133- pool .join ()
129+ pool .join ()
134130 # Remove tmp directory
135131 if cleanup == True :
136132 shutil .rmtree (outdir + "tmp/" )
137- print ("\t CodeML runtime: " , datetime .now () - starttime , " \n " )
133+ print (( "\t CodeML runtime: {0!s}" ). format ( datetime .now () - starttime ) )
138134
139135if __name__ == "__main__" :
140136 main ()
0 commit comments