0
$\begingroup$

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
$\endgroup$
2
  • 2
    $\begingroup$ Need to see what the errors are really 'sys.argv' means the file is from the command line $\endgroup$
    – M__
    Oct 18, 2020 at 18:34
  • $\begingroup$ @Michael I have added the error kindly have a look. $\endgroup$
    – M.Bioinfo
    Oct 20, 2020 at 9:16

3 Answers 3

2
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Second code ... of course and ah yes! $\endgroup$
    – M__
    Oct 20, 2020 at 13:25
2
$\begingroup$

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
$\endgroup$
1
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ so using command line by applying "" Sed ^>.*\n[^>]N[^>] MyfastaFile.fa "" will remove them? $\endgroup$
    – M.Bioinfo
    Oct 20, 2020 at 9:55
  • $\begingroup$ 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 $\endgroup$
    – juanjo75es
    Oct 20, 2020 at 10:01
  • $\begingroup$ thank you, but this command produces a similar header number of the original file nothing change I assume $\endgroup$
    – M.Bioinfo
    Oct 20, 2020 at 10:04
  • $\begingroup$ 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. $\endgroup$
    – juanjo75es
    Oct 20, 2020 at 10:07
  • $\begingroup$ 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. $\endgroup$
    – juanjo75es
    Oct 20, 2020 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.