Skip to content

Commit

Permalink
resolved #19 added docstring (and liscence), fixed nproc and removed …
Browse files Browse the repository at this point in the history
…depth function
  • Loading branch information
Sidduppal authored and evanroyrees committed Mar 26, 2020
1 parent 21ccc9e commit 666d9e2
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions autometa/common/external/samtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import subprocess

import pandas as pd
import multiprocessing as mp

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -55,22 +56,31 @@ def run(cmd):
logger.debug(f'cmd: {cmd}')
with open(os.devnull, 'w') as stdout, open(os.devnull, 'w') as stderr:
retcode = subprocess.call(cmd, stdout=stdout, stderr=stderr, shell=True)
if retcode:
if not retcode:
return True
else:
logger.warning(f'args:{cmd} retcode:{retcode}')
return False
return True

def sort(sam, out, nproc=0):
def sort(sam, out, nproc=mp.cpu_count()):
"""
Views the sam file and then sorts the alighnments by leftmost coordinates.
Parameters
----------
sam : str
</path/to/alignment.sam>
out : str
</path/to/output/file
nproc : int, optional
Number of processors to be used. By default uses all the processors of the system.
"""
cmd = f'samtools view -@{nproc} -bS {sam} | samtools sort -@{nproc} -o {out}'
run(cmd)

def depth(bam, records):
cmds = [f'samtools depth -r {record.id} {bam}' for record in records]
raise NotImplementedError

def main(args):
sort(args.sam)
depth(args.bam, args.seqs)
sort(args.sam, args.out, args.nproc)

if __name__ == '__main__':
import argparse
Expand All @@ -79,8 +89,8 @@ def main(args):
format='%(asctime)s : %(name)s : %(levelname)s : %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p')
parser = argparse.ArgumentParser()
parser.add_argument('--bam')
parser.add_argument('--sam')
parser.add_argument('--seqs')
parser.add_argument('--sam', help='</path/to/alignment.sam>')
parser.add_argument('--out', help='</path/to/output/file')
parser.add_argument('--nproc', help='Number of processors to use', default=mp.cpu_count(), type=int)
args = parser.parse_args()
main(args)

0 comments on commit 666d9e2

Please sign in to comment.