PNG as output of a tool

Hi everybody,
I’m writing the Python code for a tool that taking a matrix as input, it returns an heatmap as result.
When running the tool on Galaxy I get this error:

Traceback (most recent call last):
  File "/Users/andrea/Desktop/galaxy/tools/personal_tools/sys_workflow/sys_heatmap.py", line 16, in <module>
    s.savefig(sys.argv[3])
  File "/Users/andrea/Desktop/galaxy/database/dependencies/_conda/envs/mulled-v1-e8e5c838ec73328e9b82c3fbfb3ba227afa87188721e8f9187093dbc4fdd3cb3/lib/python3.9/site-packages/seaborn/axisgrid.py", line 65, in savefig
    self.figure.savefig(*args, **kwargs)
  File "/Users/andrea/Desktop/galaxy/database/dependencies/_conda/envs/mulled-v1-e8e5c838ec73328e9b82c3fbfb3ba227afa87188721e8f9187093dbc4fdd3cb3/lib/python3.9/site-packages/matplotlib/figure.py", line 3046, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/Users/andrea/Desktop/galaxy/database/dependencies/_conda/envs/mulled-v1-e8e5c838ec73328e9b82c3fbfb3ba227afa87188721e8f9187093dbc4fdd3cb3/lib/python3.9/site-packages/matplotlib/backend_bases.py", line 2259, in print_figure
    canvas = self._get_output_canvas(backend, format)
  File "/Users/andrea/Desktop/galaxy/database/dependencies/_conda/envs/mulled-v1-e8e5c838ec73328e9b82c3fbfb3ba227afa87188721e8f9187093dbc4fdd3cb3/lib/python3.9/site-packages/matplotlib/backend_bases.py", line 2188, in _get_output_canvas
    raise ValueError(
ValueError: Format 'dat' is not supported (supported formats: eps, jpeg, jpg, pdf, pgf, png, ps, raw, rgba, svg, svgz, tif, tiff)

I think that the problem is that Galaxy is passing a dat file as storage for the output.
In the xml file I specified that the output should be a png file but it seems to not be enough, how can I solve this?
Here’s the code:

xml file:

<tool id="sys_heatmap_tool" name="Heatmap " version="0.1.0">
  <description>of phenotype counts</description>
  <requirements>
    <requirement type="package">seaborn</requirement>
	<requirement type="package">pandas</requirement>
  </requirements>
  <command>
	  <![CDATA[python3 "$__tool_directory__/sys_heatmap.py" '$input' '$cluster' '$heatmap_out']]>
  </command>
  <inputs>
          <param name="input" type="data" label="Input file:" help="Enter the matrix with the counts of phenotypes"/>
		  <param name="cluster" type="select" label="Indicate which method use for clustering:">
			  <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>
		  </param>
		  <param name="dist" type="text" label="Distance metric to use for the data:" help="Enter the desired metrics"/>
  </inputs>
  <outputs>
	  <data format="png" name="heatmap_out" file="heatmap.png" label="Pheno count heatmap"/>
  </outputs>
  <help><![CDATA[
	  **What it does**
	  
	  Given a matrix with counts of phenotypes, the tools retrieves the corresponding heatmap with clustering.
	  There user can choose between different methods of clustering:
	  
	  -single
	  
	  -complete
	  
	  -average
	  
	  -weighted
	  
	  -centroid
	  
	  -median
	  
	  More info about those metods can be found at: https://docs.scipy.org/doc/scipy/reference/generated/scipy.cluster.hierarchy.linkage.html
	  
	  The default distance used for clusters is the euclidean. The user can enter a different distance metric from here: https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.pdist.html?highlight=pdist#scipy.spatial.distance.pdist
  ]]></help>
</tool>

python code:

import sys
import seaborn as sns
import pandas as pd

mat = pd.read_csv(sys.argv[1], sep="\t", index_col=0)

mat.index.name = 'MGI_id'
mat.columns.name = 'phen_sys'

clust=str(sys.argv[2])

s = sns.clustermap(mat,row_cluster=True,col_cluster=False, method=clust, cbar_pos=(0.01,0.01,0.01,0.2), cmap="mako")
s.savefig(sys.argv[3])

Thank you for the help.

You can do something like:
python:

s.savefig("output.png")

xml

<data format="png" name="output" from_work_dir="output.png" label="Pheno count heatmap" />

or
python:

s.savefig("output.png")
shutil.copyfile("output.png", sys.argv[2])
1 Like

Thank you, this works perfectly

1 Like