Source code

Download source archive ↓

upstream-extended/community/tools/text_processing/text_processing/find_and_replace.xml

1<tool id="tp_find_and_replace" name="Replace" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="@PROFILE@">
2    <description>parts of text</description>
3    <macros>
4        <import>macros.xml</import>
5    </macros>
6    <expand macro="creator"/>
7    <requirements>
8        <requirement type="package" version="5.32">perl</requirement>
9    </requirements>
10    <command>
11<![CDATA[
12        #for $i, $el in enumerate($find_and_replace)
13            #if $i == (len($find_and_replace) - 1)
14                #set $join = ""
15            #else 
16                #set $join = "|"
17            #end if
18            perl '$__tool_directory__/find_and_replace'
19                #if $el.searchwhere.searchwhere_select == "column":
20                    -c $el.searchwhere.column
21                #end if
22                #if $i == (len($find_and_replace) - 1)
23                    -o '$outfile'
24                #end if
25                $el.global
26                $el.caseinsensitive
27                $el.wholewords
28                $el.skip_first_line
29                $el.is_regex
30                '$el.find_pattern'
31                '$el.replace_pattern'
32                #if $i == 0
33                    '$infile'
34                #end if
35                $join
36        #end for        
37]]>
38    </command>
39    <inputs>
40        <param name="infile" format="txt" type="data" label="File to process" />
41        <repeat name="find_and_replace" title="Find and Replace" min="1">
42            <param name="find_pattern" type="text" label="Find pattern" help="Use simple text, or a valid regular expression (without backslashes // ) " >
43                <sanitizer>
44                    <valid initial="string.printable">
45                        <remove value="&apos;"/>
46                    </valid>
47                </sanitizer>
48            </param>
49            <param name="replace_pattern" type="text" label="Replace with"
50                help="Use simple text, or $&amp; (dollar-ampersand) and $1 $2 $3 to refer to matched text. See examples below." >
51                <sanitizer>
52                    <valid initial="string.printable">
53                        <remove value="&apos;"/>
54                    </valid>
55                </sanitizer>
56            </param>
57            <param name="is_regex" type="boolean" checked="false" truevalue="-r" falsevalue=""
58                label="Find-Pattern is a regular expression" help="see help section for details." />
59
60            <param name="global" type="boolean" checked="false" truevalue="-g" falsevalue=""
61                label="Replace all occurences of the pattern" />
62
63            <param name="caseinsensitive" type="boolean" checked="false" truevalue="-i" falsevalue=""
64                label="Case-Insensitive search" help="" />
65
66            <param name="wholewords" type="boolean" checked="false" truevalue="-w" falsevalue=""
67                label="Find whole-words" help="ignore partial matches (e.g. 'apple' will not match 'snapple')" />
68
69            <param name="skip_first_line" type="boolean" checked="false" truevalue="-s" falsevalue=""
70                label="Ignore first line" help="Select this option if the first line contains column headers. Text in the line will not be replaced. " />
71
72            <conditional name="searchwhere">
73                <param name="searchwhere_select" type="select" label="Find and Replace text in">
74                    <option value="line" selected="true">entire line</option>
75                    <option value="column">specific column</option>
76                </param>
77                <when value="line" />
78                <when value="column">
79                    <param name="column" label="in column" type="data_column" data_ref="infile" accept_default="true" />
80                </when>
81            </conditional>
82        </repeat>
83    </inputs>
84    <outputs>
85        <data format_source="infile" name="outfile" metadata_source="infile" />
86    </outputs>
87    <tests>
88        <test expect_num_outputs="1">
89            <param name="infile" value="find_and_replace1.txt" />
90            <repeat name="find_and_replace">
91                <param name="find_pattern" value="day" />
92                <param name="replace_pattern" value="great day" />
93                <param name="is_regex" value="False" />
94                <param name="global" value="true" />
95                <param name="caseinsensitive" value="False" />
96                <param name="wholewords" value="True" />
97            </repeat>
98            <output name="outfile" file="find_and_replace_results1.txt" />    
99        </test>
100	<!-- test that columns are split by tab not space. input has no tab loads of spaces 
101	     .. therefore the ftype needs to be set. 
102             result should be the same as in test 1 which works on whole line -->
103        <test expect_num_outputs="1">
104            <param name="infile" value="find_and_replace1.txt" ftype="tabular" />
105            <repeat name="find_and_replace">
106                <param name="find_pattern" value="day" />
107                <param name="replace_pattern" value="great day" />
108                <param name="is_regex" value="False" />
109                <param name="global" value="true" />
110                <param name="caseinsensitive" value="False" />
111                <param name="wholewords" value="True" />
112                <conditional name="searchwhere">
113                    <param name="searchwhere_select" value="column"/>
114                    <param name="column" value="1" />
115                </conditional>
116            </repeat>
117            <output name="outfile" file="find_and_replace_results1.txt" />
118        </test>
119        <test expect_num_outputs="1">
120            <param name="infile" value="find_and_replace2.txt" />
121            <repeat name="find_and_replace">
122                <param name="find_pattern" value="^chr" />
123                <param name="replace_pattern" value="" />
124                <param name="is_regex" value="True" />
125                <param name="caseinsensitive" value="False" />
126                <param name="wholewords" value="False" />
127                <conditional name="searchwhere">
128                    <param name="searchwhere_select" value="column" />
129                    <param name="column" value="3" />
130                </conditional>
131            </repeat>
132            <output name="outfile" file="find_and_replace_results2.txt" />
133        </test>
134        <test expect_num_outputs="1">
135            <param name="infile" value="find_and_replace1.txt" />
136            <repeat name="find_and_replace">
137                <param name="find_pattern" value="day" />
138                <param name="replace_pattern" value="great day" />
139                <param name="is_regex" value="False" />
140                <param name="global" value="true" />
141                <param name="caseinsensitive" value="False" />
142                <param name="wholewords" value="True" />
143            </repeat>
144            <param name="infile" value="find_and_replace1.txt" />
145            <repeat name="find_and_replace">
146                <param name="find_pattern" value="great" />
147                <param name="replace_pattern" value="best" />
148                <param name="is_regex" value="False" />
149                <param name="global" value="False" />
150                <param name="caseinsensitive" value="False" />
151                <param name="wholewords" value="True" />
152            </repeat>
153            <output name="outfile" file="find_and_replace_results3.txt" />    
154        </test>
155    </tests>
156    <help>
157<![CDATA[
158**What it does**
159
160This tool finds $ replaces text in an input dataset.
161
162.. class:: infomark
163
164The **pattern to find** can be a simple text string, or a perl **regular expression** string (depending on *pattern is a regex* check-box).
165
166.. class:: infomark
167
168When using regular expressions, the **replace pattern** can contain back-references ( e.g. \\1 )
169
170.. class:: infomark
171
172This tool uses `Perl regular expression <https://perldoc.perl.org/perlre>`_ syntax.
173
174-----
175
176**Examples of *regular-expression* Find Patterns**
177
178- **HELLO**     The word 'HELLO' (case sensitive).
179- **AG.T**      The letters A,G followed by any single character, followed by the letter T.
180- **A{4,}**     Four or more consecutive A's.
181- **chr2[012]\\t**       The words 'chr20' or 'chr21' or 'chr22' followed by a tab character.
182- **hsa-mir-([^ ]+)**        The text 'hsa-mir-' followed by one-or-more non-space characters. When using parenthesis, the matched content of the parenthesis can be accessed with **\1** in the **replace** pattern.
183
184
185**Examples of Replace Patterns**
186
187- **WORLD**  The word 'WORLD' will be placed whereever the find pattern was found.
188- **FOO-$&-BAR**  Each time the find pattern is found, it will be surrounded with 'FOO-' at the begining and '-BAR' at the end. **$&** (dollar-ampersand) represents the matched find pattern.
189- **$1**   The text which matched the first parenthesis in the Find Pattern.
190
191
192-----
193
194**Example 1**
195
196**Find Pattern:** HELLO
197**Replace Pattern:** WORLD
198**Regular Expression:** no
199**Replace what:** entire line
200
201Every time the word HELLO is found, it will be replaced with the word WORLD.
202
203-----
204
205**Example 2**
206
207**Find Pattern:** ^chr
208**Replace Pattern:** (empty)
209**Regular Expression:** yes
210**Replace what:** column 11
211
212If column 11 (of every line) begins with ther letters 'chr', they will be removed. Effectively, it'll turn "chr4" into "4" and "chrXHet" into "XHet"
213
214
215-----
216
217**Perl's Regular Expression Syntax**
218
219The Find & Replace tool searches the data for lines containing or not containing a match to the given pattern. A Regular Expression is a pattern descibing a certain amount of text.
220
221- **( ) { } [ ] . * ? + \\ ^ $** are all special characters. **\\** can be used to "escape" a special character, allowing that special character to be searched for.
222- **^** matches the beginning of a string(but not an internal line).
223- **(** .. **)** groups a particular pattern.
224- **{** n or n, or n,m **}** specifies an expected number of repetitions of the preceding pattern.
225
226  - **{n}** The preceding item is matched exactly n times.
227  - **{n,}** The preceding item ismatched n or more times.
228  - **{n,m}** The preceding item is matched at least n times but not more than m times.
229
230- **[** ... **]** creates a character class. Within the brackets, single characters can be placed. A dash (-) may be used to indicate a range such as **a-z**.
231- **.** Matches any single character except a newline.
232- ***** The preceding item will be matched zero or more times.
233- **?** The preceding item is optional and matched at most once.
234- **+** The preceding item will be matched one or more times.
235- **^** has two meaning:
236  - matches the beginning of a line or string.
237  - indicates negation in a character class. For example, [^...] matches every character except the ones inside brackets.
238- **$** matches the end of a line or string.
239- **\\|** Separates alternate possibilities.
240- **\\d** matches a single digit
241- **\\w** matches a single letter or digit or an underscore.
242- **\\s** matches a single white-space (space or tabs).
243
244]]>
245    </help>
246    <expand macro="citations" />
247</tool>