pixelfold 5.0.0

Terminal protein structure viewer and residue interaction network engine
//! Getting from what the user typed to a loaded structure and a chosen subset.

use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use pixelfold_core::parser::{Load, ParsedStructure};
use pixelfold_core::{AtomSet, Protein, select};

use crate::resolve::{self, Resolution};

/// Resolve `input` to a file on disk, downloading it if it names a PDB entry
/// that is not cached yet.
pub fn structure_path(input: &str, cache_dir: &Path) -> Result<PathBuf> {
    match resolve::resolve(input, cache_dir)? {
        Resolution::Found(path) => Ok(path),
        Resolution::Fetch { id, dest } => {
            std::fs::create_dir_all(cache_dir)?;
            eprintln!("Fetching {id} from RCSB...");
            pixelfold_fetch::fetch_cif(&id, cache_dir)?;

            Ok(dest)
        }
    }
}

/// Load a structure for analysis, without the surface, i.e. nothing here renders:
/// the model and assembly `load` names.
pub fn analysis_structure(input: &str, cache_dir: &Path, load: &Load) -> Result<Protein> {
    let path = structure_path(input, cache_dir)?;

    read(&path)
        .and_then(|parsed| parsed.protein(load))
        .with_context(|| format!("failed to load {}", path.display()))
}

/// Read and parse a structure once, for a command that works through its models.
pub fn parsed(input: &str, cache_dir: &Path) -> Result<ParsedStructure> {
    let path = structure_path(input, cache_dir)?;

    read(&path).with_context(|| format!("failed to load {}", path.display()))
}

/// The model numbers of a structure already read once, so its warnings are not
/// printed again.
pub fn model_numbers(input: &str, cache_dir: &Path) -> Result<Vec<usize>> {
    let path = structure_path(input, cache_dir)?;
    let parsed = ParsedStructure::read(&path)
        .with_context(|| format!("failed to load {}", path.display()))?;

    Ok(parsed.model_numbers())
}

/// Read the structure at `path`, printing what the parser skipped or cut short.
fn read(path: &Path) -> Result<ParsedStructure> {
    let parsed = ParsedStructure::read(path)?;
    for warning in parsed.warnings() {
        eprintln!("Warning: {warning}");
    }

    Ok(parsed)
}

/// The atoms a query chooses, or every atom when there is no query. The set may
/// be empty.
pub fn selection(protein: &Protein, query: Option<&str>) -> Result<AtomSet> {
    select(query.unwrap_or("all"), protein).map_err(|error| anyhow::anyhow!("{error}"))
}

/// The atoms a query chooses, or every atom when there is no query.
///
/// An empty result throws an error.
pub fn chosen(protein: &Protein, query: Option<&str>) -> Result<AtomSet> {
    let set = selection(protein, query)?;
    if set.is_empty() {
        anyhow::bail!(
            "the selection `{}` matched no atoms",
            query.unwrap_or("all")
        );
    }

    Ok(set)
}