从NCBI下载基因序列

切换背景色
主题: 字体: 切换行号 全选代码块(Ctrl+C复制) wisdom_love1年前贴出, Python 语言
Python代码: 从NCBI下载基因序列
01 #!/usr/bin/env python
02 # -*- coding:utf-8 -*-
03 '''
04 读取文件(每行一个ID)中的GeneID,下载其序列,并保存到文件中
05 '''
06
07 __file__    = 'download_gene.py'
08 __date__    = '2008-10-30'
09 __version__ = '0.1'
10 __author__  = 'Wubin Qu <quwubin@gmail.com> @CZlab @BMI @CHINA'
11 __blog__    = 'http://quwubin.blogspot.com'
12 __license__ = 'GPL v3 License'
13
14 from Bio import Entrez
15
16 def read_id(file_name):
17     '''从文件中读取GeneID'''
18     id_array = []
19     fh = open(file_name, 'r')
20     lines = fh.readlines()
21     for line in lines:
22         id = line.strip()
23         id_array.append(id)
24
25     fh.close()
26
27     id_array = ','.join(id_array)
28     return id_array
29
30 def download_seq (id_array):
31     '''根据GeneID下载相应格式的序列'''
32
33     result_handle = Entrez.efetch(db="nucleotide", rettype="genbank",  id=id_array)
34     result = result_handle.read()
35
36     return result
37
38 def write_to_file(file_out_name, content):
39     '''将序列写入文件中 '''
40     fh = open(file_out_name, 'w')
41     fh.write(content)
42     fh.close()
43
44 def main():
45     '''主控制程序'''
46     file_name = 'id_list.txt'
47     file_out_name = 'sequences.txt'
48     id_array = read_id(file_name)
49     result = download_seq(id_array)
50     write_to_file(file_out_name, result)
51
52 if __name__ == '__main__':
53     main()
返回正常查看模式 返回代码发芽网首页