Source code

Download source archive ↓

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

1<tool id="tp_replace_in_line" name="Replace Text" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="@PROFILE@">
2    <description>in entire line</description>
3    <macros>
4       <import>macros.xml</import>
5    </macros>
6    <expand macro="creator"/>
7    <requirements>
8        <requirement type="package" version="4.9">sed</requirement>
9    </requirements>
10    <version_command>sed --version | head -n 1</version_command>
11    <command>
12<![CDATA[
13   sed
14      -r
15      --sandbox
16      #for $replacement in $replacements:
17        -e '$replacement.sed_options'
18        -e
19        's/$replacement.find_pattern/$replacement.replace_pattern/g'
20      #end for
21      '$infile'
22      > '$outfile'
23]]>
24
25    </command>
26    <inputs>
27      <param format="txt" name="infile" type="data" label="File to process" />
28      <repeat name="replacements" title="Replacement" min="1">
29         <param name="find_pattern" type="text" label="Find pattern" help="Use simple text, or a valid regular expression">
30            <sanitizer>
31                <valid initial="string.printable">
32                    <remove value="&#39;"/>
33                    <remove value="/"/>
34                </valid>
35                <mapping initial="none">
36                    <add source="&#39;" target="&#39;&quot;&#39;&quot;&#39;" />
37                    <add source="/" target="\/"/>
38                </mapping>
39            </sanitizer>
40         </param>
41         <param name="replace_pattern" type="text" label="Replace with:" help="Use simple text, or &amp; (ampersand) and \1, \2, \3, etc. to refer to matched text. See examples below." >
42            <sanitizer>
43                <valid initial="string.printable">
44                    <remove value="&#39;"/>
45                    <remove value="/"/>
46                </valid>
47                <mapping initial="none">
48                    <add source="&#39;" target="&#39;&quot;&#39;&quot;&#39;" />
49                    <add source="/" target="\/"/>
50                </mapping>
51            </sanitizer>
52         </param>
53         <param name="sed_options" type="text" optional="true" label="Additional sed commands before replacement" help="Provide additional sed commands before the replacement (e.g., ':a;N;$!ba;')." >
54            <sanitizer>
55              <valid initial="string.printable">
56                  <remove value="&#39;"/>  <!-- Removes single quotes -->
57                  <remove value="/"/>     <!-- Removes slashes -->
58              </valid>
59              <mapping initial="none">
60                  <add source="&#39;" target="&#39;&quot;&#39;&quot;&#39;" />  <!-- Escapes single quotes -->
61                  <add source="/" target="\/"/>                         <!-- Escapes slashes -->
62              </mapping>
63            </sanitizer>
64         </param>
65      </repeat>
66    </inputs>
67    <outputs>
68  <data name="outfile" format_source="infile" metadata_source="infile"/>
69    </outputs>
70    <tests>
71         <test>
72            <param name="infile" value="replace_text_in_line1.txt" />
73            <repeat name="replacements">
74              <param name="find_pattern" value="CTC." />
75              <param name="replace_pattern" value="FOOBAR" />
76              <param name="sed_options" value="" />
77            </repeat>
78            <output name="outfile" file="replace_text_in_line_results1.txt" />
79        </test>
80        <test>
81            <param name="infile" value="replace_text_in_line1.txt" />
82            <repeat name="replacements">
83              <param name="find_pattern" value="CTC." />
84              <param name="replace_pattern" value="FOOBAR" />
85              <param name="sed_options" value="" />
86            </repeat>
87            <repeat name="replacements">
88              <param name="find_pattern" value="chr" />
89              <param name="replace_pattern" value="domain" />
90              <param name="sed_options" value="" />
91            </repeat>
92            <output name="outfile" file="replace_text_in_line_results2.txt" />
93        </test>
94        <test>
95            <param name="infile" value="replace_text_in_line1.txt" />
96            <repeat name="replacements">
97            <param name="find_pattern" value="\n" />
98            <param name="replace_pattern" value="" />
99            <param name="sed_options" value=":a;N;$!ba" />
100            </repeat>
101            <output name="outfile" file="replace_text_in_line_results3.txt" />
102        </test>
103    </tests>
104    <help>
105<![CDATA[
106**What it does**
107
108This tool performs find & replace operation on a specified file.
109
110.. class:: infomark
111
112The **pattern to find** uses the **extended regular** expression syntax (same as running 'sed -r').
113
114.. class:: infomark
115
116**TIP:** If you need more complex patterns, use the *sed* tool.
117
118-----
119
120**Examples of Find Patterns**
121
122- **HELLO**     The word 'HELLO' (case sensitive).
123- **AG.T**  The letters A,G followed by any single character, followed by the letter T.
124- **A{4,}**     Four or more consecutive A's.
125- **chr2[012]\\t**   The words 'chr20' or 'chr21' or 'chr22' followed by a tab character.
126- **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.
127
128
129**Examples of Replace Patterns**
130
131- **WORLD**  The word 'WORLD' will be placed whereever the find pattern was found.
132- **FOO-&-BAR**  Each time the find pattern is found, it will be surrounded with 'FOO-' at the beginning and '-BAR' at the end. **&** (ampersand) represents the matched find pattern.
133- **\\1**   The text which matched the first parenthesis in the Find Pattern.
134
135
136-----
137
138**Example 1**
139
140**Find Pattern:** HELLO
141**Replace Pattern:** WORLD
142
143Every time the word HELLO is found, it will be replaced with the word WORLD.
144
145
146-----
147
148**Example 2**
149
150**Find Pattern:** ^(.{4})
151**Replace Pattern:** &\\t
152
153Find the first four characters in each line, and replace them with the same text, followed by a tab character. In practice - this will split the first line into two columns.
154
155
156-----
157
158**Extended Regular Expression Syntax**
159
160The select 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.
161
162- **( ) { } [ ] . * ? + \ ^ $** are all special characters. **\\** can be used to "escape" a special character, allowing that special character to be searched for.
163- **^** matches the beginning of a string(but not an internal line).
164- **(** .. **)** groups a particular pattern.
165- **{** n or n, or n,m **}** specifies an expected number of repetitions of the preceding pattern.
166
167  - **{n}** The preceding item is matched exactly n times.
168  - **{n,}** The preceding item ismatched n or more times.
169  - **{n,m}** The preceding item is matched at least n times but not more than m times.
170
171- **[** ... **]** 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**.
172- **.** Matches any single character except a newline.
173- ***** The preceding item will be matched zero or more times.
174- **?** The preceding item is optional and matched at most once.
175- **+** The preceding item will be matched one or more times.
176- **^** has two meaning:
177  - matches the beginning of a line or string.
178  - indicates negation in a character class. For example, [^...] matches every character except the ones inside brackets.
179- **$** matches the end of a line or string.
180- **\|** Separates alternate possibilities.
181
182
183**Note**: SED uses extended regular expression syntax, not Perl syntax. **\\d**, **\\w**, **\\s** etc. are **not** supported. However, you can use SED FAQ to perform commands using special characters.
184        More complex options can look like `sed -e '$!N;s/foo/bar/;'`. Here, `$!N;` is an optional part which you only need to set in very special cases. The `foo` part is the search string, and the `bar` part is the replacement string.
185        Please read the SED FAQ here: https://www.pement.org/sed/sedfaq3.html#s3.2
186
187]]>
188    </help>
189    <expand macro="citations" />
190</tool>