text: user3332: 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
  
  
  user5190: 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.
  
  user104: 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.
  
  user9693: 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
  
  
original_text: 3332: <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>
   5190: <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>
   104: <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>
   9693: <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>
  