Skip to content

inputs

Input types for chemical structure representations.

This module provides Maker classes for creating RDKit molecules from various chemical identifiers and representations.

Examples:

>>> from jfchemistry.inputs import Smiles, PubChemCID
>>>
>>> smiles_maker = Smiles(add_hydrogens=True, remove_salts=True)
>>> smiles_job = smiles_maker.make("CCO")
>>> mol = smiles_job.output["structure"]
>>>
>>> # Retrieve molecule from PubChem
>>> pubchem_maker = PubChemCID()
>>> pubchem_job = pubchem_maker.make(702)  # Ethanol
>>> mol = pubchem_job.output["structure"]

PolymerInput dataclass

Bases: CoreMaker


              flowchart TD
              jfchemistry.inputs.PolymerInput[PolymerInput]
              jfchemistry.core.makers.core_maker.CoreMaker[CoreMaker]

                              jfchemistry.core.makers.core_maker.CoreMaker --> jfchemistry.inputs.PolymerInput
                


              click jfchemistry.inputs.PolymerInput href "" "jfchemistry.inputs.PolymerInput"
              click jfchemistry.core.makers.core_maker.CoreMaker href "" "jfchemistry.core.makers.core_maker.CoreMaker"
            

Polymer Input.

Source code in jfchemistry/inputs/polymer.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@dataclass
class PolymerInput(CoreMaker):
    """Polymer Input."""

    name: str = "Polymer Input"
    _output_model: type[PolymerInputOutput] = PolymerInputOutput
    _properties_model: type[Properties] = Properties

    @jfchem_job()
    def make(
        self, monomer: str, head: str | None = None, tail: str | None = None
    ) -> Response[_output_model]:
        """Make a polymer."""
        monomer_mol = rdmolops.AddHs(rdmolfiles.MolFromSmiles(monomer))
        head_mol = rdmolops.AddHs(rdmolfiles.MolFromSmiles(head)) if head else None
        tail_mol = rdmolops.AddHs(rdmolfiles.MolFromSmiles(tail)) if tail else None

        polymer = Polymer(
            head=RDMolMolecule(head_mol) if head_mol else None,
            monomer=RDMolMolecule(monomer_mol),
            tail=RDMolMolecule(tail_mol) if tail_mol else None,
        )
        return Response(output=PolymerInputOutput(structure=polymer))

make

make(monomer: str, head: str | None = None, tail: str | None = None) -> Response[_output_model]

Make a polymer.

Source code in jfchemistry/inputs/polymer.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
@jfchem_job()
def make(
    self, monomer: str, head: str | None = None, tail: str | None = None
) -> Response[_output_model]:
    """Make a polymer."""
    monomer_mol = rdmolops.AddHs(rdmolfiles.MolFromSmiles(monomer))
    head_mol = rdmolops.AddHs(rdmolfiles.MolFromSmiles(head)) if head else None
    tail_mol = rdmolops.AddHs(rdmolfiles.MolFromSmiles(tail)) if tail else None

    polymer = Polymer(
        head=RDMolMolecule(head_mol) if head_mol else None,
        monomer=RDMolMolecule(monomer_mol),
        tail=RDMolMolecule(tail_mol) if tail_mol else None,
    )
    return Response(output=PolymerInputOutput(structure=polymer))

PubChemCID dataclass

Bases: MoleculeInput


              flowchart TD
              jfchemistry.inputs.PubChemCID[PubChemCID]
              jfchemistry.inputs.core.MoleculeInput[MoleculeInput]
              jfchemistry.core.makers.core_maker.CoreMaker[CoreMaker]

                              jfchemistry.inputs.core.MoleculeInput --> jfchemistry.inputs.PubChemCID
                                jfchemistry.core.makers.core_maker.CoreMaker --> jfchemistry.inputs.core.MoleculeInput
                



              click jfchemistry.inputs.PubChemCID href "" "jfchemistry.inputs.PubChemCID"
              click jfchemistry.inputs.core.MoleculeInput href "" "jfchemistry.inputs.core.MoleculeInput"
              click jfchemistry.core.makers.core_maker.CoreMaker href "" "jfchemistry.core.makers.core_maker.CoreMaker"
            

Retrieve molecules from PubChem database by compound ID.

Downloads molecular structures from the PubChem database using the compound identifier (CID). The structure is retrieved in SDF format from PubChem's REST API.

ATTRIBUTE DESCRIPTION
name

Name of the input method (default: "PubChem CID Input").

TYPE: str

remove_salts

Inherited from MoleculeInput.

TYPE: bool

add_hydrogens

Inherited from MoleculeInput.

TYPE: bool

Examples:

