Skip to content

Commit

Permalink
Working wrapper script
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Minot committed Feb 6, 2018
1 parent 5557dce commit 521674c
Show file tree
Hide file tree
Showing 33 changed files with 182,233 additions and 476 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
temp/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
temp/
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ ENV PATH="/usr/midas/MIDAS-1.3.2/scripts:${PATH}"

# Add the run script to the PATH
ADD run.py /usr/midas
ADD helpers /usr/midas/helpers
ADD lib /usr/midas/lib
RUN cd /usr/midas && \
chmod +x run.py && \
ln -s /usr/midas/run.py /usr/bin/


# Run tests and then remove the folder
ADD tests /usr/midas/tests
RUN bats /usr/midas/tests/ && rm -r /usr/midas/tests/
File renamed without changes.
90 changes: 0 additions & 90 deletions helpers/fastq_utils.py

This file was deleted.

Empty file added lib/__init__.py
Empty file.
130 changes: 130 additions & 0 deletions lib/exec_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/usr/bin/python
"""Functions to help with execution of system commands."""

import os
import sys
import json
import shutil
import logging
import traceback
import subprocess


def align_reads(read_fp, # FASTQ file path
db_fp, # Local path to DB
temp_folder, # Folder for results
evalue=0.0001,
query_gencode=11,
threads=1):
"""Align a set of reads with Paladin."""
pass


def run_cmds(commands, retry=0, catchExcept=False):
"""Run commands and write out the log, combining STDOUT & STDERR."""
logging.info("Commands:")
logging.info(' '.join(commands))
p = subprocess.Popen(commands,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout, stderr = p.communicate()
exitcode = p.wait()
if stdout:
logging.info("Standard output of subprocess:")
for line in stdout.split('\n'):
logging.info(line)
if stderr:
logging.info("Standard error of subprocess:")
for line in stderr.split('\n'):
logging.info(line)

# Check the exit code
if exitcode != 0 and retry > 0:
msg = "Exit code {}, retrying {} more times".format(exitcode, retry)
logging.info(msg)
run_cmds(commands, retry=retry - 1)
elif exitcode != 0 and catchExcept:
msg = "Exit code was {}, but we will continue anyway"
logging.info(msg.format(exitcode))
else:
assert exitcode == 0, "Exit code {}".format(exitcode)


def get_reference_database(ref_db, temp_folder):
"""Get a reference database folder."""

# Get files from AWS S3
if ref_db.startswith('s3://'):
logging.info("Getting reference database from S3: " + ref_db)

# Save the database to the local temp folder
local_fp = os.path.join(temp_folder, ref_db.split('/')[-1] + "/")

assert os.path.exists(local_fp) is False

logging.info("Saving database to " + local_fp)
run_cmds([
'aws',
's3',
'sync',
'--quiet',
'--sse',
'AES256',
ref_db,
local_fp
])

return local_fp

else:
# Treat the input as a local path
logging.info("Getting reference database from local path: " + ref_db)
assert os.path.exists(ref_db)

return ref_db


def return_results(out, read_prefix, output_folder, temp_folder):
"""Write out the final results as a JSON object."""
# Make a temporary file
temp_fp = os.path.join(temp_folder, read_prefix + '.json')
with open(temp_fp, 'wt') as fo:
json.dump(out, fo)

# Compress the output
run_cmds(['gzip', temp_fp])
temp_fp = temp_fp + '.gz'

if output_folder.startswith('s3://'):
# Copy to S3
run_cmds([
'aws',
's3',
'cp',
'--quiet',
'--sse',
'AES256',
temp_fp,
output_folder])
os.unlink(temp_fp)
else:
# Copy to local folder
run_cmds(['mv', temp_fp, output_folder])


def exit_and_clean_up(temp_folder):
"""Log the error messages and delete the temporary folder."""
# Capture the traceback
logging.info("There was an unexpected failure")
exc_type, exc_value, exc_traceback = sys.exc_info()
for line in traceback.format_tb(exc_traceback):
logging.info(line)

# Delete any files that were created for this sample
logging.info("Removing temporary folder: " + temp_folder)
shutil.rmtree(temp_folder)

# Exit
logging.info("Exit type: {}".format(exc_type))
logging.info("Exit code: {}".format(exc_value))
sys.exit(exc_value)
Loading

0 comments on commit 521674c

Please sign in to comment.