<p>The 1st code snippet is not an error, it's a deprecation warning. You can remove that warning by dropping the <code>&quot;U&quot;</code> and just specifying <code>&quot;r&quot;</code> as the mode. (The <code>&quot;U&quot;</code> is for universal newlines, which is now the default behaviour since python 3, and should be specified by a separate <code>newline</code> arg anyway).</p>
<p>The 2nd code snippet is a trivial error because you're missing a closing parantheses <code>)</code> on the final line.</p>

CC BY-SA 4.0
2020-10-20T13:25:11.980Z
Second code ... of course and ah yes!
M__
CC BY-SA 4.0
2020-10-20T13:02:37.253Z
2020-10-20T13:02:37.253Z
Chris_Rands
<p>If you're not married to Python, here's a one liner in <a href="https://github.com/lh3/bioawk" rel="nofollow noreferrer">bioawk</a>:</p>
<pre><code>bioawk -c fastx '{ if($seq !~ /[Nn]/) {print &quot;&gt;&quot;$name; print $seq}}' old_file.fa &gt; new_file.fa
</code></pre>

CC BY-SA 4.0
2020-10-20T20:44:36.117Z
2020-10-20T20:44:36.117Z
Bastian Schiffthaler
<p>With Visual Studio Code you can delete them using a regular expression. Replace <code>^&gt;.*\n[^&gt;]*N[^&gt;]*</code> with an empty string
You can also do that in a similar way using the sed Linux command. You can use regular expressions in Python too.</p>

CC BY-SA 4.0
2020-10-20T09:55:58.873Z
so using command line by applying "" Sed ^>.*\n[^>]N[^>] MyfastaFile.fa  "" will remove them?
M.Bioinfo
CC BY-SA 4.0
2020-10-20T10:01:45.757Z
I don't know the exact syntax for the sed command... I think it's something like  `sed 's/^>.*\n[^>]N[^>]//g' yourfile.fa > yourfilewithoutn.fa`
juanjo75es
CC BY-SA 4.0
2020-10-20T10:04:33.137Z
thank you, but this command produces a similar header number of the original file nothing change I assume
M.Bioinfo
CC BY-SA 4.0
2020-10-20T10:07:19.763Z
That will work in VS Code... I did not try with sed but if you write it with the right syntax it will work too.
juanjo75es
CC BY-SA 4.0
2020-10-20T13:19:18.850Z
There was a  typo in the regex expression due to not marking it as code. It's `^>.*\n[^>]*N[^>]*`. The sed command also is wrong for that reason.
juanjo75es
CC BY-SA 4.0
2020-10-20T09:48:17.173Z
2020-10-20T13:17:07.133Z
2020-10-20T13:17:07.133Z
juanjo75es
<p>I have Fasta file contain multiple sequences, some of those sequences contain N's or n's, I'm trying to discard all the sequences and related header that contain Ns or ns in the file.</p>
<p>I have tried multiple python code but return an error.</p>
<pre><code>import sys
from Bio import SeqIO
handle = open(sys.argv[1], &quot;rU&quot;)
filtered = [record for record in SeqIO.parse(handle, &quot;fasta&quot;) if record.seq.count('N') == 0]
output_handle = open(&quot;N_removed.fasta&quot;, &quot;w&quot;)
SeqIO.write(filtered, output_handle, &quot;fasta&quot;)
output_handle.close()
handle.close()
</code></pre>
<p>Error:</p>
<pre><code>NsScript.py:3: DeprecationWarning: 'U' mode is deprecated
  handle = open(sys.argv[1], &quot;rU&quot;)
</code></pre>
<p>and</p>
<pre><code>import sys
from Bio import SeqIO
for record in SeqIO.parse(sys.argv[1], &quot;fasta&quot;):
    if record.seq.count('N') == 0:
        print(record.format(&quot;fasta&quot;)
</code></pre>
<p>Error:</p>
<pre><code>/RemoveNs.py&quot;, line 6
    
                                        ^
SyntaxError: unexpected EOF while parsing
</code></pre>

CC BY-SA 4.0
2020-10-18T18:34:06.187Z
Need to see what the errors are really 'sys.argv' means the file is from the command line
M__
CC BY-SA 4.0
2020-10-20T09:16:10.067Z
@Michael I have added the error kindly have a look.
M.Bioinfo
<filtering>
Discard sequences contain Ns
# Discard sequences contain Ns

(Asked by: username_0 on 2020-10-18)

I have Fasta file contain multiple sequences, some of those sequences contain N's or n's, I'm trying to discard all the sequences and related header that contain Ns or ns in the file.
I have tried multiple python code but return an error.
import sys
from Bio import SeqIO
handle = open(sys.argv[1], "rU")
filtered = [record for record in SeqIO.parse(handle, "fasta") if record.seq.count('N') == 0]
output_handle = open("N_removed.fasta", "w")
SeqIO.write(filtered, output_handle, "fasta")
output_handle.close()
handle.close()

Error:
NsScript.py:3: DeprecationWarning: 'U' mode is deprecated
  handle = open(sys.argv[1], "rU")

and
import sys
from Bio import SeqIO
for record in SeqIO.parse(sys.argv[1], "fasta"):
    if record.seq.count('N') == 0:
        print(record.format("fasta")

Error:
/RemoveNs.py", line 6
    
                                        ^
SyntaxError: unexpected EOF while parsing

**Tags:** filtering
> **Comments:**
> **username_1**: Need to see what the errors are really 'sys.argv' means the file is from the command line  
> **username_0**: @Michael I have added the error kindly have a look.  

---

## 3 answers

### Answer by username_2 on 2020-10-20. Score: 2

The 1st code snippet is not an error, it's a deprecation warning. You can remove that warning by dropping the `"U"` and just specifying `"r"` as the mode. (The `"U"` is for universal newlines, which is now the default behaviour since python 3, and should be specified by a separate `newline` arg anyway).

The 2nd code snippet is a trivial error because you're missing a closing parantheses `)` on the final line.

> **Comments:**
> **username_1**: Second code ... of course and ah yes!  

---

### Answer by username_3 on 2020-10-20. Score: 2

If you're not married to Python, here's a one liner in bioawk:
bioawk -c fastx '{ if($seq !~ /[Nn]/) {print ">"$name; print $seq}}' old_file.fa > new_file.fa


---

### Answer by username_4 on 2020-10-20. Score: 1

With Visual Studio Code you can delete them using a regular expression. Replace `^>.*\n[^>]*N[^>]*` with an empty string
You can also do that in a similar way using the sed Linux command. You can use regular expressions in Python too.

> **Comments:**
> **username_0**: so using command line by applying "" Sed ^>.*\n[^>]N[^>] MyfastaFile.fa  "" will remove them?  
> **username_4**: I don't know the exact syntax for the sed command... I think it's something like  `sed 's/^>.*\n[^>]N[^>]//g' yourfile.fa > yourfilewithoutn.fa`  
> **username_0**: thank you, but this command produces a similar header number of the original file nothing change I assume  
> **username_4**: That will work in VS Code... I did not try with sed but if you write it with the right syntax it will work too.  
> **username_4**: There was a  typo in the regex expression due to not marking it as code. It's `^>.*\n[^>]*N[^>]*`. The sed command also is wrong for that reason.  

---

