RXNMapper Documentation

repository·main·Indexed 18 days ago

https://github.com/rxn4chemistry/rxnmapper

RXNMapper provides attention-guided atom mapping on reaction SMILES using an ALBERT model trained via unsupervised learning. It includes the RXNMapper class for extracting atom maps and confidence scores, a BatchedMapper for processing large datasets with error handling, and a utility script run_rxnmapper_on_dataset.py for batch processing CSV, TSV, or JSON files.

Tokens
1.3K
Snippets
9
Records
9
Agent score
64%

What's inside RXNMapper

  1. Generate documentation with Sphinx

    main

    You can automatically generate project documentation in HTML, LaTeX, or GitHub Pages formats using Sphinx.

    First, ensure all development dependencies are installed:

    pip install -r dev_requirements.txt

    Then, navigate to the docs_source/ directory, run the module generation script, and use make to compile the desired format.

    # 1. Install dependencies
    pip install -r dev_requirements.txt
    
    # 2. Compile documentation
    cd docs_source/
    sh generate_modules_rst.sh
    
    # For HTML output
    make html
    
    # For LaTeX/PDF output
    make latex
    make latexpdf
    
    # For GitHub Pages
    make github
  2. Map a reaction dataset using run_rxnmapper_on_dataset.py

    main

    To map a large dataset of chemical reactions, use the run_rxnmapper_on_dataset.py script. The input file must be in .csv, .tsv, or .json format and must contain a column named rxn which holds the reaction data.

    Run the script from the examples/ directory using the following command structure:

    python ../scripts/run_rxnmapper_on_dataset.py  \    --file_path example_rxns.csv \    --output_path  out.json \    --batch_size 8
  3. Use BatchedMapper for batching and error handling

    main

    For processing large datasets, use BatchedMapper. It automatically handles batching and provides robust error handling. If an input reaction is invalid, it will not raise an exception; instead, it will return ">>" (for string outputs) or an empty dictionary (for info outputs).

    from rxnmapper import BatchedMapper
    
    # Initialize with a specific batch size
    rxn_mapper = BatchedMapper(batch_size=32)
    rxns = ['CC[O-]~[Na+].BrCC>>CCOCC', 'invalid>>reaction']
    
    # Returns mapped reaction strings directly
    results_strings = list(rxn_mapper.map_reactions(rxns))
    
    # Returns dictionaries containing 'mapped_rxn' and 'confidence'
    results_with_info = list(rxn_mapper.map_reactions_with_info(rxns))
  4. Use RXNMapper for atom mapping

    main

    The RXNMapper class provides attention-guided atom mapping for reaction SMILES. Use the get_attention_guided_atom_maps method to receive both the mapped reaction SMILES and a confidence score for each input.

    from rxnmapper import RXNMapper
    
    rxn_mapper = RXNMapper()
    rxns = ['CC(C)S.CN(C)C=O.Fc1cccnc1F.O=C([O-])[O-].[K+].[K+]>>CC(C)Sc1ncccc1F']
    results = rxn_mapper.get_attention_guided_atom_maps(rxns)
    
    # results format:
    # [{'mapped_rxn': '...', 'confidence': 0.9565619900376546}]
  5. Get attention-guided atom maps with get_attention_guided_atom_maps

    main

    Use the get_attention_guided_atom_maps method to perform reaction mapping on a list of reaction SMILES.

    For each reaction provided in the input list, the method returns a dictionary containing:

    • mapped_rxn: A string representing the reaction SMILES with the newly assigned atom maps.
    • confidence: A float representing the model's confidence score for the mapping.
    # rxns is a list of reaction SMILES strings
    outputs = rxn_mapper.get_attention_guided_atom_maps(rxns)
    
    for out in outputs:
        print(f"Mapped Reaction: {out['mapped_rxn']}")
        print(f"Confidence: {out['confidence']:.2f}")