comparison commons/tools/srptPhyML.py @ 31:0ab839023fe4

Uploaded
author m-zytnicki
date Tue, 30 Apr 2013 14:33:21 -0400
parents 94ab73e8a190
children
comparison
equal deleted inserted replaced
30:5677346472b5 31:0ab839023fe4
1 #!/usr/bin/env python
2
3 import user, os, sys, getopt, exceptions, logging, ConfigParser
4
5 if not os.environ.has_key( "REPET_PATH" ):
6 print "*** Error: no environment variable REPET_PATH"
7 sys.exit(1)
8 sys.path.append( os.environ["REPET_PATH"] )
9
10 import pyRepet.sql.RepetJobMySQL
11 import pyRepet.launcher.Launcher
12
13 #-----------------------------------------------------------------------------
14
15 def help():
16
17 """
18 Give the list of the command-line options.
19 """
20
21 print
22 print "usage:",sys.argv[0]," [ options ]"
23 print "options:"
24 print " -h: this help"
25 print " -g: name of the group identifier (same for all the jobs)"
26 print " -q: name of the query directory"
27 print " -S: suffix in the query directory (default='*.fa')"
28 print " -Q: name of the queue (on the cluster)"
29 print " -d: absolute path to the temporary directory"
30 print " -C: configuration file from TEdenovo or TEannot pipeline"
31 print " -t: job table name (default=jobs)"
32 print " -p: absolute path to project directory (if jobs management via files)"
33 print " -c: clean (remove job launch files and job stdout)"
34 print " -v: verbose (default=0/1/2)"
35 print
36
37 #-----------------------------------------------------------------------------
38
39 def main():
40
41 """
42 This program takes a directory as input and launches MAP on each file in it.
43 """
44
45 groupid = ""
46 queryDir = ""
47 patternSuffix = "*.fa"
48 queue = ""
49 tmpDir = ""
50 configFileName = ""
51 jobTable = "jobs"
52 projectDir = ""
53 clean = False
54 verbose = 0
55
56 try:
57 opts, args = getopt.getopt(sys.argv[1:],"hg:q:S:Q:d:C:t:p:cv:")
58 except getopt.GetoptError, err:
59 print str(err)
60 help()
61 sys.exit(1)
62 for o,a in opts:
63 if o == "-h":
64 help()
65 sys.exit(0)
66 elif o == "-g":
67 groupid = a
68 elif o == "-q":
69 queryDir = a
70 elif o == "-S":
71 patternSuffix = a
72 elif o == "-Q":
73 queue = a
74 elif o == "-d":
75 tmpDir = a
76 elif o == "-C":
77 configFileName = a
78 elif o == "-t":
79 jobTable = a
80 elif o == "-p":
81 projectDir = a
82 elif o == "-c":
83 clean = True
84 elif o == "-v":
85 verbose = int(a)
86
87 if groupid == "" or queryDir == "":
88 print "*** Error: missing compulsory options"
89 help()
90 sys.exit(1)
91
92 if os.environ["REPET_JOBS"] == "files" and projectDir == "":
93 print "*** Error: missing compulsory options for jobs management via files"
94 help()
95 sys.exit(1)
96
97 if verbose > 0:
98 print "\nbeginning of %s" % (sys.argv[0].split("/")[-1])
99 sys.stdout.flush()
100
101 #--------------------------------------------------------------------------
102
103 # create the 'log' file
104
105 logFileName = "%s_pid%s.log" % ( groupid, os.getpid() )
106 handler = logging.FileHandler( logFileName )
107 formatter = logging.Formatter( "%(asctime)s %(levelname)s: %(message)s" )
108 handler.setFormatter( formatter )
109 logging.getLogger('').addHandler( handler )
110 logging.getLogger('').setLevel( logging.DEBUG )
111 logging.info( "started" )
112
113
114 # open a connection to the MySQL table
115
116 if configFileName != "":
117 if not os.path.exists( configFileName ):
118 print "*** Error: configuration file '%s' doesn't exist" % ( configFileName )
119 sys.exit(1)
120 config = ConfigParser.ConfigParser()
121 config.readfp( open(configFileName) )
122 host = config.get("repet_env","repet_host")
123 user = config.get("repet_env","repet_user")
124 passwd = config.get("repet_env","repet_pw")
125 dbname = config.get("repet_env","repet_db")
126 else:
127 host = os.environ["REPET_HOST"]
128 user = os.environ["REPET_USER"]
129 passwd = os.environ["REPET_PW"]
130 dbname = os.environ["REPET_DB"]
131
132 if os.environ["REPET_JOBS"] == "files":
133 jobdb = pyRepet.sql.RepetJobMySQL.RepetJob( dbname = projectDir + "/" + os.environ["REPET_DB"] )
134 elif os.environ["REPET_JOBS"] == "MySQL":
135 jobdb = pyRepet.sql.RepetJobMySQL.RepetJob( user, host, passwd, dbname )
136 else:
137 print "*** Error: REPET_JOBS is '%s'" % ( os.environ["REPET_JOBS"] )
138 sys.exit(1)
139
140
141 currentDir = os.getcwd()
142 if tmpDir == "":
143 tmpDir = currentDir
144
145 # launch PhyML on each fasta file in queryDir
146 cL = pyRepet.launcher.Launcher.PhyMlLauncher( jobdb=jobdb, query=queryDir, cdir=currentDir, tmpdir=tmpDir, job_table=jobTable, queue=queue, groupid=groupid, acro="PhyML" )
147 cL.run( patternSuffix )
148
149 # clean
150 if clean == True:
151 cL.clean()
152
153
154 logging.info( "finished" )
155
156 if verbose > 0:
157 print "%s finished successfully\n" % (sys.argv[0].split("/")[-1])
158 sys.stdout.flush()
159
160 return 0
161
162 #----------------------------------------------------------------------------
163
164 if __name__ == '__main__':
165 main()