File size: 5,931 Bytes
52da96f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
import os
from utils.mpr import MultipleProcessRunner
from tqdm import tqdm
class Downloader(MultipleProcessRunner):
"""
Download files that has unified resource locator
"""
def __init__(self, base_url, save_path, overwrite=False, skip_error_info=False, **kwargs):
"""
Args:
base_url: Unified Resource Locator of pdb file
save_path: Unified Resource Locator of saving path
overwrite: whether overwrite existing files
"""
super().__init__(**kwargs)
self.base_url = base_url
self.save_path = save_path
self.overwrite = overwrite
self.skip_error_info = skip_error_info
if not overwrite:
# remove existing files in data
self.data = [uniprot for uniprot in tqdm(self.data, desc="Filtering out existing files...")
if not os.path.exists(self.save_path.format(uniprot))]
def _aggregate(self, final_path: str, sub_paths):
pass
def _target_static(self, process_id, data, sub_path, *args):
for i, uniprot in enumerate(data):
url = self.base_url.format(uniprot)
save_path = self.save_path.format(uniprot)
# shell cmd to download files
wget = f"wget -q -o /dev/null {url} -O {save_path}"
rm = f"rm {save_path}"
err = f"echo 'Error: {url} cannot be downloaded!'"
if self.skip_error_info:
err += ">/dev/null"
os.system(f"{wget} || ({rm} && {err})")
self.terminal_progress_bar(process_id, i + 1, len(data), f"Process{process_id} Downloading files...")
def run(self):
"""
Run this function to download files
"""
super().run()
def __len__(self):
return len(self.data)
@staticmethod
# Clear empty files in specific directory
def clear_empty_files(path):
cnt = 0
for file in tqdm(os.listdir(path), desc="Clearing empty files..."):
if os.path.getsize(os.path.join(path, file)) == 0:
os.remove(os.path.join(path, file))
cnt += 1
print(f"Removed {cnt} empty files")
return cnt
class AlphaDBDownloader(Downloader):
"""
Download files from AlphaFold2 database
"""
def __init__(self, uniprot_ids, type: str, save_dir: str, **kwargs):
"""
Args:
uniprots: Uniprot ids
type: Which type of files to download. Must be one of ['pdb', 'mmcif', 'plddt', "pae"]
save_dir: Saving directory
**kwargs:
"""
url_dict = {
"pdb": "https://alphafold.ebi.ac.uk/files/AF-{}-F1-model_v4.pdb",
"mmcif": "https://alphafold.ebi.ac.uk/files/AF-{}-F1-model_v4.cif",
"plddt": "https://alphafold.ebi.ac.uk/files/AF-{}-F1-confidence_v4.json",
"pae": "https://alphafold.ebi.ac.uk/files/AF-{}-F1-predicted_aligned_error_v4.json"
}
save_dict = {
"pdb": "{}.pdb",
"mmcif": "{}.cif",
"plddt": "{}.json",
"pae": "{}.json"
}
base_url = url_dict[type]
save_path = os.path.join(save_dir, save_dict[type])
super().__init__(data=uniprot_ids, base_url=base_url, save_path=save_path, **kwargs)
class PDBDownloader(Downloader):
"""
Download files from PDB
"""
def __init__(self, pdb_ids, type: str, save_dir: str, **kwargs):
"""
Args:
pdb_ids: PDB ids
type: Which type of files to download. Must be one of ['pdb', 'mmcif']
save_dir: Saving directory
"""
url_dict = {
"pdb": "https://files.rcsb.org/download/{}.pdb",
"mmcif": "https://files.rcsb.org/download/{}.cif"
}
save_dict = {
"pdb": "{}.pdb",
"mmcif": "{}.cif"
}
base_url = url_dict[type]
save_path = os.path.join(save_dir, save_dict[type])
super().__init__(data=pdb_ids, base_url=base_url, save_path=save_path, **kwargs)
class CATHDownloader(Downloader):
def __init__(self, cath_ids, save_dir, **kwargs):
"""
Download files from CATH
Args:
cath_ids: CATH ids
save_dir: Saving directory
"""
url = "http://www.cathdb.info/version/v4_3_0/api/rest/id/{}.pdb"
save_path = os.path.join(save_dir, "{}.pdb")
super().__init__(data=cath_ids, base_url=url, save_path=save_path, **kwargs)
def download_pdb(pdb_id: str, format: str, save_path: str):
"""
Download pdb file from PDB
Args:
pdb_id: PDB id
format: File , must be one of ['pdb', 'cif']
save_path: Saving path
"""
url = f"https://files.rcsb.org/download/{pdb_id}.{format}"
wget = f"wget -q -o /dev/null {url} -O {save_path}"
rm = f"rm {save_path}"
err = f"echo 'Error: {url} cannot be downloaded!'"
os.system(f"{wget} || ({rm} && {err})")
def download_af2(uniprot_id: str, format: str, save_path: str):
"""
Download files from AlphaFold2 database
Args:
uniprot_id: Uniprot id
format: File format, must be one of ['pdb', 'cif', 'plddt', 'pae']
save_path: Saving path
"""
url = f"https://alphafold.ebi.ac.uk/files/AF-{uniprot_id}-F1-model_v4.{format}"
wget = f"wget -q -o /dev/null {url} -O {save_path}"
rm = f"rm {save_path}"
err = f"echo 'Error: {url} cannot be downloaded!'"
os.system(f"{wget} || ({rm} && {err})") |