moonsmiles

    A comprehensive cheminformatics library for processing SMILES notations, analyzing molecular structures, calculating chemical properties, and comparing molecular similarity.

    cheminformatics
    molecular
    chemistry
    SMILES
    parsing
    drug-design
    similarity
    molecular-properties
    Download zip
    Version
    0.1.1
    License
    Apache-2.0
    Last updated
    last year
    Downloads
    54

    No dependencies

    #MoonSMILES - A Molecular Processing Library for MoonBit

    MoonSMILES is a cheminformatics library implemented in MoonBit language, focusing on processing SMILES (Simplified Molecular Input Line Entry System) strings and calculating molecular properties. The library provides a suite of tools for molecular structure analysis, property prediction, and similarity comparison.

    #Key Features

    #SMILES String Processing

    • Validation & Parsing: Validate SMILES string correctness, check bracket balance, and parse into internal molecular representation
    • Normalization & Standardization: Clean SMILES strings and generate canonical forms to ensure consistent representation
    • Format Conversion: Convert between SMILES strings and molecular structure objects

    #Molecular Structure Analysis

    • Atom & Bond Counting: Calculate the number of atoms and bonds in molecules
    • Ring Structure Detection: Find all ring structures and distinguish between aromatic and non-aromatic rings
    • Substructure Searching: Detect specific structural patterns within molecules
    • Formula Generation: Generate molecular formulas (like C2H6O) from structure

    #Drug Property Analysis

    • Molecular Weight Calculation: Compute exact mass of molecules
    • LogP Prediction: Estimate octanol-water partition coefficient (lipophilicity/hydrophilicity indicator)
    • Lipinski Rule Checking: Evaluate drug-likeness of molecules
    • Hydrogen Bond Properties: Calculate hydrogen bond donors and acceptors
    • Rotatable Bond Counting: Analyze molecular flexibility

    #Molecular Similarity Comparison

    • Molecular Fingerprint Generation: Create bit vectors representing molecular structural features
    • Similarity Calculation: Compare structural similarity between molecules using Tanimoto coefficient

    #Usage Examples

    #Basic SMILES Processing

    test {
    // Validate SMILES string
    let is_valid = validate_smiles("C=C(C)O") // true
    assert_eq(is_valid, true)
    }

    test {
    // Normalization
    let normalized_result = normalize_smiles("C = C ( C ) O") // returns Result[String, String]
    let normalized = normalized_result.unwrap() // or use match if .unwrap() is not available
    assert_eq(normalized, "C=C(C)O")
    }

    test {
    // Parse into molecular structure
    let molecule = parse_smiles("CCO") // ethanol
    assert_eq(molecule.atoms.length(), 3)
    }

    test {
    // Generate canonical SMILES
    let molecule = parse_smiles("CCO")
    let canonical = generate_canonical_smiles(molecule)
    assert_eq(canonical, "CCO")
    }

    #Molecular Property Calculation

    test {
    // Calculate molecular weight
    let molecule = parse_smiles("CCO")
    let mol_weight = calculate_molecular_weight(molecule) // ~46.07 g/mol
    assert_eq(mol_weight > 46.0 && mol_weight < 46.1, true)
    }

    test {
    // Calculate LogP value (partition coefficient)
    let molecule = parse_smiles("CCO")
    let logp = calculate_logp(molecule) // negative value indicates hydrophilicity
    assert_eq(logp < 0.0, true)
    }

    test {
    // Check drug-likeness
    let molecule = parse_smiles("CCO")
    let (passes_lipinski, _) = check_lipinski_rule_of_five(molecule)
    assert_eq(passes_lipinski, true)
    }

    test {
    // Count rotatable bonds
    let molecule = parse_smiles("CCO")
    let rotatable_bonds = count_rotatable_bonds(molecule)
    assert_eq(rotatable_bonds, 1)
    }

    #Structure Analysis

    test {
    // Find all rings
    let molecule = parse_smiles("c1ccccc1") // benzene
    let rings = find_all_rings(molecule)
    assert_eq(rings.length(), 1)
    }

    test {
    // Identify aromatic rings
    let molecule = parse_smiles("c1ccccc1") // benzene
    let aromatic_rings = identify_aromatic_rings(molecule)
    assert_eq(aromatic_rings.length(), 1)
    }

    test {
    // Check for specific substructures
    let molecule = parse_smiles("CCO")
    let has_hydroxyl = contains_substructure(molecule, "O") // check for hydroxyl group
    assert_eq(has_hydroxyl, true)
    }

    #Molecular Similarity

    test {
    // Calculate similarity between two molecules
    let mol1 = parse_smiles("CCO")
    let mol2 = parse_smiles("CCC")
    let similarity = calculate_similarity(mol1, mol2)
    assert_eq(similarity > 0.4, true)
    }

    test {
    // Calculate similarity directly from SMILES
    let smiles_similarity = calculate_smiles_similarity("CCO", "CCC")
    assert_eq(smiles_similarity > 0.4, true)
    }

    #Special Features

    • Support for extended atom representations including isotope labeling and charge states: [13CH3], [NH4+]
    • Support for aromaticity representation: c1ccccc1 (benzene)
    • Handling of complex ring structures and multi-ring systems

    #Application Domains

    • Drug discovery and design
    • Compound screening and virtual screening
    • Chemical database searching
    • Structure-activity relationship (SAR) studies
    • Chemical education and research

    MoonSMILES library provides a powerful and easy-to-use toolset for cheminformatics and computational chemistry research, implemented in MoonBit language for efficiency, safety, and readability.

    SmilesError

    type SmilesError

    Defines errors that can occur during SMILES processing

    Atom

    pub(all) struct Atom {
    symbol : String
    id : Int
    bonds : Array[Bond]
    charge : Int
    chiral : String
    aromatic : Bool
    isotope : Int?
    explicit_hydrogens : Int?
    }

    Represents an atom in a molecule with enhanced properties
    impl Show for Atom

    Atom::new

    fn Atom::new(symbol : String, id : Int) -> Atom

    Creates a new Atom instance with default values

    Bond

    pub(all) struct Bond {
    atom1 : Int
    atom2 : Int
    bond_type : String
    }

    Represents a bond between two atoms
    impl Show for Bond

    Molecule

    pub(all) struct Molecule {
    atoms : Array[Atom]
    bonds : Array[Bond]
    }

    Represents a molecule
    impl Show for Molecule

    Molecule::new

    fn Molecule::new() -> Molecule

    calculate_fingerprint

    fn calculate_fingerprint(mol : Molecule) -> Array[Bool]

    计算改进版分子指纹

    calculate_implicit_hydrogens

    fn calculate_implicit_hydrogens(atom : Atom, _ : Molecule) -> Int

    计算隐式氢原子数,特别处理芳香碳的情况

    calculate_logp

    fn calculate_logp(mol : Molecule) -> Double

    计算分子的脂水分配系数(LogP)估计值

    calculate_molecular_weight

    fn calculate_molecular_weight(mol : Molecule) -> Double

    Calculates the molecular weight of a molecule in g/mol, including implicit hydrogens

    calculate_similarity

    fn calculate_similarity(mol1 : Molecule, mol2 : Molecule) -> Double

    Calculates Tanimoto similarity between two molecules 返回值范围:0.0(完全不同)到1.0(完全相同)

    calculate_smiles_similarity

    fn calculate_smiles_similarity(smiles1 : String, smiles2 : String) -> Double raise SmilesError

    Calculates similarity between two SMILES strings

    calculate_smiles_weight

    fn calculate_smiles_weight(smiles : String) -> Double raise SmilesError

    Calculates the molecular weight of a SMILES string in g/mol

    check_lipinski_rule_of_five

    fn check_lipinski_rule_of_five(mol : Molecule) -> (Bool, Array[String])

    检查分子是否符合Lipinski五规则

    contains_any

    fn contains_any(s : String, chars : Array[String]) -> Bool

    Checks if a string contains any of the characters from the given array

    contains_substructure

    fn contains_substructure(mol : Molecule, pattern : String) -> Bool raise SmilesError

    Determines if a molecule contains a specific substructure pattern

    count_atoms

    fn count_atoms(smiles : String) -> Int

    Counts the number of atoms in a SMILES string

    count_atoms_in_molecule

    fn count_atoms_in_molecule(mol : Molecule) -> Int

    Counts the number of atoms in a Molecule

    count_bonds

    fn count_bonds(smiles : String) -> Int raise SmilesError

    Counts the number of bonds in a SMILES string

    count_bonds_in_molecule

    fn count_bonds_in_molecule(mol : Molecule) -> Int

    Counts the number of bonds in a Molecule

    count_hydrogen_bond_acceptors

    fn count_hydrogen_bond_acceptors(mol : Molecule) -> Int

    Calculates the number of hydrogen bond acceptors in a molecule 氢键受体是氧或氮原子

    count_hydrogen_bond_donors

    fn count_hydrogen_bond_donors(mol : Molecule) -> Int

    Calculates the number of hydrogen bond donors in a molecule 氢键供体是连接到氧或氮上的氢原子

    count_rotatable_bonds

    fn count_rotatable_bonds(mol : Molecule) -> Int

    计算分子中的可旋转键数量

    find_all_rings

    fn find_all_rings(mol : Molecule) -> Array[Array[Int]]

    使用改进的DFS查找分子中的所有环

    generate_canonical_smiles

    fn generate_canonical_smiles(mol : Molecule) -> String

    生成规范化的SMILES表示

    generate_formula

    fn generate_formula(mol : Molecule) -> String

    Generates molecular formula (e.g., C6H6O) from a Molecule

    generate_smiles

    fn generate_smiles(mol : Molecule) -> String

    Converts a Molecule structure into a SMILES string

    get_normal_valence

    fn get_normal_valence(symbol : String) -> Int

    Gets the normal valence of an element

    get_valence

    fn get_valence(atom : Atom) -> Int

    Gets the valence of an atom considering its aromatic state

    has_balanced_parentheses

    fn has_balanced_parentheses(smiles : String) -> Bool

    Checks if a SMILES string has balanced parentheses

    identify_aromatic_rings

    fn identify_aromatic_rings(mol : Molecule) -> Array[Array[Int]]

    标识分子中的芳香环

    identify_ring_bonds

    fn identify_ring_bonds(mol : Molecule) ->
    Set
    [Int]

    识别分子中的环键

    is_atom

    fn is_atom(char : String) -> Bool

    扩展原子类型支持,包括更多的元素

    is_bond

    fn is_bond(char : String) -> Bool

    Checks if a character represents a bond

    is_branch

    fn is_branch(char : String) -> Bool

    Checks if a character represents a branch

    is_lower

    fn is_lower(c : Int) -> Bool

    解析字符是否为小写字母

    is_lowercase

    fn is_lowercase(c : Int) -> Bool

    is_ring_number

    fn is_ring_number(char : String) -> Bool

    is_upper

    fn is_upper(c : Char) -> Bool

    Checks if a character is an uppercase letter (A-Z)

    normalize_smiles

    fn normalize_smiles(smiles : String) -> Result[String, String]

    Normalizes a SMILES string (e.g., removes whitespace and validates)

    parse_bracketed_atom

    fn parse_bracketed_atom(token : String) -> Atom

    解析括号原子表示法,修复同位素解析

    parse_smiles

    fn parse_smiles(smiles : String) -> Molecule raise SmilesError

    解析SMILES字符串,添加对带方括号原子的特殊处理

    parse_smiles_with_rings

    fn parse_smiles_with_rings(smiles : String) -> Molecule raise SmilesError

    Parses a SMILES string into a Molecule structure with ring support
    fn print_molecule(mol : Molecule) -> Unit

    Prints a Molecule structure for debugging

    remove_whitespace

    fn remove_whitespace(smiles : String) -> String

    Removes whitespace from a SMILES string

    string_to_int

    fn string_to_int(s : String) -> Int

    将数字字符串转换为整数

    to_lower_case

    fn to_lower_case(s : String) -> String

    Converts a string to lowercase

    to_upper_case

    fn to_upper_case(s : String) -> String

    Converts a string to uppercase

    tokenize_smiles

    fn tokenize_smiles(smiles : String) -> Array[String]

    Splits a SMILES string into tokens

    tokenize_smiles_extended

    fn tokenize_smiles_extended(smiles : String) -> Array[String]

    增强版 tokenize_smiles_extended,添加调试输出

    tokenize_smiles_with_rings

    fn tokenize_smiles_with_rings(smiles : String) -> Array[String]

    Tokenize SMILES with ring numbers and support for explicit bonds between rings

    validate_and_parse

    fn validate_and_parse(smiles : String) -> Result[Molecule, String] raise SmilesError

    Validates and parses a SMILES string into a Molecule structure

    validate_smiles

    fn validate_smiles(smiles : String) -> Bool

    Validates a SMILES string (仅检查字符有效性,不检查括号平衡)