upstream-extended/community/tools/text_processing/text_processing/sort.xml
1<tool id="tp_sort_header_tool" name="Sort" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="@PROFILE@">
2 <description>data in ascending or descending order</description>
3 <macros>
4 <import>macros.xml</import>
5 </macros>
6 <expand macro="creator"/>
7 <expand macro="requirements">
8 <requirement type="package" version="4.9">sed</requirement>
9 </expand>
10 <version_command>sort --version | head -n 1</version_command>
11 <command>
12<![CDATA[
13 (
14 export LC_ALL=C;
15 #if int($header) > 0:
16 sed -u '${header}'q &&
17 #end if
18
19 sort $unique --stable -t ' '
20
21 #for $key in $sortkeys:
22 #if $key.start_charpos and $key.end_charpos:
23 -k ${key.column}.${key.start_charpos}${key.ignore_leading_blanks},${key.column}.${key.end_charpos}${key.ignore_leading_blanks}${key.order}${key.style}${key.ignore_case}
24 #elif $key.start_charpos:
25 -k ${key.column}.${key.start_charpos}${key.ignore_leading_blanks},${key.column}${key.order}${key.style}${key.ignore_case}
26 #elif $key.end_charpos:
27 -k ${key.column}${key.ignore_leading_blanks},${key.column}.${key.end_charpos}${key.ignore_leading_blanks}${key.order}${key.style}${key.ignore_case}
28 #else:
29 -k ${key.column}${key.ignore_leading_blanks},${key.column}${key.order}${key.style}${key.ignore_case}
30 #end if
31 #end for
32
33 ) < '${infile}' > '${outfile}'
34]]>
35 </command>
36 <inputs>
37 <param format="tabular" name="infile" type="data" label="Sort Query" />
38 <param name="header" type="integer" value="0"
39 label="Number of header lines" help="Header lines will be copied to the output unchanged without operating on them.">
40 <validator type="in_range" message="Negative values are not allowed." min="0"/>
41 </param>
42
43 <repeat name="sortkeys" title="Column selections" min="1">
44 <param name="column" label="Sort on column" type="data_column" data_ref="infile" accept_default="true" />
45 <param name="start_charpos" label="considering its characters from" type="integer" min="1" optional="true" help="Leave empty (or set to 1) to use the column value starting from its first character." />
46 <param name="end_charpos" label="to and including" type="integer" min="1" optional="true" help="Leave empty to use the column value up to and including its last character." />
47 <param name="order" type="select" display="radio" label="in">
48 <option value="">Ascending order</option>
49 <option value="r">Descending order</option>
50 </param>
51 <param name="style" type="select" display="radio" label="using sort flavor">
52 <option value="n">Fast numeric sort (-n)</option>
53 <option value="g">General numeric sort ( scientific notation -g)</option>
54 <option value="V">Natural/Version sort (-V) </option>
55 <option value="">Alphabetical sort</option>
56 <option value="h">Human-readable numbers (-h)</option>
57 <option value="R">Random order (-R)</option>
58 </param>
59 <param name="ignore_case" type="boolean" checked="false" truevalue="f" falsevalue=""
60 label="ignoring case" help="Turn lowercase symbols to upper case before comparing values in this column. (-f)" />
61 <param name="ignore_leading_blanks" type="boolean" checked="false" truevalue="b" falsevalue=""
62 label="ignoring leading blanks" help="This option can be useful with Alphabetical and Natural sort (which treat spaces as actual characters) or to prevent unwanted offsets if you specified a range of character positions to consider." />
63 </repeat>
64
65 <param name="unique" type="boolean" checked="false" truevalue="--unique" falsevalue=""
66 label="Output unique values" help="Print only unique values, based on sorted key columns. See help section for details. (--unique)" />
67 </inputs>
68 <outputs>
69 <data name="outfile" format_source="infile" metadata_source="infile"/>
70 </outputs>
71 <tests>
72 <test expect_num_outputs="1">
73 <param name="infile" value="sort1.bed"/>
74 <param name="header" value="3"/>
75 <repeat name="sortkeys">
76 <param name="column" value="1"/>
77 <param name="style" value=""/>
78 <param name="order" value=""/>
79 </repeat>
80 <repeat name="sortkeys">
81 <param name="column" value="3"/>
82 <param name="style" value="n"/>
83 <param name="order" value="r"/>
84 </repeat>
85 <output name="outfile" file="sort_result1.bed"/>
86 </test>
87 <test expect_num_outputs="1">
88 <param name="infile" value="sort1.bed"/>
89 <param name="header" value="3"/>
90 <repeat name="sortkeys">
91 <param name="column" value="1"/>
92 <param name="style" value=""/>
93 <param name="order" value=""/>
94 </repeat>
95 <repeat name="sortkeys">
96 <param name="column" value="3"/>
97 <param name="style" value="n"/>
98 <param name="order" value=""/>
99 </repeat>
100 <output name="outfile" file="sort_result2.bed"/>
101 </test>
102 <test expect_num_outputs="1">
103 <param name="infile" value="sort2.bed"/>
104 <repeat name="sortkeys">
105 <param name="column" value="5"/>
106 <param name="style" value="g"/>
107 <param name="order" value=""/>
108 </repeat>
109 <output name="outfile" file="sort_result3.bed"/>
110 </test>
111 <test expect_num_outputs="1">
112 <param name="infile" value="sort3.tabular"/>
113 <param name="header" value="0"/>
114 <param name="unique" value="false"/>
115 <repeat name="sortkeys">
116 <param name="column" value="2"/>
117 <param name="start_charpos" value="7"/>
118 <param name="order" value=""/>
119 <param name="style" value="n"/>
120 </repeat>
121 <repeat name="sortkeys">
122 <param name="column" value="2"/>
123 <param name="start_charpos" value="4"/>
124 <param name="end_charpos" value="5"/>
125 <param name="order" value=""/>
126 <param name="style" value="n"/>
127 </repeat>
128 <repeat name="sortkeys">
129 <param name="column" value="2"/>
130 <param name="start_charpos" value="1"/>
131 <param name="end_charpos" value="2"/>
132 <param name="order" value="r"/>
133 <param name="style" value="n"/>
134 </repeat>
135 <output name="outfile" file="sorted3.tabular" ftype="tabular" />
136 </test>
137 <!-- Test ignore_case param -->
138 <test expect_num_outputs="1">
139 <param name="infile" value="sort4.tabular"/>
140 <param name="header" value="1"/>
141 <param name="unique" value="false"/>
142 <repeat name="sortkeys">
143 <param name="column" value="1"/>
144 <param name="order" value=""/>
145 <param name="style" value=""/>
146 <param name="ignore_case" value="true"/>
147 </repeat>
148 <repeat name="sortkeys">
149 <param name="column" value="3"/>
150 <param name="order" value="r"/>
151 <param name="style" value="n"/>
152 </repeat>
153 <output name="outfile" file="sorted4_partial.tabular" ftype="tabular" />
154 </test>
155 <!-- Test ignore_leading_blanks param -->
156 <test expect_num_outputs="1">
157 <param name="infile" value="sort4.tabular"/>
158 <param name="header" value="1"/>
159 <param name="unique" value="false"/>
160 <repeat name="sortkeys">
161 <param name="column" value="1"/>
162 <param name="start_charpos" value="1"/>
163 <param name="end_charpos" value="4"/>
164 <param name="order" value=""/>
165 <param name="style" value=""/>
166 <param name="ignore_case" value="true"/>
167 <param name="ignore_leading_blanks" value="true"/>
168 </repeat>
169 <repeat name="sortkeys">
170 <param name="column" value="1"/>
171 <param name="start_charpos" value="5"/>
172 <param name="order" value=""/>
173 <param name="style" value=""/>
174 <param name="ignore_case" value="true"/>
175 <param name="ignore_leading_blanks" value="true"/>
176 </repeat>
177 <repeat name="sortkeys">
178 <param name="column" value="3"/>
179 <param name="order" value="r"/>
180 <param name="style" value="n"/>
181 <param name="ignore_leading_blanks" value="true"/>
182 </repeat>
183 <output name="outfile" file="sorted4.tabular" ftype="tabular" />
184 </test>
185 </tests>
186 <help>
187<![CDATA[
188**What it does**
189
190This tool sorts an input file.
191
192-----
193
194**Sorting Styles**
195
196* **Fast Numeric**: sort by numeric values. Handles integer values (e.g. 43, 134) and decimal-point values (e.g. 3.14). *Does not* handle scientific notation (e.g. -2.32e2).
197* **General Numeric**: sort by numeric values. Handles all numeric notations (including scientific notation). Slower than *fast numeric*, so use only when necessary.
198* **Natural Sort**: Sort in 'natural' order (natural to humans, not to computers). See example below.
199* **Alphabetical sort**: Sort in strict alphabetical order. See example below.
200* **Human-readable numbers**: Sort human readble numbers (e.g. 1G > 2M > 3K > 400)
201* **Random order**: return lines in random order.
202
203------
204
205**Example - Header line**
206
207**Input file** (note first line is a header line, should not be sorted)::
208
209 Fruit Color Price
210 Banana Yellow 4.1
211 Avocado Green 8.0
212 Apple Red 3.0
213 Melon Green 6.1
214
215**Sorting** by **numeric order** on column **3**, with **header**, will return::
216
217 Fruit Color Price
218 Apple Red 3.0
219 Banana Yellow 4.1
220 Melon Green 6.1
221 Avocado Green 8.0
222
223
224-----
225
226**Example - Natural vs. Alphabetical sorting**
227
228Given the following list::
229
230 chr4
231 chr13
232 chr1
233 chr10
234 chr20
235 chr2
236
237**Alphabetical sort** would produce the following sorted list::
238
239 chr1
240 chr10
241 chr13
242 chr2
243 chr20
244 chr4
245
246**Natural Sort** would produce the following sorted list::
247
248 chr1
249 chr2
250 chr4
251 chr10
252 chr13
253 chr20
254
255
256.. class:: infomark
257
258If you're planning to use the file with another tool that expected sorted files (such as *join*), you should use the **Alphabetical sort**, not the **Natural Sort**. Natural sort order is easier for humans, but is unnatural for computer programs.
259
260-----
261
262**Example - Sorting based on parts of column values**
263
264The above column of chromosomes, with their constant prefix, could have been sorted in natural order also with the **Fast numeric sort** and **considering its characters from** character 4 only.
265
266In general, sorting based on just a range of characters in a column can be useful for sorting values with internal structure, in a single tool run.
267
268Consider, for example, the following column of dates, which is unfortunately not ISO-8601 formatted::
269
270 10/24/2025
271 09/18/1974
272 12/16/1998
273 03/04/2007
274
275You could modify these values with other tools first, but you can achieve correct chronological sort order with a single run of the sort tool like this:
276
277- Do a **Fast numeric sort** on the column **considering its characters from** character 7 (the start of the year).
278- Resolve ties (using another column selection section) with another **Fast numeric sort** on the same column **considering its characters from** character 1 **to and including** character 2 (the month representation).
279- Resolve remaining ties with a third **Fast numeric sort** on again the same column **considering its characters from** character 4 **to and including** character 5 (the day representation).
280
281This will result in the ascending chronological order::
282
283 09/18/1974
284 11/17/1998
285 11/18/1998
286 12/16/1998
287 03/04/2007
288 10/24/2025
289
290Before relying on in-column character ranges, make extra sure that all values are formatted consistently (in the above example, that all dates use two digits for days and months and the same overall date format).
291]]>
292 </help>
293 <expand macro="citations" />
294</tool>