view bwameth_wrapper.py @ 6:8a198580ea8d draft

Uploaded
author dpryan79
date Tue, 13 Sep 2016 16:02:05 -0400
parents 3e1095e1defa
children
line wrap: on
line source

#!/usr/bin/env python
import argparse
import subprocess
import os
import shlex

def createIndex(fname):
    """
    Create an index for a file, return where it is
    """
    d = os.mkdir("index_dir")
    os.link(fname, "index_dir/genome.fa")
    cmd = "{}/bwameth.py index index_dir/genome.fa".format(os.path.dirname(__file__))
    proc = subprocess.Popen(args=shlex.split(cmd))
    returncode = proc.wait()
    if returncode != 0:
        raise Exception("Error during '%s'" % cmd)
    return "index_dir/genome.fa"


parser = argparse.ArgumentParser(description="A wrapper around bwameth for Galaxy. There's minimal argument checking done")
parser.add_argument('-p', '--numThreads', type=int, default=4, help="number of threads")
parser.add_argument('--makeIndex', help="Given a fasta file, index it")
parser.add_argument('--premadeIndex', help="If an index already exists, this is the fasta file associated with it (the index files must be in the same directory")
parser.add_argument('--readGroup', help="The read group text, if desired")
parser.add_argument('files', nargs="+", help="Fasta files (possibly gzipped, if the file names end in .gz)")
args = parser.parse_args()

if "makeIndex" in args:
    ifile = createIndex(args.makeIndex)
else:
    ifile = args.premadeIndex

files = " ".join(['{}'.format(x) for x in args.files])
if "readGroup" in args:
    files = "{} --read-group '{}'".format(files, args.readGroup)

cmd = "{}/bwameth.py -p {} '{}' {} | samtools view -Su - - | samtools sort -@ {} -o output.bam -".format(os.path.dirname(__file__), args.numThreads, ifile, files, args.numThreads)
proc = subprocess.Popen(args=shlex.split(cmd))
returncode = proc.wait()
if returncode != 0:
    raise Exception("Error during '%s'" % cmd)