### Environment Setup and Model Download Source: https://github.com/josiehong/fiddle/blob/main/README.md Commands to initialize the project environment using Conda and download the required pre-trained model weights for Orbitrap and Q-TOF instruments. ```bash conda env create -f environment.yml bash ./running_scripts/download_models.sh ``` -------------------------------- ### Install FIDDLE Dependencies Source: https://context7.com/josiehong/fiddle/llms.txt Commands to set up the Conda environment and download the necessary pre-trained model weights for Orbitrap and Q-TOF analysis. ```bash conda env create -f environment.yml conda activate fiddle bash ./running_scripts/download_models.sh ``` -------------------------------- ### MGF Input Format Example Source: https://github.com/josiehong/fiddle/blob/main/README.md An example of the required MGF (Mascot Generic Format) file structure for inputting mass spectrometry data into FIDDLE. ```mgf BEGIN IONS TITLE=EMBL_MCF_2_0_HRMS_Library000531 PEPMASS=129.01941 CHARGE=1- PRECURSOR_TYPE=[M-H]- PRECURSOR_MZ=129.01941 COLLISION_ENERGY=50.0 SMILES=[H]OC(=O)C([H])=C(C(=O)O[H])C([H])([H])[H] FORMULA=C5H6O4 THEORETICAL_PRECURSOR_MZ=129.018785 PPM=4.844255818912111 SIMULATED_PRECURSOR_MZ=129.02032113281717 41.2041 0.410228 55.7698 0.503672 56.8647 0.461943 85.0296 100.0 129.0196 8.036902 END IONS ``` -------------------------------- ### Live Spectrum Analysis Example Source: https://context7.com/josiehong/fiddle/llms.txt A complete example demonstrating how to fetch a spectrum from GNPS using its API and then run FIDDLE's prediction model on it. This includes setting up the necessary libraries and fetching spectrum data. ```python #!/usr/bin/env python3 """Predict molecular formula for caffeine from GNPS spectrum.""" import requests import tempfile import torch import yaml from collections import OrderedDict from dataset import MGFDataset from model_tcn import MS2FNet_tcn, FormulaEncoder, RescoreHead from utils import formula_refinement, formula_to_dict, formula_to_vector, mass_calculator, vector_to_formula # Fetch spectrum from GNPS USI_URL = "https://metabolomics-usi.gnps2.org/json/" usi = "mzspec:GNPS:GNPS-LIBRARY:accession:CCMSLIB00016149314" resp = requests.get(USI_URL, params={"usi1": usi}, timeout=30) data = resp.json() ``` -------------------------------- ### MGF Input Format Specification Source: https://context7.com/josiehong/fiddle/llms.txt Example of the required Mascot Generic Format (MGF) structure, including essential metadata fields like PEPMASS, CHARGE, and FORMULA. ```mgf BEGIN IONS TITLE=EMBL_MCF_2_0_HRMS_Library000531 PEPMASS=129.01941 CHARGE=1- PRECURSOR_TYPE=[M-H]- PRECURSOR_MZ=129.01941 COLLISION_ENERGY=50.0 SMILES=[H]OC(=O)C([H])=C(C(=O)O[H])C([H])([H])[H] FORMULA=C5H6O4 41.2041 0.410228 55.7698 0.503672 56.8647 0.461943 85.0296 100.0 129.0196 8.036902 END IONS ``` -------------------------------- ### Training TCN Formula Prediction Model Source: https://context7.com/josiehong/fiddle/llms.txt Demonstrates how to train a Temporal Convolutional Network (TCN) model for molecular formula prediction using preprocessed spectrum data. Includes command-line training options and Python code for data loading, model initialization, and optimizer setup. ```python # Command-line training # python train_tcn_gpus.py \ # --train_data ./data/orbitrap_train.pkl \ # --test_data ./data/orbitrap_valid.pkl \ # --config_path ./config/fiddle_tcn_orbitrap.yml \ # --checkpoint_path ./check_point/fiddle_tcn_orbitrap.pt \ # --device 0 1 2 3 # Multi-GPU training from dataset import MS2FDataset from model_tcn import MS2FNet_tcn import torch import torch.optim as optim # Load preprocessed training data train_set = MS2FDataset( './data/orbitrap_train.pkl', noised_times=0, # Data augmentation: add Gaussian noise N(0, 0.1) padding_dim=20 # Pad spectrum for pooling layer divisibility ) # Training configuration config = { 'epochs': 500, 'batch_size': 512, 'lr': 0.001, 'weight_decay': 0.01, 'warmup_ratio': 0.1, 'early_stop_step': 10 } # Initialize model and optimizer model = MS2FNet_tcn(model_config) optimizer = optim.AdamW(model.parameters(), lr=config['lr'], weight_decay=config['weight_decay']) # Loss function: weighted MSE for formula + auxiliary losses # loss = 0.01 * MSE(mass) + MSE(atomnum) + 10 * MSE(hcnum) + weighted_MSE(formula) ``` -------------------------------- ### Initialize Contrastive Learning Dataset Source: https://context7.com/josiehong/fiddle/llms.txt Sets up the MS2FDataset_CL for training with paired spectra. Requires pre-processed pickle files and supports noise augmentation. ```python from dataset import MS2FDataset_CL from torch.utils.data import DataLoader train_set = MS2FDataset_CL('./data/orbitrap_train.pkl', noised_times=2, padding_dim=20) loader = DataLoader(train_set, batch_size=64, shuffle=True) ``` -------------------------------- ### Initialize MS2FNet_tcn Model Source: https://context7.com/josiehong/fiddle/llms.txt Demonstrates how to load configuration files and initialize the TCN-based spectrum encoder for performing a forward pass on spectral data. ```python import torch import yaml from model_tcn import MS2FNet_tcn, FormulaEncoder, RescoreHead with open('./config/fiddle_tcn_orbitrap.yml', 'r') as f: config = yaml.load(f, Loader=yaml.FullLoader) model = MS2FNet_tcn(config['model']) x = torch.randn(1, 7520) env = torch.tensor([[195.0877, 0.5, 1]]) encoded_x, pred_formula, pred_mass, pred_atomnum, pred_hcnum = model(x, env) ``` -------------------------------- ### Initialize MGFDataset for Inference Source: https://context7.com/josiehong/fiddle/llms.txt This snippet demonstrates how to configure and load an MGFDataset for mass spectrometry data processing. It defines encoding parameters such as resolution and adduct-to-charge mappings to ensure compatibility with the inference model. ```python from dataset import MGFDataset from torch.utils.data import DataLoader encoder_config = { 'resolution': 0.2, 'max_mz': 1500, 'use_simulated_precursor_mz': False, 'precursor_type': { '[M+H]+': 1, '[M-H]-': 2, '[M+Na]+': 3, '[M+2H]2+': 4, '[2M+H]+': 5, '[2M-H]-': 6 }, 'type2charge': { '[M+H]+': '+1', '[M-H]-': '-1', '[M+Na]+': '+1', '[M+2H]2+': '+2', '[2M+H]+': '+1', '[2M-H]-': '-1', '[M+H-H2O]+': '+1', '[M-H2O+H]+': '+1', '[M+H-2H2O]+': '+1', '[M+H-NH3]+': '+1', '[M+H+NH3]+': '+1', '[M+NH4]+': '+1', '[M+H-CH2O2]+': '+1', '[M+H-CH4O2]+': '+1', '[M-H-CO2]-': '-1', '[M-CHO2]-': '-1', '[M-H-H2O]-': '-1', '[M-H2O-H]-': '-1' } } dataset = MGFDataset('./demo/input_msms.mgf', encoder_config) print(f"Loaded {len(dataset)} valid spectra") ``` -------------------------------- ### Run FIDDLE Inference Source: https://github.com/josiehong/fiddle/blob/main/README.md Commands to execute the FIDDLE prediction pipeline, with options to include external tool results from SIRIUS and BUDDY for improved accuracy. ```bash python run_fiddle.py --test_data ./demo/input_msms.mgf \ --config_path ./config/fiddle_tcn_orbitrap.yml \ --resume_path ./check_point/fiddle_tcn_orbitrap.pt \ --rescore_resume_path ./check_point/fiddle_rescore_orbitrap.pt \ --result_path ./demo/output_fiddle.csv --device 0 python run_fiddle.py --test_data ./demo/input_msms.mgf \ --config_path ./config/fiddle_tcn_orbitrap.yml \ --resume_path ./check_point/fiddle_tcn_orbitrap.pt \ --rescore_resume_path ./check_point/fiddle_rescore_orbitrap.pt \ --buddy_path ./demo/output_buddy.csv \ --sirius_path ./demo/output_sirius.csv \ --result_path ./demo/output_fiddle_all.csv --device 0 ``` -------------------------------- ### Run FIDDLE Inference Source: https://context7.com/josiehong/fiddle/llms.txt Executes the main inference script to predict molecular formulas from MGF input files, supporting standalone prediction, ensemble integration, and CPU-only execution. ```bash python run_fiddle.py --test_data ./demo/input_msms.mgf --config_path ./config/fiddle_tcn_orbitrap.yml --resume_path ./check_point/fiddle_tcn_orbitrap.pt --rescore_resume_path ./check_point/fiddle_rescore_orbitrap.pt --result_path ./demo/output_fiddle.csv --device 0 python run_fiddle.py --test_data ./demo/input_msms.mgf --config_path ./config/fiddle_tcn_orbitrap.yml --resume_path ./check_point/fiddle_tcn_orbitrap.pt --rescore_resume_path ./check_point/fiddle_rescore_orbitrap.pt --buddy_path ./demo/output_buddy.csv --sirius_path ./demo/output_sirius.csv --result_path ./demo/output_fiddle_all.csv --device 0 python run_fiddle.py --test_data ./demo/input_msms.mgf --config_path ./config/fiddle_tcn_orbitrap.yml --resume_path ./check_point/fiddle_tcn_orbitrap.pt --rescore_resume_path ./check_point/fiddle_rescore_orbitrap.pt --result_path ./demo/output_fiddle.csv --no_cuda ``` -------------------------------- ### Batch Processing with DataLoader Source: https://context7.com/josiehong/fiddle/llms.txt Iterates through a dataset using DataLoader for batch processing of spectral data. It unpacks various data components including spectrum, precursor information, and neutral loss adjustments. ```python loader = DataLoader(dataset, batch_size=1, shuffle=False) for title, precursor_type, spec_arr, env_arr, neutral_add in loader: # title: spectrum identifier # precursor_type: adduct string e.g., '[M-H]-' # spec_arr: (batch, 7500) binned spectrum # env_arr: (batch, 3) [precursor_mz, nce, adduct_index] # neutral_add: (batch, 13) neutral loss adjustment vector print(f"Processing: {title[0]}, adduct: {precursor_type[0]}") ``` -------------------------------- ### Generate MGF File for MS2 Data Source: https://context7.com/josiehong/fiddle/llms.txt Creates a temporary MGF file from precursor mass and peak data. This is essential for preparing input spectra for the FIDDLE model. ```python import tempfile with tempfile.NamedTemporaryFile(suffix='.mgf', delete=False, mode='w') as f: f.write("BEGIN IONS\n") f.write(f"TITLE=caffeine_test\n") f.write(f"PEPMASS={data['precursor_mz']}\n") f.write(f"PRECURSOR_MZ={data['precursor_mz']}\n") f.write("PRECURSOR_TYPE=[M+H]+\n") f.write("COLLISION_ENERGY=40\n") for peak in data['peaks']: f.write(f"{peak[0]} {peak[1]}\n") f.write("END IONS\n") mgf_path = f.name ``` -------------------------------- ### Spectrum Processing Utilities Source: https://context7.com/josiehong/fiddle/llms.txt Contains functions for processing raw MS/MS spectra, including binning spectra into fixed-resolution arrays, parsing collision energy values from various string formats, and filtering spectral data. ```python from utils import generate_ms, parse_collision_energy, filter_spec # Bin a raw spectrum into fixed-resolution array mz_array = [128.05, 129.05, 130.07, 195.09] intensity_array = [10.0, 5.0, 100.0, 50.0] precursor_mz = 195.0877 good_spec, filtered_mz, filtered_int, spec_arr = generate_ms( x=mz_array, y=intensity_array, precursor_mz=precursor_mz, resolution=0.2, # Bin width in Da max_mz=1500, # Maximum m/z charge=1 # Precursor charge (for isotope removal) ) # spec_arr: (7500, 2) array - [normalized_intensity, max_mz_in_bin] # Parse collision energy strings from various formats ce, nce = parse_collision_energy('40 eV', precursor_mz=195.0877, charge=1) print(f"CE: {ce} eV, NCE: {nce}") # CE: 40.0 eV, NCE: 0.102... # Handles multiple formats: '40', '40 eV', '40%', 'HCD (NCE 40%)', etc. ce, nce = parse_collision_energy('NCE=40%', precursor_mz=195.0877, charge=1) print(f"CE: {ce:.2f} eV, NCE: {nce:.2f}") ``` -------------------------------- ### Execute Formula Refinement Pipeline Source: https://context7.com/josiehong/fiddle/llms.txt This snippet performs formula refinement using a best-first search algorithm constrained by mass tolerance. It dynamically updates the search space with atom types derived from initial predictions and returns refined formulas sorted by mass proximity. ```python from utils import formula_refinement, formula_to_dict target_mass = 194.0804 mass_tolerance = 20.0 ppm_mode = True top_k = 5 max_depth = 20 timeout = 300 refine_atom_type = ['C', 'O', 'N', 'H'] refine_atom_num = [3, 3, 3, -1] initial_formulas = ['C8H10N4O2', 'C7H8N4O3'] for f0 in initial_formulas: for atom, cnt in formula_to_dict(f0).items(): if atom == 'H' or atom in refine_atom_type: continue refine_atom_type.append(atom) refine_atom_num.append(max(1, int(cnt))) results = formula_refinement( f0_list=initial_formulas, M=target_mass, delta_M=mass_tolerance, ppm_mode=ppm_mode, K=top_k, D=max_depth, T=timeout, refine_atom_type=refine_atom_type, refine_atom_num=refine_atom_num, ) for i, (formula, mass) in enumerate(zip(results['formula'], results['mass'])): if formula: ppm_error = abs(mass - target_mass) / target_mass * 1e6 print(f"{i+1}. {formula}: {mass:.5f} Da ({ppm_error:.2f} ppm)") ``` -------------------------------- ### Formula Utility Functions Source: https://context7.com/josiehong/fiddle/llms.txt Provides core utility functions for converting between different molecular formula representations, including string, vector, and dictionary formats. Also includes functions for calculating monoisotopic mass and precursor m/z. ```python from utils import ( formula_to_vector, vector_to_formula, formula_to_dict, dict_to_formula, monoisotopic_mass_calculator, mass_calculator, precursor_mz_calculator ) # Formula string to atom-count vector (13 elements) # Order: [C, H, O, N, F, S, Cl, P, B, Br, I, Na, K] formula = 'C8H10N4O2' # Caffeine vec = formula_to_vector(formula) print(f"Vector: {vec}") # [8, 10, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0] # Vector back to formula string reconstructed = vector_to_formula(vec) print(f"Formula: {reconstructed}") # 'C8H10N4O2' # Parse formula to dictionary f_dict = formula_to_dict('C8H10N4O2') print(f"Dict: {f_dict}") # {'C': 8, 'H': 10, 'N': 4, 'O': 2} # Calculate monoisotopic mass mass = monoisotopic_mass_calculator('C8H10N4O2', mode='f') print(f"Monoisotopic mass: {mass:.5f} Da") # 194.08038 # Calculate precursor m/z from neutral mass precursor_mz = precursor_mz_calculator('[M+H]+', mass) print(f"[M+H]+ m/z: {precursor_mz:.5f}") # 195.08820 # Back-calculate neutral mass from observed precursor m/z neutral_mass = mass_calculator('[M+H]+', 195.0877) print(f"Neutral mass: {neutral_mass:.5f} Da") # 194.07988 ``` -------------------------------- ### Load Model and Perform Inference Source: https://context7.com/josiehong/fiddle/llms.txt Loads a pre-trained FIDDLE TCN model and performs inference on an MGF dataset. Handles state dictionary mapping and tensor preparation. ```python import yaml import torch from collections import OrderedDict with open('./config/fiddle_tcn_orbitrap.yml', 'r') as f: config = yaml.load(f, Loader=yaml.FullLoader) model = MS2FNet_tcn(config['model']) state_dict = torch.load('./check_point/fiddle_tcn_orbitrap.pt', map_location='cpu')['model_state_dict'] state_dict = OrderedDict((k.replace('module.', ''), v) for k, v in state_dict.items()) model.load_state_dict(state_dict) model.eval() dataset = MGFDataset(mgf_path, config['encoding']) spec_t = torch.from_numpy(dataset[0][2]).unsqueeze(0).float() with torch.no_grad(): _, pred_f, _, _, _ = model(spec_t, torch.from_numpy(dataset[0][3]).unsqueeze(0).float()) formula_init = vector_to_formula(pred_f[0]) ``` -------------------------------- ### Perform Siamese Rescore Model Inference Source: https://context7.com/josiehong/fiddle/llms.txt This snippet demonstrates how to load a pre-trained Siamese rescore model and rank candidate molecular formulas based on spectrum-formula interaction. It utilizes PyTorch to compute Hadamard products between normalized embeddings and outputs ranked candidates. ```python import torch import torch.nn.functional as F from model_tcn import MS2FNet_tcn, FormulaEncoder, RescoreHead from utils import formula_to_vector formula_encoder = FormulaEncoder(config['model']) rescore_head = RescoreHead(config['model']) ckpt = torch.load('./check_point/fiddle_rescore_orbitrap.pt', map_location='cpu') formula_encoder.load_state_dict(ckpt['formula_encoder_state_dict']) rescore_head.load_state_dict(ckpt['rescore_head_state_dict']) formula_encoder.eval() rescore_head.eval() candidate_formulas = ['C8H10N4O2', 'C7H8N4O3', 'C9H12N4O'] f_vecs = torch.tensor([formula_to_vector(f) for f in candidate_formulas], dtype=torch.float32) with torch.no_grad(): z_spec = F.normalize(encoded_x[0], dim=0) z_form = formula_encoder(f_vecs) z_spec_rep = z_spec.unsqueeze(0).expand(len(candidate_formulas), -1) interaction = z_spec_rep * z_form logits = rescore_head(interaction) rescore_scores = torch.sigmoid(logits).numpy() ranked = sorted(zip(rescore_scores, candidate_formulas), reverse=True) for score, formula in ranked: print(f"{formula}: {score:.6f}") ``` -------------------------------- ### Parse Prediction Results Source: https://context7.com/josiehong/fiddle/llms.txt Reads the output CSV generated by the FIDDLE pipeline to extract ranked candidate formulas and rescore values. ```python import pandas as pd results = pd.read_csv('./demo/output_fiddle.csv') print(results[['ID', 'Refined Formula (0)', 'Rescore (0)']].head()) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.