0
|
1 <tool name="plotlytabular" id="plotlytabular" version="3.0">
|
|
2 <!--Source in git at: https://github.com/fubar2/galaxy_tf_overlay-->
|
2
|
3 <!--Created by toolfactory@galaxy.org at 28/07/2023 15:52:28 using the Galaxy Tool Factory.-->
|
|
4 <description>Plotly plot generator for any small-ish Galaxy tabular data.</description>
|
0
|
5 <requirements>
|
|
6 <requirement version="1.5.3" type="package">pandas</requirement>
|
|
7 <requirement version="5.9.0" type="package">plotly</requirement>
|
|
8 </requirements>
|
|
9 <stdio>
|
|
10 <exit_code range="1:" level="fatal"/>
|
|
11 </stdio>
|
|
12 <version_command><![CDATA[echo "3.0"]]></version_command>
|
|
13 <command><![CDATA[python
|
|
14 $runme
|
|
15 --input_tab
|
|
16 $input_tab
|
|
17 --htmlout
|
|
18 $htmlout
|
|
19 --xcol
|
|
20 "$xcol"
|
|
21 --ycol
|
|
22 "$ycol"
|
|
23 --colourcol
|
|
24 "$colourcol"
|
|
25 --hovercol
|
|
26 "$hovercol"
|
|
27 --title
|
|
28 "$title"
|
|
29 --header
|
|
30 "$header"]]></command>
|
|
31 <configfiles>
|
|
32 <configfile name="runme"><![CDATA[#raw
|
|
33
|
|
34 import argparse
|
|
35 import sys
|
|
36 import math
|
|
37 import plotly.express as px
|
|
38 import pandas as pd
|
|
39 # Ross Lazarus July 2023
|
|
40 # based on various plotly tutorials
|
|
41 parser = argparse.ArgumentParser()
|
|
42 a = parser.add_argument
|
|
43 a('--input_tab',default='')
|
|
44 a('--header',default='')
|
|
45 a('--htmlout',default="test_run.html")
|
|
46 a('--xcol',default='')
|
|
47 a('--ycol',default='')
|
|
48 a('--colourcol',default='')
|
|
49 a('--hovercol',default='')
|
|
50 a('--title',default='Default plot title')
|
|
51 args = parser.parse_args()
|
1
|
52 isColour = False
|
|
53 isHover = False
|
|
54 if len(args.colourcol.strip()) > 0:
|
|
55 isColour = True
|
|
56 if len(args.hovercol.strip()) > 0:
|
|
57 isHover = True
|
0
|
58 df = pd.read_csv(args.input_tab, sep='\t')
|
|
59 MAXLEN=35
|
|
60 NCOLS = df.columns.size
|
|
61 defaultcols = ['col%d' % (x+1) for x in range(NCOLS)]
|
1
|
62 testcols = df.columns
|
0
|
63 if len(args.header.strip()) > 0:
|
|
64 newcols = args.header.split(',')
|
|
65 if len(newcols) == NCOLS:
|
|
66 df.columns = newcols
|
|
67 else:
|
|
68 sys.stderr.write('#### Supplied header %s has %d comma delimited header names - does not match the input tabular file %d columns - using col1,...coln' % (args.header, len(newcols), NCOLS))
|
|
69 df.columns = defaultcols
|
1
|
70 else: # no header supplied - check for a real one that matches the x and y axis column names
|
|
71 colsok = (args.xcol in testcols) and (args.ycol in testcols) # if they match, probably ok...should use more code and logic..
|
|
72 if not colsok:
|
2
|
73 sys.stderr.write('replacing first row of data derived header %s with %s' % (testcols, defaultcols))
|
1
|
74 df.columns = defaultcols
|
0
|
75 #df['col11'] = [-math.log(x) for x in df['col11']] # convert so large values reflect statistical surprise
|
|
76 if isHover and isColour:
|
|
77 fig = px.scatter(df, x=args.xcol, y=args.ycol, color=args.colourcol, hover_name=args.hovercol)
|
|
78 elif isHover:
|
|
79 fig = px.scatter(df, x=args.xcol, y=args.ycol, hover_name=args.hovercol)
|
|
80 elif isColour:
|
|
81 fig = px.scatter(df, x=args.xcol, y=args.ycol, color=args.colourcol)
|
|
82 else:
|
|
83 fig = px.scatter(df, x=args.xcol, y=args.ycol)
|
|
84 if args.title:
|
|
85 ftitle=dict(text=args.title, font=dict(size=50))
|
|
86 fig.update_layout(title=ftitle)
|
|
87 for scatter in fig.data:
|
|
88 scatter['x'] = [str(x)[:MAXLEN] + '..' if len(str(x)) > MAXLEN else x for x in scatter['x']]
|
|
89 scatter['y'] = [str(x)[:MAXLEN] + '..' if len(str(x)) > MAXLEN else x for x in scatter['y']]
|
|
90 if len(args.colourcol.strip()) == 0:
|
|
91 sl = str(scatter['legendgroup'])
|
|
92 if len(sl) > MAXLEN:
|
|
93 scatter['legendgroup'] = sl[:MAXLEN]
|
|
94 fig.write_html(args.htmlout)
|
|
95
|
|
96
|
|
97 #end raw]]></configfile>
|
|
98 </configfiles>
|
|
99 <inputs>
|
|
100 <param name="input_tab" type="data" optional="false" label="Tabular input file to plot" help="" format="tabular" multiple="false"/>
|
2
|
101 <param name="xcol" type="text" value="sepal_length" label="x axis for plot" help="Use a column name from the header if the file has one, or use one from the list supplied below, or use col1....colN otherwise to select the correct column"/>
|
|
102 <param name="ycol" type="text" value="sepal_width" label="y axis for plot" help="Use a column name from the header if the file has one, or use one from the list supplied below, or use col1....colN otherwise to select the correct column"/>
|
1
|
103 <param name="colourcol" type="text" value="petal_width" label="column containing a groupable variable for colour. Default none." help="Adds a legend so choose wisely "/>
|
2
|
104 <param name="hovercol" type="text" value="species_id" label="columname for hover string" help="Use a column name from the header if the file has one, or use one from the list supplied below, or use col1....colN otherwise to select the correct column"/>
|
|
105 <param name="title" type="text" value="Iris data" label="Title for the plot" help="Special characters will probably be escaped so do not use them"/>
|
0
|
106 <param name="header" type="text" value="" label="Use this comma delimited list of column header names for this tabular file. Default is None when col1...coln will be used" help="Default for Galaxy blast outputs with 25 columns. The column names supplied for xcol, ycol, hover and colour MUST match either the supplied list, or if none, col1...coln."/>
|
|
107 </inputs>
|
|
108 <outputs>
|
|
109 <data name="htmlout" format="html" label="htmlout" hidden="false"/>
|
|
110 </outputs>
|
|
111 <tests>
|
|
112 <test>
|
|
113 <output name="htmlout" value="htmlout_sample" compare="sim_size" delta="5000"/>
|
|
114 <param name="input_tab" value="input_tab_sample"/>
|
1
|
115 <param name="xcol" value="sepal_length"/>
|
|
116 <param name="ycol" value="sepal_width"/>
|
|
117 <param name="colourcol" value="petal_width"/>
|
|
118 <param name="hovercol" value="species_id"/>
|
2
|
119 <param name="title" value="Iris data"/>
|
0
|
120 <param name="header" value=""/>
|
|
121 </test>
|
|
122 </tests>
|
|
123 <help><![CDATA[
|
|
124
|
2
|
125 This is a generic version of the plotlyblast specific blastn Galaxy search output file plotter.
|
0
|
126
|
|
127
|
|
128
|
|
129 .. class:: warningmark
|
|
130
|
|
131 NOTE: Long strings in x and y tickmarks WILL BE TRUNCATED if they are too long - ".." is added to indicate truncation - otherwise some plots are squished.
|
|
132
|
|
133
|
|
134
|
|
135 .. class:: warningmark
|
|
136
|
2
|
137 NOTE: Columns with very small scientific notation floats will need to be pre-scaled in a way that doesn't confuse plotly.express with their values.
|
0
|
138
|
|
139
|
|
140
|
|
141 ----
|
|
142
|
|
143
|
|
144
|
|
145 The main reason to run this tool is to have an interactive hover text specified so it appears when hovering over each data point to supply useful information.
|
|
146
|
|
147
|
|
148
|
|
149 Assumes you want a hover display for an interactive plot to be informative
|
|
150
|
|
151
|
|
152
|
|
153 Column names are auto-generated as col1,...coln unless a comma separated list of column names is supplied as the header parameter.
|
|
154
|
|
155
|
|
156
|
|
157 For example, using a Galaxy blastn output with 25 columns, the following comma delimited string supplied as the "header" parameter will match the names of each column.
|
|
158
|
|
159 qaccver,saccver,piden,length,mismatch,gapopen,qstart,qend,sstart,send,evalue,bitscore,sallseqid,score,nident,positive,gaps,ppos,qframe,sframe,qseq,sseq,qlen,slen,salltitles
|
|
160
|
|
161
|
|
162
|
|
163 When a header is supplied, the xcol and other column names must match one of those supplied column names.
|
|
164
|
|
165 So for example, xcol = "qaccver" for the blastn header example rather than xcol = "col1" when no header is supplied.
|
|
166
|
|
167
|
|
168
|
|
169 Relies on Plotly python code released under the MIT licence: https://github.com/plotly/plotly.py/blob/master/LICENSE.txt
|
|
170
|
|
171
|
|
172
|
|
173
|
|
174
|
|
175 ------
|
|
176
|
|
177
|
|
178 Script::
|
|
179
|
|
180 import argparse
|
|
181 import sys
|
|
182 import math
|
|
183 import plotly.express as px
|
|
184 import pandas as pd
|
|
185 # Ross Lazarus July 2023
|
|
186 # based on various plotly tutorials
|
|
187 parser = argparse.ArgumentParser()
|
|
188 a = parser.add_argument
|
|
189 a('--input_tab',default='')
|
|
190 a('--header',default='')
|
|
191 a('--htmlout',default="test_run.html")
|
|
192 a('--xcol',default='')
|
|
193 a('--ycol',default='')
|
|
194 a('--colourcol',default='')
|
|
195 a('--hovercol',default='')
|
|
196 a('--title',default='Default plot title')
|
|
197 args = parser.parse_args()
|
1
|
198 isColour = False
|
|
199 isHover = False
|
|
200 if len(args.colourcol.strip()) > 0:
|
|
201 isColour = True
|
|
202 if len(args.hovercol.strip()) > 0:
|
|
203 isHover = True
|
0
|
204 df = pd.read_csv(args.input_tab, sep='\t')
|
|
205 MAXLEN=35
|
|
206 NCOLS = df.columns.size
|
|
207 defaultcols = ['col%d' % (x+1) for x in range(NCOLS)]
|
1
|
208 testcols = df.columns
|
0
|
209 if len(args.header.strip()) > 0:
|
|
210 newcols = args.header.split(',')
|
|
211 if len(newcols) == NCOLS:
|
|
212 df.columns = newcols
|
|
213 else:
|
|
214 sys.stderr.write('#### Supplied header %s has %d comma delimited header names - does not match the input tabular file %d columns - using col1,...coln' % (args.header, len(newcols), NCOLS))
|
|
215 df.columns = defaultcols
|
1
|
216 else: # no header supplied - check for a real one that matches the x and y axis column names
|
|
217 colsok = (args.xcol in testcols) and (args.ycol in testcols) # if they match, probably ok...should use more code and logic..
|
|
218 if not colsok:
|
2
|
219 sys.stderr.write('replacing first row of data derived header %s with %s' % (testcols, defaultcols))
|
1
|
220 df.columns = defaultcols
|
0
|
221 #df['col11'] = [-math.log(x) for x in df['col11']] # convert so large values reflect statistical surprise
|
|
222 if isHover and isColour:
|
|
223 fig = px.scatter(df, x=args.xcol, y=args.ycol, color=args.colourcol, hover_name=args.hovercol)
|
|
224 elif isHover:
|
|
225 fig = px.scatter(df, x=args.xcol, y=args.ycol, hover_name=args.hovercol)
|
|
226 elif isColour:
|
|
227 fig = px.scatter(df, x=args.xcol, y=args.ycol, color=args.colourcol)
|
|
228 else:
|
|
229 fig = px.scatter(df, x=args.xcol, y=args.ycol)
|
|
230 if args.title:
|
|
231 ftitle=dict(text=args.title, font=dict(size=50))
|
|
232 fig.update_layout(title=ftitle)
|
|
233 for scatter in fig.data:
|
|
234 scatter['x'] = [str(x)[:MAXLEN] + '..' if len(str(x)) > MAXLEN else x for x in scatter['x']]
|
|
235 scatter['y'] = [str(x)[:MAXLEN] + '..' if len(str(x)) > MAXLEN else x for x in scatter['y']]
|
|
236 if len(args.colourcol.strip()) == 0:
|
|
237 sl = str(scatter['legendgroup'])
|
|
238 if len(sl) > MAXLEN:
|
|
239 scatter['legendgroup'] = sl[:MAXLEN]
|
|
240 fig.write_html(args.htmlout)
|
|
241
|
|
242 ]]></help>
|
|
243 <citations>
|
|
244 <citation type="doi">10.1093/bioinformatics/bts573</citation>
|
|
245 </citations>
|
|
246 </tool>
|
|
247
|