Try this:

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.io.SMILESWriter;
import org.openscience.cdk.smiles.SmilesParser;
import org.openscience.cdk.smsd.Isomorphism;

public class Mcs {

    public static IAtomContainer getMcsAsNewContainer(IAtomContainer mol1, 
IAtomContainer mol2) throws CDKException, CloneNotSupportedException, 
IOException {
        Isomorphism mcs = new 
Isomorphism(org.openscience.cdk.smsd.interfaces.Algorithm.DEFAULT, true);
        mcs.init(mol1, mol2, true, true);
        mcs.setChemFilters(true, true, true);

        System.out.println("MCS Size: " + mcs.getTanimotoAtomSimilarity());
        System.out.println("MCS First Map: " + mcs.getFirstMapping());
        System.out.println("MCS First size: " + 
mcs.getFirstAtomMapping().size());

        mol1 = mcs.getReactantMolecule();
        mol2 = mcs.getProductMolecule();

        IMolecule mcsmolecule = 
DefaultChemObjectBuilder.getInstance().newInstance(IMolecule.class, mol1);

        List<IAtom> atomsToBeRemoved = new ArrayList<IAtom>();
        for (IAtom atom : mcsmolecule.atoms()) {
            int index = mcsmolecule.getAtomNumber(atom);
//            System.out.println("index: " +index);
            if (!mcs.getFirstMapping().containsKey(index)) {
                atomsToBeRemoved.add(atom);
            }
        }
        
        for (IAtom atom : atomsToBeRemoved) {
            mcsmolecule.removeAtomAndConnectedElectronContainers(atom);
        }

        StringWriter stringWriter = new StringWriter();
        SMILESWriter smilesWriter = new SMILESWriter(stringWriter);
        smilesWriter.write(mcsmolecule);
        smilesWriter.close();

        System.out.println("MCS SMILES: " + stringWriter.toString());

        return (IAtomContainer) mcsmolecule.clone();
    }

    @Test
    public static void main(String[] args) throws CDKException, 
CloneNotSupportedException, IOException {
        SmilesParser sp = new 
SmilesParser(DefaultChemObjectBuilder.getInstance());
        // Men_12
        IAtomContainer A1 = 
sp.parseSmiles("c1cccc(COC(=O)NC(CC(C)C)C(=O)NC(CCc2ccccc2)C(=O)COC)c1");
        // Tri_06
        IAtomContainer A2 = 
sp.parseSmiles("c1cccc(COC(=O)NC(CC(C)C)C(=O)NCC#N)c1");

        getMcsAsNewContainer(A2, A1);

        getMcsAsNewContainer(A1, A2);
    }
}

Best,

Asad



************************************************
Syed Asad Rahman (PhD, PG, B.Engg)
Research Scientist

EMBL-EBI     
WTGC,  
Hinxton CB10 1SD                                           
Cambridge, UK   

Phone: +44- (0)1223 492537      
Fax:   +44- (0)1223 494486        
[email protected]     
www.ebi.ac.uk /~asad        
                
*************************************************

On 26 Oct 2010, at 12:42, [email protected] wrote:

