I've been trying to write a fasta parser which takes a fasta text file (DNA) as an input and outputs an AA sequence and I'm only using the biopython SeqIO module for parsing the input fasta file.
I get my desired output but the problem is that whenever I run the code, I keep getting blank space on the top of my output fasta file and I really want to remove this.
I've been searching through webs but nothing has worked for me so far.
Below is the code that I have so far.
from Bio import SeqIO
CONST_CODON = {'ttt': 'F', 'tct': 'S', 'tat': 'Y', 'tgt': 'C',
'ttc': 'F', 'tcc': 'S', 'tac': 'Y', 'tgc': 'C',
'tta': 'L', 'tca': 'S', 'taa': '*', 'tga': '*',
'ttg': 'L', 'tcg': 'S', 'tag': '*', 'tgg': 'W',
'ctt': 'L', 'cct': 'P', 'cat': 'H', 'cgt': 'R',
'ctc': 'L', 'ccc': 'P', 'cac': 'H', 'cgc': 'R',
'cta': 'L', 'cca': 'P', 'caa': 'Q', 'cga': 'R',
'ctg': 'L', 'ccg': 'P', 'cag': 'Q', 'cgg': 'R',
'att': 'I', 'act': 'T', 'aat': 'N', 'agt': 'S',
'atc': 'I', 'acc': 'T', 'aac': 'N', 'agc': 'S',
'ata': 'I', 'aca': 'T', 'aaa': 'K', 'aga': 'R',
'atg': 'M', 'acg': 'T', 'aag': 'K', 'agg': 'R',
'gtt': 'V', 'gct': 'A', 'gat': 'D', 'ggt': 'G',
'gtc': 'V', 'gcc': 'A', 'gac': 'D', 'ggc': 'G',
'gta': 'V', 'gca': 'A', 'gaa': 'E', 'gga': 'G',
'gtg': 'V', 'gcg': 'A', 'gag': 'E', 'ggg': 'G'
}
def DNA2Prot(f1, f2="translated_fasta.txt"):
with open(f1, 'r') as fin, open(f2, 'w') as fout:
for seq_record in SeqIO.parse(f1,'fasta'):
sequence = seq_record.seq
sequence = sequence.lower()
fout.write('\n'+seq_record.description)
fout.write('\n')
for i in range(0,len(sequence),3):
if sequence[i:i+3] in CONST_CODON:
amino_acid = CONST_CODON[str(sequence[i:i+3])]
fout.write(amino_acid)
if __name__ == "__main__":
test = DNA2Prot('test_fasta.txt')
print test
My current output looks like this.
-----------------blank space--------------
BCB2141
IG*R*SRRESLYSD
BCA2111
MA*SRVEL*GTASSCRRAVEPI*EP
BCA2112
IEPRWVWPV*SPIEPIEIESR*SLRDPRCDAD
My desired output is:
BCB2141
IG*R*SRRESLYSD
BCA2111
MA*SRVEL*GTASSCRRAVEPI*EP
BCA2112
IEPRWVWPV*SPIEPIEIESR*SLRDPRCDAD