changeset 5:b2c1ffe6579a draft

planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/msp_sr_bowtie commit 783b6d807f921a7214d53ddace3954d6eb5f2e5c
author drosofff
date Tue, 04 Jul 2017 18:29:02 -0400
parents 615d2550977f
children b7173c0011f3
files sRbowtie.py sRbowtie.xml test-data/al.fa test-data/output.tab tool_dependencies.xml
diffstat 5 files changed, 83 insertions(+), 216 deletions(-) [+]
line wrap: on
line diff
--- a/sRbowtie.py	Wed Nov 09 11:20:44 2016 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,189 +0,0 @@
-#!/usr/bin/env python
-# small RNA oriented bowtie wrapper
-# version 1.5 17-7-2014: arg parser implementation
-# Usage sRbowtie.py <1 input_fasta_file> <2 alignment method> <3 -v mismatches> <4 out_type> <5 buildIndexIfHistory> <6 fasta/bowtie index> <7 bowtie output> <8 ali_fasta> <9 unali_fasta> <10 --num-threads \${GALAXY_SLOTS:-4}>
-# current rev: for bowtie __norc, move from --supress 2,6,7,8 to --supress 6,7,8. Future Parser must be updated to take into account this standardisation
-# Christophe Antoniewski <drosofff@gmail.com>
-
-import sys
-import os
-import subprocess
-import tempfile
-import shutil
-import argparse
-
-
-def Parser():
-    the_parser = argparse.ArgumentParser(
-        description="bowtie wrapper for small fasta reads")
-    the_parser.add_argument(
-        '--input', action="store", type=str, help="input file")
-    the_parser.add_argument(
-        '--input-format', dest="input_format", action="store", type=str, help="fasta or fastq")
-    the_parser.add_argument('--method', action="store", type=str,
-                            help="RNA, unique, multiple, k_option, n_option, a_option")
-    the_parser.add_argument('--v-mismatches', dest="v_mismatches", action="store",
-                            type=str, help="number of mismatches allowed for the alignments")
-    the_parser.add_argument(
-        '--output-format', dest="output_format", action="store", type=str, help="tabular, sam, bam")
-    the_parser.add_argument(
-        '--output', action="store", type=str, help="output file path")
-    the_parser.add_argument(
-        '--index-from', dest="index_from", action="store", type=str, help="indexed or history")
-    the_parser.add_argument('--index-source', dest="index_source",
-                            action="store", type=str, help="file path to the index source")
-    the_parser.add_argument(
-        '--aligned', action="store", type=str, help="aligned read file path, maybe None")
-    the_parser.add_argument('--unaligned', action="store",
-                            type=str, help="unaligned read file path, maybe None")
-    the_parser.add_argument('--num-threads', dest="num_threads",
-                            action="store", type=str, help="number of bowtie threads")
-    args = the_parser.parse_args()
-    return args
-
-
-def stop_err(msg):
-    sys.stderr.write('%s\n' % msg)
-    sys.exit()
-
-
-def bowtieCommandLiner(alignment_method="RNA", v_mis="1", out_type="tabular",
-                       aligned="None", unaligned="None", input_format="fasta", input="path",
-                       index="path", output="path", pslots="4"):
-    if input_format == "fasta":
-        input_format = "-f"
-    elif (input_format == "fastq") or (input_format == "fastqsanger"):
-        input_format = "-q"
-    else:
-        raise Exception('input format must be one of fasta or fastq')
-    if alignment_method == "RNA":
-        x = "-v %s -M 1 --best --strata -p %s --norc --suppress 6,7,8" % (
-            v_mis, pslots)
-    elif alignment_method == "unique":
-        x = "-v %s -m 1 -p %s --suppress 6,7,8" % (v_mis, pslots)
-    elif alignment_method == "multiple":
-        x = "-v %s -M 1 --best --strata -p %s --suppress 6,7,8" % (
-            v_mis, pslots)
-    elif alignment_method == "k_option":
-        x = "-v %s -k 1 --best -p %s --suppress 6,7,8" % (v_mis, pslots)
-    elif alignment_method == "n_option":
-        x = "-n %s -M 1 --best -p %s --suppress 6,7,8" % (v_mis, pslots)
-    elif alignment_method == "a_option":
-        x = "-v %s -a --best -p %s --suppress 6,7,8" % (v_mis, pslots)
-    if aligned == "None" and unaligned == "None":
-        fasta_command = ""
-    elif aligned != "None" and unaligned == "None":
-        fasta_command = " --al %s" % aligned
-    elif aligned == "None" and unaligned != "None":
-        fasta_command = " --un %s" % unaligned
-    else:
-        fasta_command = " --al %s --un %s" % (aligned, unaligned)
-    x = x + fasta_command
-    if out_type == "tabular":
-        return "bowtie %s %s %s %s > %s" % (x, index, input_format, input, output)
-    elif out_type == "sam":
-        return "bowtie %s -S %s %s %s > %s" % (x, index, input_format, input, output)
-    elif out_type == "bam":
-        return "bowtie %s -S %s %s %s |samtools view -bS - > %s" % (
-            x, index, input_format, input, output)
-
-
-def bowtie_squash(fasta):
-    # make temp directory for bowtie indexes
-    tmp_index_dir = tempfile.mkdtemp()
-    ref_file = tempfile.NamedTemporaryFile(dir=tmp_index_dir)
-    ref_file_name = ref_file.name
-    # by default, delete the temporary file, but ref_file.name is now stored
-    # in ref_file_name
-    ref_file.close()
-    # symlink between the fasta source file and the deleted ref_file name
-    os.symlink(fasta, ref_file_name)
-    # bowtie command line, which will work after changing dir
-    # (cwd=tmp_index_dir)
-    cmd1 = 'bowtie-build -f %s %s' % (ref_file_name, ref_file_name)
-    try:
-        FNULL = open(os.devnull, 'w')
-        # a path string for a temp file in tmp_index_dir. Just a string
-        tmp = tempfile.NamedTemporaryFile(dir=tmp_index_dir).name
-        # creates and open a file handler pointing to the temp file
-        tmp_stderr = open(tmp, 'wb')
-        # both stderr and stdout of bowtie-build are redirected in  dev/null
-        proc = subprocess.Popen(
-            args=cmd1, shell=True, cwd=tmp_index_dir, stderr=FNULL, stdout=FNULL)
-        returncode = proc.wait()
-        tmp_stderr.close()
-        FNULL.close()
-        sys.stdout.write(cmd1 + "\n")
-    except Exception as e:
-        # clean up temp dir
-        if os.path.exists(tmp_index_dir):
-            shutil.rmtree(tmp_index_dir)
-            stop_err('Error indexing reference sequence\n' + str(e))
-    # no Cleaning if no Exception, tmp_index_dir has to be cleaned after
-    # bowtie_alignment()
-    # bowtie fashion path without extention
-    index_full_path = os.path.join(tmp_index_dir, ref_file_name)
-    return tmp_index_dir, index_full_path
-
-
-def bowtie_alignment(command_line, flyPreIndexed=''):
-    # make temp directory just for stderr
-    tmp_index_dir = tempfile.mkdtemp()
-    tmp = tempfile.NamedTemporaryFile(dir=tmp_index_dir).name
-    tmp_stderr = open(tmp, 'wb')
-    # conditional statement for sorted bam generation viewable in Trackster
-    if "samtools" in command_line:
-        # recover the final output file name
-        target_file = command_line.split()[-1]
-        path_to_unsortedBam = os.path.join(tmp_index_dir, "unsorted.bam")
-        path_to_sortedBam = os.path.join(tmp_index_dir, "unsorted.bam.sorted")
-        first_command_line = " ".join(
-            command_line.split()[:-3]) + " -o " + path_to_unsortedBam + " - "
-        # example: bowtie -v 0 -M 1 --best --strata -p 12 --suppress 6,7,8 -S
-        # /home/galaxy/galaxy-dist/bowtie/Dmel/dmel-all-chromosome-r5.49 -f
-        # /home/galaxy/galaxy-dist/database/files/003/dataset_3460.dat
-        # |samtools view -bS -o /tmp/tmp_PgMT0/unsorted.bam -
-        # generates an "unsorted.bam.sorted.bam file", NOT an
-        # "unsorted.bam.sorted" file
-        second_command_line = "samtools sort  %s %s" % (
-            path_to_unsortedBam, path_to_sortedBam)
-        # fileno() method return the file descriptor number of tmp_stderr
-        p = subprocess.Popen(
-            args=first_command_line, cwd=tmp_index_dir, shell=True, stderr=tmp_stderr.fileno())
-        returncode = p.wait()
-        sys.stdout.write("%s\n" % first_command_line + str(returncode))
-        p = subprocess.Popen(
-            args=second_command_line, cwd=tmp_index_dir, shell=True, stderr=tmp_stderr.fileno())
-        returncode = p.wait()
-        sys.stdout.write("\n%s\n" % second_command_line + str(returncode))
-        if os.path.isfile(path_to_sortedBam + ".bam"):
-            shutil.copy2(path_to_sortedBam + ".bam", target_file)
-    else:
-        p = subprocess.Popen(
-            args=command_line, shell=True, stderr=tmp_stderr.fileno())
-        returncode = p.wait()
-        sys.stdout.write(command_line + "\n")
-    tmp_stderr.close()
-    # cleaning if the index was created in the fly
-    if os.path.exists(flyPreIndexed):
-        shutil.rmtree(flyPreIndexed)
-    # cleaning tmp files and directories
-    if os.path.exists(tmp_index_dir):
-        shutil.rmtree(tmp_index_dir)
-    return
-
-
-def __main__():
-    args = Parser()
-    F = open(args.output, "w")
-    if args.index_from == "history":
-        tmp_dir, index_path = bowtie_squash(args.index_source)
-    else:
-        tmp_dir, index_path = "dummy/dymmy", args.index_source
-    command_line = bowtieCommandLiner(args.method, args.v_mismatches, args.output_format,
-                                      args.aligned, args.unaligned, args.input_format, args.input,
-                                      index_path, args.output, args.num_threads)
-    bowtie_alignment(command_line, flyPreIndexed=tmp_dir)
-    F.close()
-if __name__ == "__main__":
-    __main__()
--- a/sRbowtie.xml	Wed Nov 09 11:20:44 2016 -0500
+++ b/sRbowtie.xml	Tue Jul 04 18:29:02 2017 -0400
@@ -1,28 +1,58 @@
-<tool id="bowtieForSmallRNA" name="sRbowtie" version="1.1.2.1">
+<tool id="bowtieForSmallRNA" name="sRbowtie" version="1.2">
     <description>for FASTA small reads</description>
     <requirements>
-        <requirement type="package" version="1.1.2">bowtie</requirement>
+        <requirement type="package" version="1.1.2=py27_2">bowtie</requirement>
         <requirement type="package" version="1.2">samtools</requirement>
     </requirements>
-    <command><![CDATA[
-        python '$__tool_directory__'/sRbowtie.py
-        --input '$input'
-        --input-format '$input.extension'
-        --method '$method'
-        --v-mismatches $v_mismatches
-        --output-format $output_format
-        --output '$output'
-        --index-from '$refGenomeSource.genomeSource'
-        ## the very source of the index (indexed or fasta file)
-        --index-source
+    <command detect_errors="exit_code"><![CDATA[
         #if $refGenomeSource.genomeSource == "history":
-            '$refGenomeSource.ownFile'
+            bowtie-build -f $refGenomeSource.ownFile local_index &&
         #else:
-            '$refGenomeSource.index.fields.path'
+            ln -f -s $refGenomeSource.index.fields.path local_index &&
+        #end if
+        #if $input.extension == "fasta":
+            #set format = "-f"
+        #elif $input.extension == "fastq":
+            #set format = "-q"
+        #end if
+
+        ## set the method_prefix
+        #if $method == "RNA":
+            #set method_prefix = "-v %s -M 1 --best --strata --norc" % str($v_mismatches)
+        #elif $method == "unique":
+            #set method_prefix = "-v %s -m 1" % str($v_mismatches)
+        #elif $method == "multiple":
+            #set method_prefix = "-v %s -M 1 --best --strata" % str($v_mismatches)
+        #elif $method == "k_option":
+            #set method_prefix = "-v %s -k 1 --best" % str($v_mismatches)
+        #elif $method == "n_option":
+            #set method_prefix = "-n %s -M 1 --best" % str($v_mismatches)
+        #elif $method == "a_option":
+            #set method_prefix = "-v %s -a --best" % str($v_mismatches)
         #end if
-        --aligned '$aligned'
-        --unaligned '$unaligned'
-        --num-threads \${GALAXY_SLOTS:-4} ## number of processors to be handled by bowtie
+ 
+        ## set the extra_output
+        #if $additional_fasta == "No":
+            #set extra_output = ""
+        #elif $additional_fasta == "al":
+            #set extra_output = " --al %s " % str($aligned)
+        #elif $additional_fasta == "unal":
+            #set extra_output = " --un %s " % str($unaligned)
+        #else:
+            #set extra_output = " --al %s --un %s " % (str($aligned), str($unaligned))
+        #end if
+       
+        #set $method_postfix = " %s %s " % ($method_prefix, $extra_output)
+
+        ## run the bowtie alignement
+        #if $output_format == "tabular":
+            bowtie -p \${GALAXY_SLOTS:-4} $method_postfix --suppress 6,7,8 local_index $format '$input' > $output
+        #elif $output_format == "sam":
+            bowtie -p \${GALAXY_SLOTS:-4} $method_postfix -S local_index $format '$input' > '$output'
+        #elif $output_format == "bam":
+            bowtie -p \${GALAXY_SLOTS:-4} $method_postfix -S local_index $format '$input' | samtools view -u - | samtools sort -@ "\${GALAXY_SLOTS:-4}" -T tmp -O bam -o $output
+        #end if
+        ##### | samtools view -uS
         ]]></command>
     <inputs>
         <param format="fasta, fastq" help="Only with clipped, raw fasta files" label="Input fasta file: reads clipped from their adapter" name="input" type="data" />
@@ -118,6 +148,26 @@
             <param name="output_format" value="bam" />
             <output file="output2.bam" ftype="bam" compare="sim_size" delta="1000" name="output" />
         </test>
+        <test>
+            <param name="genomeSource" value="history" />
+            <param ftype="fasta" name="ownFile" value="297_reference.fa" />
+            <param name="method" value="multiple" />
+            <param ftype="fasta" name="input" value="input.fa" />
+            <param name="v_mismatches" value="1" />
+            <param name="output_format" value="tabular" />
+            <output file="output.tab" ftype="tabular" name="output" />
+        </test>
+        <test>
+            <param name="genomeSource" value="history" />
+            <param ftype="fasta" name="ownFile" value="297_reference.fa" />
+            <param name="method" value="multiple" />
+            <param ftype="fasta" name="input" value="input.fa" />
+            <param name="v_mismatches" value="1" />
+            <param name="additional_fasta" value="al" />
+            <param name="output_format" value="tabular" />
+            <output file="output.tab" ftype="tabular" name="output" />
+            <output file="al.fa" ftype="fasta" name="aligned" />
+        </test>
     </tests>
     <help>
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/al.fa	Tue Jul 04 18:29:02 2017 -0400
@@ -0,0 +1,10 @@
+>1
+CATTCTGCGAAGAAGC
+>2
+GGAGAGACGACGTCAATTACT
+>3
+AATTTCGAAACTACACCTGGAAGGTAACCA
+>4
+ATCGGATTTTCTTATTTATATTCAGGGTATTAAAGAATCTAT
+>5
+TAAGATACTGATAAGGAAACTACCAATA
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/output.tab	Tue Jul 04 18:29:02 2017 -0400
@@ -0,0 +1,5 @@
+1	+	FBgn0000005_297	1151	CATTCTGCGAAGAAGC
+2	+	FBgn0000005_297	1167	GGAGAGACGACGTCAATTACT
+3	+	FBgn0000005_297	1188	AATTTCGAAACTACACCTGGAAGGTAACCA
+4	+	FBgn0000005_297	1218	ATCGGATTTTCTTATTTATATTCAGGGTATTAAAGAATCTAT
+5	+	FBgn0000005_297	1260	TAAGATACTGATAAGGAAACTACCAATA
--- a/tool_dependencies.xml	Wed Nov 09 11:20:44 2016 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,9 +0,0 @@
-<?xml version="1.0"?>
-<tool_dependency>
-    <package name="bowtie" version="1.1.2">
-         <repository changeset_revision="a1c1a92e13a6" name="package_bowtie_1_1_2" owner="iuc" toolshed="https://toolshed.g2.bx.psu.edu" />
-    </package>
-    <package name="samtools" version="1.2">
-         <repository changeset_revision="f6ae3ba3f3c1" name="package_samtools_1_2" owner="iuc" toolshed="https://toolshed.g2.bx.psu.edu" />
-    </package>
-</tool_dependency>