<tool id="anndata_import" name="Import Anndata" version="@TOOL_VERSION@+galaxy2" profile="@PROFILE@">
    <description>from different formats</description>
    <macros>
        <import>macros.xml</import>
        <xml name="params_10x">
            <param name="barcodes" type="data" format="tabular,txt" label="Barcodes (whitelist) file with one barcode per line"/>
            <param name="var_names" type="select" label="Variables index">
                <option value="gene_symbols">gene_symbols</option>
                <option value="gene_ids">gene_ids</option>
            </param>
            <param name="make_unique" type="boolean" truevalue="True" falsevalue="False" checked="true" label="Make the variable index unique by appending '-1', '-2'?"/>
            <param name="gex_only" type="boolean" truevalue="True" falsevalue="False" checked="true" label="Keep only 'Gene Expression' data and ignore other feature types?"/>
        </xml>
    </macros>
    <expand macro="bio_tools"/>
    <expand macro="requirements">
        <requirement type="package" version="1.10.2">scanpy</requirement>
    </expand>
    <expand macro="version_command"/>
    <command detect_errors="exit_code"><![CDATA[
#if $in.adata_format == 'mtx'
    mkdir mtx
    #if $in.tenx.use == 'legacy_10x'
        && cp '$in.matrix' 'mtx/matrix.mtx'
        && cp '$in.tenx.genes' 'mtx/genes.tsv'
        && cp '$in.tenx.barcodes' 'mtx/barcodes.tsv'
    #else if $in.tenx.use == 'v3_10x'
        && cp '$in.matrix' 'mtx/matrix.mtx'
        && gzip 'mtx/matrix.mtx'
        && cp '$in.tenx.features' 'mtx/features.tsv'
        && gzip 'mtx/features.tsv'
        && cp '$in.tenx.barcodes' 'mtx/barcodes.tsv'
        && gzip 'mtx/barcodes.tsv'
    #end if
    &&
#else if $in.adata_format == 'umi_tools'
    ## avoid gzipping in the inputdir
    gzip -c '$in.input' > umi_tools_input.gz
    &&
#end if

@CMD@
      ]]></command>
    <configfiles>
        <configfile name="script_file"><![CDATA[
@CMD_imports@
#if $in.adata_format == 'loom'
adata = ad.read_loom(
    '$in.input',
    sparse=$in.sparse,
    cleanup=$in.cleanup,
    X_name='$in.x_name',
    obs_names='$in.obs_names',
    var_names='$in.var_names')

#else if $in.adata_format == 'tabular'
    #set delimiter=$in.input.metadata.delimiter
    #if $delimiter != ','
        #set delimiter='\\t'
    #end if
adata = ad.read_csv(
    '$in.input',
    delimiter='$delimiter',
    first_column_names=$in.first_column_names)
## convert into sparse matrix. Dense matrix needs large disk space and higher access times
from scipy.sparse import csr_matrix
adata.X = csr_matrix(adata.X)

#else if $in.adata_format == '10x_h5'
import scanpy as sc
adata = sc.read_10x_h5('$in.input')

#else if $in.adata_format == 'mtx'
    #if $in.tenx.use == 'no'
adata = ad.read_mtx(filename='$in.matrix')
    #else
import scanpy as sc
adata = sc.read_10x_mtx(
    'mtx',
    var_names='$in.tenx.var_names',
    make_unique=$in.tenx.make_unique,
    cache=False,
    gex_only=$in.tenx.gex_only)
    #end if

#else if $in.adata_format == 'umi_tools'
adata = ad.read_umi_tools('umi_tools_input.gz')

#else if $in.adata_format == 'custom'
import pandas as pd

adata = ad.read_mtx(filename='$in.mtx')
adata = adata.transpose().copy()

obs = pd.read_csv('$in.obs', sep='\t', index_col=0)
var = pd.read_csv('$in.var', sep='\t', index_col=0)

if adata.shape[0] != obs.shape[0]:
    raise ValueError(f"Mismatch: adata has {adata.shape[0]} cells, but obs has {obs.shape[0]} rows.")

if adata.shape[1] != var.shape[0]:
    raise ValueError(f"Mismatch: adata has {adata.shape[1]} genes, but var has {var.shape[0]} rows.")


adata.obs = obs
adata.var = var

#end if
adata.write('anndata.h5ad', compression='gzip')
print(adata)
]]></configfile>
    </configfiles>
    <inputs>
        <conditional name="in">
            <param name="adata_format" type="select" label="Create annadata from">
                <option value="loom">Loom data</option>
                <option value="tabular">Tabular, CSV, TSV</option>
                <option value="10x_h5">H5 format from Cell ranger or not</option>
                <option value="mtx">Matrix Market (mtx), from Cell ranger or not</option>
                <option value="umi_tools">UMI tools count matrix</option>
                <option value="custom">custom (with annotated cells and genes tables)</option>
            </param>
            <when value="loom">
                <param name="input" type="data" format="loom" label="Annotated data matrix"/>
                <param name="sparse" type="boolean" truevalue="True" falsevalue="False" checked="true" label="Is the data matrix to read sparse?"/>
                <param name="cleanup" type="boolean" truevalue="True" falsevalue="False" checked="false" label="Cleanup?"/>
                <param name="x_name" type="text" value="spliced" label="X_name"/>
                <param name="obs_names" type="text" value="CellID" label="obs_names"/>
                <param name="var_names" type="text" value="Gene" label="var_names"/>
            </when>
            <when value="tabular">
                <param name="input" type="data" format="tabular,csv,tsv" label="Annotated data matrix"/>
                <param name="first_column_names" type="boolean" truevalue="True" falsevalue="False" checked="true" label="Does the first column store the row names?"/>
            </when>
            <when value="10x_h5" >
                <param name="input" type="data" format="h5" label="Data matrix"/>
            </when>
            <when value="mtx">
                <param name="matrix" type="data" format="mtx" label="Matrix in Matrix Market format"/>
                <conditional name="tenx">
                    <param name="use" type="select" label="Use 10x Genomics formatted mtx">
                        <option value="no">No</option>
                        <option value="legacy_10x">Output from Cell Ranger v2 or earlier versions</option>
                        <option value="v3_10x">Output from Cell Ranger v3 or later versions</option>
                    </param>
                    <when value="no"/>
                    <when value="legacy_10x">
                        <param name="genes" type="data" format="tabular" label="2-column genes file with gene id and gene name"/>
                        <expand macro="params_10x"/>
                    </when>
                    <when value="v3_10x">
                        <param name="features" type="data" format="tabular" label="3-column features file with feature id, feature name and feature type" help="For scRNA-seq, features are genes. The 3rd column typically contains the word 'Gene Expression'"/>
                        <expand macro="params_10x"/>
                    </when>
                </conditional>
            </when>
            <when value="umi_tools">
                <param name="input" type="data" format="tabular" label="condensed count matrix from UMI tools"/>
            </when>
            <when value="custom">
                <param name="mtx" type="data" format="mtx" label="Matrix in Matrix Market format"/>
                <param name="obs" type="data" format="tabular,tabular.gz" label="Annotated cells (first column will be obs_index)"/>
                <param name="var" type="data" format="tabular,tabular.gz" label="Annotated genes (first column will be var_index)"/>
            </when>
        </conditional>
    </inputs>
    <outputs>
        <data name="anndata" format="h5ad" from_work_dir="anndata.h5ad" label="Anndata import on ${on_string}"/>
    </outputs>
    <tests>
        <test expect_num_outputs="1">
            <conditional name="in">
                <param name="adata_format" value="loom"/>
                <param name="input" ftype="loom" value="krumsiek11.loom" />
                <param name="sparse" value="True"/>
                <param name="cleanup" value="False"/>
                <param name="x_name"  value="spliced"/>
                <param name="obs_names" value="CellID" />
                <param name="var_names" value="Gene"/>
            </conditional>
            <assert_stdout>
                <has_text_matching expression="ad.read_loom"/>
                <has_text_matching expression="sparse=True"/>
                <has_text_matching expression="cleanup=False"/>
                <has_text_matching expression="X_name='spliced'"/>
                <has_text_matching expression="obs_names='CellID'"/>
                <has_text_matching expression="var_names='Gene'"/>
            </assert_stdout>
            <output name="anndata" ftype="h5ad">
                <assert_contents>
                    <has_h5_keys keys="obs/cell_type"/>
                    <has_h5_keys keys="obs/obs_names"/>
                    <has_h5_keys keys="var/var_names"/>
                </assert_contents>
            </output>
        </test>
        <test expect_num_outputs="1">
            <conditional name="in">
                <param name="adata_format" value="tabular"/>
                <param name="input" value="adata.csv"/>
                <param name="first_column_names" value="true"/>
            </conditional>
            <assert_stdout>
                <has_text_matching expression="ad.read_csv"/>
                <has_text_matching expression="delimiter=','"/>
                <has_text_matching expression="first_column_names=True"/>
                <has_text_matching expression="3 × 2"/>
            </assert_stdout>
        </test>
        <test expect_num_outputs="1">
            <conditional name="in">
                <param name="adata_format" value="tabular"/>
                <param name="input" value="adata.tsv"/>
                <param name="first_column_names" value="true"/>
            </conditional>
            <assert_stdout>
                <has_text_matching expression="ad.read_csv"/>
                <has_text_matching expression="delimiter='\\t'"/>
                <has_text_matching expression="first_column_names=True"/>
                <has_text_matching expression="3 × 2"/>
            </assert_stdout>
        </test>
        <test expect_num_outputs="1">
            <conditional name="in">
                <param name="adata_format" value="mtx"/>
                <param name="matrix" value="matrix_10x_v1.2.0.mtx"/>
                <conditional name="tenx">
                    <param name="use" value="no"/>
                </conditional>
            </conditional>
            <assert_stdout>
                <has_text_matching expression="343 × 12"/>
            </assert_stdout>
        </test>
        <test expect_num_outputs="1">
            <conditional name="in">
                <param name="adata_format" value="mtx"/>
                <param name="matrix" value="matrix_10x_v1.2.0.mtx"/>
                <conditional name="tenx">
                    <param name="use" value="legacy_10x"/>
                    <param name="genes" value="genes_10x_v1.2.0.tsv"/>
                    <param name="barcodes" value="barcodes_10x_v1.2.0.tsv"/>
                    <param name="var_names" value="gene_symbols"/>
                    <param name="make_unique" value="true"/>
                    <param name="gex_only" value="true"/>
                </conditional>
            </conditional>
            <assert_stdout>
                <has_text_matching expression="12 × 343"/>
            </assert_stdout>
            <output name="anndata" ftype="h5ad">
                <assert_contents>
                    <has_h5_keys keys="var/gene_ids"/>
                </assert_contents>
            </output>
        </test>
        <test expect_num_outputs="1">
            <conditional name="in">
                <param name="adata_format" value="mtx"/>
                <param name="matrix" value="matrix_10x_v3.0.0.mtx"/>
                <conditional name="tenx">
                    <param name="use" value="v3_10x"/>
                    <param name="features" value="features_10x_v3.0.0.tsv"/>
                    <param name="barcodes" value="barcodes_10x_v3.0.0.tsv"/>
                    <param name="var_names" value="gene_symbols"/>
                    <param name="make_unique" value="true"/>
                    <param name="gex_only" value="true"/>
                </conditional>
            </conditional>
            <assert_stdout>
                <has_text_matching expression="1107 × 507"/>
            </assert_stdout>
            <output name="anndata" ftype="h5ad">
                <assert_contents>
                    <has_h5_keys keys="var/gene_ids"/>
                    <has_h5_keys keys="var/feature_types"/>
                </assert_contents>
            </output>
        </test>
        <test expect_num_outputs="1">
            <conditional name="in">
                <param name="adata_format" value="umi_tools"/>
                <param name="input" value="umi_tools.tsv"/>
            </conditional>
            <assert_stdout>
                <has_text_matching expression="2 × 13"/>
            </assert_stdout>
        </test>
        <test expect_num_outputs="1"><!-- 10x h5 test -->
            <conditional name="in">
                <param name="adata_format" value="10x_h5"/>
                <param name="input" value="dropletutils_input.h5"/>
            </conditional>
            <output name="anndata">
                <assert_contents>
                    <has_text text="HDF"/>
                    <has_text text="ENSG00000258728" />
                    <has_text text="GCGAGAAAGTTGTAGA" />
                </assert_contents>
            </output>
        </test>
        <test expect_num_outputs="1">
            <conditional name="in">
                <param name="adata_format" value="custom"/>
                <param name="mtx" value="trimmed_matrix.mtx"/>
                <param name="obs" value="trimmed_barcode.tabular"/>
                <param name="var" value="trimmed_gene.tabular"/>
            </conditional>
            <assert_stdout>
                <has_text_matching expression="1000 × 500"/>
            </assert_stdout>
            <output name="anndata" ftype="h5ad">
                <assert_contents>
                    <has_h5_keys keys="var/symbol,var/mito"/>
                </assert_contents>
            </output>
        </test>
    </tests>
    <help><![CDATA[

**What it does**

This tool creates an AnnData from several input types:

- Loom (`read_loom method <https://anndata.readthedocs.io/en/latest/generated/anndata.io.read_loom.html>`__)
- Tabular (`read_csv method <https://anndata.readthedocs.io/en/latest/generated/anndata.io.read_csv.html>`__)
- Matrix Market (mtx), from Cell ranger or not (`read_mtx method <https://anndata.readthedocs.io/en/latest/generated/anndata.io.read_mtx.html>`__)
- UMI tools (`read_umi_tools method <https://anndata.readthedocs.io/en/latest/generated/anndata.io.read_umi_tools.html>`__)

@HELP@
    ]]></help>
    <expand macro="citations"/>
</tool>
