Biopython BLAST简介基础教程文档

收录于 2023-04-20 00:10:05 · بالعربية · English · Español · हिंदीName · 日本語 · Русский язык · 中文繁體

BLAST代表基本本地路线搜索工具( Basic Local Alignment Search Tool),它用于找出生物学序列之间的相似区域。Biopython提供了Bio.Blast模块来处理NCBI BLAST操作。您可以在本地连接或Internet连接上运行BLAST。
让我们在下一节中简要了解这两个连接 -

1. 通过互联网运行

Biopython提供了Bio.Blast.NCBIWWW模块来调用BLAST的在线版本。为此,我们需要导入以下模块 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio.Blast import NCBIWWW
NCBIWW模块提供qblast功能来查询BLAST在线版本 - https://blast.ncbi.nlm.nih.gov/Blast.cgi 。qblast支持在线版本支持的所有参数。
要获取有关此模块的任何帮助说明,请使用以下命令并了解功能 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> help(NCBIWWW.qblast)
Help on function qblast in module Bio.Blast.NCBIWWW:
qblast(
   program, database, sequence,
   url_base = 'https://blast.ncbi.nlm.nih.gov/Blast.cgi',
   auto_format = None,
   composition_based_statistics = None,
   db_genetic_code = None,
   endpoints = None,
   entrez_query = '(none)',
   expect = 10.0,
   filter = None,
   gapcosts = None,
   genetic_code = None,
   hitlist_size = 50,
   i_thresh = None,
   layout = None,
   lcase_mask = None,
   matrix_name = None,
   nucl_penalty = None,
   nucl_reward = None,
   other_advanced = None,
   perc_ident = None,
   phi_pattern = None,
   query_file = None,
   query_believe_defline = None,
   query_from = None,
   query_to = None,
   searchsp_eff = None,
   service = None,
   threshold = None,
   ungapped_alignment = None,
   word_size = None,
   alignments = 500,
   alignment_view = None,
   descriptions = 500,
   entrez_links_new_window = None,
   expect_low = None,
   expect_high = None,
   format_entrez_query = None,
   format_object = None,
   format_type = 'XML',
   ncbi_gi = None,
   results_file = None,
   show_overview = None,
   megablast = None,
   template_type = None,
   template_length = None
)
   BLAST search using NCBI's QBLAST server or a cloud service provider.
   Supports all parameters of the qblast API for Put and Get.
   Please note that BLAST on the cloud supports the NCBI-BLAST Common
   URL API (http://ncbi.github.io/blast-cloud/dev/api.html).
   To use this feature, please set url_base to 'http://host.my.cloud.service.provider.com/cgi-bin/blast.cgi' and
   format_object = 'Alignment'. for more details, please see 8. Biopython – Overview of BLAST
https://blast.ncbi.nlm.nih.gov/Blast.cgi?PAGE_TYPE = BlastDocs&DOC_TYPE = CloudBlast
   Some useful parameters:
   - program blastn, blastp, blastx, tblastn, or tblastx (lower case)
   - database Which database to search against (e.g. "nr").
   - sequence The sequence to search.
   - ncbi_gi True/False whether to give 'gi' identifier.
   - descriptions Number of descriptions to show. def 500.
   - alignments Number of alignments to show. def 500.
   - expect An expect value cutoff. def 10.0.
   - matrix_name Specify an alt. matrix (PAM30, PAM70, BLOSUM80, BLOSUM45).
   - filter "none" turns off filtering. Default no filtering
   - format_type "HTML", "Text", "ASN.1", or "XML". Def. "XML".
   - entrez_query Entrez query to limit Blast search
   - hitlist_size Number of hits to return. Default 50
   - megablast True/False whether to use MEga BLAST algorithm (blastn only)
   - service plain, psi, phi, rpsblast, megablast (lower case)
   This function does no checking of the validity of the parameters
   and passes the values to the server as is. More help is available at:
   https://ncbi.github.io/blast-cloud/dev/api.html
通常,qblast函数的参数类似于在BLAST网页上设置的参数。这使qblast函数易于理解,并减少了使用它的学习曲线。

2. 连接和搜索

要了解连接和搜索BLAST在线版本的过程,可以通过Biopython对在线BLAST服务器进行简单的序列搜索(在本地序列文件中可用)。
第1步 - 在Biopython目录中创建一个名为blast_example.fasta的文件,并提供以下序列信息作为输入:
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
Example of a single sequence in FASTA/Pearson format:
>sequence A ggtaagtcctctagtacaaacacccccaatattgtgatataattaaaattatattcatat
tctgttgccagaaaaaacacttttaggctatattagagccatcttctttgaagcgttgtc
>sequence B ggtaagtcctctagtacaaacacccccaatattgtgatataattaaaattatattca
tattctgttgccagaaaaaacacttttaggctatattagagccatcttctttgaagcgttgtc
第2步 - 导入NCBIWWW模块。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio.Blast import NCBIWWW
第3步 - 使用python IO模块打开序列文件blast_example.fasta。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> sequence_data = open("blast_example.fasta").read()
>>> sequence_data
'Example of a single sequence in FASTA/Pearson format:\n\n\n> sequence
A\nggtaagtcctctagtacaaacacccccaatattgtgatataattaaaatt
atattcatat\ntctgttgccagaaaaaacacttttaggctatattagagccatcttctttg aagcgttgtc\n\n'
第4步 - 现在,调用传递序列数据作为主要参数的qblast函数。另一个参数代表数据库(nt)和内部程序(blastn)。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> result_handle = NCBIWWW.qblast("blastn", "nt", sequence_data)
>>> result_handle
<_io.StringIO object at 0x000001EC9FAA4558>
blast_results保存搜索结果。可以将其保存到文件中以供以后使用,也可以对其进行解析以获取详细信息。我们将在接下来的部分中学习如何做。
第5步 - 可以使用Seq对象完成相同的功能,而不是使用整个fasta文件,如下所示 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio import SeqIO
>>> seq_record = next(SeqIO.parse(open('blast_example.fasta'),'fasta'))
>>> seq_record.id
'sequence'
>>> seq_record.seq
Seq('ggtaagtcctctagtacaaacacccccaatattgtgatataattaaaattatat...gtc',
SingleLetterAlphabet())
传递Seq对象和record.seq作为主要参数给qblast函数,并调用这个函数。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> result_handle = NCBIWWW.qblast("blastn", "nt", seq_record.seq)
>>> print(result_handle)
<_io.StringIO object at 0x000001EC9FAA4558>
BLAST将自动为序列分配一个标识符。
第6步 - result_handle对象将拥有整个结果,并且可以保存到文件中以备后用。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> with open('results.xml', 'w') as save_file:
>>> blast_results = result_handle.read()
>>> save_file.write(blast_results)
我们将在后面的部分中了解如何解析结果文件。

3. 运行独立的BLAST

本节说明如何在本地系统中运行BLAST。如果您在本地系统中运行BLAST,它可能会更快,并且还用于创建自己的数据库以针对序列进行搜索。
连接BLAST
通常,不建议在本地运行BLAST,因为其体积较大,运行软件需要付出额外的努力以及所涉及的成本。在线BLAST足以满足基本和高级目的。当然,有时您可能需要在本地安装它。
考虑到进行频繁的在线搜索,这可能需要大量时间和大量网络,并且如果有专有序列数据或与IP相关的问题,那么建议在本地安装。
为此,需要执行以下步骤 -
第1步 - 使用给定的链接下载并安装最新的blast二进制文件 - ftp://ftp.ncbi.nlm.nih.gov/blast/executables/blast+/LATEST/ 第2步 - 使用以下链接下载并解压缩最新的必要数据库 - ftp://ftp.ncbi.nlm.nih.gov/blast/db/
BLAST软件在其站点中提供了许多数据库。从blast数据库站点下载alu.n.gz文件,并将其解压缩到alu文件夹中。该文件为FASTA格式。要在blast应用程序中使用此文件,需要先将文件从FASTA格式转换为blast数据库格式。BLAST提供了makeblastdb应用程序来执行此转换。
使用以下代码片段 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
cd /path/to/alu
makeblastdb -in alu.n -parse_seqids -dbtype nucl -out alun
运行上面的代码将解析输入文件alu.n并将BLAST数据库创建为多个文件alun.nsq,alun.nsi等。现在可以查询该数据库以查找序列。我们已经在本地服务器中安装了BLAST,并且还具有示例BLAST数据库,可以查询该数据库。
第3步 - 创建一个样本序列文件来查询数据库,创建一个文件search.fsa并将以下数据放入其中。
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>gnl|alu|Z15030_HSAL001056 (Alu-J)
AGGCTGGCACTGTGGCTCATGCTGAAATCCCAGCACGGCGGAGGACGGCGGAAGATTGCT
TGAGCCTAGGAGTTTGCGACCAGCCTGGGTGACATAGGGAGATGCCTGTCTCTACGCAAA
AGAAAAAAAAAATAGCTCTGCTGGTGGTGCATGCCTATAGTCTCAGCTATCAGGAGGCTG
GGACAGGAGGATCACTTGGGCCCGGGAGTTGAGGCTGTGGTGAGCCACGATCACACCACT
GCACTCCAGCCTGGGTGACAGAGCAAGACCCTGTCTCAAAACAAACAAATAA
>gnl|alu|D00596_HSAL003180 (Alu-Sx)
AGCCAGGTGTGGTGGCTCACGCCTGTAATCCCACCGCTTTGGGAGGCTGAGTCAGATCAC
CTGAGGTTAGGAATTTGGGACCAGCCTGGCCAACATGGCGACACCCCAGTCTCTACTAAT
AACACAAAAAATTAGCCAGGTGTGCTGGTGCATGTCTGTAATCCCAGCTACTCAGGAGGC
TGAGGCATGAGAATTGCTCACGAGGCGGAGGTTGTAGTGAGCTGAGATCGTGGCACTGTA
CTCCAGCCTGGCGACAGAGGGAGAACCCATGTCAAAAACAAAAAAAGACACCACCAAAGG
TCAAAGCATA
>gnl|alu|X55502_HSAL000745 (Alu-J)
TGCCTTCCCCATCTGTAATTCTGGCACTTGGGGAGTCCAAGGCAGGATGATCACTTATGC
CCAAGGAATTTGAGTACCAAGCCTGGGCAATATAACAAGGCCCTGTTTCTACAAAAACTT
TAAACAATTAGCCAGGTGTGGTGGTGCGTGCCTGTGTCCAGCTACTCAGGAAGCTGAGGC
AAGAGCTTGAGGCTACAGTGAGCTGTGTTCCACCATGGTGCTCCAGCCTGGGTGACAGGG
CAAGACCCTGTCAAAAGAAAGGAAGAAAGAACGGAAGGAAAGAAGGAAAGAAACAAGGAG
AG
序列数据是从alu.n文件中收集的; 因此,它与数据库匹配。
第4步 - BLAST软件提供了许多应用程序来搜索数据库,这里使用blastn。blastn应用程序至少需要三个参数:db,query和out。db是指要搜索的数据库; query是要匹配的序列,out是要存储结果的文件。现在,运行以下命令以执行此简单查询-
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
blastn -db alun -query search.fsa -out results.xml -outfmt 5
运行上面的命令将搜索并在result.xml文件中提供输出,如下所示(部分数据)-
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
<?xml version = "1.0"?>
<!DOCTYPE BlastOutput PUBLIC "-//NCBI//NCBI BlastOutput/EN"
   "http://www.ncbi.nlm.nih.gov/dtd/NCBI_BlastOutput.dtd">
<BlastOutput>
   <BlastOutput_program>blastn</BlastOutput_program>
   <BlastOutput_version>BLASTN 2.7.1+</BlastOutput_version>
   <BlastOutput_reference>Zheng Zhang, Scott Schwartz, Lukas Wagner, and Webb
      Miller (2000), "A greedy algorithm for aligning DNA sequences", J
      Comput Biol 2000; 7(1-2):203-14.
   </BlastOutput_reference>
   <BlastOutput_db>alun</BlastOutput_db>
   <BlastOutput_query-ID>Query_1</BlastOutput_query-ID>
   <BlastOutput_query-def>gnl|alu|Z15030_HSAL001056 (Alu-J)</BlastOutput_query-def>
   <BlastOutput_query-len>292</BlastOutput_query-len>
   <BlastOutput_param>
      <Parameters>
         <Parameters_expect>10</Parameters_expect>
         <Parameters_sc-match>1</Parameters_sc-match>
         <Parameters_sc-mismatch>-2</Parameters_sc-mismatch>
         <Parameters_gap-open>0</Parameters_gap-open>
         <Parameters_gap-extend>0</Parameters_gap-extend>
         <Parameters_filter>L;m;</Parameters_filter>
      </Parameters>
   </BlastOutput_param>
   <BlastOutput_iterations>
      <Iteration>
         <Iteration_iter-num>1</Iteration_iter-num><Iteration_query-ID>Query_1</Iteration_query-ID>
         <Iteration_query-def>gnl|alu|Z15030_HSAL001056 (Alu-J)</Iteration_query-def>
         <Iteration_query-len>292</Iteration_query-len>
         <Iteration_hits>
         <Hit>
            <Hit_num>1</Hit_num>
            <Hit_id>gnl|alu|Z15030_HSAL001056</Hit_id>
            <Hit_def>(Alu-J)</Hit_def>
            <Hit_accession>Z15030_HSAL001056</Hit_accession>
            <Hit_len>292</Hit_len>
            <Hit_hsps>
               <Hsp>
                 <Hsp_num>1</Hsp_num>
                  <Hsp_bit-score>540.342</Hsp_bit-score>
                  <Hsp_score>292</Hsp_score>
                  <Hsp_evalue>4.55414e-156</Hsp_evalue>
                  <Hsp_query-from>1</Hsp_query-from>
                  <Hsp_query-to>292</Hsp_query-to>
                  <Hsp_hit-from>1</Hsp_hit-from>
                  <Hsp_hit-to>292</Hsp_hit-to>
                  <Hsp_query-frame>1</Hsp_query-frame>
                  <Hsp_hit-frame>1</Hsp_hit-frame>
                  <Hsp_identity>292</Hsp_identity>
                  <Hsp_positive>292</Hsp_positive>
                  <Hsp_gaps>0</Hsp_gaps>
                  <Hsp_align-len>292</Hsp_align-len>
                  <Hsp_qseq>
                     AGGCTGGCACTGTGGCTCATGCTGAAATCCCAGCACGGCGGAGGACGGCGGAAGATTGCTTGAGCCTAGGAGTTTG
                     CGACCAGCCTGGGTGACATAGGGAGATGCCTGTCTCTACGCAAAAGAAAAAAAAAATAGCTCTGCTGGTGGTGCATG
                     CCTATAGTCTCAGCTATCAGGAGGCTGGGACAGGAGGATCACTTGGGCCCGGGAGTTGAGGCTGTGGTGAGCC
                     ACGATCACACCACTGCACTCCAGCCTGGGTGACAGAGCAAGACCCTGTCTCAAAACAAACAAATAA
                  </Hsp_qseq>
                  <Hsp_hseq>
                     AGGCTGGCACTGTGGCTCATGCTGAAATCCCAGCACGGCGGAGGACGGCGGAAGATTGCTTGAGCCTAGGA
                     GTTTGCGACCAGCCTGGGTGACATAGGGAGATGCCTGTCTCTACGCAAAAGAAAAAAAAAATAGCTCTGCT
                     GGTGGTGCATGCCTATAGTCTCAGCTATCAGGAGGCTGGGACAGGAGGATCACTTGGGCCCGGGAGTTGAGG
                     CTGTGGTGAGCCACGATCACACCACTGCACTCCAGCCTGGGTGACAGAGCAAGACCCTGTCTCAAAACAAAC
                     AAATAA
                  </Hsp_hseq>
                  <Hsp_midline>
                     |||||||||||||||||||||||||||||||||||||||||||||||||||||
                     |||||||||||||||||||||||||||||||||||||||||||||||||||||
                     |||||||||||||||||||||||||||||||||||||||||||||||||||||
                     |||||||||||||||||||||||||||||||||||||||||||||||||||||
                     |||||||||||||||||||||||||||||||||||||||||||||||||||||
                     |||||||||||||||||||||||||||
                  </Hsp_midline>
               </Hsp>
            </Hit_hsps>
         </Hit>
         .........................
         .........................
         .........................
         </Iteration_hits>
         <Iteration_stat>
            <Statistics>
               <Statistics_db-num>327</Statistics_db-num>
               <Statistics_db-len>80506</Statistics_db-len>
               <Statistics_hsp-lenv16</Statistics_hsp-len>
               <Statistics_eff-space>21528364</Statistics_eff-space>
               <Statistics_kappa>0.46</Statistics_kappa>
               <Statistics_lambda>1.28</Statistics_lambda>
               <Statistics_entropy>0.85</Statistics_entropy>
            </Statistics>
         </Iteration_stat>
      </Iteration>
   </BlastOutput_iterations>
</BlastOutput>
上面的命令可以使用以下代码在python内部运行 -
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio.Blast.Applications import NcbiblastnCommandline
>>> blastn_cline = NcbiblastnCommandline(query = "search.fasta", db = "alun",
outfmt = 5, out = "results.xml")
>>> stdout, stderr = blastn_cline()
在这里,第一个是blast输出的句柄,第二个是blast命令生成的可能的错误输出。
由于我们已将输出文件提供为命令行参数(out = "results.xml"),并将输出格式设置为XML(outfmt = 5),因此输出文件将保存在当前工作目录中。

4. 解析BLAST结果

通常,使用NCBIXML模块将BLAST输出解析为XML格式。为此,需要导入以下模块-
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> from Bio.Blast import NCBIXML
现在,使用python open方法直接打开文件,并使用NCBIXML parse方法,如下所示-
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
>>> E_VALUE_THRESH = 1e-20
>>> for record in NCBIXML.parse(open("results.xml")):
>>> if record.alignments:
>>> print("\n")
>>> print("query: %s" % record.query[:100])
>>> for align in record.alignments:
>>> for hsp in align.hsps:
>>> if hsp.expect < E_VALUE_THRESH:
>>> print("match: %s " % align.title[:100])
执行上面代码将产生如下输出:
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-25
query: gnl|alu|Z15030_HSAL001056 (Alu-J)
match: gnl|alu|Z15030_HSAL001056 (Alu-J)
match: gnl|alu|L12964_HSAL003860 (Alu-J)
match: gnl|alu|L13042_HSAL003863 (Alu-FLA?)
match: gnl|alu|M86249_HSAL001462 (Alu-FLA?)
match: gnl|alu|M29484_HSAL002265 (Alu-J)
query: gnl|alu|D00596_HSAL003180 (Alu-Sx)
match: gnl|alu|D00596_HSAL003180 (Alu-Sx)
match: gnl|alu|J03071_HSAL001860 (Alu-J)
match: gnl|alu|X72409_HSAL005025 (Alu-Sx)
query: gnl|alu|X55502_HSAL000745 (Alu-J)
match: gnl|alu|X55502_HSAL000745 (Alu-J)