Код: Выделить всё
def fetch_pdb_data(*pdb_ids):
url = "https://data.rcsb.org/graphql"
headers = {"Content-Type": "application/json"}
variables = {"entry_ids": list(pdb_ids)}
response = requests.post(url, headers=headers, json={"query": PDB_GRAPHQL_QUERY, "variables": variables})
if response.status_code == 200:
result = response.json()
print(json.dumps(result, indent=4)) # Debugging output to print the response structure
# Ensure the 'data' field is present and check the structure of entries
if 'data' in result:
for entry in result['data']['entries']:
print(f"Entry: {entry}") # Debugging: Print the full entry structure
# Try constructing the final output while being more cautious with missing keys
return {
entry['rcsb_id']: {
"resolution": entry.get('rcsb_entry_info', {}).get('resolution_combined', [None])[0],
"entities": [get_entity_info(entity) for entity in entry.get('polymer_entities', [])]
}
for entry in result['data']['entries']
}
else:
print(f"Error in data response: {result}")
return {"error": result.get("errors", "Unknown error")}
else:
print(f"HTTP error {response.status_code}")
return {"error": f"HTTP error {response.status_code}"}
def get_entity_info(entity):
print(f"Entity: {entity}") # Debugging: Print each entity to check its structure
# Check if pdbx_description exists before trying to access it
return {
"molecule": entity.get('pdbx_description', "N/A"), # Access pdbx_description directly
"uniprot_ids": entity.get('rcsb_polymer_entity_container_identifiers', {}).get('uniprot_ids', [])
}
Код: Выделить всё
PDB_GRAPHQL_QUERY = """
query structure ($entry_ids: [String!]!) {
entries(entry_ids: $entry_ids) {
rcsb_id
rcsb_entry_info {
resolution_combined
}
polymer_entities {
pdbx_description
rcsb_polymer_entity_container_identifiers {
uniprot_ids
}
}
}
}
"""
Ошибку 2: Файл не найден
даже если скрипт и ассоциированные файлы находятся в одной папке. Я не уверен, что я делаю неправильно на данный момент. Ожидаемый результат здесь должен заключаться в том, что родственный код сможет использовать то, что я вставил выше, и создавать выходные данные .csv, содержащие: идентификатор PDB, разрешение и идентификатор UniProt.
Подробнее здесь: https://stackoverflow.com/questions/790 ... m-rcsb-dat