This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Wrapping protein sequence output using python

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")
gene python biopython sequence

Untested, but add this function somewhere at the top:

def split_every_70(s): return [s[i:i+70] for i in range(0,len(s),70)]

then replace outfile.write(.....) with:

outfile.write("\".join([locus_tag,split_every_70(gene)])+"\n")

Also, are you trying to make a fasta file or something? Because if you want to keep this a tab-delimited table then using \n in 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 :>

2 answers

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'

This is cool - and its in the standard lib :) Thanks for sharing!

Pretty nice! Thanks James.

Great! Exactly what I wanted. It did the job. Many thanks to you James.

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.