Writing to Sequence
For writing to the file Bio.Seq module has a write() method, which writes the set of sequences to the file and returns an integer representing the number of records written. Ensure to close the handle after calling the handle else data gets flushed to disk. Syntax and arguments of write() method are given below :
Bio.SeqIO.write(sequences, handle, format)
Arguments | Description |
---|---|
sequences | List or iterator of SeqRecord object(or single SeqRecord in Biopython version 1.54 or later) |
handle | Handle to file or takes filename as string(older versions only take handle) |
format | File format to write as a lowercase string |
Note: To download files click here
Python3
# Import libraries from Bio import SeqIO from Bio.Seq import Seq from Bio.SeqRecord import SeqRecord rec1 = SeqRecord(Seq( "MMYQQGCFAGGTVLRLAKDLAENNRGARVLVVCSEITAVTFRGPSETHLDSMVGQALFGD" + "GAGAVIVGSDPDLSVERPLYELVWTGATLLPDSEGAIDGHLREVGLTFHLLKDVPGLISK" + "NIEKSLKEAFTPLGISDWNSTFWIAHPGGPAILDQVEAKLGLKEEKMRATREVLSEYGNM" ), id = "gi|14150838|gb|AAK54648.1|AF376133_1" , description = "chalcone synthase [Cucumis sativus]" ) rec2 = SeqRecord(Seq( "MVTVEEFRRAQCAEGPATVMAIGTATPSNCVDQSTYPDYYFRITNSEHKVELKEKFKRMC" + "EKSMIKKRYMHLTEEILKENPNICAYMAPSLDARQDIVVVEVPKLGKEAAQKAIKEWGQP" + "KSKITHLVFCTTSGVDMPGCDYQLTKLLGLRPSVKRFMMYQQGCFAGGTVLRMAKDLAEN" + "NKGARVLVVCSEITAVTFRGPNDTHLDSLVGQALFGDGAAAVIIGSDPIPEVERPLFELV" + "SAAQTLLPDSEGAIDGHLREVGLTFHLLKDVPGLISKNIEKSLVEAFQPLGISDWNSLFW" + "IAHPGGPAILDQVELKLGLKQEKLKATRKVLSNYGNMSSACVLFILDEMRKASAKEGLGT" + "TGEGLEWGVLFGFGPGLTVETVVLHSVAT" ), id = "gi|13925890|gb|AAK49457.1|" , description = "chalcone synthase [Nicotiana tabacum]" ) sequences = [rec1, rec2] # Writing to file with open ( "example.fasta" , "w" ) as output_handle: SeqIO.write(sequences, output_handle, "fasta" ) for record in SeqIO.parse( "example.fasta" , "fasta" ): print ( "ID %s" % record. id ) print ( "Sequence length %i" % len (record)) |
Output:
Biopython – Sequence input/output
Biopython has an inbuilt Bio.SeqIO module which provides functionalities to read and write sequences from or to a file respectively. Bio.SeqIO supports nearly all file handling formats used in Bioinformatics. Biopython strictly follows single approach to represent the parsed data sequence to the user with the SeqRecord object.