Skip to content

Commit

Permalink
resolved #19 removed run function, removed intermedediate files and r…
Browse files Browse the repository at this point in the history
…enamed stderr and stdout
  • Loading branch information
Sidduppal authored and evanroyrees committed Mar 26, 2020
1 parent 00b3591 commit 8060943
Showing 1 changed file with 27 additions and 44 deletions.
71 changes: 27 additions & 44 deletions autometa/common/external/samtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@
"""
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/>.
Expand All @@ -35,42 +31,8 @@
logger = logging.getLogger(__name__)


def run(cmd, bam):
"""
Run `cmd` via subprocess.
Parameters
----------
cmd : str
Executable input str
bam : str
</path/to/output/alignment.bam>
Returns
-------
bool
True if retcode == 0. i.e. function run successfully.
Raises
------
ChildProcessError
Function did not run successfully, returns retcode.
"""

logger.debug(f'cmd: {cmd}')
log_samtools_dir = os.path.dirname(bam)
tempdir = tempfile.mkdtemp(suffix=None, prefix='samtools', dir=log_samtools_dir)
samtools_stderr=os.path.join(tempdir, 'samtools_stderr')
samtools_stdout=os.path.join(tempdir, 'samtools_stdout')
with open(samtools_stdout, 'w') as stdout, open(samtools_stderr, 'w') as stderr:
retcode = subprocess.call(cmd, stdout=stdout, stderr=stderr, shell=True)
if retcode != 0:
raise ChildProcessError(f'Samtools not successfully run, retcode: {retcode}')
shutil.rmtree(tempdir, ignore_errors=True)
return True

def sort(sam, bam, nproc=mp.cpu_count()):
"""
"""
Views then sorts sam file by leftmost coordinates and outputs to bam.
Parameters
Expand All @@ -81,11 +43,33 @@ def sort(sam, bam, nproc=mp.cpu_count()):
</path/to/output/alignment.bam>
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 {bam}'
run(cmd,bam)
Raises
------
ChildProcessError
Samtools did not run successfully, returns retcode.
"""

cmd = f'samtools view -@{nproc} -bS {sam} | samtools sort -@{nproc} -o {bam}'
logger.debug(f'cmd: {cmd}')
samtools_out_dir = os.path.dirname(bam)
tempdir = tempfile.mkdtemp(suffix=None, prefix='samtools', dir=samtools_out_dir)
os.chdir(tempdir)
samtools_err = os.path.join(samtools_out_dir, 'samtools.err')
samtools_out = os.path.join(samtools_out_dir, 'samtools.out')
with open(samtools_out, 'w') as stdout, open(samtools_err, 'w') as stderr:
try:
retcode = subprocess.call(cmd, stdout=stdout, stderr=stderr, shell=True)
if retcode != 0:
raise ChildProcessError(f'Samtools not successfully run, retcode: {retcode}')
except ChildProcessError as err:
raise err
finally:
shutil.rmtree(tempdir, ignore_errors=True)
os.remove(samtools_err)
os.remove(samtools_out)

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

Expand All @@ -100,5 +84,4 @@ def main(args):
parser.add_argument('--bam', help='</path/to/output/alignment.bam>', type=str)
parser.add_argument('--nproc', help='Number of processors to use', default=mp.cpu_count(), type=int)
args = parser.parse_args()
main(args)

main(args)

0 comments on commit 8060943

Please sign in to comment.