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
| import requests import json import os
def get_PDBFile(dir,proteins_list): ''' create dir/MetaJson, dir/PDB and dir/null_proteins.txt ''' cnt = 0 null_proteins = [] meta_dir = os.path.join(dir,'MetaJson') if not os.path.exists(meta_dir): os.makedirs(meta_dir) pdb_dir = os.path.join(dir,'PDB') if not os.path.exists(pdb_dir): os.makedirs(pdb_dir)
for i in proteins_list: resp = requests.get('https://alphafold.com/api/prediction/'+str(i)+'?key=***') if resp.status_code == 200: json_res = json.loads(resp.text)[0] path_meta = os.path.join(meta_dir,str(i)+'.json') with open(path_meta, 'w', encoding='utf-8') as json_file: json.dump(json_res, json_file, ensure_ascii=False, indent=4) pdb = requests.get(json_res.get('pdbUrl'))
path_pdb = os.path.join(pdb_dir,str(i)+'.pdb') open(path_pdb,'wb').write(pdb.content) cnt += 1 else: print(i) null_proteins.append(i) path_null = os.path.join(dir,'null_proteins.txt') with open(path_null, 'w', encoding='utf-8') as file: for item in null_proteins: file.write(f"{item}\n") print(f'{cnt} pdbfiles were collected !')
|