<p>If you want to stick to <code>grep</code>, use a scripting language such as Perl to generate the regex programmatically. For example:</p>
<pre><code>perl -le 'print join &quot;&quot;, map &quot;[${_}N]&quot;, split //, $ARGV[0];' ATCGCTATCG 
</code></pre>
<p>Prints:</p>
<pre><code>[AN][TN][CN][GN][CN][TN][AN][TN][CN][GN]
</code></pre>
<p>You can use it in <code>grep</code> like so:</p>
<pre><code>grep '[AN][TN][CN][GN][CN][TN][AN][TN][CN][GN]' &lt;&lt;&lt; ATNGCNATCG
</code></pre>
<p>Prints:</p>
<pre><code>ATNGCNATCG
</code></pre>
<p>If that works for you, you could make it into a little bash function that also runs the <code>grep</code>. Add these lines to your <code>~/.bashrc</code>:</p>
<pre class="lang-bash prettyprint-override"><code>grepN(){
    seq=&quot;$1&quot;
    file=&quot;$2&quot;
    pattern=$(perl -le 'print join &quot;&quot;, map &quot;[${_}N]&quot;, split //, $ARGV[0];' &quot;$seq&quot;)
    grep &quot;$pattern&quot; &quot;$file&quot;
}
</code></pre>
<p>You can now run</p>
<pre><code>grepN ATCGCTATCG my.fastq
</code></pre>
<p>Of course, this is not a good idea since the sequence might be in different lines, but that's what you were doing originally.</p>

CC BY-SA 4.0
2021-03-24T14:16:32.500Z
Awesome, I'll try this out. It will at least save me the time typing out all those N's hah.
RPINerd
CC BY-SA 4.0
2021-03-24T14:42:40.143Z
It does involve one extra step, but saves me tons of time and gets me the data I need! Thanks!
RPINerd
CC BY-SA 4.0
2021-03-30T17:19:43.677Z
@RPINerd I edited Timur's very clever solution to add a shell function that you can use to do the whole thing in one step. I hope you don't mind, Timur, please let me know if you'd rather I posted a separate answer. It just seemed like a trivial extension of yours and you did all the work so...
terdon
CC BY-SA 4.0
2021-03-30T18:17:47.023Z
@terdon Your edits and suggestions are always welcome! Thank you.
Timur Shtatland
CC BY-SA 4.0
2021-03-24T01:39:57.780Z
2021-03-30T17:18:45.377Z
2021-03-30T17:18:45.377Z
Timur Shtatland
<p>Okay so I'm using grep to try and get a preview of some trimming operations that are not going as expected..
Lets say that my sequence in the FastQ file is: ATNGCNATCG</p>
<p>What I want to do is..</p>
<p><code>grep &quot;ATCGCTATCG&quot; my.fastq</code></p>
<p>..and match the sequence given above</p>
<p>Surely there is some way or some existing tool that I can use besides doing:
<code>grep &quot;[A|N][T|N][C|N][G|N][C|N]...etc.&quot;</code></p>

CC BY-SA 4.0
2021-03-24T11:37:09.763Z
Your fix is going to match NNNNN, ANNNC and etc. - are you sure this is what you want?
Greg
CC BY-SA 4.0
2021-03-24T14:06:17.173Z
Technically, no it's not what I want, but as this is just trying to get a picture for what's going on under the hood I think it wont be a problem :)
RPINerd
CC BY-SA 4.0
2021-03-30T17:20:25.837Z
Note that this will fail if the target sequence is split across multiple fastq lines. The fastq format does not _require_ only one line of sequence per entry, you can have multiple ones.
terdon
<fastq><trimming><grep>
Telling grep to treat N as [ATCG]
# Telling grep to treat N as [ATCG]

(Asked by: username_0 on 2021-03-23)

Okay so I'm using grep to try and get a preview of some trimming operations that are not going as expected..
Lets say that my sequence in the FastQ file is: ATNGCNATCG

What I want to do is..

`grep "ATCGCTATCG" my.fastq`

..and match the sequence given above

Surely there is some way or some existing tool that I can use besides doing:
`grep "[A|N][T|N][C|N][G|N][C|N]...etc."`

**Tags:** fastq, trimming, grep
> **Comments:**
> **username_1**: Your fix is going to match NNNNN, ANNNC and etc. - are you sure this is what you want?  
> **username_0**: Technically, no it's not what I want, but as this is just trying to get a picture for what's going on under the hood I think it wont be a problem :)  
> **username_2**: Note that this will fail if the target sequence is split across multiple fastq lines. The fastq format does not _require_ only one line of sequence per entry, you can have multiple ones.  

---

## 1 answers

### Answer by username_3 on 2021-03-24. Score: 5 | Accepted answer

If you want to stick to grep, use a scripting language such as Perl to generate the regex programmatically. For example:
perl -le 'print join "", map "[${_}N]", split //, $ARGV[0];' ATCGCTATCG 

Prints:
[AN][TN][CN][GN][CN][TN][AN][TN][CN][GN]

You can use it in grep like so:
grep '[AN][TN][CN][GN][CN][TN][AN][TN][CN][GN]' <<< ATNGCNATCG

Prints:
ATNGCNATCG

If that works for you, you could make it into a little bash function that also runs the grep. Add these lines to your ~/.bashrc:
grepN(){
    seq="$1"
    file="$2"
    pattern=$(perl -le 'print join "", map "[${_}N]", split //, $ARGV[0];' "$seq")
    grep "$pattern" "$file"
}

You can now run
grepN ATCGCTATCG my.fastq

Of course, this is not a good idea since the sequence might be in different lines, but that's what you were doing originally.

> **Comments:**
> **username_0**: Awesome, I'll try this out. It will at least save me the time typing out all those N's hah.  
> **username_0**: It does involve one extra step, but saves me tons of time and gets me the data I need! Thanks!  
> **username_2**: @RPINerd I edited Timur's very clever solution to add a shell function that you can use to do the whole thing in one step. I hope you don't mind, Timur, please let me know if you'd rather I posted a separate answer. It just seemed like a trivial extension of yours and you did all the work so...  
> **username_3**: @terdon Your edits and suggestions are always welcome! Thank you.  

---

