Biopython - Sequence Alignment - GeeksforGeeks

11 Oct, 2020
Sequence alignment is a process in which two or more DNA, RNA or Protein sequences are arranged in order specifically to identify the region of similarity among them. Identification of similar provides a lot of information about what traits are conserved among species, how much close are different species genetically, how species evolve, etc. Biopython has a wide range of functionalities for sequence alignment.
Reading Sequence Alignment: Bio.AlignIo provided by Biopython is used to read and write sequence alignments. There are a lot of formats available in bioinformatics to specify sequence alignment data is similar to sequence data. Bio.AlignIO has an API similar to Bio.SeqIO, the only difference is that the Bio.SeqIO works on sequence data while the Bio.AlignIO works on sequence data alignment. Below are some steps to download a sample sequence alignment file :
First open the browser and visit http://pfam.xfam.org/family/browse, where you can see all Pfam families in alphabetical order.
Now choose any family having a lesser number of seed value, as it contains minimum data and easy to work. Let’s move one with PF18225 (http://pfam.xfam.org/family/PF18225).
Click on the alignment section and download the required sequence alignment file in Stockholm format.
Example:
Python3
# Import librariesfrom Bio import AlignIO  # Creating Sequence Alignmentalignment = AlignIO.read(open("PF18225_seed.txt"), "stockholm")  # Print alignment objectprint(alignment)  # Show alignment sequence recordprint("Showing Alignment Sequence Record")for align in alignment:     print(align.seq)
Output:
SingleLetterAlphabet() alignment with 5 rows and 65 columnsAINRNTQQLTQDLRAMPNWSLRFVYIVDRNNQDLLKRPLPPGIM...NRK B3PFT7_CELJU/62-126AVNATEREFTERIRTLPHWARRNVFVLDSQGFEIFDRELPSPVA...NRT K4KEM7_SIMAS/61-125MQNTPAERLPAIIEKAKSKHDINVWLLDRQGRDLLEQRVPAKVA...EGP B7RZ31_9GAMM/59-123ARRHGQEYFQQWLERQPKKVKEQVFAVDQFGRELLGRPLPEDMA...KKP A0A143HL37_9GAMM/57-121TRRHGPESFRFWLERQPVEARDRIYAIDRSGAEILDRPIPRGMA...NKP A0A0X3UC67_9GAMM/57-121
Showing Alignment Sequence RecordAINRNTQQLTQDLRAMPNWSLRFVYIVDRNNQDLLKRPLPPGIMVLAPRLTAKHPYDKVQDRNRKAVNATEREFTERIRTLPHWARRNVFVLDSQGFEIFDRELPSPVADLMRKLDLDRPFKKLERKNRTMQNTPAERLPAIIEKAKSKHDINVWLLDRQGRDLLEQRVPAKVATVANQLRGRKRRAFARHREGPARRHGQEYFQQWLERQPKKVKEQVFAVDQFGRELLGRPLPEDMAPMLIALNYRNRESHAQVDKKPTRRHGPESFRFWLERQPVEARDRIYAIDRSGAEILDRPIPRGMAPLFKVLSFRNREDQGLVNNKP
Reading Multiple Alignments: Generally, most sequence alignment files contain single alignment data, where the read() method is enough to parse it. In the case of multiple sequence alignments, more than two sequences are compared for the best sequence match among them and the result in a single file having multiple sequence alignment. If the sequence alignment format has more than one sequence alignment, then the parse() method is used instead of read() which returns an iterable object which can be iterated to get the actual alignments. A basic example is given below :
Python3
# Import librariesfrom Bio import AlignIO   # Parsing Sequence Alignmentalignment = AlignIO.parse(open("PF18225_seed.txt"), "stockholm")  # Show alignment generatorprint(alignment)  # Printing alignment for alignment in alignments:     print(alignment)
Output:
<generator object parse at 0x00000214C9FDB990>
SingleLetterAlphabet() alignment with 5 rows and 65 columnsAINRNTQQLTQDLRAMPNWSLRFVYIVDRNNQDLLKRPLPPGIM...NRK B3PFT7_CELJU/62-126AVNATEREFTERIRTLPHWARRNVFVLDSQGFEIFDRELPSPVA...NRT K4KEM7_SIMAS/61-125MQNTPAERLPAIIEKAKSKHDINVWLLDRQGRDLLEQRVPAKVA...EGP B7RZ31_9GAMM/59-123ARRHGQEYFQQWLERQPKKVKEQVFAVDQFGRELLGRPLPEDMA...KKP A0A143HL37_9GAMM/57-121TRRHGPESFRFWLERQPVEARDRIYAIDRSGAEILDRPIPRGMA...NKP A0A0X3UC67_9GAMM/57-121
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 ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Python | Get unique values from a list
Create a directory in Python
Defaultdict in Python
Python | Pandas dataframe.groupby()