>>> from jfchemistry.inputs import PubChemCID
>>>
>>> # Retrieve ethanol (CID: 702)
>>> pubchem = PubChemCID()
>>> job = pubchem.make(702)
>>> mol = job.output["structure"]
>>>
>>> # Retrieve without adding hydrogens
>>> pubchem_no_h = PubChemCID(add_hydrogens=False)
>>> job = pubchem_no_h.make(2244)  # Aspirin
Source code in jfchemistry/inputs/pubchem.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
@dataclass
class PubChemCID(MoleculeInput):
    """Retrieve molecules from PubChem database by compound ID.

    Downloads molecular structures from the PubChem database using the
    compound identifier (CID). The structure is retrieved in SDF format
    from PubChem's REST API.

    Attributes:
        name: Name of the input method (default: "PubChem CID Input").
        remove_salts: Inherited from MoleculeInput.
        add_hydrogens: Inherited from MoleculeInput.

    Examples:
        >>> from jfchemistry.inputs import PubChemCID
        >>>
        >>> # Retrieve ethanol (CID: 702)
        >>> pubchem = PubChemCID()
        >>> job = pubchem.make(702)
        >>> mol = job.output["structure"]
        >>>
        >>> # Retrieve without adding hydrogens
        >>> pubchem_no_h = PubChemCID(add_hydrogens=False)
        >>> job = pubchem_no_h.make(2244)  # Aspirin
    """

    name: str = "PubChem CID Input"

    def _get_structure(self, input: int | str) -> rdchem.Mol:
        """Fetch the structure from PubChem database.

        Downloads the molecular structure from PubChem using the compound ID
        and converts it to an RDKit Mol object.

        Args:
            input: PubChem compound ID (CID) as integer or string.

        Returns:
            RDKit Mol object from PubChem SDF data.
        """
        import requests
        from rdkit.Chem import rdmolfiles

        url = f"https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/{input}/sdf"
        resp = requests.get(url)
        mol = rdmolfiles.MolFromMolBlock(resp.content.decode("utf-8"), removeHs=False)
        return mol

    @job(files="files", properties="properties")
    def make(self, input: int) -> Response[Output]:
        """Create a workflow job to retrieve a molecule from PubChem.

        Args:
            input: PubChem compound ID (CID).

        Returns:
            Response containing:
                - structure: RDMolMolecule from PubChem
                - files: MOL file representation

        """
        return super()._make(input)

make

make(input: int) -> Response[Output]

Create a workflow job to retrieve a molecule from PubChem.

PARAMETER DESCRIPTION
input

PubChem compound ID (CID).

TYPE: int

RETURNS DESCRIPTION
Response[Output]

Response containing: - structure: RDMolMolecule from PubChem - files: MOL file representation

Source code in jfchemistry/inputs/pubchem.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
@job(files="files", properties="properties")
def make(self, input: int) -> Response[Output]:
    """Create a workflow job to retrieve a molecule from PubChem.

    Args:
        input: PubChem compound ID (CID).

    Returns:
        Response containing:
            - structure: RDMolMolecule from PubChem
            - files: MOL file representation

    """
    return super()._make(input)

Smiles dataclass

Bases: MoleculeInput


              flowchart TD
              jfchemistry.inputs.Smiles[Smiles]
              jfchemistry.inputs.core.MoleculeInput[MoleculeInput]
              jfchemistry.core.makers.core_maker.CoreMaker[CoreMaker]

                              jfchemistry.inputs.core.MoleculeInput --> jfchemistry.inputs.Smiles
                                jfchemistry.core.makers.core_maker.CoreMaker --> jfchemistry.inputs.core.MoleculeInput
                



              click jfchemistry.inputs.Smiles href "" "jfchemistry.inputs.Smiles"
              click jfchemistry.inputs.core.MoleculeInput href "" "jfchemistry.inputs.core.MoleculeInput"
              click jfchemistry.core.makers.core_maker.CoreMaker href "" "jfchemistry.core.makers.core_maker.CoreMaker"
            

Create molecules from SMILES strings.

Parses SMILES (Simplified Molecular Input Line Entry System) strings and converts them to RDKit Mol objects. Supports standard SMILES notation including stereochemistry and aromaticity.

ATTRIBUTE DESCRIPTION
name

Name of the input method (default: "SMILES Input").

TYPE: str

remove_salts

Inherited from MoleculeInput.

TYPE: bool

add_hydrogens

Inherited from MoleculeInput.

TYPE: bool

Source code in jfchemistry/inputs/smiles.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@dataclass
class Smiles(MoleculeInput):
    """Create molecules from SMILES strings.

    Parses SMILES (Simplified Molecular Input Line Entry System) strings
    and converts them to RDKit Mol objects. Supports standard SMILES
    notation including stereochemistry and aromaticity.

    Attributes:
        name: Name of the input method (default: "SMILES Input").
        remove_salts: Inherited from MoleculeInput.
        add_hydrogens: Inherited from MoleculeInput.
    """

    name: str = "SMILES Input"

    def _get_structure(self, input: int | str) -> rdchem.Mol:
        """Parse SMILES string and convert to RDKit molecule.

        Args:
            input: SMILES string representation of the molecule.

        Returns:
            RDKit Mol object parsed from SMILES.

        Examples:
            >>> smiles = Smiles()
            >>> mol = smiles._get_structure("CCO")
            >>> mol.GetNumAtoms()  # Without hydrogens
            3
        """
        mol = rdmolfiles.MolFromSmiles(input)
        if mol is None:
            raise ValueError(f"Failed to parse SMILES string: {input}")
        return mol

    @job(files="files", properties="properties")
    def make(self, input: str) -> Response[Output]:
        """Create a workflow job to generate a molecule from SMILES.

        Args:
            input: SMILES string.

        Returns:
            Response containing:
                - structure: RDMolMolecule from SMILES
                - files: MOL file representation
        """
        resp = super()._make(input)
        return resp

make

make(input: str) -> Response[Output]

Create a workflow job to generate a molecule from SMILES.

PARAMETER DESCRIPTION
input

SMILES string.

TYPE: str

RETURNS DESCRIPTION
Response[Output]

Response containing: - structure: RDMolMolecule from SMILES - files: MOL file representation

Source code in jfchemistry/inputs/smiles.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
@job(files="files", properties="properties")
def make(self, input: str) -> Response[Output]:
    """Create a workflow job to generate a molecule from SMILES.

    Args:
        input: SMILES string.

    Returns:
        Response containing:
            - structure: RDMolMolecule from SMILES
            - files: MOL file representation
    """
    resp = super()._make(input)
    return resp