Mercurial > repos > drosofff > msp_sr_bowtie
changeset 0:64064dccdb11 draft
Imported from capsule None
author | drosofff |
---|---|
date | Mon, 03 Nov 2014 10:24:38 -0500 |
parents | |
children | b50d7228b678 |
files | sRbowtie.py sRbowtie.xml test-data/sRbowtie.fa test-data/sRbowtie.out tool-data/bowtie_indices.loc.sample tool_data_table_conf.xml.sample tool_dependencies.xml |
diffstat | 7 files changed, 434 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sRbowtie.py Mon Nov 03 10:24:38 2014 -0500 @@ -0,0 +1,123 @@ +#!/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, os, subprocess, tempfile, shutil, 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 fasta file") + 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="path", index="path", output="path", pslots="4"): + 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 -f %s > %s" % (x, index, input, output) + elif out_type=="sam": + return "bowtie %s -S %s -f %s > %s" % (x, index, input, output) + elif out_type=="bam": + return "bowtie %s -S %s -f %s |samtools view -bS - > %s" % (x, index, input, output) + +def bowtie_squash(fasta): + tmp_index_dir = tempfile.mkdtemp() # make temp directory for bowtie indexes + ref_file = tempfile.NamedTemporaryFile( dir=tmp_index_dir ) + ref_file_name = ref_file.name + ref_file.close() # by default, delete the temporary file, but ref_file.name is now stored in ref_file_name + os.symlink( fasta, ref_file_name ) # symlink between the fasta source file and the deleted ref_file name + cmd1 = 'bowtie-build -f %s %s' % (ref_file_name, ref_file_name ) # bowtie command line, which will work after changing dir (cwd=tmp_index_dir) + try: + FNULL = open(os.devnull, 'w') + tmp = tempfile.NamedTemporaryFile( dir=tmp_index_dir ).name # a path string for a temp file in tmp_index_dir. Just a string + tmp_stderr = open( tmp, 'wb' ) # creates and open a file handler pointing to the temp file + proc = subprocess.Popen( args=cmd1, shell=True, cwd=tmp_index_dir, stderr=FNULL, stdout=FNULL ) # both stderr and stdout of bowtie-build are redirected in dev/null + returncode = proc.wait() + tmp_stderr.close() + FNULL.close() + sys.stdout.write(cmd1 + "\n") + except Exception, 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() + index_full_path = os.path.join(tmp_index_dir, ref_file_name) # bowtie fashion path without extention + 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: + target_file = command_line.split()[-1] # recover the final output file name + 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 - + second_command_line = "samtools sort %s %s" % (path_to_unsortedBam, path_to_sortedBam) # generates an "unsorted.bam.sorted.bam file", NOT an "unsorted.bam.sorted" file + p = subprocess.Popen(args=first_command_line, cwd=tmp_index_dir, shell=True, stderr=tmp_stderr.fileno()) # fileno() method return the file descriptor number of tmp_stderr + 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, index_path, args.output, args.num_threads) + bowtie_alignment(command_line, flyPreIndexed=tmp_dir) + F.close() +if __name__=="__main__": __main__()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sRbowtie.xml Mon Nov 03 10:24:38 2014 -0500 @@ -0,0 +1,212 @@ +<tool id="bowtieForSmallRNA" name="sRbowtie" version="1.1.0"> + <description>for FASTA small reads</description> + <requirements> + <requirement type="package" version="0.12.7">bowtie</requirement> + <requirement type="package" version="0.1.18">samtools</requirement> + </requirements> + <parallelism method="basic"></parallelism> + <command interpreter="python"> sRbowtie.py --input $input + --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 + #if $refGenomeSource.genomeSource == "history": + $refGenomeSource.ownFile + #else: + $refGenomeSource.index.fields.path + #end if + --aligned $aligned + --unaligned $unaligned + --num-threads \${GALAXY_SLOTS:-4} ## number of processors to be handled by bowtie + </command> + <inputs> + <param name="input" type="data" format="fasta" label="Input fasta file: reads clipped from their adapter" help="Only with clipped, raw fasta files"/> +<!-- which method will be used --> + <param name="method" type="select" label="What kind of matching do you want to do?" help="bowtie parameters adjusted to the type of matching. RNA option match to only one strand"> + <option value="RNA">Match on sense strand RNA reference index, multiple mappers randomly matched at a single position</option> + <option value="unique">Match unique mappers on DNA reference index</option> + <option value="multiple" selected="true">Match on DNA, multiple mappers randomly matched at a single position</option> + <option value="k_option">Match on DNA as fast as possible, without taking care of mapping issues (for raw annotation of reads)</option> + <option value="n_option">Match on DNA - RNAseq mode (-n bowtie option)</option> + <option value="a_option">Match and report all valid alignments</option> + </param> +<!-- END of which method will be used --> + <param name="v_mismatches" type="select" label="Number of mismatches allowed" help="specify the -v bowtie option"> + <option value="0">0</option> + <option value="1" selected="true">1</option> + <option value="2">2</option> + <option value="3">3</option> + </param> +<!-- bowtie index selection --> + <conditional name="refGenomeSource"> + <param name="genomeSource" type="select" label="Will you select a reference genome from your history or use a built-in index?" help="Built-ins were indexed using default options"> + <option value="indexed">Use a built-in index</option> + <option value="history">Use one from the history</option> + </param> + <when value="indexed"> + <param name="index" type="select" label="Select a DNA reference index" help="if your genome of interest is not listed - contact GED team"> + <options from_data_table="bowtie_indexes"> + <!-- <filter type="sort_by" column="2" /> + <validator type="no_options" message="No indexes are available" /> --> + </options> + </param> + </when> + <when value="history"> + <param name="ownFile" type="data" format="fasta" label="Select a fasta file, to serve as index reference" /> + </when> + </conditional> +<!-- END bowtie index selection --> + <param name="output_format" type="select" label="Select output format" help="Note that the BAM will be viewable in trackster only if you choose a full genome referenced for Trackster usage. see the doc below"> + <option value="tabular" select="true">tabular</option> + <option value="sam">sam</option> + <option value="bam">bam</option> + </param> + <param name="additional_fasta" type="select" label="additional fasta output" help="to get aligned and unaligned reads in fasta format"> + <option value="No" select="true">No</option> + <option value="al">aligned</option> + <option value="unal">unaligned</option> + <option value="al_and_unal">both aligned and unaligned</option> + </param> + </inputs> + <outputs> + <data format="tabular" name="output" label="Bowtie Output"> + <change_format> + <when input="output_format" value="sam" format="sam" /> + <when input="output_format" value="bam" format="bam" /> + </change_format> +<!-- Set metadata based on reference genome --> + <actions> + <conditional name="refGenomeSource.genomeSource"> + <when value="indexed"> + <action type="metadata" name="dbkey"> + <option type="from_data_table" name="bowtie_indexes" column="1" offset="0"> + <filter type="param_value" column="0" value="#" compare="startswith" keep="False"/> + <filter type="param_value" ref="refGenomeSource.index" column="0"/> + </option> + </action> + </when> + <when value="history"> + <action type="metadata" name="dbkey"> + <option type="from_param" name="refGenomeSource.ownFile" param_attribute="dbkey" /> + </action> + </when> + </conditional> + </actions> + </data> + <data format="fasta" name="aligned" label="Matched reads"> + <filter>additional_fasta == "al" or additional_fasta == "al_and_unal"</filter> + </data> + <data format="fasta" name="unaligned" label ="Unmatched reads"> + <filter>additional_fasta == "unal" or additional_fasta == "al_and_unal"</filter> + </data> + </outputs> + + <test> + <param name="genomeSource" value="indexed" /> + <param name="index" value="/home/galaxy/galaxy-dist/bowtie/Dmel/dmel-all-chromosome-r5.49" /> + <param name="method" value="multiple" /> + <param name="input" ftype="fasta" value="sRbowtie.fa" /> + <param name="v_mismatches" value="1" /> + <param name="output_format" value="tabular" /> + <output name="output" ftype="tabular" value="sRbowtie.out" /> + <output name="aligned" value="None" /> + <output name="unaligned" value="None" /> + </test> + + <help> + +**What it does** + +Bowtie_ is a short read aligner designed to be ultrafast and memory-efficient. It is developed by Ben Langmead and Cole Trapnell. Please cite: Langmead B, Trapnell C, Pop M, Salzberg SL. Ultrafast and memory-efficient alignment of short DNA sequences to the human genome. Genome Biology 10:R25. + +.. _Bowtie: http://bowtie-bio.sourceforge.net/index.shtml + +A generic "Map with Bowtie for Illumina" Galaxy tool is available in the main Galaxy distribution. + +However, this Bowtie wrapper tool only takes FASTQ files as inputs. + +The sRbowtie wrapper specifically works with short reads FASTA inputs (-v bowtie mode) + +------ + +**OPTIONS** + +.. class:: infomark + +This script uses Bowtie to match reads on a reference index. + +Depending on the type of matching, different bowtie options are used: + +**Match on sense strand RNA reference index, multiple mappers randomly matched at a single position** + +Match on RNA reference, SENSE strand, randomly attributing multiple mapper to target with least mismatches, the polarity column is suppressed in the bowtie tabular report: + +*-v [0,1,2,3] -M 1 --best --strata -p 12 --norc --suppress 2,6,7,8* + +**Match unique mappers on DNA reference index** + +Match ONLY unique mappers on DNA reference index + +*-v [0,1,2,3] -m 1 -p 12 --suppress 6,7,8* + +Note that using this option with -v values other than 0 is questionnable... + +**Match on DNA, multiple mappers randomly matched at a single position** + +Match multiple mappers, randomly attributing multiple mapper to target with least mismatches, number of mismatch allowed specified by -v option: + +*-v [0,1,2,3] -M 1 --best --strata -p 12 --suppress 6,7,8* + +**Match on DNA as fast as possible, without taking care of mapping issues (for raw annotation of reads)** + +Match with highest speed, not guaranteeing best hit for speed gain: + +*-v [0,1,2,3] -k 1 --best -p 12 --suppress 6,7,8* + + +----- + +**Input formats** + +.. class:: warningmark + +*The only accepted format for the script is a raw fasta list of reads, clipped from their adapter* + +----- + +**OUTPUTS** + +If you choose tabular as the output format, you will obtain the matched reads in standard bowtie output format, having the following columns:: + + Column Description + -------- -------------------------------------------------------- + 1 FastaID fasta identifier + 2 polarity + or - depending whether the match was reported on the forward or reverse strand + 3 target name of the matched target + 4 Offset O-based coordinate of the miR on the miRBase pre-miR sequence + 5 Seq sequence of the matched Read + +If you choose SAM, you will get the output in unordered SAM format. + +.. class:: warningmark + +if you choose BAM, the output will be in sorted BAM format. +To be viewable in Trackster, several condition must be fulfilled: + +.. class:: infomark + +Reads must have been matched to a genome whose chromosome names are compatible with Trackster genome indexes + +.. class:: infomark + +the database/Build (dbkey) which is indicated for the dataset (Pencil - Database/Build field) must match a Trackster genome index. + +Please contact the Galaxy instance administrator if your genome is not referenced + +**Matched and unmatched fasta reads can be retrieved, for further analyses** + + </help> +</tool>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/sRbowtie.fa Mon Nov 03 10:24:38 2014 -0500 @@ -0,0 +1,40 @@ +>1 +GTATTGTAAGTGGCAGAGTGGC +>2 +TGGAATGTAAAGAAGTATGGAG +>3 +GTGGGGAGTTTGGATGGGGCGGCA +>4 +AATGGCACTGGAAGAATTCACGG +>5 +GTACGGACAAGGGGAATC +>6 +TTGGGTTCTGGGGGGAGTATGG +>7 +GTGGGGAGTTTCGCTGGGGCGGCA +>8 +TAAGGGTCGGGTAGTGAGGGC +>9 +AGCTGGGACTGAGGACTG +>10 +AGCTGGGACTGAGGACTGC +>11 +AAGGGTCGGGTAGTGAGG +>12 +GTCGGGTAGTGAGGGCCTT +>13 +TGGTGGGGCTTGGAACAATTGGAGGGC +>14 +TGACGGAAGGGCACCACC +>15 +TGGAATGTAAAGAAGTATGGAG +>16 +TTGGGTTCTGGGGGGAGT +>17 +TCAGGTGGGGAGTTTGGCTGGGGCGGCACA +>18 +TTGGGTATAGGGGCGAAAGA +>19 +AGCGGGCGTGCTTGTGGAC +>20 +GTCGAATTTGGGTATAGGGGC
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/sRbowtie.out Mon Nov 03 10:24:38 2014 -0500 @@ -0,0 +1,5 @@ +2 + 2L 20487495 TGGAATGTAAAGAAGTATGGAG +4 - 2L 11953463 CCGTGAATTCTTCCAGTGCCATT +15 + 2L 20487495 TGGAATGTAAAGAAGTATGGAG +14 - Uextra 7115665 GGTGGTGCCCTTCCGTCA +18 + Uextra 7726410 TTGGGTATAGGGGCGAAAGA
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool-data/bowtie_indices.loc.sample Mon Nov 03 10:24:38 2014 -0500 @@ -0,0 +1,37 @@ +#This is a sample file distributed with Galaxy that enables tools +#to use a directory of Bowtie indexed sequences data files. You will +#need to create these data files and then create a bowtie_indices.loc +#file similar to this one (store it in this directory) that points to +#the directories in which those files are stored. The bowtie_indices.loc +#file has this format (longer white space characters are TAB characters): +# +#<unique_build_id> <dbkey> <display_name> <file_base_path> +# +#So, for example, if you had hg18 indexed stored in +#/depot/data2/galaxy/bowtie/hg18/, +#then the bowtie_indices.loc entry would look like this: +# +#hg18 hg18 hg18 /depot/data2/galaxy/bowtie/hg18/hg18 +# +#and your /depot/data2/galaxy/bowtie/hg18/ directory +#would contain hg18.*.ebwt files: +# +#-rw-r--r-- 1 james universe 830134 2005-09-13 10:12 hg18.1.ebwt +#-rw-r--r-- 1 james universe 527388 2005-09-13 10:12 hg18.2.ebwt +#-rw-r--r-- 1 james universe 269808 2005-09-13 10:12 hg18.3.ebwt +#...etc... +# +#Your bowtie_indices.loc file should include an entry per line for each +#index set you have stored. The "file" in the path does not actually +#exist, but it is the prefix for the actual index files. For example: +# +#hg18canon hg18 hg18 Canonical /depot/data2/galaxy/bowtie/hg18/hg18canon +#hg18full hg18 hg18 Full /depot/data2/galaxy/bowtie/hg18/hg18full +#/orig/path/hg19 hg19 hg19 /depot/data2/galaxy/bowtie/hg19/hg19 +#...etc... +# +#Note that for backwards compatibility with workflows, the unique ID of +#an entry must be the path that was in the original loc file, because that +#is the value stored in the workflow for that parameter. That is why the +#hg19 entry above looks odd. New genomes can be better-looking. +#
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool_data_table_conf.xml.sample Mon Nov 03 10:24:38 2014 -0500 @@ -0,0 +1,8 @@ +<!-- Use the file tool_data_table_conf.xml.oldlocstyle if you don't want to update your loc files as changed in revision 4550:535d276c92bc--> +<tables> + <!-- Locations of indexes in the Bowtie mapper format --> + <table name="bowtie_indexes" comment_char="#"> + <columns>value, dbkey, name, path</columns> + <file path="tool-data/bowtie_indices.loc" /> + </table> +</tables>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool_dependencies.xml Mon Nov 03 10:24:38 2014 -0500 @@ -0,0 +1,9 @@ +<?xml version="1.0"?> +<tool_dependency> + <package name="bowtie" version="0.12.7"> + <repository changeset_revision="f54826948b0b" name="package_bowtie_0_12_7" owner="devteam" toolshed="https://testtoolshed.g2.bx.psu.edu" /> + </package> + <package name="samtools" version="0.1.18"> + <repository changeset_revision="c0f72bdba484" name="package_samtools_0_1_18" owner="devteam" toolshed="https://testtoolshed.g2.bx.psu.edu" /> + </package> +</tool_dependency>