forked from bjpop/rubra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
213 lines (190 loc) · 6.72 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Various useful utilities for the pipeline.
import os.path
import sys
import errno
import subprocess
from ruffus.proxy_logger import *
import logging
import os
from shell_command import shellCommand
from cluster_job import (PBS_Script, runJobAndWait)
import re
# A simple container object
class Bag:
pass
# XXX I don't think the defaults really belong here.
defaultOptionsModule = ['pipeline_config']
defaultWalltime = None # use the default walltime of the scheduler
defaultModules = []
defaultQueue = 'batch'
defaultMemInGB = None # use the default mem of the scheduler
defaultDistributed = False
defaultLogDir = 'log'
defaultLogFile = 'pipeline.log'
defaultStyle = 'run'
defaultProcs = 4
defaultPaired = False
stageDefaults = {
'distributed': defaultDistributed,
'walltime': defaultWalltime,
'memInGB': defaultMemInGB,
'modules': defaultModules,
'queue': defaultQueue
}
pipeline = {
'logDir': defaultLogDir,
'logFile': defaultLogFile,
'style': defaultStyle,
'procs': defaultProcs,
'paired': defaultPaired,
'verbose': 0
}
defaultConfig = {
'reference': None,
'sequences': [],
'optionsModule': defaultOptionsModule,
'stageDefaults': stageDefaults,
'pipeline': pipeline
}
def mkDir(dir):
if not os.path.exists(dir):
try:
os.mkdir(dir, 0777)
except IOError, e:
sys.exit('%s\nFailed to make directory %s' % (e, dir))
def mkLink(source, target):
try:
os.symlink(source, target)
except OSError, e:
if e.errno != errno.EEXIST:
sys.exit('%s\nFailed to create symlink %s from %s' % (e, target, source))
# or just raise?
def mkForceLink(source, target):
"""Create a symlink, overwriting any existing symlink."""
if os.path.isfile(target):
os.remove(target)
os.symlink(source, target)
def initLog(options):
logDir = options.pipeline['logDir']
logFile = os.path.join(logDir, options.pipeline['logFile'])
mkDir(logDir)
loggerArgs={}
loggerArgs["file_name"] = logFile
loggerArgs["level"] = logging.DEBUG
loggerArgs["rotating"] = False
loggerArgs["maxBytes"]=20000
loggerArgs["backupCount"]=10
loggerArgs["formatter"]="%(asctime)s - %(message)s"
(proxy, mutex) = make_shared_logger_and_proxy (setup_std_shared_logger, "NGS_pipeline", loggerArgs)
return { 'proxy': proxy, 'mutex': mutex }
# Look for a particular option in a stage, otherwise return the result
def getStageOptions(options, stage, optionName):
try:
return options.stages[stage][optionName]
except KeyError:
return options.stageDefaults[optionName]
# returns the exit status of the job if possible otherwise None
def distributedCommand(stage, comm, options):
time = getStageOptions(options, stage, 'walltime')
mods = getStageOptions(options, stage, 'modules')
queue = getStageOptions(options, stage, 'queue')
mem = getStageOptions(options, stage, 'memInGB')
logDir = options.pipeline['logDir']
verbosity = options.pipeline['verbose']
script = PBS_Script(command=comm, walltime=time, name=stage, memInGB=mem, queue=queue, moduleList=mods, logDir=logDir)
return runJobAndWait(script, stage, logDir, verbosity)
# check the exit status of the command and if == 0 then write a checkpoint file to indicate success.
def runStageCheck(stage, flag_file, logger, options, *args):
status = runStage(stage, logger, options, *args)
if status == 0:
open(flag_file, 'w').close()
else:
command = getCommand(stage, options)
commandStr = command(*args)
print('Error: command failed: %s' % commandStr)
# returns exit status of the executed command or None
def runStage(stage, logger, options, *args):
command = getCommand(stage, options)
commandStr = command(*args)
logStr = stage + ': ' + commandStr
logInfo(logStr, logger)
if getStageOptions(options, stage, 'distributed'):
exitStatus = distributedCommand(stage, commandStr, options)
return exitStatus
else:
(stdoutStr, stderrStr, exitStatus) = shellCommand(commandStr)
if exitStatus != 0:
msg = ("Failed to run '%s'\n%s%sNon-zero exit status %s" %
(commandStr, stdoutStr, stderrStr, exitStatus))
logInfo(msg, logger)
return exitStatus
# This converts a short-hand command string, such as:
# 'bwa aln -t 8 %ref %seq > %out'
# into:
# 'lambda x1, x2, x3: "bwa aln -t 8 %s %s > %s" % (x1, x2, x3)'
def commandToLambda(command):
(expanded,numPats) = re.subn('%[^ ]*', '%s', command)
args = []
for n in range(numPats):
args.append("x" + str(n))
argsTuple = str(','.join(args))
lamStr = 'lambda %s : "%s" %% (%s)' % (argsTuple, expanded, argsTuple)
return lamStr
def getCommand(name, options):
try:
commandStr = getStageOptions(options, name, 'command')
return eval(commandToLambda(commandStr))
except KeyError:
exit("command: %s, is not defined in the options file" % name)
def logInfo(msg, logger):
with logger['mutex']: logger['proxy'].info(msg)
def splitPath(path):
(prefix, base) = os.path.split(path)
(name, ext) = os.path.splitext(base)
return (prefix, name, ext)
def getOptionsModule(args):
if args.opts != None:
return args.opts
else:
return defaultOptionsModule
def getOptions(args):
configModules = getOptionsModule(args)
options = Bag()
for module in configModules:
try:
imported = __import__(module)
except ImportError:
exit('Could not find configuration file: %s' % (module + '.py'))
for name in dir(imported):
if name[:2] != '__':
setattr(options, name, getattr(imported, name))
if args.style != None:
options.pipeline['style'] = args.style
if args.verbose != None:
options.pipeline['verbose'] = args.verbose
if args.force != None:
options.pipeline['force'] = args.force
if args.end != None:
options.pipeline['end'] = args.end
if args.rebuild != None:
options.pipeline['rebuild'] = args.rebuild
return options
def mkLogFile(logDir, fullFilename, extension):
prefix,name,ext = splitPath(fullFilename)
return os.path.join(logDir, name + extension)
def mkTempFilename(file):
return file + '.tmp'
# truncate a file to zero bytes, and preserve its original modification time
def zeroFile(file):
if os.path.exists(file):
# save the current time of the file
timeInfo = os.stat(file)
try:
f = open(file,'w')
except IOError:
pass
else:
f.truncate(0)
f.close()
# change the time of the file back to what it was
os.utime(file,(timeInfo.st_atime, timeInfo.st_mtime))