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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
#!/usr/bin/env python
#
# pyFlow - a lightweight parallel task engine
#
# Copyright (c) 2012-2017 Illumina, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
# WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
#
"""
This demonstrates a run of a prototype BCL to BWA BAM workflow
created as a production-scale proof of concept for pyflow.
The bwa workflow is written into the BWAWorkflow object. See
bwaworkflow.py for implementation details of this class.
Finally, make sure configuration settings in BWAWorkflowConfig
are appropriate before running.
"""
import os, sys
scriptDir = os.path.abspath(os.path.dirname(__file__))
scriptName = os.path.basename(__file__)
runScript1 = """#!/usr/bin/env python
# BWAWorkflow run script auto-generated by command: %s
import os.path, sys
scriptDir=os.path.abspath(os.path.dirname(__file__))
sys.path.append('%s')
from bwaworkflow import BWAWorkflow
class WorkflowOptions(object) :
""" % (" ".join(sys.argv), scriptDir)
runScript2 = """
def get_run_options() :
from optparse import OptionParser
import textwrap
epilog=\"""Note this script can be re-run to continue the workflow run in case of interruption.
Also note that dryRun option has limited utility when task definition depends on upstream task results,
in which case the dry run will not cover the full 'live' run task set)\"""
# no epilog in py 2.4! hack-in the feature instead:
class MyOptionParser(OptionParser) :
def __init__(self, *args, **kwargs):
self.myepilog = None
try:
self.myepilog = kwargs.pop('epilog')
except KeyError:
pass
OptionParser.__init__(self,*args, **kwargs)
def print_help(self,*args,**kwargs) :
OptionParser.print_help(self,*args, **kwargs)
if self.myepilog != None :
sys.stdout.write("%s\\n" % (textwrap.fill(self.myepilog)))
parser = MyOptionParser(epilog=epilog)
parser.add_option("-m", "--mode", type="string",dest="mode",
help="select run mode (local|sge)")
parser.add_option("-j", "--jobs", type="string",dest="jobs",
help="number of jobs (default: 1 for local mode, 'unlimited' for sge mode)")
parser.add_option("-e","--mailTo", type="string",dest="mailTo",action="append",
help="send email notification of job completion status to this address (may be provided multiple times for more than one email address)")
parser.add_option("-d","--dryRun", dest="isDryRUn",action="store_true",
help="dryRun workflow code without actually running command-tasks")
(options,args) = parser.parse_args()
if len(args) :
parser.print_help()
sys.exit(2)
if options.mode == None :
parser.print_help()
sys.exit(2)
elif options.mode not in ["local","sge"] :
parser.error("Invalid mode. Available modes are: local, sge")
if options.jobs == None :
if options.mode == "sge" :
options.jobs == "unlimited"
else :
options.jobs == "1"
elif (options.jobs != "unlimited") and (int(options.jobs) <= 0) :
parser.error("Jobs must be 'unlimited' or an integer greater than 1")
return options
runOptions=get_run_options()
flowOptions=WorkflowOptions()
flowOptions.outputDir=scriptDir
wflow = BWAWorkflow(flowOptions)
retval=wflow.run(mode=runOptions.mode,
nCores=runOptions.jobs,
dataDirRoot=scriptDir,
mailTo=runOptions.mailTo,
isContinue="Auto",
isForceContinue=True,
isDryRun=runOptions.isDryRUn)
sys.exit(retval)
"""
def checkArg(x, label, checkfunc) :
if x != None:
x = os.path.abspath(x)
if not checkfunc(x) :
raise Exception("Can't find %s: '%s'" % (label, x))
return x
def checkDirArg(dir, label) :
return checkArg(dir, label, os.path.isdir)
def checkFileArg(file, label) :
return checkArg(file, label, os.path.isfile)
def get_option_parser(defaults, configFileName, isAllHelp=False) :
from optparse import OptionGroup, OptionParser, SUPPRESS_HELP
import textwrap
description = """This script configures a bcl to BWA alignmed BAM workflow.
Given a bcl basecalls directory the workflow will create fastq's using CASAVA's
bcl to fastq converter, then align each fastq using bwa, and finally consolidate
the output into a single BAM file for for each Project/Sample combination.
The configuration process will produce a workflow run script, which can be used to
execute the workflow on a single node or through sge with a specific job limit.
"""
epilog = """Default parameters will always be read from the file '%s' if it exists.
This file is searched for in the current working directory first -- if
it is not found then the directory containing this script is searched as well.
The current set of default parameters may be written to this file using the --writeConfig switch,
which takes all current defaults and arguments, writes these to the
configuration file and exits without setting up a workflow run script as usual.
""" % (configFileName)
# no epilog in py 2.4! hack-in the feature instead:
class MyOptionParser(OptionParser) :
def __init__(self, *args, **kwargs):
self.myepilog = None
try:
self.myepilog = kwargs.pop('epilog')
except KeyError:
pass
OptionParser.__init__(self, *args, **kwargs)
def print_help(self, *args, **kwargs) :
OptionParser.print_help(self, *args, **kwargs)
if self.myepilog != None :
sys.stdout.write("%s\n" % (textwrap.fill(self.myepilog)))
parser = MyOptionParser(description=description, epilog=epilog)
parser.set_defaults(**defaults)
parser.add_option("--allHelp", action="store_true", dest="isAllHelp",
help="show all extended/hidden options")
group = OptionGroup(parser, "Workflow options")
group.add_option("--bclBasecallsDir", type="string", dest="bclBasecallsDirList", metavar="DIR", action="append",
help="BCL basecalls directory. Call this option multiple times to specify multiple bcl directories, samples with the same name will be combined over all flowcells after alignmnet. [required] (default: %default)")
group.add_option("--bclTilePattern", type="string", dest="bclTilePatternList", metavar="PATTERN", action="append",
help="BCL converter tiles expression used to select a subsset of tiles (eg. 's_1') call this option either once for each basecalls dir or not at all (default: %default)")
group.add_option("--genomeFasta", type="string", dest="genomeFasta",
help="Genome fasta file which includes BWA index in the same directory [required] (default: %default)")
group.add_option("--outputDir", type="string", dest="outputDir",
help="BCL basecalls directory [required] (default: %default)")
group.add_option("--sampleName", type="string", dest="sampleNameList", metavar="sampleName", action="append",
help="Restrict analysis to given sampleName. This option can be provided more than once for multiple sample names. If no names are provided all samples are analyzed (default: %default)")
parser.add_option_group(group)
secgroup = OptionGroup(parser, "Extended options",
"These options are not likely to be reset after initial configuration in a new site, they will not be printed here if a default exists from the configuration file or otherwise, unless --allHelp is specified")
# used to access isAnyHelp from the maybeHelp function
class Hack : isAnyHelp = False
def maybeDefHelp(key, msg) :
if isAllHelp or (key not in defaults) :
Hack.isAnyHelp = True
return msg
return SUPPRESS_HELP
secgroup.add_option("--casavaDir", type="string", dest="casavaDir",
help=maybeDefHelp("casavaDir", "casava 1.8.2+ installation directory [required] (default: %default)"))
secgroup.add_option("--bwaBin", type="string", dest="bwaBin",
help=maybeDefHelp("bwaBin", "bwa binary [required] (default: %default)"))
secgroup.add_option("--samtoolsBin", type="string", dest="samtoolsBin",
help=maybeDefHelp("samtoolsBin", "samtools binary [required] (default: %default)"))
secgroup.add_option("--picardDir", type="string", dest="picardDir",
help=maybeDefHelp("picardDir", "casava 1.8.2+ installation directory [required] (default: %default)"))
if not Hack.isAnyHelp:
secgroup.description = "hidden"
parser.add_option_group(secgroup)
def maybeHelp(key, msg) :
if isAllHelp : return msg
return SUPPRESS_HELP
configgroup = OptionGroup(parser, "Config options")
configgroup.add_option("--writeConfig", action="store_true", dest="isWriteConfig",
help=maybeHelp("writeConfig", "Write new default configuration file based on current defaults and agruments. Defaults written to: '%s'" % (configFileName)))
if not isAllHelp :
configgroup.description = "hidden"
parser.add_option_group(configgroup)
return parser
def get_run_options() :
from ConfigParser import SafeConfigParser
configFileName = scriptName + ".ini"
if not os.path.isfile(configFileName) :
configPath = os.path.join(scriptDir, configFileName)
else :
configPath = os.path.join('.', configFileName)
configSectionName = scriptName
config = SafeConfigParser()
config.optionxform = str
config.read(configPath)
configOptions = {}
if config.has_section(configSectionName) :
for (k, v) in config.items(configSectionName) :
if v == "" : continue
configOptions[k] = v
defaults = { 'outputDir' : './results',
'bclToFastqMaxCores' : 12,
'samtoolsSortMemPerCore' : 1000000000, # samtools sort uses about 2x what you tell it to...
'alnMaxCores' : 8, # presumably bwa aln will become increasingly inefficient per core, so we don't want to let this go forever...
'isKeepFastq' : True, # important to keep these during testing, but not for production
}
defaults.update(configOptions)
parser = get_option_parser(defaults, configFileName)
(options, args) = parser.parse_args()
if options.isAllHelp :
parser = get_option_parser(defaults, configFileName, True)
parser.print_help()
sys.exit(2)
if len(args) : # or (len(sys.argv) == 1):
parser.print_help()
sys.exit(2)
# sanitize arguments before writing defaults, check for missing arguments after:
#
def checkListRepeats(list, itemLabel) :
if list == None : return
if len(set(list)) != len(list) :
parser.error("Repeated %s entries" % (itemLabel))
if options.bclBasecallsDirList != None :
for i, bclDir in enumerate(options.bclBasecallsDirList) :
options.bclBasecallsDirList[i] = checkDirArg(bclDir, "bcl basecalls directory")
# tmp for testing:
# checkListRepeats(options.bclBasecallsDirList,"bcl basecalls directory")
if (options.bclTilePatternList != None) and \
(len(options.bclBasecallsDirList) != len(options.bclTilePatternList)) :
parser.error("Unexpected number of bclTilPattern entries")
checkListRepeats(options.sampleNameList, "sample name")
options.casavaDir = checkDirArg(options.casavaDir, "casava directory")
options.genomeFasta = checkFileArg(options.genomeFasta, "genome fasta file")
options.bwaBin = checkFileArg(options.bwaBin, "bwa binary")
options.samtoolsBin = checkFileArg(options.samtoolsBin, "samtools binary")
if options.isWriteConfig == True :
if not config.has_section(configSectionName) :
config.add_section(configSectionName)
for k, v in vars(options).iteritems() :
if k == "isWriteConfig" : continue
if v == None : v = ""
config.set(configSectionName, k, str(v))
configfp = open(configFileName, "w")
config.write(configfp)
configfp.close()
sys.exit(0)
def noArgOrError(msg) :
if len(sys.argv) <= 1 :
parser.print_help()
sys.exit(2)
else :
parser.error(msg)
def assertOption(arg, label) :
if arg == None:
noArgOrError("No %s specified" % (label))
def assertList(list, itemLabel) :
if (list == None) or (len(list) == 0) :
noArgOrError("List containing %s (s) is empty or missing" % (itemLabel))
else :
for item in list :
assertOption(item, itemLabel)
assertList(options.bclBasecallsDirList, "bcl basecalls directory")
assertList(options.sampleNameList, "sample name")
assertOption(options.genomeFasta, "genome fasta file")
assertOption(options.outputDir, "output directory")
assertOption(options.casavaDir, "casava directory")
assertOption(options.picardDir, "picard directory")
assertOption(options.bwaBin, "bwa binary")
assertOption(options.samtoolsBin, "samtools binary")
return options
from bwaworkflow import BWAWorkflow, ensureDir
def main() :
options = get_run_options()
# instantiate workflow object to trigger parameter validation only
#
wflow = BWAWorkflow(options)
# generate runscript:
#
scriptFile = os.path.join(options.outputDir, "runWorkflow.py")
ensureDir(options.outputDir)
sfp = open(scriptFile, "w")
sfp.write(runScript1)
# there must be a nicer way to reverse eval() an object -- maybe human readable pickle is what we want here?
for k, v in vars(options).iteritems() :
if isinstance(v, basestring) :
sfp.write(" %s = '%s'\n" % (k, v))
else:
sfp.write(" %s = %s\n" % (k, v))
sfp.write("\n")
sfp.write(runScript2)
sfp.close()
os.chmod(scriptFile, 0755)
notefp = sys.stdout
notefp.write("""
Successfully created workflow run script. To execute the workflow, run the following script and set appropriate options:
%s
""" % (scriptFile))
if __name__ == "__main__" :
main()
|