rstrip() should be better than strip() to avoid unwanted trimming in the head of line
Hi,
I'm trying to rename all the sequences, my purpose is to add the taxonomy to each accession number in query.
The original ones look like this:
>YP_003612801.1
MTDYLLLFVGTVLVNNFVLVKFLGLCPFMGVSKKLETAMGMGLATTFVMTMASICAWLIDTWILIPLGLV
YLRTLAFILVIAVVVQFTEMVVRKTSPALYRLLGIFLPLITTNCAVLGVALLNINLGHNFMQSALYGFSA
AVGFSLVMVLFASIRERLAAADIPAPFRGNAIALVTAGLMSLAFMGFSGLVKL
After I run my script it looks like this
>YP_003612801.1
_Firmicutes_Clostridia_Clostridiales
MTDYLLLFVGTVLVNNFVLVKFLGLCPFMGVSKKLETAMGMGLATTFVMTMASICAWLIDTWILIPLGLV
YLRTLAFILVIAVVVQFTEMVVRKTSPALYRLLGIFLPLITTNCAVLGVALLNINLGHNFMQSALYGFSA
AVGFSLVMVLFASIRERLAAADIPAPFRGNAIALVTAGLMSLAFMGFSGLVKL
I don't know why there are the empty lines among different lines and I want the taxonomy be appended to the same line to the accession number instead of the new line , so this is what i want:
>YP_003612801.1_Firmicutes_Clostridia_Clostridiales
MTDYLLLFVGTVLVNNFVLVKFLGLCPFMGVSKKLETAMGMGLATTFVMTMASICAWLIDTWILIPLGLV
YLRTLAFILVIAVVVQFTEMVVRKTSPALYRLLGIFLPLITTNCAVLGVALLNINLGHNFMQSALYGFSA
AVGFSLVMVLFASIRERLAAADIPAPFRGNAIALVTAGLMSLAFMGFSGLVKL
If I want to run in python does anyone know it?
2 answers
The lines you read include the end-of-line (eol) from the input file. The print command adds its own end-of-line. So you end up with two eol hence one blank line. You can fix this using strip() on the line you read. For example line.strip() will discard eol from line.
This is my guess:
1, your use readline() to get lines from the original file
2, when you use write() to write lines to the new file, you append a \n into the tail of each line
I can take a look at your code if you post it
with open("sequence.fasta") as file:
with open("taxonomy") as name:
for line in taxonomy.readlines():
for i in file.readlines():
if i.startswith(">"):
print(i+"_"+line)
else:
print(i)
Use print with end=""
print(i+"_"+line, end="")
In addition, you can combine your with statements:
with open("sequence.fasta") as file, open("taxonomy") as name:
and your for loops:
for line, i in zip(name, file):
Your code has taxonomy.readlines(), but I assume that should be name.readlines(). There is also no reason to call .readlines() since you are simply iterating over the file. You don't need to load it entirely in memory.
print() will automatically add a line break in the tail
Log in to answer this question.
Your script is doing something wonky, and without looking at your script, we can't help you. Also, please use the formatting bar (especially the
codeoption) to present your post better. I've done it for you this time.Thanks , I just formatted it!