Source code

Download source archive ↓

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

1<tool id="tp_grep_tool" name="Search in textfiles" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="@PROFILE@">
2    <description>(grep)</description>
3    <macros>
4        <import>macros.xml</import>
5    </macros>
6    <expand macro="creator"/>
7    <requirements>
8        <requirement type="package" version="3.11">grep</requirement>
9        <requirement type="package" version="4.9">sed</requirement><!-- for ansi2html.sh -->
10    </requirements>
11    <stdio>
12        <exit_code range="2:" level="fatal" description="grep failed" />
13    </stdio>
14    <version_command>grep --version | head -n 1</version_command>
15    <command>
16<![CDATA[
17        #if str($color) == "COLOR":
18            GREP_COLOR='1;34'
19            grep
20                --color=always
21                $regex_type
22                -A $lines_after
23                -B $lines_before
24                $invert
25                $case_sensitive
26                -- '${url_paste}'
27                '${infile}' | '$__tool_directory__/ansi2html.sh' > '${output}'
28        #else:
29            grep
30                $regex_type
31                -A $lines_after
32                -B $lines_before
33                --no-group-separator
34                $invert
35                $case_sensitive
36                 -- '${url_paste}'
37                '${infile}' > '${output}'
38        #end if
39]]>
40    </command>
41    <inputs>
42        <param name="infile" format="txt" type="data" label="Select lines from" />
43
44        <param name="invert" type="select" label="that">
45            <option value="">Match</option>
46            <option value="-v">Don't Match</option>
47        </param>
48
49        <param name="regex_type" type="select" label="Type of regex">
50            <option value="-G">Basic (-G)</option>
51            <option value="-P" selected="true">Perl (-P)</option>
52            <option value="-E">Extended (egrep -E)</option>
53        </param>
54
55        <param name="url_paste" type="text" label="Regular Expression" help="See below for more details">
56            <sanitizer>
57                <valid initial="string.printable">
58                    <remove value="&apos;"/>
59                </valid>
60            </sanitizer>
61        </param>
62
63        <param name="case_sensitive" type="select" label="Match type" help="(-i)">
64            <option value="-i">case insensitive</option>
65            <option value="">case sensitive</option>
66        </param>
67        <param name="lines_before" type="integer" value="0"
68            label="Show lines preceding the matched line" help="leave it at zero unless you know what you're doing. (-B)" />
69        <param name="lines_after" type="integer" value="0"
70            label="Show lines trailing the matched line" help="leave it at zero unless you know what you're doing. (-A)" />
71        <param name="color" type="select"  label="Output">
72            <option value="NOCOLOR">text file (for further processing)</option>
73            <option value="COLOR">Highlighted HTML (for easier viewing)</option>
74        </param>
75
76    </inputs>
77    <outputs>
78        <data name="output" format_source="infile" metadata_source="infile">
79            <change_format>
80                <when input="color" value="COLOR" format="html"/>
81            </change_format>
82        </data>
83    </outputs>
84    <tests>
85        <!-- grep a FASTA file for sequences with specific motif -->
86        <test>
87            <param name="infile" value="grep1.txt" />
88            <param name="case_sensitive" value="-i" />
89            <param name="regex_type" value="-P" />
90            <param name="invert" value="" />
91            <param name="url_paste" value="AA.{2}GT" />
92            <param name="lines_before" value="1" />
93            <param name="lines_after" value="0" />
94            <param name="color" value="NOCOLOR" />
95            <output name="output" file="grep_results1.txt" />
96        </test>
97        <!-- grep a FASTA file for sequences with specific motif -
98             show highlighed output -->
99        <test>
100            <param name="infile" value="grep1.txt" />
101            <param name="case_sensitive" value="-i" />
102            <param name="regex_type" value="-P" />
103            <param name="invert" value="" />
104            <param name="url_paste" value="AA.{2}GT" />
105            <param name="lines_before" value="0" />
106            <param name="lines_after" value="0" />
107            <param name="color" value="COLOR" />
108            <output name="output" file="grep_results2.html" />
109        </test>
110        <!-- tests egrep -->
111        <test>
112            <param name="infile" value="egrep1.txt" />
113            <param name="case_sensitive" value="-i" />
114            <param name="regex_type" value="-E" />
115            <param name="invert" value="" />
116            <param name="url_paste" value="[^ ]+" />
117            <param name="lines_before" value="0" />
118            <param name="lines_after" value="0" />
119            <param name="color" value="NOCOLOR" />
120            <output name="output" file="egrep_results1.txt" />
121        </test>
122        <!-- tests basic regex; + must be backslashed to match -->
123        <test>
124            <param name="infile" value="egrep1.txt" />
125            <param name="case_sensitive" value="-i" />
126            <param name="regex_type" value="-G" />
127            <param name="invert" value="" />
128            <param name="url_paste" value="[^ ]\+" />
129            <param name="lines_before" value="0" />
130            <param name="lines_after" value="0" />
131            <param name="color" value="NOCOLOR" />
132            <output name="output" file="egrep_results1.txt" />
133        </test>
134        <!-- tests regex;matching nothing -->
135        <test>
136            <param name="infile" value="egrep1.txt" />
137            <param name="case_sensitive" value="-i" />
138            <param name="regex_type" value="-G" />
139            <param name="invert" value="" />
140            <param name="url_paste" value="not existent pattern" />
141            <param name="lines_before" value="0" />
142            <param name="lines_after" value="0" />
143            <param name="color" value="NOCOLOR" />
144            <output name="output">
145                <assert_contents>
146                    <has_size value="0"/>
147                </assert_contents>
148            </output>
149        </test>
150        <!-- tests invalid regex; i.e. that we still get exit code 2 -->
151        <test expect_failure="true" expect_exit_code="2">
152            <param name="infile" value="egrep1.txt" />
153            <param name="case_sensitive" value="-i" />
154            <param name="regex_type" value="-G" />
155            <param name="invert" value="" />
156            <param name="url_paste" value="\(" />
157            <param name="lines_before" value="0" />
158            <param name="lines_after" value="0" />
159            <param name="color" value="NOCOLOR" />
160        </test>
161    </tests>
162    <help>
163<![CDATA[
164**What it does**
165
166This tool runs the unix **grep** command on the selected data file.
167
168.. class:: infomark
169
170**TIP:** This tool uses the **perl** regular expression syntax (same as running 'grep -P'). This is **NOT** the POSIX or POSIX-extended syntax (unlike the awk/sed tools).
171
172
173**Further reading**
174
175- Wikipedia's Regular Expression page (http://en.wikipedia.org/wiki/Regular_expression)
176- Regular Expressions cheat-sheet (PDF) (http://www.addedbytes.com/cheat-sheets/download/regular-expressions-cheat-sheet-v2.pdf)
177- Grep Tutorial (http://www.panix.com/~elflord/unix/grep.html)
178
179-----
180
181**Grep Examples**
182
183- **AGC.AAT** would match lines with AGC followed by any character, followed by AAT (e.g. **AGCQAAT**, **AGCPAAT**, **AGCwAAT**)
184- **C{2,5}AGC** would match lines with 2 to 5 consecutive Cs followed by AGC
185- **TTT.{4,10}AAA** would match lines with 3 Ts, followed by 4 to 10 characters (any characeters), followed by 3 As.
186- **^chr([0-9A-Za-z])+** would match lines that begin with chromsomes, such as lines in a BED format file.
187- **(ACGT){1,5}** would match at least 1 "ACGT" and at most 5 "ACGT" consecutively.
188- **hsa|mmu** would match lines containing "hsa" or "mmu" (or both).
189
190-----
191
192**Regular Expression Syntax**
193
194The 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.
195
196- **( ) { } [ ] . * ? + \ ^ $** are all special characters. **\\** can be used to "escape" a special character, allowing that special character to be searched for.
197- **^** matches the beginning of a string(but not an internal line).
198- **\\d** matches a digit, same as [0-9].
199- **\\D** matches a non-digit.
200- **\\s** matches a whitespace character.
201- **\\S** matches anything BUT a whitespace.
202- **\\t** matches a tab.
203- **\\w** matches an alphanumeric character ( A to Z, 0 to 9 and underscore )
204- **\\W** matches anything but an alphanumeric character.
205- **(** .. **)** groups a particular pattern.
206- **\\Z** matches the end of a string(but not a internal line).
207- **{** n or n, or n,m **}** specifies an expected number of repetitions of the preceding pattern.
208
209  - **{n}** The preceding item is matched exactly n times.
210  - **{n,}** The preceding item ismatched n or more times.
211  - **{n,m}** The preceding item is matched at least n times but not more than m times.
212
213- **[** ... **]** 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**.
214- **.** Matches any single character except a newline.
215- ***** The preceding item will be matched zero or more times.
216- **?** The preceding item is optional and matched at most once.
217- **+** The preceding item will be matched one or more times.
218- **^** has two meaning:
219  - matches the beginning of a line or string.
220  - indicates negation in a character class. For example, [^...] matches every character except the ones inside brackets.
221- **$** matches the end of a line or string.
222- **\|** Separates alternate possibilities.
223
224]]>
225    </help>
226    <expand macro="citations" />
227</tool>