Component SMARTS import with MolImporter

User 677b9c22ff

12-09-2007 04:15:16

Hi,


I have the following component SMARTS and I use MolImporter and force


SMARTS detection with setquerymode(true)


Code:






Molecule myQuery = null;            


MolImporter mi = createMolImporter(queryfilename );


// needed for SMARTS


mi.setQueryMode(true);


while ((mi.read()) != null) {


        //needed for SMARTS 


        mi.setQueryMode(true);


   myQuery = mi.read();


}








However either the import crashes or the SMARTS are detected as CXSMILES:





The SMARTS are:


[#7]


[#7].[#7]


[#7].[#7].[#7]


N


N.N


N.N.N





output:


cxsmiles chemaxon.formats.MolImporter@1ef9157


cxsmiles chemaxon.formats.MolImporter@1ef9157


cxsmiles chemaxon.formats.MolImporter@1ef9157


cxsmiles chemaxon.formats.MolImporter@1ef9157





If I start with other SMARTS:


[!#1!#6][!#1!#6]


*


[#6]~*


[#6]~[#6]


[#7]


[#7].[#7]


[#7].[#7].[#7]


N


N.N


N.N.N





output:


cxsmarts chemaxon.formats.MolImporter@17431b9


cxsmarts chemaxon.formats.MolImporter@17431b9


cxsmarts chemaxon.formats.MolImporter@17431b9


cxsmarts chemaxon.formats.MolImporter@17431b9


cxsmarts chemaxon.formats.MolImporter@17431b9


cxsmarts chemaxon.formats.MolImporter@17431b9





In either way I never can fully read all the SMARTS, probably due to conversion errors. I can read them into Instant-JChem. So is there


a better way to force SMARTS detection within the API?





Thank you


Tobias

ChemAxon 7c2d26e5cf

12-09-2007 07:52:01

Define the input format to MolImporter to prefer this in MolImporter by format recognition. You can assign import options to the input file name (in '{' and '}' brackets). See the API: MolImporter(String)


Code:
MolImporter importer = new MolImporter(filename+"{smarts}");



To check the format of imported molecule, you can use the Molecule.getInputFormat() method.

User 677b9c22ff

12-09-2007 18:48:32

Hi Tamas,


thanks, unfortunately this does not resolve the problematic component SMARTS issue.





bad-smarts3.smi


Code:
[!#1!#6][!#1!#6]


*


[#6]~*


[#6]~[#6]


[#7]


[#7].[#7]


[#7].[#7].[#7]


N


N.N


N.N.N






JAVA example code


Code:



package examples;





import java.io.File;


import java.io.FileNotFoundException;


import java.io.IOException;





import chemaxon.formats.MolFormatException;


import chemaxon.formats.MolImporter;


import chemaxon.marvin.plugin.PluginException;


import chemaxon.struc.Molecule;





public class SMARTSproblem {


   /** Defines a MolImporter object to the structure file. */


   private static MolImporter createMolImporter(String filename,String format) throws  IOException{


      MolImporter mi = null;


      try {


         File f = new File(filename);


         // FileInputStream fis = new FileInputStream(f);


         mi = new MolImporter(f + format);


         // following "get" code has no function?


         System.out.println("LineCount: "+mi.getLineCount());


         System.out.println("NumRecords: "+mi.estimateNumRecords());


         System.out.println("RecordCount: "+mi.getRecordCount());


         System.out.println("RecordCountMax: "+mi.getRecordCountMax());


         


      } catch (FileNotFoundException ex) {


         System.err.println(filename + ": not found");


         System.exit(1);


      } catch (MolFormatException ex) {


         System.err.println(filename + ": " + ex.getMessage());


         System.exit(1);


      } catch (Exception ex) {


         System.err.println("Error: " + filename


               + " is not a structure file.");


         System.exit(1);


      }


      return mi;


   }








   public static void main(String[] args) throws PluginException ,   MolFormatException, IOException  {


      String queryfilename = "Z:bad-smarts3.smi";


      try {


         MolImporter mi = createMolImporter(queryfilename, "{smarts}" );


         Molecule myQuery = null;


         int i = 0;


         while ((mi.read()) != null) {


            i++;


            //*** This does not work for certain SMARTS


            mi.setQueryMode(true);


            String myformat = mi.getFormat();


            String out = mi.toString();


            System.out.print (myformat+" "+out+" ");


            myQuery = mi.read();


            System.out.println(myQuery.getInputFormat());


         } 


         mi.close();


         System.out.println("Input contained 10 SMARTS - imported: "+i);


      } catch (IOException ioe) {


         ioe.printStackTrace();


      }


   }


}

















Output:





Code:



LineCount: 0


NumRecords: -1


RecordCount: 0


RecordCountMax: 0


smarts chemaxon.formats.MolImporter@9173ef smarts


smarts chemaxon.formats.MolImporter@9173ef smarts


smarts chemaxon.formats.MolImporter@9173ef smarts


smarts chemaxon.formats.MolImporter@9173ef smarts


smarts chemaxon.formats.MolImporter@9173ef smarts


Input contained 10 SMARTS - imported: 5








Still it reads only 5 SMARTS instead of all 10.


Tobias

ChemAxon 7c2d26e5cf

14-09-2007 10:24:50

We will check it.

ChemAxon 25dcd765a3

14-09-2007 13:10:03

Hi!


You read every second molecule only:


Code:



        while ((mi.read()) != null) {


            i++;


            //*** This does not work for certain SMARTS


            mi.setQueryMode(true);


            String myformat = mi.getFormat();


            String out = mi.toString();


            System.out.print (myformat+" "+out+" ");


            myQuery = mi.read();


            System.out.println(myQuery.getInputFormat());


         }





mi.read() returns a molecule or null


in your code you store every even molecule in "myQuery = mi.read();".


The odd ones are not used in the while cycle condition.


So the correct code is the following:


Code:



         while ((myQuery=mi.read()) != null) {


            i++;


            //*** This does not work for certain SMARTS


            mi.setQueryMode(true);


            String myformat = mi.getFormat();


            String out = mi.toString();


            System.out.print (myformat+" "+out+" ");


            System.out.println(myQuery.getInputFormat());


         }








Andras

User 677b9c22ff

14-09-2007 18:44:26

Hi Andras and Tamas,


thank you for the solution.





Running it with MolImporter mi = createMolImporter(queryfilename, "{smiles}" );


Code:
LineCount: 0


NumRecords: -1


RecordCount: 0


RecordCountMax: 0


smiles chemaxon.formats.MolImporter@9173ef smarts [!#1!#6][!#1!#6]


smiles chemaxon.formats.MolImporter@9173ef smiles  *


smiles chemaxon.formats.MolImporter@9173ef smarts [#6]~*


smiles chemaxon.formats.MolImporter@9173ef smarts [#6]~[#6]


smiles chemaxon.formats.MolImporter@9173ef smarts [#7]


smiles chemaxon.formats.MolImporter@9173ef smarts [#7].[#7]


smiles chemaxon.formats.MolImporter@9173ef smarts [#7].[#7].[#7]


smiles chemaxon.formats.MolImporter@9173ef smiles N


smiles chemaxon.formats.MolImporter@9173ef smiles N.N


smiles chemaxon.formats.MolImporter@9173ef smiles N.N.N


Input contained 10 SMARTS - imported: 10









Running it with MolImporter mi = createMolImporter(queryfilename, "{smarts}" ); does it correctly.





Code:
LineCount: 0


NumRecords: -1


RecordCount: 0


RecordCountMax: 0


smarts chemaxon.formats.MolImporter@9173ef smarts


smarts chemaxon.formats.MolImporter@9173ef smarts


smarts chemaxon.formats.MolImporter@9173ef smarts


smarts chemaxon.formats.MolImporter@9173ef smarts


smarts chemaxon.formats.MolImporter@9173ef smarts


smarts chemaxon.formats.MolImporter@9173ef smarts


smarts chemaxon.formats.MolImporter@9173ef smarts


smarts chemaxon.formats.MolImporter@9173ef smarts


smarts chemaxon.formats.MolImporter@9173ef smarts


smarts chemaxon.formats.MolImporter@9173ef smarts


Input contained 10 SMARTS - imported: 10






Code:





Code:
package examples;





import java.io.File;


import java.io.FileNotFoundException;


import java.io.IOException;





import chemaxon.formats.MolFormatException;


import chemaxon.formats.MolImporter;


import chemaxon.marvin.plugin.PluginException;


import chemaxon.struc.Molecule;





public class SMARTSimporter {


   /** Defines a MolImporter object to the structure file. */


   


   private static MolImporter createMolImporter(String filename,String format) throws  IOException{


      MolImporter mi = null;


      try {


         File f = new File(filename);


         // FileInputStream fis = new FileInputStream(f);


         mi = new MolImporter(f + format);


         // following "get" code has no function?


         System.out.println("LineCount: "+mi.getLineCount());


         System.out.println("NumRecords: "+mi.estimateNumRecords());


         System.out.println("RecordCount: "+mi.getRecordCount());


         System.out.println("RecordCountMax: "+mi.getRecordCountMax());


         


      } catch (FileNotFoundException ex) {


         System.err.println(filename + ": not found");


         System.exit(1);


      } catch (MolFormatException ex) {


         System.err.println(filename + ": " + ex.getMessage());


         System.exit(1);


      } catch (Exception ex) {


         System.err.println("Error: " + filename


               + " is not a structure file.");


         System.exit(1);


      }


      return mi;


   }








   public static void main(String[] args) throws PluginException ,   MolFormatException, IOException  {


      String queryfilename = "Z:bad-smarts3.smi";


      try {


         MolImporter mi = createMolImporter(queryfilename, "{smarts}" );


         Molecule myQuery = null;


         int i = 0;


         while ((myQuery = mi.read()) != null) {


            i++;


            String myformat = mi.getFormat();


            String out = mi.toString();


            System.out.print (myformat+" "+out+" ");


            //myQuery  mi.read();


            System.out.println(myQuery.getInputFormat());


         } 


         mi.close();


         System.out.println("Input contained 10 SMARTS - imported: "+i);


      } catch (IOException ioe) {


         ioe.printStackTrace();


      }


   }


}








Tobias