Repository 'squidpy_spatial'
hg clone https://toolshed.g2.bx.psu.edu/repos/goeckslab/squidpy_spatial

Changeset 0:5b17a47d1ade (2024-07-11)
Next changeset 1:2a5036c29889 (2024-10-31)
Commit message:
planemo upload for repository https://github.com/goeckslab/tools-mti/tree/main/tools/squidpy commit 721eaced787aa3b04d96ad91f6b4540f26b23949
added:
main_macros.xml
squidpy_scatter.py
squidpy_spatial.py
squidpy_spatial.xml
test-data/imc.h5ad
test-data/imc_centrality_scores.png
test-data/imc_co_occurrence.png
test-data/imc_interaction_matrix.png
test-data/imc_kmeans.h5ad
test-data/imc_nhood_enrichment.png
test-data/imc_ripley.png
test-data/imc_sn.h5ad
test-data/scatter_image.png
b
diff -r 000000000000 -r 5b17a47d1ade main_macros.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main_macros.xml Thu Jul 11 22:22:41 2024 +0000
[
@@ -0,0 +1,64 @@
+<macros>
+    <token name="@TOOL_VERSION@">1.5.0</token>
+    <token name="@PROFILE@">20.01</token>
+    <token name="@VERSION_SUFFIX@">1</token> 
+
+    <xml name="macro_stdio">
+        <stdio>
+            <exit_code range="1:" level="fatal" description="Error occurred. Please check Tool Standard Error" />
+        </stdio>
+    </xml>
+
+    <xml name="squidpy_requirements">
+        <requirements>
+            <requirement type="package" version="@TOOL_VERSION@">squidpy</requirement>
+        </requirements>
+    </xml>
+
+    <xml name="citations">
+        <citations>
+            <citation type="doi">10.1038/s41592-021-01358-2</citation>
+        </citations>
+    </xml>
+
+    <xml name="squidpy_spatial_options">
+        <section name="options" title="Advanced Graph Options" expanded="false">
+            <yield/>
+             <!-- <param argument="copy" type="hidden" value="false" help="If True, return the result, otherwise save it to the adata object." /> -->
+        </section>
+    </xml>
+
+    <xml name="squidpy_plotting_options">
+        <section name="plotting_options" title="Plotting Options" expanded="false">
+            <yield />
+            <!-- <param argument="legend_kwargs" type="text" value="" optional="true" label="Keyword arguments for matplotlib.pyplot.legend()" /> -->
+            <param argument="figsize" type="text" value="" optional="true" label="Size of the figure in inches" help="Optional. e.g.: (12, 12)." />
+            <param argument="dpi" type="integer" value="" optional="true" label="Dots per inch" help="Optional" />
+        </section>
+    </xml>
+
+    <xml name="squidpy_plotting_option_palette">
+        <param argument="palette" type="text" value="" optional="true" label="Categorical colormap for the clusters" help="Comma delimited for multiple. If None, use anndata.AnnData.uns ['{cluster_key}_colors'], if available." />
+    </xml>
+
+    <xml name="squidpy_plotting_options_more">
+        <expand macro="squidpy_plotting_options">
+            <yield />
+            <param argument="annotate" type="boolean" checked="false" label="Whether to annotate the cells of the heatmap?" />
+            <param argument="method" type="select" label="The linkage method to be used for dendrogram/clustering" help="see scipy.cluster.hierarchy.linkage().">
+                <option value="none" selected="true">None</option>
+                <option value="single">single</option>
+                <option value="complete">complete</option>
+                <option value="average">average</option>
+                <option value="weighted">weighted</option>
+                <option value="centroid">centroid</option>
+                <option value="median">median</option>
+                <option value="ward">ward</option>
+            </param>
+            <param argument="title" type="text" value="" optional="true" label="The title of the plot" help="Optional." />
+            <param argument="cmap" type="text" value="viridis" label="Continuous colormap to use" help="Refer to `matplotlib.pyplot.cmap`." />
+            <!-- <param argument="cbar_kwargs" type="text" value="" label="Keyword arguments for matplotlib.figure.Figure.colorbar()" /> -->
+            <expand macro="squidpy_plotting_option_palette" />
+        </expand>
+    </xml>
+</macros>
b
diff -r 000000000000 -r 5b17a47d1ade squidpy_scatter.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/squidpy_scatter.py Thu Jul 11 22:22:41 2024 +0000
[
@@ -0,0 +1,87 @@
+import argparse
+import ast
+import json
+import warnings
+
+import squidpy as sq
+from anndata import read_h5ad
+
+
+def main(inputs, output_plot):
+
+    """
+    inputs : str
+        File path to galaxy tool JSON inputs config file
+    output_plot: str
+        File path to save the plotting image
+    """
+    warnings.simplefilter('ignore')
+
+    # read inputs JSON
+    with open(inputs, 'r') as param_handler:
+        params = json.load(param_handler)
+
+    # collapse param dict hierarchy, parse inputs
+    plot_opts = params.pop('plot_opts')
+    legend_opts = params.pop('legend_opts')
+    aes_opts = params.pop('aesthetic_opts')
+    options = {**params, **plot_opts, **legend_opts, **aes_opts}
+
+    # read input anndata file
+    adata_fh = options.pop('anndata')
+    adata = read_h5ad(adata_fh)
+
+    # ensure spatial coords in anndata.obsm['spatial']
+    # if not, populate with user provided X/Y coord column names
+    x, y = options.pop('x_coord'), options.pop('y_coord')
+    if 'spatial' not in adata.obsm:
+        try:
+            adata.obsm['spatial'] = adata.obs[[x, y]].values
+        except Exception as e:
+            print(e)
+
+    # scan thru tool params,
+    # replace None values, and reformat specific parameters
+    for k, v in options.items():
+        if not isinstance(v, str):
+            continue
+
+        if v in ('', 'none'):
+            options[k] = None
+            continue
+
+        if k == 'groups':
+            options[k] = [e.strip() for e in v.split(',')]
+
+        elif k == 'crop_coord':
+            # split str on commas into tuple of coords
+            # then nest in list (expected by squidpy function)
+            options[k] = [tuple([int(e.strip()) for e in v.split(',')])]
+
+        elif k == 'figsize':
+            options[k] = ast.literal_eval(v)
+
+    # not exposing this parameter for now. Only useful to change for ST data
+    # and can otherwise just be problematic.
+    # Explicitly setting to None is necessary to avoid an error
+    options['shape'] = None
+
+    # call squidpy spatial scatter function, unpack tool params
+    sq.pl.spatial_scatter(
+        adata=adata,
+        save='image.png',
+        **options
+    )
+
+
+if __name__ == '__main__':
+
+    aparser = argparse.ArgumentParser()
+    aparser.add_argument(
+        "-i", "--inputs", dest="inputs", required=True)
+    aparser.add_argument(
+        "-p", "--output_plot", dest="output_plot", required=False)
+
+    args = aparser.parse_args()
+
+    main(args.inputs, args.output_plot)
b
diff -r 000000000000 -r 5b17a47d1ade squidpy_spatial.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/squidpy_spatial.py Thu Jul 11 22:22:41 2024 +0000
[
@@ -0,0 +1,105 @@
+import argparse
+import ast
+import json
+import warnings
+
+import pandas as pd
+import squidpy as sq
+from anndata import read_h5ad
+
+
+def main(inputs, anndata, output, output_plot):
+    """
+    Parameter
+    ---------
+    inputs : str
+        File path to galaxy tool parameter.
+    anndata : str
+        File path to anndata containing phenotyping info.
+    output : str
+        File path to output.
+    output_plot: str or None
+        File path to save the plotting image.
+    """
+    warnings.simplefilter('ignore')
+
+    with open(inputs, 'r') as param_handler:
+        params = json.load(param_handler)
+
+    adata = read_h5ad(anndata)
+
+    if 'spatial' not in adata.obsm:
+        try:
+            adata.obsm['spatial'] = adata.obs[['X_centroid', 'Y_centroid']].values
+        except Exception as e:
+            print(e)
+
+    tool = params['analyses']['selected_tool']
+    tool_func = getattr(sq.gr, tool)
+
+    options = params['analyses']['options']
+
+    for k, v in options.items():
+        if not isinstance(v, str):
+            continue
+
+        if v in ('', 'none'):
+            options[k] = None
+            continue
+
+        if k == 'genes':    # for spatial_autocorr and sepal
+            options[k] = [e.strip() for e in v.split(',')]
+        elif k == 'radius':    # for spatial_neighbors
+            options[k] = ast.literal_eval(v)
+        elif k == 'interactions':    # for ligrec
+            options[k] = pd.read_csv(v, sep="\t")
+        elif k == 'max_neighs':
+            options[k] = int(v)      # for sepal
+
+    cluster_key = params['analyses'].get('cluster_key')
+    if cluster_key:
+        tool_func(adata, cluster_key, **options)
+    else:
+        tool_func(adata, **options)
+
+    if output_plot:
+        plotting_options = params['analyses']['plotting_options']
+        for k, v in plotting_options.items():
+            if not isinstance(v, str):
+                continue
+
+            if v in ('', 'none'):
+                plotting_options[k] = None
+                continue
+
+            if k == 'figsize':
+                options[k] = ast.literal_eval(v)
+            elif k in ('palette', 'score', 'source_groups', 'target_groups'):
+                options[k] = [e.strip() for e in v.split(',')]
+            elif k == 'means_range':        # ligrec
+                v = v.strip()
+                if v[0] == '(':
+                    v = v[1:]
+                if v[-1] == ')':
+                    v = v[:-1]
+                options[k] = tuple([float(e.strip()) for e in v.split(',', 1)])
+
+        plotting_func = getattr(sq.pl, tool)
+        if cluster_key:
+            plotting_func(adata, cluster_key, save=output_plot, **plotting_options)
+        else:       # TODO Remove this, since all plottings need cluster key
+            plotting_func(adata, save=output_plot, **plotting_options)
+
+    adata.write(output)
+
+
+if __name__ == '__main__':
+    aparser = argparse.ArgumentParser()
+    aparser.add_argument("-i", "--inputs", dest="inputs", required=True)
+    aparser.add_argument("-e", "--output", dest="output", required=True)
+    aparser.add_argument("-a", "--anndata", dest="anndata", required=True)
+    aparser.add_argument("-p", "--output_plot", dest="output_plot", required=False)
+
+    args = aparser.parse_args()
+
+    main(args.inputs, args.anndata, args.output, args.output_plot)
b
diff -r 000000000000 -r 5b17a47d1ade squidpy_spatial.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/squidpy_spatial.xml Thu Jul 11 22:22:41 2024 +0000
[
b'@@ -0,0 +1,338 @@\n+<tool id="squidpy_spatial" name="Analyze and visualize spatial multi-omics data" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="@PROFILE@">\n+    <description>with Squidpy</description>\n+    <macros>\n+        <import>main_macros.xml</import>\n+    </macros>\n+    <edam_operations>\n+        <edam_operation>operation_3443</edam_operation>\n+    </edam_operations>\n+    <expand macro="squidpy_requirements"/>\n+    <expand macro="macro_stdio" />\n+    <version_command>echo "@TOOL_VERSION@"</version_command>\n+    <command detect_errors="aggressive">\n+        <![CDATA[\n+        export TQDM_DISABLE=True &&\n+        python \'$__tool_directory__/squidpy_spatial.py\'\n+            --inputs \'$inputs\'\n+            --anndata \'$anndata\'\n+            --output \'$output\'\n+            #if $analyses.selected_tool in [\'nhood_enrichment\', \'centrality_scores\', \'interaction_matrix\', \'ligrec\', \'ripley\', \'co_occurrence\']:\n+                 --output_plot image.png\n+            #end if\n+        ]]>\n+    </command>\n+    <configfiles>\n+        <inputs name="inputs" data_style="paths"/>\n+    </configfiles>\n+    <inputs>\n+        <param name="anndata" type="data" format="h5ad" label="Select the input anndata" />\n+        <conditional name="analyses">\n+            <param name="selected_tool" type="select" label="Select an analysis">\n+                <option value="spatial_neighbors" selected="true">Spatial neighbors -- Create a graph from spatial coordinates</option>\n+                <option value="nhood_enrichment">nhood_enrichment -- Compute neighborhood enrichment by permutation test.</option>\n+                <option value="co_occurrence" >co_occurrence -- Compute co-occurrence probability of clusters</option>\n+                <option value="centrality_scores">centrality_scores -- Compute centrality scores per cluster or cell type</option>\n+                <option value="interaction_matrix">interaction_matrix -- Compute interaction matrix for clusters</option>\n+                <option value="ripley">ripley -- Calculate various Ripley\xe2\x80\x99s statistics for point processes</option>\n+                <option value="ligrec">ligrec -- Perform the permutation test as described in [Efremova et al., 2020]</option>\n+                <option value="spatial_autocorr">spatial_autocorr -- Calculate Global Autocorrelation Statistic (Moran\xe2\x80\x99s I or Geary\xe2\x80\x99s C)</option>\n+                <option value="sepal">sepal -- Identify spatially variable genes with Sepal</option>\n+            </param>\n+            <when value="spatial_neighbors">\n+                <expand macro="squidpy_spatial_options">\n+                    <param argument="spatial_key" type="text" value="spatial" optional="false" label="The Key where spatial coordinates are stored" help="Key in `anndata.AnnData.obsm`." />\n+                    <param argument="coord_type" type="select" label="Type of coordinate system">\n+                        <option value="grid" selected="true">grid</option>\n+                        <option value="generic">generic</option>\n+                        <option value="none">None - use `grid` when `spatial_key` is in `anndata.AnnData.uns with `n_neighs` = 6 (Visium), otherwise `generic`</option>\n+                    </param>\n+                    <param argument="n_neighs" type="integer" value="6" label="Number of neighboring tiles" help="When the `coord_type` is generic, this\'s number of neighborhoods for non-grid data and only used when `delaunay` is False." />\n+                    <param argument="radius" type="text" value="" optional="true" label="Radius" help="Only available when coord_type = \'generic\'. If float, this is the neighborhood radius to compute the graph; if tuple, this is edge range [min(radius), max(radius)] used prune the final graph." />\n+                    <param argument="delaunay" type="boolean" checked="false" optional="true" label="Whether to compute the graph from Delaunay triangulation" help="Only used when coord_type = \'generic\'." />\n+     '..b'al>\n+    </inputs>\n+    <outputs>\n+        <data format="h5ad" name="output" label="Squidpy.gr.${analyses.selected_tool} on ${on_string}" />\n+        <data from_work_dir="figures/image.png" format="png" name="output_plot" label="Squidpy.pl.${analyses.selected_tool} on ${on_string}" >\n+            <filter>analyses[\'selected_tool\'] in [\'nhood_enrichment\', \'centrality_scores\', \'interaction_matrix\', \'ligrec\', \'ripley\', \'co_occurrence\']</filter>\n+        </data>\n+    </outputs>\n+    <tests>\n+        <test expect_num_outputs="1">\n+            <param name="anndata" value="imc.h5ad" ftype="h5ad" />\n+            <param name="selected_tool" value="spatial_neighbors" />\n+            <output name="output">\n+                <assert_contents>\n+                    <has_h5_keys keys="uns/spatial_neighbors" />\n+                </assert_contents>\n+            </output>\n+        </test>\n+        <test expect_num_outputs="2">\n+            <param name="anndata" value="imc_sn.h5ad" ftype="h5ad" />\n+            <param name="selected_tool" value="nhood_enrichment" />\n+            <param name="cluster_key" value="cell type" />\n+            <output name="output">\n+                <assert_contents>\n+                    <has_h5_keys keys="uns/cell type_nhood_enrichment" />\n+                </assert_contents>\n+            </output>\n+            <output name="output_plot" file="imc_nhood_enrichment.png" compare="sim_size" delta="2000" />\n+        </test>\n+        <test expect_num_outputs="2">\n+            <param name="anndata" value="imc_sn.h5ad" ftype="h5ad" />\n+            <param name="selected_tool" value="co_occurrence" />\n+            <param name="cluster_key" value="cell type" />\n+            <output name="output">\n+                <assert_contents>\n+                    <has_h5_keys keys="uns/cell type_co_occurrence" />\n+                </assert_contents>\n+            </output>\n+            <output name="output_plot" file="imc_co_occurrence.png" compare="sim_size" delta="2000" />\n+        </test>\n+        <test expect_num_outputs="2">\n+            <param name="anndata" value="imc_sn.h5ad" ftype="h5ad" />\n+            <param name="selected_tool" value="centrality_scores" />\n+            <param name="cluster_key" value="cell type" />\n+            <output name="output">\n+                <assert_contents>\n+                    <has_h5_keys keys="uns/cell type_centrality_scores" />\n+                </assert_contents>\n+            </output>\n+            <output name="output_plot" file="imc_centrality_scores.png" compare="sim_size" delta="2000" />\n+        </test>\n+        <test expect_num_outputs="2">\n+            <param name="anndata" value="imc_sn.h5ad" ftype="h5ad" />\n+            <param name="selected_tool" value="interaction_matrix" />\n+            <param name="cluster_key" value="cell type" />\n+            <output name="output">\n+                <assert_contents>\n+                    <has_h5_keys keys="uns/cell type_interactions" />\n+                </assert_contents>\n+            </output>\n+            <output name="output_plot" file="imc_interaction_matrix.png" compare="sim_size" delta="2000" />\n+        </test>\n+        <test expect_num_outputs="2">\n+            <param name="anndata" value="imc_sn.h5ad" ftype="h5ad" />\n+            <param name="selected_tool" value="ripley" />\n+            <param name="cluster_key" value="cell type" />\n+            <output name="output">\n+                <assert_contents>\n+                    <has_h5_keys keys="uns/cell type_ripley_F" />\n+                </assert_contents>\n+            </output>\n+            <output name="output_plot" file="imc_ripley.png" compare="sim_size" delta="2000" />\n+        </test>\n+    </tests>\n+    <help>\n+        <![CDATA[\n+**What it does**\n+\n+This tool includes various of single cell spatial analysis utils provided by Squidpy.\n+\n+**Input**\n+\n+*AnnData*\n+\n+**Output**\n+\n+*Anndata*\n+\n+*Plotting (PNG) if applicable*\n+\n+\n+        ]]>\n+    </help>\n+    <expand macro="citations" />\n+</tool>\n'
b
diff -r 000000000000 -r 5b17a47d1ade test-data/imc.h5ad
b
Binary file test-data/imc.h5ad has changed
b
diff -r 000000000000 -r 5b17a47d1ade test-data/imc_centrality_scores.png
b
Binary file test-data/imc_centrality_scores.png has changed
b
diff -r 000000000000 -r 5b17a47d1ade test-data/imc_co_occurrence.png
b
Binary file test-data/imc_co_occurrence.png has changed
b
diff -r 000000000000 -r 5b17a47d1ade test-data/imc_interaction_matrix.png
b
Binary file test-data/imc_interaction_matrix.png has changed
b
diff -r 000000000000 -r 5b17a47d1ade test-data/imc_kmeans.h5ad
b
Binary file test-data/imc_kmeans.h5ad has changed
b
diff -r 000000000000 -r 5b17a47d1ade test-data/imc_nhood_enrichment.png
b
Binary file test-data/imc_nhood_enrichment.png has changed
b
diff -r 000000000000 -r 5b17a47d1ade test-data/imc_ripley.png
b
Binary file test-data/imc_ripley.png has changed
b
diff -r 000000000000 -r 5b17a47d1ade test-data/imc_sn.h5ad
b
Binary file test-data/imc_sn.h5ad has changed
b
diff -r 000000000000 -r 5b17a47d1ade test-data/scatter_image.png
b
Binary file test-data/scatter_image.png has changed