StructurePreprocessor.fetch_alphafold

StructurePreprocessor.fetch_alphafold(df_seq, out_folder, file_format='pdb', timeout=30.0, skip_existing=True, on_failure='nan', return_df=False, max_workers=None)[source]

Download AlphaFold model + Predicted Aligned Error (PAE) files for every entry into a folder.

Fetches each entry’s F1 structure and PAE sidecar from the AlphaFold Protein Structure Database [Varadi22] (https://alphafold.ebi.ac.uk), saving them under the canonical filenames encode_pdb() / encode_pae() / get_dssp() already resolve — so a single call populates the pdb_folder / pae_folder those methods consume. The download URLs are resolved through the AlphaFold API, so the fetch tracks the current data version automatically (the file naming moved v4 → v6 and will move again). This is the fetch_ (web) acquisition counterpart to the local get_ tools; it downloads all entries in one bulk call.

Added in version 1.1.0.

Parameters:
  • df_seq (pd.DataFrame, shape (n_samples, n_seq_info)) – DataFrame containing an entry column with unique protein identifiers (UniProt accessions). The entry values are used as the AlphaFold-DB accessions to download.

  • out_folder (str or pathlib.Path) – Destination directory for the downloaded files. Its parent must exist; the leaf directory is created if absent.

  • file_format ({'pdb', 'cif'}, default='pdb') –

    Structure file type to download from AlphaFold DB (both are accepted by the downstream resolvers):

    • 'pdb': legacy Protein Data Bank format; matches the encode_pdb / get_dssp examples.

    • 'cif': Crystallographic Information File (mmCIF), the modern format without the line/column limits that constrain 'pdb' for very large structures.

  • timeout (float, default=30.0) – Per-request timeout in seconds.

  • skip_existing (bool, default=True) – If True, an entry whose model and PAE files both already exist in out_folder is not re-downloaded; an entry with only one of the two present re-fetches only the missing file.

  • on_failure ({'nan', 'drop', 'raise'}, default='nan') – Policy for entries missing from AlphaFold DB (a 404 on either file): 'nan' keeps the row marked not-ok; 'drop' removes it from the returned status; 'raise' raises RuntimeError.

  • return_df (bool, default=False) – If True, also return an echo of df_seq with an appended boolean alphafold_ok column as a second element.

  • max_workers (int, optional) – Number of threads for concurrent downloads. None or 1 (default) fetches entries sequentially. Greater than 1 downloads on a thread pool; the status table is reassembled in input order and is identical to the sequential result. Concurrency is opt-in because parallel requests to AlphaFold DB risk HTTP-429 throttling that can turn successful downloads into failures.

Returns:

  • df_status (pd.DataFrame, shape (n_samples, 7)) – Per-entry download status with columns entry, model_ok, pae_ok, alphafold_ok, skipped, model_path, pae_path.

  • df_seq_out (pd.DataFrame) – Returned only when return_df=True. Echo of df_seq plus a boolean alphafold_ok column.

Notes

  • Only single-fragment models are fetched (the canonical AF-<entry>-F1-…-v4 filenames). Proteins fragmented across F2, F3, … in AlphaFold DB have no F1 model and are reported as not-ok.

  • Network failures other than a 404 (timeout, 5xx) raise RuntimeError and abort the bulk download — they are not absorbed by on_failure, which governs only the missing-from-AlphaFold Database (AF-DB) case.

Raises:
  • ValueError – On invalid arguments.

  • RuntimeError – On network / response failure, or if any entry is missing under on_failure='raise'.

Examples

fetch_alphafold bulk-downloads each entry’s AlphaFold-DB model file and its Predicted Aligned Error (PAE) sidecar from https://alphafold.ebi.ac.uk into one folder, saving them under the names encode_pdb / encode_pae / get_dssp already resolve, so a single call populates the folder the encoders consume. It is the fetch_ (web) acquisition verb, the structure-side analog of AnnotationPreprocessor.fetch_uniprot.

It returns a per-entry status DataFrame (entry, model_ok, pae_ok, alphafold_ok, skipped, model_path, pae_path). A 404 (accession not in AlphaFold DB, or a fragmented F2+ protein) is the soft failure governed by on_failure; other network errors raise RuntimeError.

Requires aaanalysis[pro] and network access.

import warnings
import tempfile
from pathlib import Path
import aaanalysis as aa
aa.options['verbose'] = False
warnings.filterwarnings('ignore')

# Three short human proteins from the bundled gamma-secretase set; their ``entry``
# values are UniProt accessions, which is what AlphaFold DB is keyed by.
df_seq = aa.load_dataset(name='DOM_GSEC', n=10)
df_seq = df_seq[df_seq['entry'].isin(['Q14802', 'O43914', 'P01135'])].reset_index(drop=True)

strp = aa.StructurePreprocessor(verbose=False)
af_dir = Path(tempfile.mkdtemp()) / 'alphafold'   # use a persistent project folder in real work

df_status = strp.fetch_alphafold(df_seq=df_seq, out_folder=af_dir)
aa.display_df(df_status[['entry', 'model_ok', 'pae_ok', 'alphafold_ok', 'skipped']], n_rows=10, show_shape=True)
DataFrame shape: (3, 5)
  entry model_ok pae_ok alphafold_ok skipped
1 Q14802 True True True False
2 P01135 True True True False
3 O43914 True True True False

The folder now holds one <entry>.pdb model and one AF-<entry>-F1-predicted_aligned_error_v4.json sidecar per entry, exactly where the encoders look for them, so you can encode straight from it:

print(sorted(p.name for p in af_dir.iterdir()))

dict_pdb = strp.encode_pdb(df_seq=df_seq, pdb_folder=af_dir, features=['plddt'])
dict_pae = strp.encode_pae(df_seq=df_seq, pae_folder=af_dir, features=['pae_row_mean'])
print({entry: (dict_pdb[entry].shape, dict_pae[entry].shape) for entry in dict_pdb})
['AF-O43914-F1-predicted_aligned_error_v4.json', 'AF-P01135-F1-predicted_aligned_error_v4.json', 'AF-Q14802-F1-predicted_aligned_error_v4.json', 'O43914.pdb', 'P01135.pdb', 'Q14802.pdb']
{'Q14802': ((87, 1), (87, 1)), 'P01135': ((160, 1), (160, 1)), 'O43914': ((113, 1), (113, 1))}

file_format selects the structure file type ('pdb' or the modern 'cif'); timeout bounds each request in seconds; skip_existing=True leaves files that are already present alone and re-fetches only what is missing (here the PAE sidecars are reused and only the .cif models are downloaded); on_failure governs 404 soft failures ('nan' keeps the row marked not-ok, 'drop' removes it, 'raise' raises); return_df=True also returns an echo of df_seq with an alphafold_ok column; max_workers > 1 downloads on a thread pool with input-ordered, byte-identical results (opt-in, because parallel AlphaFold-DB requests risk HTTP-429 throttling).

df_status_cif, df_seq_out = strp.fetch_alphafold(df_seq=df_seq, out_folder=af_dir, file_format='cif',
                                                 timeout=60.0, skip_existing=True, on_failure='nan',
                                                 return_df=True, max_workers=2)
aa.display_df(df_seq_out[['entry', 'gene', 'alphafold_ok']], n_rows=10, show_shape=True)
DataFrame shape: (3, 3)
  entry gene alphafold_ok
1 Q14802 FXYD3 True
2 P01135 TGFA True
3 O43914 TYROBP True