Skip to content

Commit

Permalink
Add support for a .ini config file in Unicycler
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Lee committed Jun 11, 2020
1 parent 5b5676d commit 961e9e0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
32 changes: 14 additions & 18 deletions src/python/toil/UnicyclerJob.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from argparse import ArgumentParser
import logging
import os
from pathlib import Path

from toil.job import Job
from toil.common import Toil
Expand All @@ -20,7 +21,7 @@ def __init__(self, read_one_file_id, read_two_file_id,
read_one_file_name="R1.fastq.gz",
read_two_file_name="R2.fastq.gz",
mode=None,
depth_filter=None,
config_file_path=None,
*args, **kwargs):
"""
Parameters
Expand All @@ -35,11 +36,8 @@ def __init__(self, read_one_file_id, read_two_file_id,
name of directory for output
parent_rv : dict
dictionary of return values from the parent job
mode : Optional[str]
one of "conservative", "normal", or "bold".
depth_filter : Optional[float]
filter out contigs lower than this fraction of the chromosomal
depth, if doing so does not result in graph dead ends
config_file_path : Optional[str]
a path to a .ini file containing arguments to be passed to the CLI call
"""
super(UnicyclerJob, self).__init__(*args, **kwargs)
self.read_one_file_id = read_one_file_id
Expand All @@ -49,7 +47,7 @@ def __init__(self, read_one_file_id, read_two_file_id,
self.output_directory = output_directory
self.parent_rv = parent_rv
self.mode = mode
self.depth_filter = depth_filter
self.config_file_path = config_file_path

def run(self, fileStore):
"""
Expand Down Expand Up @@ -86,11 +84,11 @@ def run(self, fileStore):
"--out",
os.path.join(working_dir, self.output_directory),
]
# add in optional args
if self.mode is not None:
parameters.extend(["--mode", self.mode])
if self.depth_filter is not None:
parameters.extend(["--depth_filter", str(self.depth_filter)])

if self.config_file_path is not None:
parsed_params = utilities.parseConfigFile(self.config_file_path)
parameters.extend(parsed_params)
logger.info("Adding parsed params to CLI call: " + str(parsed_params))

apiDockerCall(
self,
Expand Down Expand Up @@ -160,10 +158,9 @@ def run(self, fileStore):
help="the well specification")
parser.add_argument('-o', '--output-directory', default=None,
help="the directory containing all output files")
parser.add_argument("-m", "--mode", default="normal", type=str,
help='one of "conservative", "normal", or "bold"')
parser.add_argument("--depth-filter", default=None, type=float,
help="filter out contigs lower than this fraction of depth")
parser.add_argument("-c", "--config", default=None,
help="a .ini file with args to be passed to Unicycler")

options = parser.parse_args()
if options.output_directory is None:
options.output_directory = options.plate_spec + "_" + options.well_spec
Expand All @@ -184,8 +181,7 @@ def run(self, fileStore):
read_one_file_ids[0],
read_two_file_ids[0],
options.output_directory,
mode=options.mode,
depth_filter=options.depth_filter
config_file_path=str(Path(options.config).absolute()) if options.config is not None else None,
)
unicycler_rv = toil.start(unicycler_job)

Expand Down
30 changes: 30 additions & 0 deletions src/python/toil/utilities.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
from configparser import ConfigParser

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -241,3 +242,32 @@ def exportWellAssemblyFiles(toil, assembler, output_directory, well_specs,
# Export all apc output files from the file store
exportFiles(toil, well_output_directory,
well_assembly_rvs[iW]['apc_rv'])


def parseConfigFile(config_file_path):
"""
Given a .ini file with args to pass to the assembler, create a list of args add to the apiDockerCall.
If a config arg's value is True, it will be treated as a boolean switch and only the key will be passed.
Parameters
----------
config_file_path : str
the location of the .ini file
Returns
-------
list[str]
the parsed CLI args
"""
cfg = ConfigParser()
cfg.read(config_file_path)

result = []
for section_name, section_obj in cfg.items():
for arg_name, arg_value in section_obj.items():
result.append(arg_name)
if arg_value.lower() != "true":
result.append(arg_value)
logger.info("Parsed " + config_file_path + ". Got args: " + str(result))
return result

0 comments on commit 961e9e0

Please sign in to comment.