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 thepdb_folder/pae_folderthose methods consume. The download URLs are resolved through the AlphaFold API, so the fetch tracks the current data version automatically (the file naming movedv4→v6and will move again). This is thefetch_(web) acquisition counterpart to the localget_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
entrycolumn with unique protein identifiers (UniProt accessions). Theentryvalues 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 theencode_pdb/get_dsspexamples.'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 inout_folderis 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'raisesRuntimeError.return_df (bool, default=False) – If
True, also return an echo ofdf_seqwith an appended booleanalphafold_okcolumn as a second element.max_workers (int, optional) – Number of threads for concurrent downloads.
Noneor1(default) fetches entries sequentially. Greater than1downloads 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 ofdf_seqplus a booleanalphafold_okcolumn.
Notes
Only single-fragment models are fetched (the canonical
AF-<entry>-F1-…-v4filenames). Proteins fragmented acrossF2,F3, … in AlphaFold DB have noF1model and are reported as not-ok.Network failures other than a 404 (timeout, 5xx) raise
RuntimeErrorand abort the bulk download — they are not absorbed byon_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_alphafoldbulk-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 namesencode_pdb/encode_pae/get_dsspalready resolve, so a single call populates the folder the encoders consume. It is thefetch_(web) acquisition verb, the structure-side analog ofAnnotationPreprocessor.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 fragmentedF2+protein) is the soft failure governed byon_failure; other network errors raiseRuntimeError.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>.pdbmodel and oneAF-<entry>-F1-predicted_aligned_error_v4.jsonsidecar 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_formatselects the structure file type ('pdb'or the modern'cif');timeoutbounds each request in seconds;skip_existing=Trueleaves files that are already present alone and re-fetches only what is missing (here the PAE sidecars are reused and only the.cifmodels are downloaded);on_failuregoverns 404 soft failures ('nan'keeps the row marked not-ok,'drop'removes it,'raise'raises);return_df=Truealso returns an echo ofdf_seqwith analphafold_okcolumn;max_workers > 1downloads 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