All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.actelion.research.chem.shredder.FragmentGenerator Maven / Gradle / Ivy

There is a newer version: 2024.11.2
Show newest version
package com.actelion.research.chem.shredder;
/**
 *
 * Idorsia Pharmaceuticals Ltd. 2020
 * Thomas Liphardt
 *
 */

import com.actelion.research.chem.*;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;

/**
 * NOTE!!! The returned bitsets indicate atom indeces in the CANONIZED molecule, and NOT
 *         necessarily indeces in the original molecule !!!
 *
 *
 * Enumerates all "connected heavy atom fragments", i.e. connected subgraphs of heavy atoms, of specific size.
 *
 * Uses the CONSENS algorithm (see supplement of Rarey "Connected Subgraph Fingerprints" publication
 * for explanation and proof of correctness).
 *
 */
public class FragmentGenerator {

    private final int mMinSize;
    private final int mMaxSize;

    private final StereoMolecule mMol;

    private final int mAtomCount;

    private final BitSet[] mForbiddenBits;
    private final BitSet[] mNeighbourBits;

    final private List mFragmentBits = new ArrayList<>();

    public List getFragments() {
        return mFragmentBits;
    }

    public List getFragmentsAsIntArrays() {
        List fragments = new ArrayList<>(mFragmentBits.size());
        for(int zi = 0; zi getFragmentsAsBooleanArrays() {
        List fragments = new ArrayList<>(mFragmentBits.size());
        for(int zi = 0; zi getFragmentsAsMolecules() {
        List frags_as_bool_arrays = this.getFragmentsAsBooleanArrays();
        List fragments = new ArrayList<>(mFragmentBits.size());

        for(int zi = 0; zi= mMaxSize) {
            return;
        }

        for(int v=0;v= mMinSize && (cardinality+1) <= mMaxSize) {
                    //System.out.println("add: "+next[0].toString());
                    mFragmentBits.add(next[0]);
                }
                generate_recursive(next[0],next[1],next[2],frag_size+1);
            }
        }
    }

    /**
     * performs the add_candidate step
     *
     * @param frag
     * @param forbidden
     * @param cand
     * @param v
     * @return Array with { s' , f', c' }
     */
    private BitSet[] add_candidate(  BitSet frag, BitSet forbidden, BitSet cand , int v ) {
        BitSet frag_p = (BitSet) frag.clone();
        frag_p.set(v);

        BitSet forbidden_p = (BitSet) forbidden.clone();
        BitSet c2 = ((BitSet)cand.clone());
        c2.and((BitSet) mForbiddenBits[v].clone());
        forbidden_p.or( c2 );

        BitSet cand_p = (BitSet) cand.clone();
        cand_p.or(mNeighbourBits[v]);
        cand_p.andNot(forbidden_p);
        cand_p.andNot(frag_p);

        return new BitSet[]{ frag_p, forbidden_p, cand_p };
    }



    public static void main(String args[]) {
        String smiles_A = "O1CCCCC1N1CCCCC1";
        String smiles_B = "O1C=C[C@H]([C@H]1O2)c3c2cc(OC)c4c3OC(=O)C5=C4CCC(=O)5";

        String idcode_C = "daD@@DjUZxHH@B";//"figq@@DL\\AIfUYyUywqZLzIV}ZjjdF@@Bh@@@";

        String smiles_D = "CCCC(C)N(CCC)CCC";
        //String idcode_C = "el^ZDH@BGODfkDPIAIfV[WWyUyVvebSaRikXy{zBBbjz`@`@jjDJAz`@";

        SmilesParser sp = new SmilesParser();
        StereoMolecule mA = new StereoMolecule();
        StereoMolecule mB = new StereoMolecule();
        StereoMolecule mC = new StereoMolecule();
        StereoMolecule mD = new StereoMolecule();

        try {
            sp.parse(mA,smiles_A);
            sp.parse(mB,smiles_B);
            sp.parse(mD,smiles_D);
        } catch (Exception e) {
            e.printStackTrace();
        }

        IDCodeParser icp = new IDCodeParser();
        icp.parse(mC,idcode_C);

        FragmentGenerator fg_A = new FragmentGenerator(mB,10,10);
        fg_A.computeFragments();
        List mols_a = fg_A.getFragmentsAsMolecules();
        System.out.println("Frag[idcode]");
        for(StereoMolecule mia : mols_a) {
            System.out.println(mia.getIDCode());
        }

        System.out.println("\n\n");

        //FragmentGenerator fg = new FragmentGenerator(mA,0,6);
        long ts_a = System.currentTimeMillis();
        FragmentGenerator fg = new FragmentGenerator(mB,10,10);
        long ts_b = System.currentTimeMillis();

        System.out.println("Num Fragments: "+fg.getFragments().size());
        System.out.println("Time: "+(ts_b-ts_a));

        System.out.println("Try enumerate all fragements for C");
        long ts_c = System.currentTimeMillis();
        FragmentGenerator fC = new FragmentGenerator(mC,1,40);
        fC.computeFragments();
        long ts_d = System.currentTimeMillis();
        System.out.println("Frags: "+fC.getFragments().size()+" Time: "+(ts_d-ts_c));

        System.out.println("Frags[idcode]");
        int cnt = 0;
        for(BitSet bci : fC.getFragments()) {
            StereoMolecule mi = new StereoMolecule();
            boolean bia[] = new boolean[mC.getAtoms()];
            for(int zi=0;zi




© 2015 - 2024 Weber Informatics LLC | Privacy Policy