Python Programming for Biology: Bioinformatics and Beyond


Example FASTA format writer



Download 7,75 Mb.
Pdf ko'rish
bet84/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   80   81   82   83   84   85   86   87   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

Example FASTA format writer

Suppose  we  have  a  sequence  as  either  a  list  or  string  of  one-letter  codes.  Then  we  can

create a FASTA file with that sequence in it quite easily. A function is defined which takes

arguments for the textual comment, the residue sequence and file location:

def writeSingleFastaSequence(comment, sequence, fastaFile):

fileObj = open(fastaFile, "w")

fileObj.write('> %s\n' % comment)

for (n, code) in enumerate(sequence):




if n > 0 and n % 60 == 0:

fileObj.write('\n')

fileObj.write(code)

if n % 60 != 0:

fileObj.write('\n')

fileObj.close()

Notice  how  much  simpler  this  is  than  reading  a  FASTA  file,  although  getting  the

newline characters correct is still a bit fiddly. The function outputs 60 one-letter codes on

each line. The enumerate function is useful in circumstances like this because it not only

gives the residue code but it also gives its position, n, in the sequence. If n is a multiple of

60  (which  is  what  ‘n  %  60  ==  0’  checks)  and  if  n  >  0  then  a  carriage  return  should  be

added to the output. And a final carriage return also needs adding at the end if n is not a

multiple of 60 (if it is then one has already been added).

To expand upon the above example and write multiple sequence records to a FASTA-

format  file  the  next  function  takes  multiple  sequence  strings  and  a  corresponding  list  of

comments. Also, we accept an optional argument to state the width of the sequence lines.

To  increase  the  efficiency  of  the  process  we  avoid  looping  though  all  of  the  characters

(and  writing  them  separately);  the  sequences  have  the  newline  characters  inserted  at  the

appropriate  places  and  are  written  in  their  entirety.  The  number  of  sequence  lines  is

calculated using the width, so that by default a sequence of 60 residues or fewer needs one

line  but  61  residues  need  two.  The  number  of  lines  is  used  to  generate  a  list  of  sub-

sequences by taking the appropriate slice out of the seq for each line, i.e. from width*x to

width*(x+1)for each. The sub-sequences are joined with newlines to generate a long string

of lines.

def writeFastaSeqs(comments, sequences, fastaFile, width=60):

fileObj = open(fastaFile, 'w')

for i, seq in enumerate(sequences):

numLines = 1 + (len(seq)-1)//width

seqLines = [seq[width*x:width*(x+1)] for x in range(numLines)]

seq = '\n'.join(seqLines)

fileObj.write('> %s\n%s\n' % (comments[i], seq))

fileObj.close()

Note how the format ‘> %s\n%s\n’ inserts both the comment and the sequence text at

the  same  time,  thus  avoiding  calling  file.write()  again.  The  join()  function  adds  ‘\n’  in

between all the sequence lines but we still need that extra ‘\n’ at the end. See

Chapter 11

for examples that illustrate how to write FASTA files using BioPython modules.


Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   80   81   82   83   84   85   86   87   ...   514




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish