使用python批量下载ensembl数据库指定类型的文件
通过python的ftplib批量下载ensembl中指定类型的文件,并且同一个物种的文件保存在一个文件夹中#/usr/bin/pythonimport ftplibimport osimport time###设置下载路径,下载文件类型HOST='ftp.ensemblgenomes.org'DIRN='/pub/release-50/plants/fasta/'feature_lst=['cd
·
- 通过python的ftplib批量下载ensembl中指定类型的文件,并且同一个物种的文件保存在一个文件夹中
- 经常碰到没下载完就停止,遇到这种情况可再次执行脚本,之前下载的文件不会被覆盖。如再次执行,log和summary文件会有点问题,不过不影响下载
--------------------------------------------------------------------------------------------
2021-03-25更新,增加下载gff3和gtf的功能
#!/usr/bin/python
import ftplib
import os
HOST='ftp.ensemblgenomes.org'
def mkdir(path):
dir=os.path.exists(path)
if not dir:
os.makedirs(path)
def get_cdna_cds_pep():
feature_lst=['cdna','cds','pep']
DIRN='/pub/release-50/plants/fasta'
logFile=open('download.log','a')
###链接主机,定义端口###
try:
ftp=ftplib.FTP(HOST)
except ftplib.error_perm:
logFile.write("Can not contect %s\n" %HOST)
return
logFile.write("Connect %s successfully\n"%HOST)
###匿名登陆 f.login()###
try:
ftp.login()
except ftplib.error_perm:
logFile.write("Fail to login\n")
ftp.quit()
return
logFile.write("Login successfully\n")
###获取下载文件###
species_lst=ftp.nlst(DIRN)
for species in species_lst: #遍历每个物种的文件夹
speciesName=species.split("/")[-1]
mkdir(speciesName)
for feature in feature_lst: #遍历每个物种文件夹中的pep, cdna, cds文件夹
path=species+"/"+feature
for file in ftp.nlst(path):
outFilename=file.split("/")[-1]
if 'fa.gz' in file and os.path.exists(outFilename) is False:
with open(speciesName+"/"+outFilename, 'wb') as outfile:
ftp.retrbinary('RETR %s' % file, outfile.write)
logFile.write("file %s is dowloaded successfully\n" %outFilename)
logFile.close()
def get_gff_gtf():
feature_lst=['gff3','gtf']
DIRN='/pub/release-50/plants/'
logFile=open('download.log','a')
###链接主机,定义端口###
try:
ftp=ftplib.FTP(HOST)
except ftplib.error_perm:
logFile.write("Can not contect %s\n" %HOST)
return
logFile.write("Connect %s successfully\n"%HOST)
###匿名登陆 f.login()###
try:
ftp.login()
except ftplib.error_perm:
logFile.write("Fail to login\n")
ftp.quit()
return
logFile.write("Login successfully\n")
###获取下载文件###
for feature in feature_lst:
suffix="50."+feature+".gz" #下载文件后缀
species_lst=ftp.nlst(DIRN+feature)
for species in species_lst: #遍历每个物种的文件夹
speciesName=species.split("/")[-1]
mkdir(speciesName)
for file in ftp.nlst(species):
outFilename=file.split("/")[-1]
if suffix in file and os.path.exists(outFilename) is False:
with open(speciesName+"/"+outFilename, 'wb') as outfile:
ftp.retrbinary('RETR %s' % file, outfile.write)
logFile.write("file %s is dowloaded successfully\n" %outFilename)
logFile.close()
if __name__ == '__main__':
#get_cdna_cds_pep()
get_gff_gtf()
更多推荐
已为社区贡献4条内容
所有评论(0)