Mercurial > repos > greg > bam_to_scidx
changeset 0:962b0366a980 draft
Uploaded
author | greg |
---|---|
date | Tue, 24 Nov 2015 08:13:56 -0500 |
parents | |
children | 24a8165055bd |
files | BAMtoIDX.jar bam_to_scidx.py bam_to_scidx.xml test-data/input.bam test-data/output.scidx |
diffstat | 5 files changed, 140 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bam_to_scidx.py Tue Nov 24 08:13:56 2015 -0500 @@ -0,0 +1,69 @@ +""" +bam_to_scidx.py + +Input: BAM file +Output: Converted scidx file +""" +import optparse +import os +import shutil +import subprocess +import sys +import tempfile + +BUFF_SIZE = 1048576 + + +def get_stderr_exception(tmp_err, tmp_stderr): + """ + Return a stderr string of reasonable size. + """ + tmp_stderr.close() + # Get stderr, allowing for case where it's very large. + tmp_stderr = open(tmp_err, 'rb') + stderr_str = '' + buffsize = BUFF_SIZE + try: + while True: + stderr_str += tmp_stderr.read(buffsize) + if not stderr_str or len(stderr_str) % buffsize != 0: + break + except OverflowError: + pass + tmp_stderr.close() + return stderr_str + + +def stop_err(msg): + sys.stderr.write(msg) + sys.exit(1) + + +parser = optparse.OptionParser() +parser.add_option('-j', '--jar_file', dest='jar_file', type='string', help='BAMtoIDX.jar') +parser.add_option('-b', '--input_bam', dest='input_bam', type='string', help='Input dataset in BAM format') +parser.add_option('-i', '--input_bai', dest='input_bai', type='string', help='Input dataset index') +parser.add_option('-r', '--read', dest='read', type='int', default=0, help='Reads.') +parser.add_option('-o', '--output', dest='output', type='string', help='Output dataset in scidx format') +options, args = parser.parse_args() + +tmp_dir = tempfile.mkdtemp(prefix='tmp-scidx-') +tmp_out = tempfile.NamedTemporaryFile().name +tmp_stdout = open(tmp_out, 'wb') +tmp_err = tempfile.NamedTemporaryFile().name +tmp_stderr = open(tmp_err, 'wb') + +# Link input BAM file and associated index file into the working directory. +input_data_file_name = "input.bam" +os.link(options.input_bam, os.path.join(tmp_dir, input_data_file_name)) +os.link(options.input_bai, os.path.join(tmp_dir, "%s.bai" % input_data_file_name)) + +cmd = "java -jar %s -b %s -s %d -o %s" % (options.jar_file, input_data_file_name, options.read, options.output) +proc = subprocess.Popen(args=cmd, stdout=tmp_stdout, stderr=tmp_stderr, shell=True, cwd=tmp_dir) +return_code = proc.wait() +if return_code != 0: + error_message = get_stderr_exception(tmp_err, tmp_stderr) + stop_err(error_message) + +if tmp_dir and os.path.exists(tmp_dir): + shutil.rmtree(tmp_dir)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bam_to_scidx.xml Tue Nov 24 08:13:56 2015 -0500 @@ -0,0 +1,50 @@ +<tool id="bam_to_scidx" name="Convert BAM to ScIdx" version="1.0.0"> + <description></description> + <command> + <![CDATA[ + python $__tool_directory__/bam_to_scidx.py + -j $__tool_directory__/BAMtoIDX.jar + -b "$input_bam" + -i "${input_bam.metadata.bam_index}" + -r $read + -o "$output" + ]]> + </command> + <inputs> + <param name="input_bam" type="data" format="bam" label="BAM file" help="BAM file must be sorted and indexed" /> + <param name="read" type="select" label="Read to output"> + <option value="0" selected="True">Read1</option> + <option value="1">Read2</option> + <option value="2">Combined</option> + </param> + </inputs> + <outputs> + <data name="output" format="scidx" label="${tool.name} on ${on_string}" /> + </outputs> + <tests> + <test> + <param name="input" value="input.bam" ftype="bam" /> + <param name="read" value="0" /> + <output name="output" file="output.scidx" /> + </test> + </tests> + <help> + +**What it does** + +Converts BAM data to ScIdx, the Strand-specific coordinate count format, which is used by +tools within the Chip-exo Galaxy flavor. The Chip-exo Galaxy flavor is used by the Center for +Eukaryotic Gene Regulation labs at The Pennsylvania State University. ScIdx files are 1-based. + + </help> + <citations> + <citation type="bibtex"> + @unpublished{None, + author = {None}, + title = {None}, + year = {None}, + eprint = {None}, + url = {http://www.huck.psu.edu/content/research/independent-centers-excellence/center-for-eukaryotic-gene-regulation} + }</citation> + </citations> +</tool>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/output.scidx Tue Nov 24 08:13:56 2015 -0500 @@ -0,0 +1,21 @@ +#2015-11-23 20:18:56.51;input.bam;READ1 +chrom index forward reverse value +chr17 1820100 0 1 1 +chr17 1849175 1 0 1 +chr17 2778159 0 1 1 +chr17 3205518 0 1 1 +chr17 12974350 1 0 1 +chr17 20624912 0 1 1 +chr17 27071176 0 1 1 +chr17 34920819 1 0 1 +chr17 39810583 0 1 1 +chr17 43542379 0 1 1 +chr17 45800656 1 0 1 +chr17 47124830 0 1 1 +chr17 48697995 0 1 1 +chr17 54474619 0 1 1 +chr17 64901353 0 1 1 +chr17 69072332 1 0 1 +chr17 76184543 1 0 1 +chr17 76518878 0 1 1 +chr17 76941015 1 0 1