| 3 | 1 #!/usr/bin/env python | 
|  | 2 import argparse | 
|  | 3 import subprocess | 
|  | 4 import os | 
|  | 5 import shlex | 
|  | 6 | 
|  | 7 def createIndex(fname): | 
|  | 8     """ | 
|  | 9     Create an index for a file, return where it is | 
|  | 10     """ | 
|  | 11     d = os.mkdir("index_dir") | 
|  | 12     os.link(fname, "index_dir/genome.fa") | 
|  | 13     cmd = "{}/bwameth.py index index_dir/genome.fa".format(os.path.dirname(__file__)) | 
|  | 14     proc = subprocess.Popen(args=shlex.split(cmd)) | 
|  | 15     returncode = proc.wait() | 
|  | 16     if returncode != 0: | 
|  | 17         raise Exception("Error during '%s'" % cmd) | 
|  | 18     return "index_dir/genome.fa" | 
|  | 19 | 
|  | 20 | 
|  | 21 parser = argparse.ArgumentParser(description="A wrapper around bwameth for Galaxy. There's minimal argument checking done") | 
|  | 22 parser.add_argument('-p', '--numThreads', type=int, default=4, help="number of threads") | 
|  | 23 parser.add_argument('--makeIndex', help="Given a fasta file, index it") | 
|  | 24 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") | 
|  | 25 parser.add_argument('--readGroup', help="The read group text, if desired") | 
|  | 26 parser.add_argument('files', nargs="+", help="Fasta files (possibly gzipped, if the file names end in .gz)") | 
|  | 27 args = parser.parse_args() | 
|  | 28 | 
|  | 29 if "makeIndex" in args: | 
|  | 30     ifile = createIndex(args.makeIndex) | 
|  | 31 else: | 
|  | 32     ifile = args.premadeIndex | 
|  | 33 | 
|  | 34 files = " ".join(['{}'.format(x) for x in args.files]) | 
|  | 35 if "readGroup" in args: | 
|  | 36     files = "{} --read-group '{}'".format(files, args.readGroup) | 
|  | 37 | 
|  | 38 cmd = "{}/bwameth.py -p {} '{}' {} | samtools view -Su - - | samtools sort -@ {} -o output.bam -".format(os.path.dirname(__file__), args.numThreads, ifile, files, args.numThreads) | 
|  | 39 proc = subprocess.Popen(args=shlex.split(cmd)) | 
|  | 40 returncode = proc.wait() | 
|  | 41 if returncode != 0: | 
|  | 42     raise Exception("Error during '%s'" % cmd) |