> Send Cdk-user mailing list submissions to
>       [email protected]
> 
> To subscribe or unsubscribe via the World Wide Web, visit
>       https://lists.sourceforge.net/lists/listinfo/cdk-user
> or, via email, send a message with subject or body 'help' to
>       [email protected]
> 
> You can reach the person managing the list at
>       [email protected]
> 
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Cdk-user digest..."
> 
> 
> Today's Topics:
> 
>   1. substract substructure (Tobias Girschick)
>   2. Re: substract substructure (Tobias Girschick)
>   3. Re: substract substructure (Rajarshi Guha)
> 
> 
> ----------------------------------------------------------------------
> 
> Message: 1
> Date: Tue, 26 Oct 2010 10:56:35 +0200
> From: Tobias Girschick <[email protected]>
> Subject: [Cdk-user] substract substructure
> To: [email protected]
> Message-ID:
>       <[email protected]>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> Hi,
> 
> I would like to substract a substructure from a structure and keep the
> remaining parts. There was a similar post in the mailing list history:
> 
> http://sourceforge.net/mailarchive/message.php?msg_id=200702050602.05302.e.willighagen%40science.ru.nl
> 
> The difference (is it a difference?) is, that I don't calculate the
> substructure with CDK code but get it from somewhere else. My code looks
> like this (but doesn't work):
> 
> while (sdfreader.hasNext()) {
>            /*
>            Substructure: O=Cc1ccccc1
>            A:            OC(=O)c1cccc(c1)Cl
>            B:            CC(=O)c1cccc(F)c1
>            C:            Nc1cccc(c1)C(O)=O
>            */
> 
>            IMolecule mol2store = (IMolecule)sdfreader.next();
> 
> AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(mol2store);
>            CDKHueckelAromaticityDetector.detectAromaticity(mol2store);
> 
>            FileInputStream fes = new FileInputStream(f_bb);
>            IteratingMDLReader sdreader = new IteratingMDLReader(fes,
> DefaultChemObjectBuilder.getInstance());
>            IMolecule mol = null;
>            while (sdreader.hasNext()) {
>                mol = (IMolecule) sdreader.next();
>            }
>            sdreader.close();
>            fes.close();
> 
>            String smiles = sg.createSMILES(mol);
>            System.out.println("S: " + smiles);
>            smiles = sg.createSMILES(mol2store);
>            System.out.println("A: " + smiles);
> 
>            Iterable<IAtom> i = mol.atoms();
>            Iterator<IAtom> atoms = i.iterator();
>            while(atoms.hasNext()){
>                IAtom a = atoms.next();
>                mol2store.removeAtomAndConnectedElectronContainers(a);
>            }
>            smiles = sg.createSMILES(mol2store);
>            System.out.println("B: " + smiles);
>            smiles = sg.createSMILES(mol);
>            System.out.println("S: " + smiles);
>            System.out.println("-------------");
>        }
> 
> -- 
> Tobias Girschick
> mailto: [email protected]
> -------------- next part --------------
> An HTML attachment was scrubbed...
> 
> ------------------------------
> 
> Message: 2
> Date: Tue, 26 Oct 2010 10:59:21 +0200
> From: Tobias Girschick <[email protected]>
> Subject: Re: [Cdk-user] substract substructure
> To: [email protected]
> Message-ID:
>       <[email protected]>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> sorry, I sent the message before it was ready....here the complete version
> 
> Hi,
> 
> I would like to substract a substructure from a structure and keep the
> remaining parts. There was a similar post in the mailing list history:
> 
> http://sourceforge.net/mailarchive/message.php?msg_id=200702050602.05302.e.willighagen%40science.ru.nl
> 
> The difference (is it a difference?) is, that I don't calculate the
> substructure with CDK code but get it from somewhere else. My code looks
> like this (but doesn't work):
> 
> while (sdfreader.hasNext()) {
>            /*
>            Substructure: O=Cc1ccccc1
>            A:            OC(=O)c1cccc(c1)Cl
>            B:            CC(=O)c1cccc(F)c1
>            C:            Nc1cccc(c1)C(O)=O
>            */
> 
>            // read the molecules A, B, C
>            IMolecule mol2store = (IMolecule)sdfreader.next();
> 
> AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(mol2store);
>            CDKHueckelAromaticityDetector.detectAromaticity(mol2store);
> 
>            // read the substructure
>            FileInputStream fes = new FileInputStream(f_bb);
>            IteratingMDLReader sdreader = new IteratingMDLReader(fes,
> DefaultChemObjectBuilder.getInstance());
>            IMolecule mol = null;
>            while (sdreader.hasNext()) {
>                mol = (IMolecule) sdreader.next();
>            }
>            sdreader.close();
>            fes.close();
> 
>            String smiles = sg.createSMILES(mol);
>            System.out.println("S: " + smiles);
>            smiles = sg.createSMILES(mol2store);
>            System.out.println("A: " + smiles);
> 
>            Iterable<IAtom> i = mol.atoms();
>            Iterator<IAtom> atoms = i.iterator();
>            while(atoms.hasNext()){
>                IAtom a = atoms.next();
>                mol2store.removeAtomAndConnectedElectronContainers(a);
>            }
>            smiles = sg.createSMILES(mol2store);
>            System.out.println("B: " + smiles);
>            smiles = sg.createSMILES(mol);
>            System.out.println("S: " + smiles);
>            System.out.println("-------------");
>        }
> 
> Considering the SMILES output, the atoms haven't been substracted from the
> molecules ABC.  Any thoughts?
> 
> On Tue, Oct 26, 2010 at 10:56 AM, Tobias Girschick <
> [email protected]> wrote:
> 
>> Hi,
>> 
>> I would like to substract a substructure from a structure and keep the
>> remaining parts. There was a similar post in the mailing list history:
>> 
>> 
>> http://sourceforge.net/mailarchive/message.php?msg_id=200702050602.05302.e.willighagen%40science.ru.nl
>> 
>> The difference (is it a difference?) is, that I don't calculate the
>> substructure with CDK code but get it from somewhere else. My code looks
>> like this (but doesn't work):
>> 
>> while (sdfreader.hasNext()) {
>>            /*
>>            Substructure: O=Cc1ccccc1
>>            A:            OC(=O)c1cccc(c1)Cl
>>            B:            CC(=O)c1cccc(F)c1
>>            C:            Nc1cccc(c1)C(O)=O
>>            */
>> 
>>            IMolecule mol2store = (IMolecule)sdfreader.next();
>> 
>> AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(mol2store);
>>            CDKHueckelAromaticityDetector.detectAromaticity(mol2store);
>> 
>>            FileInputStream fes = new FileInputStream(f_bb);
>>            IteratingMDLReader sdreader = new IteratingMDLReader(fes,
>> DefaultChemObjectBuilder.getInstance());
>>            IMolecule mol = null;
>>            while (sdreader.hasNext()) {
>>                mol = (IMolecule) sdreader.next();
>>            }
>>            sdreader.close();
>>            fes.close();
>> 
>>            String smiles = sg.createSMILES(mol);
>>            System.out.println("S: " + smiles);
>>            smiles = sg.createSMILES(mol2store);
>>            System.out.println("A: " + smiles);
>> 
>>            Iterable<IAtom> i = mol.atoms();
>>            Iterator<IAtom> atoms = i.iterator();
>>            while(atoms.hasNext()){
>>                IAtom a = atoms.next();
>>                mol2store.removeAtomAndConnectedElectronContainers(a);
>>            }
>>            smiles = sg.createSMILES(mol2store);
>>            System.out.println("B: " + smiles);
>>            smiles = sg.createSMILES(mol);
>>            System.out.println("S: " + smiles);
>>            System.out.println("-------------");
>>        }
>> 
>> --
>> Tobias Girschick
>> mailto: [email protected]
>> 
> 
> 
> 
> -- 
> Tobias Girschick
> mailto: [email protected]
> -------------- next part --------------
> An HTML attachment was scrubbed...
> 
> ------------------------------
> 
> Message: 3
> Date: Tue, 26 Oct 2010 07:42:06 -0400
> From: Rajarshi Guha <[email protected]>
> Subject: Re: [Cdk-user] substract substructure
> To: Tobias Girschick <[email protected]>
> Cc: [email protected]
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes
> 
> This cannot work since the atom from mol is a different instance from  
> the atom in mol2store, so that  
> mol2store.removeAtomAndConnectedElectronContainers(a) will never  
> remove any atoms.
> 
> In fact, given the substructure you must perform a substructure search  
> to get the mapping of the target atoms to the substructure atoms and  
> use that mapping to remove the target atoms.
> 
> See http://rguha.net/code/java/#umcss for an example - most likely it  
> won't work with the latest CDK as it was written a long time ago, but  
> should point you in the direction
> 
> On Oct 26, 2010, at 4:59 AM, Tobias Girschick wrote:
> 
>> sorry, I sent the message before it was ready....here the complete  
>> version
>> 
>> Hi,
>> 
>> I would like to substract a substructure from a structure and keep  
>> the remaining parts. There was a similar post in the mailing list  
>> history:
>> 
>> http://sourceforge.net/mailarchive/message.php?msg_id=200702050602.05302.e.willighagen%40science.ru.nl
>> 
>> The difference (is it a difference?) is, that I don't calculate the  
>> substructure with CDK code but get it from somewhere else. My code  
>> looks like this (but doesn't work):
>> 
>> while (sdfreader.hasNext()) {
>>            /*
>>            Substructure: O=Cc1ccccc1
>>            A:            OC(=O)c1cccc(c1)Cl
>>            B:            CC(=O)c1cccc(F)c1
>>            C:            Nc1cccc(c1)C(O)=O
>>            */
>> 
>>            // read the molecules A, B, C
>>            IMolecule mol2store = (IMolecule)sdfreader.next();
>> 
>> AtomContainerManipulator 
>> .percieveAtomTypesAndConfigureAtoms(mol2store);
>> 
>> CDKHueckelAromaticityDetector.detectAromaticity(mol2store);
>> 
>>            // read the substructure
>>            FileInputStream fes = new FileInputStream(f_bb);
>>            IteratingMDLReader sdreader = new  
>> IteratingMDLReader(fes, DefaultChemObjectBuilder.getInstance());
>>            IMolecule mol = null;
>>            while (sdreader.hasNext()) {
>>                mol = (IMolecule) sdreader.next();
>>            }
>>            sdreader.close();
>>            fes.close();
>> 
>>            String smiles = sg.createSMILES(mol);
>>            System.out.println("S: " + smiles);
>>            smiles = sg.createSMILES(mol2store);
>>            System.out.println("A: " + smiles);
>> 
>>            Iterable<IAtom> i = mol.atoms();
>>            Iterator<IAtom> atoms = i.iterator();
>>            while(atoms.hasNext()){
>>                IAtom a = atoms.next();
>>                mol2store.removeAtomAndConnectedElectronContainers(a);
>>            }
>>            smiles = sg.createSMILES(mol2store);
>>            System.out.println("B: " + smiles);
>>            smiles = sg.createSMILES(mol);
>>            System.out.println("S: " + smiles);
>>            System.out.println("-------------");
>>        }
>> 
>> Considering the SMILES output, the atoms haven't been substracted  
>> from the molecules ABC.  Any thoughts?
>> 
>> On Tue, Oct 26, 2010 at 10:56 AM, Tobias Girschick 
>> <[email protected] 
>>> wrote:
>> Hi,
>> 
>> I would like to substract a substructure from a structure and keep  
>> the remaining parts. There was a similar post in the mailing list  
>> history:
>> 
>> http://sourceforge.net/mailarchive/message.php?msg_id=200702050602.05302.e.willighagen%40science.ru.nl
>> 
>> The difference (is it a difference?) is, that I don't calculate the  
>> substructure with CDK code but get it from somewhere else. My code  
>> looks like this (but doesn't work):
>> 
>> while (sdfreader.hasNext()) {
>>            /*
>>            Substructure: O=Cc1ccccc1
>>            A:            OC(=O)c1cccc(c1)Cl
>>            B:            CC(=O)c1cccc(F)c1
>>            C:            Nc1cccc(c1)C(O)=O
>>            */
>> 
>>            IMolecule mol2store = (IMolecule)sdfreader.next();
>> 
>> AtomContainerManipulator 
>> .percieveAtomTypesAndConfigureAtoms(mol2store);
>> 
>> CDKHueckelAromaticityDetector.detectAromaticity(mol2store);
>> 
>>            FileInputStream fes = new FileInputStream(f_bb);
>>            IteratingMDLReader sdreader = new  
>> IteratingMDLReader(fes, DefaultChemObjectBuilder.getInstance());
>>            IMolecule mol = null;
>>            while (sdreader.hasNext()) {
>>                mol = (IMolecule) sdreader.next();
>>            }
>>            sdreader.close();
>>            fes.close();
>> 
>>            String smiles = sg.createSMILES(mol);
>>            System.out.println("S: " + smiles);
>>            smiles = sg.createSMILES(mol2store);
>>            System.out.println("A: " + smiles);
>> 
>>            Iterable<IAtom> i = mol.atoms();
>>            Iterator<IAtom> atoms = i.iterator();
>>            while(atoms.hasNext()){
>>                IAtom a = atoms.next();
>>                mol2store.removeAtomAndConnectedElectronContainers(a);
>>            }
>>            smiles = sg.createSMILES(mol2store);
>>            System.out.println("B: " + smiles);
>>            smiles = sg.createSMILES(mol);
>>            System.out.println("S: " + smiles);
>>            System.out.println("-------------");
>>        }
>> 
>> -- 
>> Tobias Girschick
>> mailto: [email protected]
>> 
>> 
>> 
>> -- 
>> Tobias Girschick
>> mailto: [email protected]
>> ------------------------------------------------------------------------------
>> Nokia and AT&T present the 2010 Calling All Innovators-North America  
>> contest
>> Create new apps & games for the Nokia N8 for consumers in  U.S. and  
>> Canada
>> $10 million total in prizes - $4M cash, 500 devices, nearly $6M in  
>> marketing
>> Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi  
>> Store
>> http://p.sf.net/sfu/nokia-dev2dev_______________________________________________
>> Cdk-user mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/cdk-user
> 
> ----------------------------------------------------
> Rajarshi Guha        | NIH Chemical Genomics Center
> http://www.rguha.net | http://ncgc.nih.gov
> ----------------------------------------------------
> A committee is a group that keeps the minutes and loses hours.
>       -- Milton Berle
> 
> 
> 
> 
> 
> ------------------------------
> 
> ------------------------------------------------------------------------------
> Nokia and AT&T present the 2010 Calling All Innovators-North America contest
> Create new apps & games for the Nokia N8 for consumers in  U.S. and Canada
> $10 million total in prizes - $4M cash, 500 devices, nearly $6M in marketing
> Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store 
> http://p.sf.net/sfu/nokia-dev2dev
> 
> ------------------------------
> 
> _______________________________________________
> Cdk-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/cdk-user
> 
> 
> End of Cdk-user Digest, Vol 53, Issue 21
> ****************************************


------------------------------------------------------------------------------
Nokia and AT&T present the 2010 Calling All Innovators-North America contest
Create new apps & games for the Nokia N8 for consumers in  U.S. and Canada
$10 million total in prizes - $4M cash, 500 devices, nearly $6M in marketing
Develop with Nokia Qt SDK, Web Runtime, or Java and Publish to Ovi Store 
http://p.sf.net/sfu/nokia-dev2dev
_______________________________________________
Cdk-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/cdk-user

Reply via email to