2222import subprocess
2323import itertools
2424import argparse
25+ import logging
2526
2627from multiprocessing import Pool
2728from random import randrange
3031try :
3132 import plotter .structplot as sp
3233 import sanity_checks .sanity as sanity
34+ import colorer .colorer as colorer
3335except ImportError :
3436 import structure_threader .plotter .structplot as sp
3537 import structure_threader .sanity_checks .sanity as sanity
38+ import structure_threader .colorer .colorer as colorer
3639
3740# Where are we?
3841CWD = os .getcwd ()
3942
43+ # Set default log level and format
44+ logging .basicConfig (format = '%(levelname)s: %(message)s' , level = logging .INFO )
45+
4046
4147def gracious_exit (* args ):
42- """Graciously exit the program."""
43- print ("\r Exiting graciously, murdering child processes and cleaning output"
44- " directory." , end = "" )
48+ """
49+ Graciously exit the program.
50+ """
51+ logging .critical ("\r Exiting graciously, murdering child processes and "
52+ "cleaning output directory." )
4553 os .chdir (CWD )
4654 sys .exit (0 )
4755
4856
4957def runprogram (wrapped_prog , iterations , arg ):
50- """Run each wrapped program job. Return the worker status.
58+ """
59+ Run each wrapped program job. Return the worker status.
5160 This attribute will be populated with the worker exit code and output file
5261 and returned. The first element is the exit code itself (0 if normal exit
5362 and -1 in case of errors). The second element contains the output file
@@ -108,7 +117,7 @@ def runprogram(wrapped_prog, iterations, arg):
108117 if arg .external_prog .endswith (".py" ) is False :
109118 cli = cli [1 :]
110119
111- print ("Running: " + " " .join (cli ))
120+ logging . info ("Running: " + " " .join (cli ))
112121 program = subprocess .Popen (cli ,
113122 stdout = subprocess .PIPE ,
114123 stderr = subprocess .PIPE )
@@ -130,8 +139,8 @@ def runprogram(wrapped_prog, iterations, arg):
130139
131140 logfile = open (os .path .join (arg .outpath , "K" + str (K ) + "_rep" +
132141 str (rep_num ) + ".stlog" ), "w" )
133- print ("Writing logfile for K" + str (K ) + ", replicate " +
134- str (rep_num ) + ". Please wait..." )
142+ logging . info ("Writing logfile for K" + str (K ) + ", replicate " +
143+ str (rep_num ) + ". Please wait..." )
135144 logfile .write (out )
136145 logfile .write (err )
137146 logfile .close ()
@@ -140,7 +149,9 @@ def runprogram(wrapped_prog, iterations, arg):
140149
141150
142151def structure_threader (Ks , replicates , threads , wrapped_prog , arg ):
143- """Do the threading book-keeping to spawn jobs at the asked rate."""
152+ """
153+ Do the threading book-keeping to spawn jobs at the asked rate.
154+ """
144155
145156 if wrapped_prog != "structure" :
146157 replicates = [1 ]
@@ -164,21 +175,24 @@ def structure_threader(Ks, replicates, threads, wrapped_prog, arg):
164175 # populated with the cli commands that generated the errors
165176 error_list = [x [1 ] for x in pool if x [0 ] == - 1 ]
166177
167- print ("\n ==============================\n " )
178+ logging . info ("\n ==============================\n " )
168179 if error_list :
169- print ("%s %s runs exited with errors. Check the log files of "
170- "the following output files:" % (len (error_list ), wrapped_prog ))
180+ logging .critical ("%s %s runs exited with errors. Check the log files of"
181+ " the following output files:" ,
182+ len (error_list ), wrapped_prog )
171183 for out in error_list :
172- print (out )
184+ logging . error (out )
173185 else :
174- print ("All %s jobs finished successfully." % len (pool ))
186+ logging . info ("All %s jobs finished successfully." , len (pool ))
175187
176188 os .chdir (CWD )
177189
178190
179191def structure_harvester (resultsdir , wrapped_prog ):
180- """Run structureHarvester or fastChooseK to perform the Evanno test or the
181- likelihood testing on the results."""
192+ """
193+ Run structureHarvester or fastChooseK to perform the Evanno test or the
194+ likelihood testing on the results.
195+ """
182196 outdir = os .path .join (resultsdir , "bestK" )
183197 if not os .path .exists (outdir ):
184198 os .mkdir (outdir )
@@ -201,8 +215,10 @@ def structure_harvester(resultsdir, wrapped_prog):
201215
202216
203217def create_plts (resultsdir , wrapped_prog , Ks , bestk , arg ):
204- """Create plots from result dir.
205- :param resultsdir: path to results directory"""
218+ """
219+ Create plots from result dir.
220+ :param resultsdir: path to results directory
221+ """
206222
207223 plt_list = [x for x in Ks if x != 1 ] # Don't plot K=1
208224
@@ -468,9 +484,12 @@ def argument_parser(args):
468484
469485 return arguments
470486
487+
471488def main ():
472- """Main function, where variables are set and other functions get called
473- from."""
489+ """
490+ Main function, where variables are set and other functions get called
491+ from.
492+ """
474493
475494 arg = argument_parser (sys .argv [1 :])
476495
@@ -567,5 +586,6 @@ def main():
567586 sp .main (infiles , arg .format , arg .outpath , bestk , popfile = arg .popfile ,
568587 indfile = arg .indfile , filter_k = bestk )
569588
589+
570590if __name__ == "__main__" :
571591 main ()
0 commit comments