-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sam Minot
committed
Feb 6, 2018
1 parent
5557dce
commit 521674c
Showing
33 changed files
with
182,233 additions
and
476 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
temp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
temp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.