<tool id="qiime_split_libraries" name="Split libraries" version="@WRAPPER_VERSION@.0">
    <description>according to barcodes specified in mapping file</description>
    <macros>
        <import>macros.xml</import>
    </macros>
    <expand macro="requirements"/>
    <version_command>split_libraries.py --version</version_command>
    <command detect_errors="aggressive"><![CDATA[
        split_libraries.py
            --map '$map'
            -o split_libraries
            --fasta '$fasta'

            #if str($add_qual.add_qual_test) == 'true'
                --qual '$add_qual.qual'
                --min_qual_score '$add_qual.min_qual_score'
                --qual_score_window '$add_qual.qual_score_window'
                $add_qual.discard_bad_windows
                $add_qual.record_qual_scores
            #end if

            --min_seq_length '$min_seq_length'
            --max_seq_length '$max_seq_length'
            $trim_seq_length
            $keep_primer
            $keep_barcode
            --max_ambig '$max_ambig'
            --max_homopolymer '$max_homopolymer'
            --max_primer_mismatch '$max_primer_mismatch'
            #if str( $barcode_selector.barcode_type ) != "custom_length"
                --barcode_type '$barcode_selector.barcode_type'
            #else
                --barcode_type '$barcode_selector.barcode_length'
            #end if
            --max_barcode_errors '$max_barcode_errors'
            --start_numbering_at '$start_numbering_at'
            $retain_unassigned_reads
            $disable_bc_correction
            $disable_primers
            $reverse_primers_test.reverse_primers
            #if str($reverse_primers_test.reverse_primers) == '--reverse_primers':
                --reverse_primer_mismatches '$reverse_primers_test.reverse_primer_mismatches'
            #end if
            #if str($median_length_filtering):
                --median_length_filtering '$median_length_filtering'
            #end if
            #if str($added_demultiplex_field):
                --added_demultiplex_field '$added_demultiplex_field'
            #end if
    ]]></command>
    <inputs>
        <param argument="--map" label="Metadata mapping filepath" type="data" format="tabular,txt,tsv,csv" help="The file must contain header line indicating SampleID in the first column and BarcodeSequence in the second, LinkerPrimerSequence in the third. It is recommended to check the mapping file using the dedicated file"/>
        <param argument="--fasta" type="data" format="fasta" label="Input fasta files" multiple="True"/>
        <conditional name="add_qual">
            <param name="add_qual_test" type="select" label="Add quality files">
                <option value="true">Yes</option>
                <option value="false" selected="true">No</option>
            </param>
            <when value="true">
                <param argument="--qual" type="data" format="qual,qual454,qualillumina,qualsolexa,qualsolid" label="Input quality files (optional)" multiple="True"/>
                <param argument="--min_qual_score" type="integer" value="25" label="Minimum average quality score allowed in read"/>
                <param argument="--qual_score_window" type="integer" value="0" label="Size of the sliding window" help="If the average score of a continuous set of w nucleotides falls below the threshold, the sequence is discarded. A good value would be 50. 0 (zero) means no filtering. Must pass a .qual file (see -q parameter) if this functionality is enabled. Default behavior for this function is to truncate the sequence at the beginning of the poor quality window, and test for minimal length (-l parameter) of the resulting sequence"/>
                <param argument="--discard_bad_windows" type="boolean" label="Discard any sequences where a bad window is found?" truevalue="--discard_bad_windows" falsevalue="" checked="False" help="It will work if the sliding window length is bigger than 0"/>
                <param argument="--record_qual_scores" type="boolean" label="Record quality scores for all sequences that are recorded?" truevalue="--record_qual_scores" falsevalue="" checked="False" help="If this option is enabled, a file named seqs_filtered.qual will be created in the output directory, and will contain the same sequence IDs in the seqs.fna file and sequence quality scores matching the bases present in the seqs.fna file"/>
            </when>
            <when value="false"/>
        </conditional>
        <param argument="--min_seq_length" type="integer" value="200" label="Minimum sequence length"/>
        <param argument="--max_seq_length" type="integer" value="1000" label="Maximum sequence length"/>
        <param argument="--trim_seq_length" type="boolean" label="Compute sequence lengths after trimming and barcodes?" truevalue="--trim_seq_length" falsevalue="" checked="False"/>
        <param argument="--keep_primer" type="boolean" label="Remove primer from sequences?" truevalue="" falsevalue="--keep_primer" checked="True"/>
        <param argument="--keep_barcode" type="boolean" label="Remove barcode from sequences?" truevalue="" falsevalue="--keep_barcode" checked="True"/>
        <param argument="--max_ambig" type="integer" value="6" label="Maximum number of ambiguous bases"/>
        <param argument="--max_homopolymer" type="integer" value="6" label="Maximum length of homopolymer run"/>
        <param argument="--max_primer_mismatch" type="integer" value="0" label="Maximum number of primer mismatch"/>
        <conditional name="barcode_selector">
            <param argument="--barcode_type" type="select" label="Type of barcode">
                <option value="hamming_8">hamming_8</option>
                <option value="golay_12" selected="true">golay_12</option>
                <option value="variable_length">variable_length (disable any barcode correction)</option>
                <option value="custom_length">Custom length</option>
            </param>
            <when value="hamming_8"/>
            <when value="golay_12"/>
            <when value="variable_length"/>
            <when value="custom_length">
                <param name="barcode_length" type="integer" value="4" label="Barcode length"/>
            </when>
        </conditional>
        <param argument="--max_barcode_errors" type="float" value="1.5" label="Maximum number of errors in barcode"/>
        <param argument="--start_numbering_at" type="integer" value="1" label="Sequence id to use for the first sequence"/>
        <param argument="--retain_unassigned_reads" type="boolean" label="Retain sequences with are Unassigned in the output sequence file?" truevalue="--retain_unassigned_reads" falsevalue="" checked="False"/>
        <param argument="--disable_bc_correction" type="boolean" label="Disable attempts to find nearest corrected barcode?" truevalue="--disable_bc_correction" falsevalue="" checked="False" help="It can improve performance"/>
        <param argument="--disable_primers" type="boolean" label="Disable primer usage when demultiplexing?" truevalue="--disable_primers" falsevalue="" checked="False" help="It should be enabled for unusual circumstances, such as analyzing Sanger sequence data generated with different primers"/>
        <conditional name="reverse_primers_test">
            <param argument="--reverse_primers" type="select" label="Enable removal of the reverse primer and any subsequence sequence from the end of each read?">
                <option value="--reverse_primers">Yes</option>
                <option value="" selected="true">No</option>
            </param>
            <when value=""/>
            <when value="--reverse_primers">
                <param argument="--reverse_primer_mismatches" type="integer" value="0" label="Number of allowed mismatches for reverse primers"/>
            </when>
        </conditional>
        <param argument="--median_length_filtering" type="integer" label="Median length filtering (optional)" help="It disables minimum and maximum sequence length filtering, and instead calculates the median sequence length and filters the sequences based upon the number of median absolute deviations specified by this parameter. Any sequences with lengths outside the number of deviations will be removed" optional="True"/>
        <param argument="--added_demultiplex_field" type="text" label="Field to use in the mapping file as additional demultiplexing (optional)" help="It can be used with or without barcodes.  All combinations of barcodes/primers and these fields must be unique. The fields must contain values that can be parsed from the fasta labels such as 'plate=R_2008_12_09'. In this case, 'plate' would be the column header and 'R_2008_12_09' would be the field data (minus quotes) in the mapping file. To use the run prefix from the fasta label, such as 'FLP3FBN01ELBSX', where 'FLP3FBN01' is generated from the run ID, use 'run_prefix' and set the run prefix to be used as the data under the column header 'run_prefix'" optional="True"/>
        <param argument="--truncate_ambi_bases" type="boolean" label="Enable to truncate at the first N character encountered in the sequences?" truevalue="--truncate_ambi_bases" falsevalue="" checked="False" help="This will disable testing for ambiguous bases"/>
    </inputs>
    <outputs>
        <data name="sequences" format="fasta" from_work_dir="split_libraries/*.fna" label="${tool.name} on ${on_string}: sequences"/>
        <data name="log" format="txt" from_work_dir="split_libraries/split_library_log.txt" label="${tool.name} on ${on_string}: log"/>
        <data name="histograms" format="txt" from_work_dir="split_libraries/histograms.txt" label="${tool.name} on ${on_string}: histograms"/>
        <data name="quality" format="qual,qual454,qualillumina,qualsolexa,qualsolid" from_work_dir="split_libraries/*.qual" label="${tool.name} on ${on_string}: quality">
            <filter>add_qual['record_qual_scores']!=""</filter>
        </data>
    </outputs>
    <tests>
        <test>
            <param name="map" value="split_libraries/mapping_file.txt"/>
            <param name="fasta" value="split_libraries/reads_1.fna,split_libraries/reads_2.fna"/>
            <param name="add_qual_test" value="true"/>
            <param name="qual" value="split_libraries/reads_1.qual,split_libraries/reads_2.qual"/>
            <param name="min_qual_score" value="25"/>
            <param name="qual_score_window" value="0"/>
            <param name="discard_bad_windows" value=""/>
            <param name="record_qual_scores" value="--record_qual_scores"/>
            <param name="min_seq_length" value="200" />
            <param name="max_seq_length" value="1000" />
            <param name="trim_seq_length" value=""/>
            <param name="keep_primer" value=""/>
            <param name="keep_barcode" value=""/>
            <param name="max_ambig" value="6" />
            <param name="max_homopolymer" value="6"/>
            <param name="max_primer_mismatch" value="0"/>
            <param name="barcode_type" value="golay_12"/>
            <param name="barcode_length" value=""/>
            <param name="max_barcode_errors" value="1.5"/>
            <param name="start_numbering_at" value="1"/>
            <param name="retain_unassigned_reads" value=""/>
            <param name="disable_bc_correction" value=""/>
            <param name="disable_primers" value=""/>
            <param name="reverse_primers" value=""/>
            <param name="reverse_primer_mismatches" value=""/>
            <param name="truncate_ambi_bases" value=""/>
            <output name="sequences">
                <assert_contents>
                    <has_text text="PC.634_1" />
                    <has_text text="bc_diffs" />
                </assert_contents>
            </output>
            <output name="log" file="split_libraries/split_library_log"/>
            <output name="histograms" file="split_libraries/histograms.txt"/>
            <output name="quality">
                <assert_contents>
                    <has_text text="PC.634_1" />
                    <has_text text="bc_diffs" />
                </assert_contents>
            </output>
        </test>
    </tests>
    <help><![CDATA[
**What it does**

This tool splits libraries according to barcodes specified in mapping file.

Since newer sequencing technologies provide many reads per run (e.g. the 454 GS FLX Titanium series can produce 400-600 million base pairs with 400-500 base pair read lengths) researchers are now finding it useful to combine multiple samples into a single 454 run. This multiplexing is achieved through the application of a pyrosequencing-tailored nucleotide barcode design (described in (Parameswaran et al., 2007)). By assigning individual, unique sample specific barcodes, multiple sequencing runs may be performed in parallel and the resulting reads can later be binned according to sample. The script %prog performs this task, in addition to several quality filtering steps including user defined cut-offs for: sequence lengths; end-trimming; minimum quality score. To summarize, by using the fasta, mapping, and quality files, the program %prog will parse sequences that meet user defined quality thresholds and then rename each read with the appropriate Sample ID, thus formatting the sequence data for downstream analysis. If a combination of different sequencing technologies are used in any particular study, %prog can be used to perform the quality-filtering for each library individually and the output may then be combined.

Sequences from samples that are not found in the mapping file (no corresponding barcode) and sequences without the correct primer sequence will be excluded. Additional scripts can be used to exclude sequences that match a given reference sequence (e.g. the human genome; exclude_seqs_by_blast.py) and/or sequences that are flagged as chimeras (identify_chimeric_seqs.py).

More information about this tool is available on
`QIIME documentation <http://qiime.org/scripts/split_libraries.html>`_.
    ]]></help>
    <citations>
        <expand macro="citations"/>
    </citations>
</tool>
