当前位置: 首页 > article >正文

NCBI-get-spesis-ref-IDs_fast.py

import requests
import os
import re

def download_genome_first(gcf_id):
    # 根据GCF或者GCA号动态选择base_url
    if gcf_id.startswith("GCF"):
        base_url = "https://ftp.ncbi.nlm.nih.gov/genomes/all/GCF/"
    elif gcf_id.startswith("GCA"):
        base_url = "https://ftp.ncbi.nlm.nih.gov/genomes/all/GCA/"
    else:
        print(f"Invalid ID: {gcf_id}. Skipping...")
        return

    # 提取GCF/GCA号的数字部分并按三位分割
    parts = gcf_id.split('_')[1]  # 提取数字部分
    path_parts = [parts[i:i + 3] for i in range(0, len(parts), 3)]
    path_parts.pop()
    ftp_path = base_url + "/".join(path_parts)
    #print(f"Downloading from {ftp_path}")

    # 下载文件
    try:
        kv = {'user-Agent': 'Mozilla/5.0'}
        response = requests.get(ftp_path, headers=kv)
        response.encoding = response.apparent_encoding
    except Exception as e:
        print(f"第一次爬取失败: {e}")
        return

    html = response.text
    pattern = rf'<a href="({gcf_id}[^/]+)/">'
    url = re.findall(pattern, html)
    if not url:
        print(f"未找到匹配的目录: {gcf_id}")
        return

    url_2 = ftp_path + '/' + url[0] + '/' + url[0] + '_genomic.fna.gz'
    print(url_2)

    try:
        response_2 = requests.get(url_2, headers=kv)
    except Exception as e:
        print(f"第二次爬取失败: {e}")
        return

    # 检查请求是否成功
    if response_2.status_code == 200:
        # 从URL中提取文件名
        file_name = url_2.split("/")[-1]

        # 创建完整的文件路径
        file_path = os.path.join(output_dir, file_name)
        os.makedirs(output_dir, exist_ok=True)

        # 将下载的内容写入文件
        with open(file_path, 'wb') as file:
            file.write(response_2.content)
        print(f"Downloaded {file_name} to {output_dir}")
    else:
        print(f"Failed to download file from {url_2}. Status code: {response_2.status_code}")

def batch_download(gcf_file):
    # 读取GCF/GCA编号列表
    with open(gcf_file, 'r') as file:
        gcf_ids = [line.strip() for line in file.readlines()]

    # 批量下载
    for gcf_id in gcf_ids:
        print(f"Processing: {gcf_id}")
        download_genome_first(gcf_id)

# 使用示例
gcf_file = "./species-gcaids.txt"
# 这里是储存GCF/GCA号的txt文件存储路径
output_dir = "./downloads"
# 此处的下载文件存储的目录可以进行修改
batch_download(gcf_file)
print("所有文件已经下载完毕!")


http://www.kler.cn/news/302968.html

相关文章:

  • AI与艺术的碰撞:当机器开始创作,创造力何在?
  • HarmonyOS4升级到Harmonyos Next(Api 11)系列教程
  • 音视频开发之旅(93)-图像超分增强之Real-ESRGAN
  • Vue 3 组合式 API
  • C++ | Leetcode C++题解之第393题UTF-8编码验证
  • RabbitMQ SDK 支持发布、消费,连接恢复,死信队列,多种使用场景
  • db.fsyncLock() 和 db.fsyncUnlock()
  • 前后端分离项目实现SSE
  • Redis Sentinel(哨兵)详解
  • 从JVM角度看对象创建过程和内存布局
  • 基于微信小程序+Java+SSM+Vue+MySQL的宿舍管理系统
  • F1C100S/F1C200S的资料来源说明
  • 快速切换淘宝最新镜像源npm
  • js 时间戳转日期格式
  • 【STM32笔记】STM32CubeIDE介绍
  • 漫谈设计模式 [18]:策略模式
  • ISO 21434与网络安全管理系统(CSMS)的协同作用
  • Java 入门指南:Java 并发编程 —— 同步工具类 Exchanger(交换器)
  • 学生请假管理系统
  • pytest钩子函数
  • Ubuntu22.04安装nginx
  • SpringBoot项目用Aspose-Words将Word转换为PDF文件正常显示中文的正确姿势
  • RP2040 C SDK clocks时钟源配置使用
  • 【Kubernetes】K8s 的鉴权管理(二):基于属性 / 节点 / Webhook 的访问控制
  • 《PhysDiff: Physics-Guided Human Motion Diffusion Model》ICCV2023
  • Rust使用Actix-web和SeaORM库开发WebAPI通过Swagger UI查看接口文档
  • 若依框架使用MyBatis-Plus中的baseMapper的方法报错Invalid bound statement (not found):
  • 中电金信:金融级数字底座“源启”:打造新型数字基础设施 筑牢千行百业数字化转型发展基石
  • sponge创建的服务与dtm连接使用etcd、consul、nacos进行服务注册与发现
  • GPT-4与ChatGPT:人工智能对话的新时代【含国内可用gpt】