Skip to content

Commit

Permalink
resolved KwanLab#19 added docstring (and liscence), fixed nproc and r…
Browse files Browse the repository at this point in the history
…emoved depth function
  • Loading branch information
Sidduppal committed Mar 18, 2020
1 parent bc390bc commit 30054c3
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions autometa/common/external/samtools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Copyright 2020 Ian J. Miller, Evan R. Rees, Kyle Wolf, Siddharth Uppal,
Shaurya Chanana, Izaak Miller, Jason C. Kwan
This file is part of Autometa.
Autometa is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Autometa is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Autometa. If not, see <http://www.gnu.org/licenses/>.
Script containing wrapper functions for samtools
"""

Expand All @@ -10,6 +28,7 @@
import subprocess

import pandas as pd
import multiprocessing as mp

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -37,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 @@ -61,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 30054c3

Please sign in to comment.