• 玩转ChatGPT:批量下载Alphafold的蛋白pdb文件


    一、写在前面

    突发奇想,想批量下载Alphafold网站的蛋白pdb文件,后续再做个分子对接用。又不想手动下载,来求助CSDN和GPT。

    二、CSDN白嫖基础代码

    CSDN大神多,这不,找到一个:
    Alphafold批量下载蛋白的pdb文件_pdb文件下载-CSDN博客

    不过使用的是谷歌浏览器,我自己用的是Edge浏览器,因此需要魔改。

    三、GPT魔改代码

    话不多说,直接上步骤。

    (1)安装Selenium包

    selenium是自动化测试工具,可以理解为Python使用它来操作网页。

    安装代码:

    pip install selenium 

    或者

    conda install selenium

    (2)安装Selenium驱动

    Selenium需要一个驱动程序来与所选的浏览器交互,不同浏览器驱动不同。

    我的是Edge浏览器,下载地址:

    Microsoft Edge WebDriver - Microsoft Edge Developer

    当然,是有版本区别的,因此,先看看我们的版本号:

    下载相应版本的驱动:

    解压,但是要记住放的路劲地址,比如我的是:O:/msedgedriver.exe

    (3)GPT魔改后的代码

    咒语要点主要是告诉GPT我们使用的是Edge浏览器、驱动放置的地点等,经过几轮Debug,代码如下:

    1. import os
    2. from selenium import webdriver
    3. from selenium.webdriver.common.by import By
    4. from selenium.webdriver.edge.service import Service
    5. from selenium.webdriver import EdgeOptions
    6. from selenium.webdriver.support.ui import WebDriverWait
    7. from selenium.webdriver.support import expected_conditions as EC
    8. import time
    9. # Constants
    10. EDGE_DRIVER_PATH = r'O:/msedgedriver.exe'
    11. WORK_PATH = r"O:/"
    12. PROTEIN_FILE = "test1.txt"
    13. XPATH_DOWNLOAD_LINK = "//*[@id=\"main-content-area\"]/app-entry/div[1]/div/app-summary-text/div/div[1]/div[2]/a[1]"
    14. PAGE_LOAD_TIMEOUT = 2500
    15. EXPLICIT_WAIT_TIMEOUT = 30 # 设置显式等待的超时时间
    16. # Edge Options
    17. edge_options = EdgeOptions()
    18. # edge_options.add_argument('--headless')
    19. edge_options.add_argument('--disable-gpu')
    20. service = Service(executable_path=EDGE_DRIVER_PATH)
    21. # Change working directory
    22. os.chdir(WORK_PATH)
    23. # Read proteins from file
    24. with open(PROTEIN_FILE, "r") as f:
    25. proteins = [line.strip() for line in f.readlines()]
    26. failed_downloads = []
    27. # Initialize browser instance
    28. with webdriver.Edge(service=service, options=edge_options) as driver:
    29. driver.set_page_load_timeout(PAGE_LOAD_TIMEOUT)
    30. wait = WebDriverWait(driver, EXPLICIT_WAIT_TIMEOUT)
    31. for protein_id in proteins:
    32. print(f"{protein_id} is downloading!")
    33. driver.get(f"https://alphafold.ebi.ac.uk/entry/{protein_id}")
    34. try:
    35. download_link = wait.until(EC.element_to_be_clickable((By.XPATH, XPATH_DOWNLOAD_LINK)))
    36. download_link.click()
    37. time.sleep(4) # Wait for the file to download
    38. print(f"{protein_id} succeed!")
    39. except Exception as e:
    40. print(f"{protein_id} download failed!")
    41. failed_downloads.append(protein_id)
    42. # Write failed downloads to file
    43. with open("failed.txt", "w") as df:
    44. df.write("\n".join(failed_downloads))

    (4)运行

    首先,把需要下载的蛋白的Uniport的ID整理到一个txt文件,蛋白后面不要有空格,一个蛋白一行!!!我的保存路劲为:O:/。

    准备就绪,运行代码:

    批量下载Alphafold的蛋白pdb文件

    可以看到,自动打开网页,点击下载,简单粗暴有内涵~

  • 相关阅读:
    非洲数十个金融组织遭遇黑客攻击
    jsqlparser:实现基于SQL语法分析的SQL注入攻击检查
    TLS回调函数
    zabbix监控网络连接状态
    Python 基础问题
    bigemap如何添加在线地图图源列表,持续更新中
    Win11无法将值写入注册表项如何解决?
    期货平盘(期货大单压盘)
    【python爬虫】—图片爬取
    Go编译原理系列9(函数内联)
  • 原文地址:https://blog.csdn.net/qq_30452897/article/details/134085275