Biopython教程

Biopython序列

Biopython序列详细操作教程
Biopython序列是指一系列字母,用于表示生物体的蛋白质,DNA或RNA。它由Seq类表示。Seq类在Bio.Seq模块中定义。
下面来看看如何在Biopython中创建一个简单的序列,如下所示:
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio.Seq import Seq
>>> seq = Seq("AGCT")
>>> seq
Seq('AGCT')
>>> print(seq)
AGCT
在这里,我们创建了一个简单的蛋白质序列AGCT,每个字母代表丙氨酸,甘氨酸,半胱氨酸和苏氨酸。
每个Seq对象都有两个重要的属性:
data - 实际序列字符串(AGCT) alphabet - 用于表示序列的类型。例如 DNA序列,RNA序列等。默认情况下,它不代表任何序列,本质上是通用的。

1. Alphabet模块

Seq对象包含Alphabet属性,用于指定序列类型,字母和可能的操作。它在Bio.Alphabet模块中定义。Alphabet可以定义如下:
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio.Seq import Seq
>>> myseq = Seq("AGCT")
>>> myseq
Seq('AGCT')
>>> myseq.alphabet
Alphabet()
Alphabet模块提供以下类来表示不同类型的序列。Alphabet是所有字母类型的基类。SingleLetterAlphabet- 具有大小为1的字母的通用字母。它从Alphabet派生,所有其他字母类型也从它派生。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio.Seq import Seq
>>> from Bio.Alphabet import single_letter_alphabet
>>> test_seq = Seq('AGTACACTGGT', single_letter_alphabet)
>>> test_seq
Seq('AGTACACTGGT', SingleLetterAlphabet())
ProteinAlphabet - 通用单字母蛋白质字母。用法如下:
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio.Seq import Seq
>>> from Bio.Alphabet import generic_protein
>>> test_seq = Seq('AGTACACTGGT', generic_protein)
>>> test_seq
Seq('AGTACACTGGT', ProteinAlphabet())
NucleotideAlphabet - 通用单字母核苷酸字母。用法如下:
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio.Seq import Seq
>>> from Bio.Alphabet import generic_nucleotide
>>> test_seq = Seq('AGTACACTGGT', generic_nucleotide) >>> test_seq
Seq('AGTACACTGGT', NucleotideAlphabet())
DNAAlphabet - 通用单字母DNA字母。用法如下:
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio.Seq import Seq
>>> from Bio.Alphabet import generic_dna
>>> test_seq = Seq('AGTACACTGGT', generic_dna)
>>> test_seq
Seq('AGTACACTGGT', DNAAlphabet())
RNAAlphabet - 通用单字母RNA字母。用法如下:
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio.Seq import Seq
>>> from Bio.Alphabet import generic_rna
>>> test_seq = Seq('AGTACACTGGT', generic_rna)
>>> test_seq
Seq('AGTACACTGGT', RNAAlphabet())
Biopython模块Bio.Alphabet.IUPAC提供了IUPAC社区定义的基本序列类型。它包含以下类:
IUPACProtein (protein) - IUPAC 20个标准氨基酸的蛋白质字母。 ExtendedIUPACProtein (extended_protein) - 扩展的大写IUPAC蛋白单字母字母,包括X。 IUPACAmbiguousDNA (ambiguous_dna) - 大写IUPAC含混的DNA。 IUPACUnambiguousDNA (unambiguous_dna) - 大写IUPAC明确的DNA(GATC)。 ExtendedIUPACDNA (extended_dna) - 扩展的IUPAC DNA字母。 IUPACAmbiguousRNA (ambiguous_rna) - 大写IUPAC含混的RNA。 IUPACUnambiguousRNA (unambiguous_rna) - 大写IUPAC明确RNA(GAUC)。
考虑一下IUPACProtein类的简单示例,如下所示 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio.Alphabet import IUPAC
>>> protein_seq = Seq("AGCT", IUPAC.protein)
>>> protein_seq
Seq('AGCT', IUPACProtein())
>>> protein_seq.alphabet
此外,Biopython通过Bio.Data模块公开所有与生物信息学相关的配置数据。例如,IUPACData.protein_letters具有IUPACProtein字母的可能字母。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio.Data import IUPACData
>>> IUPACData.protein_letters
'ACDEFGHIKLMNPQRSTVWY'

2. 基本操作

本节简要说明了Seq类中可用的所有基本操作。序列类似于python字符串。我们可以按顺序执行python字符串操作,例如切片,计数,串联,查找,拆分和剥离。
使用以下代码获取各种输出。
依次获取第一个值:
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> seq_string = Seq("AGCTAGCT")
>>> seq_string[0]
'A'
打印前两个值:
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> seq_string[0:2]
Seq('AG')
打印所有值:
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> seq_string[ : ]
Seq('AGCTAGCT')
执行长度和计数操作:
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> len(seq_string)
8
>>> seq_string.count('A')
2
要添加两个序列:
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio.Alphabet import generic_dna, generic_protein
>>> seq1 = Seq("AGCT", generic_dna)
>>> seq2 = Seq("TCGA", generic_dna)
>>> seq1+seq2
Seq('AGCTTCGA', DNAAlphabet())
在这里,上述两个序列对象seq1,seq2是通用DNA序列,因此可以添加它们并生成新序列。不能添加字母到不兼容的序列,例如下面指定的蛋白质序列和DNA序列:
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> dna_seq = Seq('AGTACACTGGT', generic_dna)
>>> protein_seq = Seq('AGUACACUGGU', generic_protein)
>>> dna_seq + protein_seq
.....
.....
TypeError: Incompatible alphabets DNAAlphabet() and ProteinAlphabet()
>>>
要添加两个或多个序列,请先将其存储在python列表中,然后使用for循环进行检索,最后将其添加在一起,如下所示:
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio.Alphabet import generic_dna
>>> list = [Seq("AGCT",generic_dna),Seq("TCGA",generic_dna),Seq("AAA",generic_dna)]
>>> for s in list:
... print(s)
...
AGCT
TCGA
AAA
>>> final_seq = Seq(" ",generic_dna)
>>> for s in list:
... final_seq = final_seq + s
...
>>> final_seq
Seq('AGCTTCGAAAA', DNAAlphabet())
在下面示例代码中,将根据要求给出各种代码以获取输出。
更改序列的大小写。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio.Alphabet import generic_rna
>>> rna = Seq("agct", generic_rna)
>>> rna.upper()
Seq('AGCT', RNAAlphabet())
检查python成员和身份运算符。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> rna = Seq("agct", generic_rna)
>>> 'a' in rna
True
>>> 'A' in rna
False
>>> rna1 = Seq("AGCT", generic_dna)
>>> rna is rna1
False
查找给定序列内的单个字母或字母序列。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> protein_seq = Seq('AGUACACUGGU', generic_protein)
>>> protein_seq.find('G')
1
>>> protein_seq.find('GG')
8
执行拆分操作。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> protein_seq = Seq('AGUACACUGGU', generic_protein)
>>> protein_seq.split('A')
[Seq('', ProteinAlphabet()), Seq('GU', ProteinAlphabet()),
   Seq('C', ProteinAlphabet()), Seq('CUGGU', ProteinAlphabet())]
在序列中执行剥离操作。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> strip_seq = Seq(" AGCT ")
>>> strip_seq
Seq(' AGCT ')
>>> strip_seq.strip()
Seq('AGCT')
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4