Biopython - Sequence input/output - GeeksforGeeks

22 Oct, 2020
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.
SeqRecord object provided by the Bio.SeqRecord module holds the metadata of the sequence as well as the information about the sequence. Some main data information are listed below :
Biopython Seq module has a built-in read() method which takes a sequence file and turns it into a single SeqRecord according to the file format. It is able to parse sequence files having exactly one record, if the file has no records or more than one record then an exception is raised. Syntax and arguments of the read() method are given below :
Bio.SeqIO.read(handle, format, alphabet=None)

Python3
# Import librariesfrom Bio import SeqIO  # Reading filerecord = SeqIO.read("sequence.gb", "genbank")  # Showing recordsprint("ID: %s" % record.id)print("Sequence length: %i" % len(record))print("Sequence description: %s" % record.description)
Output:
The Parse() method provided by the Bio.Seq module is used when we have to read multiple records from the handle. It basically converts the sequence file into an iterator which returns the SeqRecords. If the file contains string data then it must be converted to handle to parse it. The file formats where alphabet can’t be determined, it is useful to specify the alphabet explicitly(ex. FASTA). Syntax and arguments of parse() method are given below :
Bio.SeqIO.parse(handle, format, alphabet=None)

Python3
# Import librariesfrom Bio import SeqIO  # Parsing filefilename = "sequence.fasta"for record in SeqIO.parse(filename, "fasta"):      # Showing records    print("ID: %s" % record.id)    print("Sequence length: %i" % len(record))    print("Sequence description: %s" % record.description)
Output :
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)

Note: To download files click here
Python3
# Import librariesfrom Bio import SeqIOfrom Bio.Seq import Seqfrom 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 filewith 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:
Python-BioPython
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Defaultdict in Python
Python | os.path.join() method
Python | Get unique values from a list
Selecting rows in pandas DataFrame based on conditions
Create a directory in Python
Python | Pandas dataframe.groupby()