This is cool - and its in the standard lib :) Thanks for sharing!
• 1 views
•
link
Hi,
Can someone please show me how to output protein sequence wrapped at 70 characters per line using the following code:
for record in SeqIO.parse(filename, "genbank"):
for feature in record.features:
if feature.type == "CDS":
locus_tag = feature.qualifiers.get("locus_tag", ["NoGeneID"])[0]
gene = feature.qualifiers.get("gene", ["NoGeneID"])[0]
outfile.write("\t".join([locus_tag,gene])+"\n")
Python has a nifty module called textwrap which you can use to wrap a long string, for example:
import textwrap dna_seq = 'GTAAGTCCGCTCGCGTAGCTAGCTAGCTGACTGACTGACTGATCGAT' textwrap.fill(dna_seq, width=5) 'GTAAG\nTCCGC\nTCGCG\nTAGCT\nAGCTA\nGCTGA\nCTGAC\nTGACT\nGATCG\nAT'
Hi there,
when I have to write a sequence and break line each "n" character, I use the re module from python. In the example, "\n" is inserted all 4 characters.
import re
seq = "ATCG" * 10
formated_seq = re.sub("(.{4})", "\\1\n", seq, 0, re.DOTALL)
print formated_seq
So, you can format your variable containing the sequence before writing it!
John and and Wocka, thanks for your help and time.
Log in to answer this question.
Untested, but add this function somewhere at the top:
then replace
outfile.write(.....)with:Also, are you trying to make a fasta file or something? Because if you want to keep this a tab-delimited table then using
\nin both your data and for your newline delimiter is a bad idea.But maybe thats just how it goes in the magical world of bioinformatics data formats :>