|
4
|
1 <tool id="tp_replace_in_column" name="Replace Text" version="@BASE_VERSION@.0">
|
|
3
|
2 <description>in a specific column</description>
|
|
4
|
3 <macros>
|
|
|
4 <import>macros.xml</import>
|
|
|
5 </macros>
|
|
|
6 <expand macro="requirements">
|
|
3
|
7 <requirement type="package" version="4.1.0">gnu_awk</requirement>
|
|
4
|
8 </expand>
|
|
|
9 <version_command>awk --version | head -n 1</version_command>
|
|
3
|
10 <command interpreter="sh">
|
|
4
|
11 <![CDATA[
|
|
|
12 ##adapt to awk's quirks - to pass an acutal backslash - two backslashes are required (just like in a C string)
|
|
3
|
13 REPLACE_PATTERN=\${$replace_pattern//\\/\\\\};
|
|
4
|
14 awk
|
|
|
15 -v OFS="\t"
|
|
|
16 --re-interval
|
|
|
17 --sandbox "{ \$$column = gensub( /$find_pattern/, \"$replace_pattern\", \"g\", \$$column ) ; print \$0 ; }"
|
|
|
18 "$infile"
|
|
|
19 > "$output"
|
|
|
20 ]]>
|
|
3
|
21 </command>
|
|
|
22 <inputs>
|
|
4
|
23 <param format="tabular" name="infile" type="data" label="File to process" />
|
|
|
24 <param name="column" label="in column" type="data_column" data_ref="infile" accept_default="true" />
|
|
3
|
25
|
|
|
26 <param name="find_pattern" type="text" size="20" label="Find pattern" help="Use simple text, or a valid regular expression (without backslashes // ) " >
|
|
|
27 <sanitizer>
|
|
|
28 <valid initial="string.printable">
|
|
|
29 <remove value="'"/>
|
|
|
30 </valid>
|
|
|
31 </sanitizer>
|
|
|
32 </param>
|
|
|
33 <param name="replace_pattern" type="text" size="20" label="Replace with" help="Use simple text, or & (ampersand) and \\1 \\2 \\3 to refer to matched text. See examples below." >
|
|
|
34 <sanitizer>
|
|
|
35 <valid initial="string.printable">
|
|
|
36 <remove value="'"/>
|
|
|
37 </valid>
|
|
|
38 </sanitizer>
|
|
|
39 </param>
|
|
|
40 </inputs>
|
|
4
|
41 <outputs>
|
|
|
42 <data format="input" name="output" metadata_source="infile" />
|
|
|
43 </outputs>
|
|
3
|
44 <tests>
|
|
|
45 <test>
|
|
4
|
46 <param name="infile" value="replace_text_in_column_in1.txt" ftype="tabular" />
|
|
|
47 <param name="column" value="4" />
|
|
|
48 <param name="find_pattern" value=".+_(R.)" />
|
|
|
49 <param name="replace_pattern" value="\1" />
|
|
3
|
50 <output name="output" file="replace_text_in_column_output1.txt" />
|
|
|
51 </test>
|
|
|
52 </tests>
|
|
|
53 <help>
|
|
4
|
54 <![CDATA[
|
|
3
|
55 **What it does**
|
|
|
56
|
|
|
57 This tool performs find & replace operation on a specified column in a given file.
|
|
|
58
|
|
|
59 .. class:: infomark
|
|
|
60
|
|
|
61 The **pattern to find** uses the **extended regular** expression syntax (same as running 'awk --re-interval').
|
|
|
62
|
|
|
63 .. class:: infomark
|
|
|
64
|
|
|
65 **TIP:** If you need more complex patterns, use the *awk* tool.
|
|
|
66
|
|
|
67 -----
|
|
|
68
|
|
|
69
|
|
|
70 **Examples of Find Patterns**
|
|
|
71
|
|
|
72 - **HELLO** The word 'HELLO' (case sensitive).
|
|
|
73 - **AG.T** The letters A,G followed by any single character, followed by the letter T.
|
|
|
74 - **A{4,}** Four or more consecutive A's.
|
|
|
75 - **chr2[012]\\t** The words 'chr20' or 'chr21' or 'chr22' followed by a tab character.
|
|
|
76 - **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.
|
|
|
77
|
|
|
78
|
|
|
79 **Examples of Replace Patterns**
|
|
|
80
|
|
|
81 - **WORLD** The word 'WORLD' will be placed whereever the find pattern was found.
|
|
|
82 - **FOO-&-BAR** Each time the find pattern is found, it will be surrounded with 'FOO-' at the begining and '-BAR' at the end. **&** (ampersand) represents the matched find pattern.
|
|
|
83 - **\\1** The text which matched the first parenthesis in the Find Pattern.
|
|
|
84
|
|
|
85
|
|
|
86 -----
|
|
|
87
|
|
|
88 **Example 1**
|
|
|
89
|
|
|
90 **Find Pattern:** HELLO
|
|
|
91 **Replace Pattern:** WORLD
|
|
|
92
|
|
|
93 Every time the word HELLO is found, it will be replaced with the word WORLD. This operation affects only the selected column.
|
|
|
94
|
|
|
95 -----
|
|
|
96
|
|
|
97 **Example 2**
|
|
|
98
|
|
|
99 **Find Pattern:** ^(.{4})
|
|
|
100 **Replace Pattern:** &\\t
|
|
|
101
|
|
|
102 Find 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. This operation affects only the selected column.
|
|
|
103
|
|
|
104
|
|
|
105 -----
|
|
|
106
|
|
|
107 **Extened Regular Expression Syntax**
|
|
|
108
|
|
|
109 The 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.
|
|
|
110
|
|
|
111 - **( ) { } [ ] . * ? + \ ^ $** are all special characters. **\\** can be used to "escape" a special character, allowing that special character to be searched for.
|
|
|
112 - **^** matches the beginning of a string(but not an internal line).
|
|
|
113 - **(** .. **)** groups a particular pattern.
|
|
|
114 - **{** n or n, or n,m **}** specifies an expected number of repetitions of the preceding pattern.
|
|
|
115
|
|
|
116 - **{n}** The preceding item is matched exactly n times.
|
|
|
117 - **{n,}** The preceding item ismatched n or more times.
|
|
|
118 - **{n,m}** The preceding item is matched at least n times but not more than m times.
|
|
|
119
|
|
|
120 - **[** ... **]** 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**.
|
|
|
121 - **.** Matches any single character except a newline.
|
|
|
122 - ***** The preceding item will be matched zero or more times.
|
|
|
123 - **?** The preceding item is optional and matched at most once.
|
|
|
124 - **+** The preceding item will be matched one or more times.
|
|
|
125 - **^** has two meaning:
|
|
|
126 - matches the beginning of a line or string.
|
|
|
127 - indicates negation in a character class. For example, [^...] matches every character except the ones inside brackets.
|
|
|
128 - **$** matches the end of a line or string.
|
|
|
129 - **\|** Separates alternate possibilities.
|
|
|
130
|
|
|
131
|
|
|
132 **Note**: AWK uses extended regular expression syntax, not Perl syntax. **\\d**, **\\w**, **\\s** etc. are **not** supported.
|
|
|
133
|
|
4
|
134 @REFERENCES@
|
|
|
135 ]]>
|
|
3
|
136 </help>
|
|
|
137 </tool>
|