changeset 0:6820983ba5d5 draft

Uploaded
author crs4
date Tue, 18 Mar 2014 07:49:22 -0400
parents
children ebb02ba5987c
files COPYING bwa_mem.py bwa_mem.xml readme.rst tool-data/bwa_index.loc.sample tool_data_table_conf.xml.sample tool_dependencies.xml
diffstat 7 files changed, 747 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/COPYING	Tue Mar 18 07:49:22 2014 -0400
@@ -0,0 +1,182 @@
+Copyright (c) 2009-2013 Pennsylvania State University
+Copyright © 2013 Yufei Luo <luoyufei@gmail.com>
+Copyright © 2014 CRS4 Srl. http://www.crs4.it/
+Created by:
+Kelly Vincent <kpvincent@bx.psu.edu>
+Daniel Blankenberg <dan@bx.psu.edu>
+Yufei Luo <luoyufei@gmail.com>
+Nicola Soranzo <nicola.soranzo@crs4.it>
+
+Licensed under the Academic Free License version 3.0
+
+ 1) Grant of Copyright License. Licensor grants You a worldwide, royalty-free, 
+    non-exclusive, sublicensable license, for the duration of the copyright, to 
+    do the following:
+
+    a) to reproduce the Original Work in copies, either alone or as part of a 
+       collective work;
+
+    b) to translate, adapt, alter, transform, modify, or arrange the Original 
+       Work, thereby creating derivative works ("Derivative Works") based upon 
+       the Original Work;
+
+    c) to distribute or communicate copies of the Original Work and Derivative 
+       Works to the public, under any license of your choice that does not 
+       contradict the terms and conditions, including Licensor's reserved 
+       rights and remedies, in this Academic Free License;
+
+    d) to perform the Original Work publicly; and
+
+    e) to display the Original Work publicly.
+
+ 2) Grant of Patent License. Licensor grants You a worldwide, royalty-free, 
+    non-exclusive, sublicensable license, under patent claims owned or 
+    controlled by the Licensor that are embodied in the Original Work as 
+    furnished by the Licensor, for the duration of the patents, to make, use, 
+    sell, offer for sale, have made, and import the Original Work and 
+    Derivative Works.
+
+ 3) Grant of Source Code License. The term "Source Code" means the preferred 
+    form of the Original Work for making modifications to it and all available 
+    documentation describing how to modify the Original Work. Licensor agrees 
+    to provide a machine-readable copy of the Source Code of the Original Work 
+    along with each copy of the Original Work that Licensor distributes. 
+    Licensor reserves the right to satisfy this obligation by placing a 
+    machine-readable copy of the Source Code in an information repository 
+    reasonably calculated to permit inexpensive and convenient access by You 
+    for as long as Licensor continues to distribute the Original Work.
+
+ 4) Exclusions From License Grant. Neither the names of Licensor, nor the 
+    names of any contributors to the Original Work, nor any of their 
+    trademarks or service marks, may be used to endorse or promote products 
+    derived from this Original Work without express prior permission of the 
+    Licensor. Except as expressly stated herein, nothing in this License 
+    grants any license to Licensor's trademarks, copyrights, patents, trade 
+    secrets or any other intellectual property. No patent license is granted 
+    to make, use, sell, offer for sale, have made, or import embodiments of 
+    any patent claims other than the licensed claims defined in Section 2. 
+    No license is granted to the trademarks of Licensor even if such marks 
+    are included in the Original Work. Nothing in this License shall be 
+    interpreted to prohibit Licensor from licensing under terms different 
+    from this License any Original Work that Licensor otherwise would have a 
+    right to license.
+
+ 5) External Deployment. The term "External Deployment" means the use, 
+    distribution, or communication of the Original Work or Derivative Works 
+    in any way such that the Original Work or Derivative Works may be used by 
+    anyone other than You, whether those works are distributed or 
+    communicated to those persons or made available as an application 
+    intended for use over a network. As an express condition for the grants 
+    of license hereunder, You must treat any External Deployment by You of 
+    the Original Work or a Derivative Work as a distribution under 
+    section 1(c).
+
+ 6) Attribution Rights. You must retain, in the Source Code of any Derivative 
+    Works that You create, all copyright, patent, or trademark notices from 
+    the Source Code of the Original Work, as well as any notices of licensing 
+    and any descriptive text identified therein as an "Attribution Notice." 
+    You must cause the Source Code for any Derivative Works that You create 
+    to carry a prominent Attribution Notice reasonably calculated to inform 
+    recipients that You have modified the Original Work.
+
+ 7) Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that 
+    the copyright in and to the Original Work and the patent rights granted 
+    herein by Licensor are owned by the Licensor or are sublicensed to You 
+    under the terms of this License with the permission of the contributor(s) 
+    of those copyrights and patent rights. Except as expressly stated in the 
+    immediately preceding sentence, the Original Work is provided under this 
+    License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or 
+    implied, including, without limitation, the warranties of 
+    non-infringement, merchantability or fitness for a particular purpose. 
+    THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This 
+    DISCLAIMER OF WARRANTY constitutes an essential part of this License. 
+    No license to the Original Work is granted by this License except under 
+    this disclaimer.
+
+ 8) Limitation of Liability. Under no circumstances and under no legal 
+    theory, whether in tort (including negligence), contract, or otherwise, 
+    shall the Licensor be liable to anyone for any indirect, special, 
+    incidental, or consequential damages of any character arising as a result 
+    of this License or the use of the Original Work including, without 
+    limitation, damages for loss of goodwill, work stoppage, computer failure 
+    or malfunction, or any and all other commercial damages or losses. This 
+    limitation of liability shall not apply to the extent applicable law 
+    prohibits such limitation.
+
+ 9) Acceptance and Termination. If, at any time, You expressly assented to 
+    this License, that assent indicates your clear and irrevocable acceptance 
+    of this License and all of its terms and conditions. If You distribute or 
+    communicate copies of the Original Work or a Derivative Work, You must 
+    make a reasonable effort under the circumstances to obtain the express 
+    assent of recipients to the terms of this License. This License 
+    conditions your rights to undertake the activities listed in Section 1, 
+    including your right to create Derivative Works based upon the Original 
+    Work, and doing so without honoring these terms and conditions is 
+    prohibited by copyright law and international treaty. Nothing in this 
+    License is intended to affect copyright exceptions and limitations 
+    (including "fair use" or "fair dealing"). This License shall terminate 
+    immediately and You may no longer exercise any of the rights granted to 
+    You by this License upon your failure to honor the conditions in 
+    Section 1(c).
+
+10) Termination for Patent Action. This License shall terminate 
+    automatically and You may no longer exercise any of the rights granted 
+    to You by this License as of the date You commence an action, including 
+    a cross-claim or counterclaim, against Licensor or any licensee alleging 
+    that the Original Work infringes a patent. This termination provision 
+    shall not apply for an action alleging patent infringement by 
+    combinations of the Original Work with other software or hardware.
+
+11) Jurisdiction, Venue and Governing Law. Any action or suit relating to 
+    this License may be brought only in the courts of a jurisdiction wherein 
+    the Licensor resides or in which Licensor conducts its primary business, 
+    and under the laws of that jurisdiction excluding its conflict-of-law 
+    provisions. The application of the United Nations Convention on 
+    Contracts for the International Sale of Goods is expressly excluded. Any 
+    use of the Original Work outside the scope of this License or after its 
+    termination shall be subject to the requirements and penalties of 
+    copyright or patent law in the appropriate jurisdiction. This section 
+    shall survive the termination of this License.
+
+12) Attorneys' Fees. In any action to enforce the terms of this License or 
+    seeking damages relating thereto, the prevailing party shall be entitled 
+    to recover its costs and expenses, including, without limitation, 
+    reasonable attorneys' fees and costs incurred in connection with such 
+    action, including any appeal of such action. This section shall survive 
+    the termination of this License.
+
+13) Miscellaneous. If any provision of this License is held to be 
+    unenforceable, such provision shall be reformed only to the extent 
+    necessary to make it enforceable.
+
+14) Definition of "You" in This License. "You" throughout this License, 
+    whether in upper or lower case, means an individual or a legal entity 
+    exercising rights under, and complying with all of the terms of, this 
+    License. For legal entities, "You" includes any entity that controls, is 
+    controlled by, or is under common control with you. For purposes of this 
+    definition, "control" means (i) the power, direct or indirect, to cause 
+    the direction or management of such entity, whether by contract or 
+    otherwise, or (ii) ownership of fifty percent (50%) or more of the 
+    outstanding shares, or (iii) beneficial ownership of such entity.
+
+15) Right to Use. You may use the Original Work in all ways not otherwise 
+    restricted or conditioned by this License or by law, and Licensor 
+    promises not to interfere with or be responsible for such uses by You.
+
+16) Modification of This License. This License is Copyright © 2005 Lawrence 
+    Rosen. Permission is granted to copy, distribute, or communicate this 
+    License without modification. Nothing in this License permits You to 
+    modify this License as applied to the Original Work or to Derivative 
+    Works. However, You may modify the text of this License and copy, 
+    distribute or communicate your modified version (the "Modified 
+    License") and apply it to other original works of authorship subject to 
+    the following conditions: (i) You may not indicate in any way that your 
+    Modified License is the "Academic Free License" or "AFL" and you may not 
+    use those names in the name of your Modified License; (ii) You must 
+    replace the notice specified in the first paragraph above with the 
+    notice "Licensed under <insert your license name here>" or with a notice 
+    of your own that is not confusingly similar to the notice in this 
+    License; and (iii) You may not claim that your original works are open 
+    source software unless your Modified License has been approved by Open 
+    Source Initiative (OSI) and You comply with its license review and 
+    certification process.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bwa_mem.py	Tue Mar 18 07:49:22 2014 -0400
@@ -0,0 +1,273 @@
+# -*- coding: utf-8 -*-
+#!/usr/bin/env python
+## yufei.luo@gustave.roussy 22/07/2013
+## Copyright © 2014 CRS4 Srl. http://www.crs4.it/
+## Modified by:
+## Nicola Soranzo <nicola.soranzo@crs4.it>
+
+"""
+Runs BWA on single-end or paired-end data.
+Produces a SAM file containing the mappings.
+Works with BWA version >= 0.7.5.
+NOTICE: In this wrapper, we only use 'mem' for mapping step.
+
+usage: bwa_mem.py [options]
+
+See below for options
+"""
+
+import optparse, os, shutil, subprocess, sys, tempfile
+
+def stop_err( msg ):
+    sys.stderr.write( '%s\n' % msg )
+    sys.exit()
+
+def check_is_double_encoded( fastq ):
+    # check that first read is bases, not one base followed by numbers
+    bases = [ 'A', 'C', 'G', 'T', 'a', 'c', 'g', 't', 'N' ]
+    nums = [ '0', '1', '2', '3' ]
+    for line in file( fastq, 'rb'):
+        if not line.strip() or line.startswith( '@' ):
+            continue
+        if len( [ b for b in line.strip() if b in nums ] ) > 0:
+            return False
+        elif line.strip()[0] in bases and len( [ b for b in line.strip() if b in bases ] ) == len( line.strip() ):
+            return True
+        else:
+            raise Exception, 'First line in first read does not appear to be a valid FASTQ read in either base-space or color-space'
+    raise Exception, 'There is no non-comment and non-blank line in your FASTQ file'
+
+def __main__():
+    descr = "bwa_mem.py: Map (long length) reads against a reference genome with BWA-MEM."
+    parser = optparse.OptionParser(description=descr)
+    parser.add_option( '-t', '--threads', default=1, help='The number of threads to use [1]' )
+    parser.add_option( '--ref', help='The reference genome to use or index' )
+    parser.add_option( '-f', '--fastq', help='The (forward) fastq file to use for the mapping' )
+    parser.add_option( '-F', '--rfastq', help='The reverse fastq file to use for mapping if paired-end data' )
+    parser.add_option( '-u', '--output', help='The file to save the output (SAM format)' )
+    parser.add_option( '-g', '--genAlignType', help='The type of pairing (single or paired)' )
+    parser.add_option( '--params', help='Parameter setting to use (pre_set or full)' )
+    parser.add_option( '-s', '--fileSource', help='Whether to use a previously indexed reference sequence or one form history (indexed or history)' )
+    parser.add_option( '-D', '--dbkey', help='Dbkey for reference genome' )
+
+    parser.add_option( '-k', '--minEditDistSeed', default=19, type=int, help='Minimum edit distance to the seed [19]' )
+    parser.add_option( '-w', '--bandWidth', default=100, type=int, help='Band width for banded alignment [100]' )
+    parser.add_option( '-d', '--offDiagonal', default=100, type=int, help='off-diagonal X-dropoff [100]' )
+    parser.add_option( '-r', '--internalSeeds', default=1.5, type=float, help='look for internal seeds inside a seed longer than {-k} * FLOAT [1.5]' )
+    parser.add_option( '-c', '--seedsOccurrence', default=10000, type=int, help='skip seeds with more than INT occurrences [10000]' )
+    parser.add_option( '-S', '--mateRescue', default=False, help='skip mate rescue' )
+    parser.add_option( '-P', '--skipPairing', default=False, help='skpe pairing, mate rescue performed unless -S also in use' )
+    parser.add_option( '-A', '--seqMatch', default=1, type=int, help='score of a sequence match' )
+    parser.add_option( '-B', '--mismatch', default=4, type=int, help='penalty for a mismatch' )
+    parser.add_option( '-O', '--gapOpen', default=6, type=int, help='gap open penalty' )
+    parser.add_option( '-E', '--gapExtension', default=None, help='gap extension penalty; a gap of size k cost {-O} + {-E}*k [1]' )
+    parser.add_option( '-L', '--clipping', default=5, type=int, help='penalty for clipping [5]' )
+    parser.add_option( '-U', '--unpairedReadpair', default=17, type=int, help='penalty for an unpaired read pair [17]' )
+    parser.add_option( '-p', '--interPairEnd', default=False, help='first query file consists of interleaved paired-end sequences' )
+    parser.add_option( '--rgid', help='Read group identifier' )
+    parser.add_option( '--rgsm', help='Sample' )
+    parser.add_option( '--rgpl', choices=[ 'CAPILLARY', 'LS454', 'ILLUMINA', 'SOLID', 'HELICOS', 'IONTORRENT', 'PACBIO' ], help='Platform/technology used to produce the reads' )
+    parser.add_option( '--rglb', help='Library name' )
+    parser.add_option( '--rgpu', help='Platform unit (e.g. flowcell-barcode.lane for Illumina or slide for SOLiD)' )
+    parser.add_option( '--rgcn', help='Sequencing center that produced the read' )
+    parser.add_option( '--rgds', help='Description' )
+    parser.add_option( '--rgdt', help='Date that run was produced (ISO8601 format date or date/time, like YYYY-MM-DD)' )
+    parser.add_option( '--rgfo', help='Flow order' )
+    parser.add_option( '--rgks', help='The array of nucleotide bases that correspond to the key sequence of each read' )
+    parser.add_option( '--rgpg', help='Programs used for processing the read group' )
+    parser.add_option( '--rgpi', help='Predicted median insert size' )
+    parser.add_option( '-T', '--minScore', default=30, type=int, help='minimum score to output [30]' )
+    parser.add_option( '-M', '--mark', default=False, help='mark shorter split hits as secondary (for Picard/GATK compatibility)' )
+    parser.add_option( '-H', '--suppressHeader', dest='suppressHeader', help='Suppress header' )
+    (options, args) = parser.parse_args()
+    if len(args) > 0:
+        parser.error('Wrong number of arguments')
+
+    # output version # of tool
+    try:
+        tmp = tempfile.NamedTemporaryFile().name
+        tmp_stdout = open( tmp, 'wb' )
+        proc = subprocess.Popen( args='bwa 2>&1', shell=True, stdout=tmp_stdout )
+        tmp_stdout.close()
+        returncode = proc.wait()
+        stdout = None
+        for line in open( tmp_stdout.name, 'rb' ):
+            if line.lower().find( 'version' ) >= 0:
+                stdout = line.strip()
+                break
+        if stdout:
+            sys.stdout.write( 'BWA %s\n' % stdout )
+        else:
+            raise Exception
+    except:
+        sys.stdout.write( 'Could not determine BWA version\n' )
+
+    fastq = options.fastq
+    if options.rfastq:
+        rfastq = options.rfastq
+
+    # make temp directory for placement of indices
+    tmp_index_dir = tempfile.mkdtemp()
+    tmp_dir = tempfile.mkdtemp()
+    # index if necessary
+    if options.fileSource == 'history':
+        ref_file = tempfile.NamedTemporaryFile( dir=tmp_index_dir )
+        ref_file_name = ref_file.name
+        ref_file.close()
+        os.symlink( options.ref, ref_file_name )
+        # determine which indexing algorithm to use, based on size
+        try:
+            size = os.stat( options.ref ).st_size
+            if size <= 2**30:
+                indexingAlg = 'is'
+            else:
+                indexingAlg = 'bwtsw'
+        except:
+            indexingAlg = 'is'
+        indexing_cmds = '-a %s' % indexingAlg
+        cmd1 = 'bwa index %s %s' % ( indexing_cmds, ref_file_name )
+        try:
+            tmp = tempfile.NamedTemporaryFile( dir=tmp_index_dir ).name
+            tmp_stderr = open( tmp, 'wb' )
+            proc = subprocess.Popen( args=cmd1, shell=True, cwd=tmp_index_dir, stderr=tmp_stderr.fileno() )
+            returncode = proc.wait()
+            tmp_stderr.close()
+            # get stderr, allowing for case where it's very large
+            tmp_stderr = open( tmp, 'rb' )
+            stderr = ''
+            buffsize = 1048576
+            try:
+                while True:
+                    stderr += tmp_stderr.read( buffsize )
+                    if not stderr or len( stderr ) % buffsize != 0:
+                        break
+            except OverflowError:
+                pass
+            tmp_stderr.close()
+            if returncode != 0:
+                raise Exception, stderr
+        except Exception, e:
+            # clean up temp dirs
+            if os.path.exists( tmp_index_dir ):
+                shutil.rmtree( tmp_index_dir )
+            if os.path.exists( tmp_dir ):
+                shutil.rmtree( tmp_dir )
+            stop_err( 'Error indexing reference sequence. ' + str( e ) )
+    else:
+        ref_file_name = options.ref
+    # if options.illumina13qual:
+    #     illumina_quals = "-I"
+    # else:
+    #     illumina_quals = ""
+
+    # set up aligning and generate aligning command args
+    start_cmds = '-t %s ' % options.threads
+    if options.params == 'pre_set':
+        # aligning_cmds = '-t %s %s' % ( options.threads, illumina_quals )
+        #start_cmds = '-t %s ' % options.threads
+        end_cmds = ' '
+        print start_cmds, end_cmds
+
+    else:
+        end_cmds = '-k %s -w %s -d %s -r %s -c %s -A %s -B %s -O %s -L %s -U %s -T %s ' % (options.minEditDistSeed, options.bandWidth, options.offDiagonal, options.internalSeeds, options.seedsOccurrence, options.seqMatch, options.mismatch, options.gapOpen, options.clipping, options.unpairedReadpair, options.minScore)
+        if options.mateRescue:
+            end_cmds += '-S '
+            if options.skipPairing:
+                end_cmds += '-P '
+        else:
+            if options.skipPairing:
+                print "Option Error and will not be considered, you should also choose 'skip mate rescue -S' option! "
+        if options.gapExtension != None:
+            end_cmds += '-E %s ' % options.gapExtension
+
+        if options.rgid:
+            if not options.rglb or not options.rgpl or not options.rgsm or not options.rglb:
+                stop_err( 'If you want to specify read groups, you must include the ID, LB, PL, and SM tags.' )
+            # readGroup = '@RG\tID:%s\tLB:%s\tPL:%s\tSM:%s' % ( options.rgid, options.rglb, options.rgpl, options.rgsm )
+            readGroup = '@RG\tID:%s\tLB:%s\tPL:%s\tSM:%s' % ( options.rgid, options.rglb, options.rgpl, options.rgsm )
+            if options.rgpu:
+                readGroup += '\tPU:%s' % options.rgpu
+            if options.rgcn:
+                readGroup += '\tCN:%s' % options.rgcn
+            if options.rgds:
+                readGroup += '\tDS:%s' % options.rgds
+            if options.rgdt:
+                readGroup += '\tDT:%s' % options.rgdt
+            if options.rgfo:
+                readGroup += '\tFO:%s' % options.rgfo
+            if options.rgks:
+                readGroup += '\tKS:%s' % options.rgks
+            if options.rgpg:
+                readGroup += '\tPG:%s' % options.rgpg
+            if options.rgpi:
+                readGroup += '\tPI:%s' % options.rgpi
+            end_cmds += ' -R "%s" ' % readGroup
+
+        if options.interPairEnd:
+            end_cmds += '-p %s ' % options.interPairEnd
+        if options.mark:
+            end_cmds += '-M '
+
+
+    if options.genAlignType == 'paired':
+        cmd = 'bwa mem %s %s %s %s %s > %s' % ( start_cmds, ref_file_name, fastq, rfastq, end_cmds, options.output )
+    else:
+        cmd = 'bwa mem %s %s %s %s > %s' % ( start_cmds, ref_file_name, fastq, end_cmds, options.output )
+
+  # perform alignments
+    buffsize = 1048576
+    try:
+        # need to nest try-except in try-finally to handle 2.4
+        try:
+            try:
+                tmp = tempfile.NamedTemporaryFile( dir=tmp_dir ).name
+                tmp_stderr = open( tmp, 'wb' )
+                print "The cmd is %s" % cmd
+                proc = subprocess.Popen( args=cmd, shell=True, cwd=tmp_dir, stderr=tmp_stderr.fileno() )
+                returncode = proc.wait()
+                tmp_stderr.close()
+                # get stderr, allowing for case where it's very large
+                tmp_stderr = open( tmp, 'rb' )
+                stderr = ''
+                try:
+                    while True:
+                        stderr += tmp_stderr.read( buffsize )
+                        if not stderr or len( stderr ) % buffsize != 0:
+                            break
+                except OverflowError:
+                    pass
+                tmp_stderr.close()
+                if returncode != 0:
+                    raise Exception, stderr
+            except Exception, e:
+                raise Exception, 'Error generating alignments. ' + str( e )
+            # remove header if necessary
+            if options.suppressHeader == 'true':
+                tmp_out = tempfile.NamedTemporaryFile( dir=tmp_dir)
+                tmp_out_name = tmp_out.name
+                tmp_out.close()
+                try:
+                    shutil.move( options.output, tmp_out_name )
+                except Exception, e:
+                    raise Exception, 'Error moving output file before removing headers. ' + str( e )
+                fout = file( options.output, 'w' )
+                for line in file( tmp_out.name, 'r' ):
+                    if not ( line.startswith( '@HD' ) or line.startswith( '@SQ' ) or line.startswith( '@RG' ) or line.startswith( '@PG' ) or line.startswith( '@CO' ) ):
+                        fout.write( line )
+                fout.close()
+            # check that there are results in the output file
+            if os.path.getsize( options.output ) > 0:
+                sys.stdout.write( 'BWA run on %s-end data' % options.genAlignType )
+            else:
+                raise Exception, 'The output file is empty. You may simply have no matches, or there may be an error with your input file or settings.'
+        except Exception, e:
+            stop_err( 'The alignment failed.\n' + str( e ) )
+    finally:
+        # clean up temp dir
+        if os.path.exists( tmp_index_dir ):
+            shutil.rmtree( tmp_index_dir )
+        if os.path.exists( tmp_dir ):
+            shutil.rmtree( tmp_dir )
+
+if __name__ == "__main__":
+    __main__()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bwa_mem.xml	Tue Mar 18 07:49:22 2014 -0400
@@ -0,0 +1,208 @@
+<tool id="bwa_mem" name="Map with BWA-MEM" version="0.7.7">
+  <requirements>
+    <requirement type="package" version="0.7.7">bwa</requirement>
+  </requirements>
+  <description></description>
+  <parallelism method="basic"></parallelism>
+  <version_command>bwa 2&gt;&amp;1 | grep "Version: " | sed -e 's/Version: //'</version_command>
+  <command interpreter="python">
+    bwa_mem.py
+      --threads="\${GALAXY_SLOTS:-1}"
+      --fileSource="${genomeSource.refGenomeSource}"
+      #if $genomeSource.refGenomeSource == "history":
+        ##build index on the fly
+        --ref="${genomeSource.ownFile}"
+        --dbkey="${dbkey}"
+      #else:
+        ##use precomputed indexes
+        --ref="${genomeSource.indices.fields.path}"
+      #end if
+
+      ## input file(s)
+      --fastq="${paired.fastq}"
+      #if $paired.sPaired == "paired":
+        --rfastq="${paired.rfastq}"
+      #end if
+
+      ## output file
+      --output="${output}"
+
+      ## run parameters
+      --genAlignType="${paired.sPaired}"
+      --params="${params.source_select}"
+      #if $params.source_select != "pre_set":
+        --minEditDistSeed="${params.minEditDistSeed}"
+        --bandWidth="${params.bandWidth}"
+        --offDiagonal="${params.offDiagonal}"
+        --internalSeeds="${params.internalSeeds}"
+        --seedsOccurrence="${params.seedsOccurrence}"
+        --mateRescue="${params.mateRescue}"
+        --skipPairing="${params.skipPairing}"
+        --seqMatch="${params.seqMatch}"
+        --mismatch="${params.mismatch}"
+        --gapOpen="${params.gapOpen}"
+        --gapExtension="${params.gapExtension}"
+        --clipping="${params.clipping}"
+        --unpairedReadpair="${params.unpairedReadpair}"
+        --interPairEnd="${params.interPairEnd}"
+        --minScore="${params.minScore}"
+        --mark="${params.mark}"
+
+        #if $params.readGroup.specReadGroup == "yes"
+          --rgid="${params.readGroup.rgid}"
+          --rgsm="${params.readGroup.rgsm}"
+          --rgpl="${params.readGroup.rgpl}"
+          --rglb="${params.readGroup.rglb}"
+          --rgpu="${params.readGroup.rgpu}"
+          --rgcn="${params.readGroup.rgcn}"
+          --rgds="${params.readGroup.rgds}"
+          --rgdt="${params.readGroup.rgdt}"
+          --rgfo="${params.readGroup.rgfo}"
+          --rgks="${params.readGroup.rgks}"
+          --rgpg="${params.readGroup.rgpg}"
+          --rgpi="${params.readGroup.rgpi}"
+        #end if
+      #end if
+
+      ## suppress output SAM header
+      --suppressHeader="${suppressHeader}"
+  </command>
+
+  <inputs>
+    <conditional name="genomeSource">
+      <param name="refGenomeSource" type="select" label="Will you select a reference genome from your history or use a built-in index?">
+        <option value="indexed">Use a built-in index</option>
+        <option value="history">Use one from the history</option>
+      </param>
+      <when value="indexed">
+        <param name="indices" type="select" label="Select a reference genome">
+          <options from_data_table="bwa_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" metadata_name="dbkey" label="Select a reference from history" />
+      </when>
+    </conditional>
+    <conditional name="paired">
+      <param name="sPaired" type="select" label="Is this library mate-paired?">
+        <option value="single">Single-end</option>
+        <option value="paired">Paired-end</option>
+      </param>
+      <when value="single">
+        <param name="fastq" type="data" format="fastqsanger,fastqillumina" label="FASTQ file" help="FASTQ with either Sanger-scaled quality values (fastqsanger) or Illumina-scaled quality values (fastqillumina)" />
+      </when>
+      <when value="paired">
+        <param name="fastq" type="data" format="fastqsanger,fastqillumina" label="Forward FASTQ file" help="FASTQ with either Sanger-scaled quality values (fastqsanger) or Illumina-scaled quality values (fastqillumina)" />
+        <param name="rfastq" type="data" format="fastqsanger,fastqillumina" label="Reverse FASTQ file" help="FASTQ with either Sanger-scaled quality values (fastqsanger) or Illumina-scaled quality values (fastqillumina)" />
+      </when>
+    </conditional>
+    <conditional name="params">
+      <param name="source_select" type="select" label="BWA settings to use" help="For most mapping needs use Commonly Used settings. If you want full control use Full Parameter List">
+        <option value="pre_set">Commonly Used</option>
+        <option value="full">Full Parameter List</option>
+      </param>
+      <when value="pre_set" />
+      <when value="full">
+        <param name="minEditDistSeed" type="integer" value="19" label="Minimum seed length" />
+        <param name="bandWidth" type="integer" value="100" label="Band width for banded alignment" />
+        <param name="offDiagonal" type="integer" value="100" label="off-diagonal X-dropoff" />
+        <param name="internalSeeds" type="float" value="1.5" label="look for internal seeds inside a seed longer than {-k} * FLOAT [1.5]" />
+        <param name="seedsOccurrence" type="integer" value="10000" label="skip seeds with more than INT occurrences" />
+        <param name="mateRescue" type="boolean" truevalue="True" falsevalue="False" checked="False" label="skip seeds with more than INT occurrences" />
+        <param name="skipPairing" type="boolean" truevalue="True" falsevalue="False" checked="False" label="skpe pairing, mate rescue performed unless -S also in use" />
+        <param name="seqMatch" type="integer" value="1" label="score of a sequence match" />
+        <param name="mismatch" type="integer" value="4" label="penalty for a mismatch" />
+        <param name="gapOpen" type="integer" value="6" label="gap open penalty" />
+        <param name="gapExtension" type="text" value="None" label="gap extension penalty; a gap of size k cost {-O} + {-E}*k [1]" />
+        <param name="clipping" type="integer" value="5" label="penalty for clipping" />
+        <param name="unpairedReadpair" type="integer" value="17" label="penalty for an unpaired read pair" />
+        <param name="interPairEnd" type="boolean" truevalue="True" falsevalue="False" checked="False" label="first query file consists of interleaved paired-end sequences" />
+        <param name="minScore" type="integer" value="30" label="minimum score to output" />
+        <param name="mark" type="boolean" truevalue="True" falsevalue="False" checked="False" label="mark shorter split hits as secondary (for Picard/GATK compatibility)" />
+
+        <conditional name="readGroup">
+          <param name="specReadGroup" type="select" label="Specify the read group for this file? (samse/sampe -r)">
+            <option value="yes">Yes</option>
+            <option value="no" selected="True">No</option>
+          </param>
+          <when value="yes">
+            <param name="rgid" type="text" size="25" label="[Essential]Read group identifier (ID). Each @RG line must have a unique ID. The value of ID is used in the RG 
+tags of alignment records. Must be unique among all read groups in header section." help="Required if RG specified. Read group 
+IDs may be modified when merging SAM files in order to handle collisions." />
+            <param name="rgpl" type="text" size="25" label="[Essential]Platform/technology used to produce the reads (PL)" help="Required if RG specified. Valid values : CAPILLARY, LS454, ILLUMINA, 
+SOLID, HELICOS, IONTORRENT and PACBIO" />
+            <param name="rglb" type="text" size="25" label="[Essential]Library name (LB)" help="Required if RG specified" />
+            <param name="rgsm" type="text" size="25" label="[Essential]Sample (SM)" help="Required if RG specified. Use pool name where a pool is being sequenced" />
+            <param name="rgpu" type="text" size="25" label="Platform unit (PU)" help="Optional. Unique identifier (e.g. flowcell-barcode.lane for Illumina or slide for SOLiD)" />
+            <param name="rgcn" type="text" size="25" label="Sequencing center that produced the read (CN)" help="Optional" />
+            <param name="rgds" type="text" size="25" label="Description (DS)" help="Optional" />
+            <param name="rgdt" type="text" size="25" label="Date that run was produced (DT)" help="Optional. ISO8601 format date or date/time, like YYYY-MM-DD" />
+            <param name="rgfo" type="text" size="25" label="Flow order (FO). The array of nucleotide bases that correspond to the nucleotides used for each 
+flow of each read." help="Optional. Multi-base flows are encoded in IUPAC format, and non-nucleotide flows by 
+various other characters. Format : /\*|[ACMGRSVTWYHKDBN]+/" />
+            <param name="rgks" type="text" size="25" label="The array of nucleotide bases that correspond to the key sequence of each read (KS)" help="Optional" />
+            <param name="rgpg" type="text" size="25" label="Programs used for processing the read group (PG)" help="Optional" />
+            <param name="rgpi" type="text" size="25" label="Predicted median insert size (PI)" help="Optional" />
+          </when>
+          <when value="no" />
+        </conditional>
+      </when>
+    </conditional>
+    <param name="suppressHeader" type="boolean" truevalue="true" falsevalue="false" checked="False" label="Suppress the header in the output SAM file" help="BWA produces SAM with several lines of header information" />
+  </inputs>
+
+  <outputs>
+    <data format="sam" name="output" label="${tool.name} on ${on_string}: mapped reads">
+      <actions>
+        <conditional name="genomeSource.refGenomeSource">
+          <when value="indexed">
+            <action type="metadata" name="dbkey">
+              <option type="from_data_table" name="bwa_indexes" column="1">
+                <filter type="param_value" column="0" value="#" compare="startswith" keep="False"/>
+                <filter type="param_value" ref="genomeSource.indices" column="0"/>
+              </option>
+            </action>
+          </when>
+          <when value="history">
+            <action type="metadata" name="dbkey">
+              <option type="from_param" name="genomeSource.ownFile" param_attribute="dbkey" />
+            </action>
+          </when>
+        </conditional>
+      </actions>
+    </data>
+  </outputs>
+
+  <tests>
+    <test>
+    </test>
+    <test>
+    </test>
+    <test>
+    </test>
+  </tests>
+  <help>
+**What it does**
+
+BWA is a software package for mapping low-divergent sequences against a large reference genome, such as the human genome. BWA-MEM, which is the latest algorithm, is generally recommended for high-quality queries as it is faster and more accurate. BWA-MEM also has better performance than BWA-backtrack for 70-100bp Illumina reads.
+
+------
+
+**Input formats**
+
+BWA accepts files in either Sanger FASTQ format (galaxy type *fastqsanger*) or Illumina FASTQ format (galaxy type *fastqillumina*). Use the FASTQ Groomer to prepare your files.
+
+------
+
+**License and citation**
+
+This tool uses `BWA`_, which is licensed separately. Please cite |Li2013|_.
+
+.. _BWA: http://bio-bwa.sourceforge.net/
+.. |Li2013| replace:: Li, H. (2013) Aligning sequence reads, clone sequences and assembly contigs with BWA-MEM. arXiv:1303.3997 [q-bio.GN]
+.. _Li2013: http://arxiv.org/abs/1303.3997
+  </help>
+</tool>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/readme.rst	Tue Mar 18 07:49:22 2014 -0400
@@ -0,0 +1,33 @@
+BWA-MEM wrapper
+===============
+
+This is a wrapper for BWA_, a software package for mapping low-divergent sequences against a large reference genome. This wrapper supports only the latest algorithm, BWA-MEM, which is generally recommended for high-quality queries as it is faster and more accurate. BWA-MEM also has better performance than BWA-backtrack for 70-100bp Illumina reads. 
+
+.. _BWA: http://bio-bwa.sourceforge.net/
+
+If you need a wrapper for the old BWA-backtrack algorithm, you may install http://toolshed.g2.bx.psu.edu/view/devteam/bwa_wrappers repository.
+
+Configuration
+-------------
+
+bwa_mem tool may be configured to use more than one CPU core by selecting an appropriate destination for this tool in Galaxy job_conf.xml file (see https://wiki.galaxyproject.org/Admin/Config/Jobs and https://wiki.galaxyproject.org/Admin/Config/Performance/Cluster ).
+
+If you are using Galaxy release_2013.11.04 or later, this tool will automatically use the number of CPU cores allocated by the job runner according to the configuration of the destination selected for this tool.
+
+If instead you are using an older Galaxy release, you should also add a line
+
+  GALAXY_SLOTS=N; export GALAXY_SLOTS
+
+(where N is the number of CPU cores allocated by the job runner for this tool) to the file
+
+  <tool_dependencies_dir>/bwa/0.7.7/crs4/bwa_mem/<hash_string>/env.sh
+
+Version history
+---------------
+
+- Release 0: Initial release in the Tool Shed. This is a fork of http://toolshed.g2.bx.psu.edu/view/yufei-luo/bwa_0_7_5 repository with the following changes: Remove .loc file, only .loc.sample should be included. Fix bwa_index.loc.sample file to contain only comments. Add suppressHeader param as in bwa_wrappers. Use $GALAXY_SLOTS environment variable when available. Add <version_command> and <help>. Remove unused import. Fix spacing and typos. Use new recommended citation. Add tool_dependencies.xml . Rename to bwa_mem. Remove definitively colorspace support. Use optparse instead of argparse since Galaxy still supports Python 2.6 .
+
+Development
+-----------
+
+Development is hosted at https://bitbucket.org/nsoranzo/bwa_mem . Contributions and bug reports are very welcome!
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tool-data/bwa_index.loc.sample	Tue Mar 18 07:49:22 2014 -0400
@@ -0,0 +1,38 @@
+#This is a sample file distributed with Galaxy that enables tools
+#to use a directory of BWA indexed sequences data files. You will need
+#to create these data files and then create a bwa_index.loc file
+#similar to this one (store it in this directory) that points to
+#the directories in which those files are stored. The bwa_index.loc
+#file has this format (longer white space characters are TAB characters):
+#
+#<unique_build_id>   <dbkey>   <display_name>   <file_path>
+#
+#So, for example, if you had phiX indexed stored in 
+#/depot/data2/galaxy/phiX/base/, 
+#then the bwa_index.loc entry would look like this:
+#
+#phiX174   phiX   phiX Pretty   /depot/data2/galaxy/phiX/base/phiX.fa
+#
+#and your /depot/data2/galaxy/phiX/base/ directory
+#would contain phiX.fa.* files:
+#
+#-rw-r--r--  1 james    universe 830134 2005-09-13 10:12 phiX.fa.amb
+#-rw-r--r--  1 james    universe 527388 2005-09-13 10:12 phiX.fa.ann
+#-rw-r--r--  1 james    universe 269808 2005-09-13 10:12 phiX.fa.bwt
+#...etc...
+#
+#Your bwa_index.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:
+#
+#phiX174				phiX	phiX174			/depot/data2/galaxy/phiX/base/phiX.fa
+#hg18canon				hg18	hg18 Canonical	/depot/data2/galaxy/hg18/base/hg18canon.fa
+#hg18full				hg18	hg18 Full		/depot/data2/galaxy/hg18/base/hg18full.fa
+#/orig/path/hg19.fa		hg19	hg19			/depot/data2/galaxy/hg19/base/hg19.fa
+#...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	Tue Mar 18 07:49:22 2014 -0400
@@ -0,0 +1,7 @@
+<tables>
+    <!-- Locations of indexes in the BWA mapper format -->
+    <table name="bwa_indexes" comment_char="#">
+        <columns>value, dbkey, name, path</columns>
+        <file path="tool-data/bwa_index.loc" />
+    </table>
+</tables>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tool_dependencies.xml	Tue Mar 18 07:49:22 2014 -0400
@@ -0,0 +1,6 @@
+<?xml version="1.0"?>
+<tool_dependency>
+    <package name="bwa" version="0.7.7">
+        <repository changeset_revision="def70e393020" name="package_bwa_0_7_7" owner="iuc" toolshed="http://toolshed.g2.bx.psu.edu" />
+    </package>
+</tool_dependency>