Reading and Writing to SDF

User a9d76a1173

16-12-2011 20:31:54

Hi ,


 


I need to read a sdf file (missing charge and stereo data in molTable)  and write a sdf file using chemaxon. The basic idea is to add charge and stereo information missing in the moltable to sdf file and write it out.


The reading part works fine. However the output file is empty.


Here is the code


private void marvinSDFWrite()
      {
       try
       {
       FileInputStream fileInputStream = new FileInputStream(Settings.getCurrentDirectory()+sep+Settings.getJobName()+"_filtered.sdf");
       FileOutputStream os = new FileOutputStream(Settings.getCurrentDirectory()+sep+"molconnIn.sdf");
       MolImporter molImporter = new MolImporter(fileInputStream);
       MolExporter molExporter = new MolExporter(os,"sdf");
       Molecule molecule;
        while ((molecule = molImporter.read()) != null) {
            molExporter.write(molecule);
            }
        molImporter.close();
        molExporter.close();
       }
       catch (Exception e)
       {
           System.out.println(e.toString());
       }
      }


 


What am I doing wrong?? I am getting java.lang.NullPointerException

ChemAxon 25dcd765a3

19-12-2011 10:53:16

I have created a simple test which works based on your code.


It works for me without error.


Actually could you please test if it works for you, if not please send the exception.


I hope this helps

User a9d76a1173

19-12-2011 14:00:38










volfi wrote:

I have created a simple test which works based on your code.


It works for me without error.


Actually could you please test if it works for you, if not please send the exception.


I hope this helps



It did work with test.sdf and other sdf files I had. But never works within my program.


Here is the print stack trace


java.lang.NullPointerException
    at chemaxon.struc.MolAtom.hasSMARTSPropsExcluding(MolAtom.java:4034)
    at chemaxon.marvin.io.formats.mdl.MolExport.hasSMARTSOnlyProps(MolExport.java:2833)
    at chemaxon.marvin.io.formats.mdl.MolExport.appendCtabV2(MolExport.java:578)
    at chemaxon.marvin.io.formats.mdl.MolExport.appendCtab(MolExport.java:541)
    at chemaxon.marvin.io.formats.mdl.MolExport.convert(MolExport.java:371)
    at chemaxon.formats.MolExporter.write(MolExporter.java:759)
    at edu.uconn.pharmacy.molfind.CompoundFilter.marvinSDFWrite(CompoundFilter.java:517)
    at edu.uconn.pharmacy.molfind.CompoundFilter.runFilters(CompoundFilter.java:151)
    at edu.uconn.pharmacy.molfind.CompoundFilterWorker.doInBackground(CompoundFilterWorker.java:50)
    at edu.uconn.pharmacy.molfind.CompoundFilterWorker.doInBackground(CompoundFilterWorker.java:14)
    at javax.swing.SwingWorker$1.call(SwingWorker.java:277)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at javax.swing.SwingWorker.run(SwingWorker.java:316)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:680)

ChemAxon 25dcd765a3

19-12-2011 14:07:28

Could you please attach the sdf file which causes the problem.

User a9d76a1173

19-12-2011 14:15:48










volfi wrote:

Could you please attach the sdf file which causes the problem.



It is the same sdf you sent..test.sdf I think it is not a problem with sdf..In my program I call a method that writes a sdf within a swing worker thread.For some reason it doesn't work with any sdf file..


When user clicks a botton it starts a swing worker thread and pass data to that thread..swing worker thread calls CompoundFilter.runFilters()


RunFilters call a private method marvinSDFWrite() which writes the sdf.

User a9d76a1173

19-12-2011 15:16:04










lochana wrote:










volfi wrote:

Could you please attach the sdf file which causes the problem.



It is the same sdf you sent..test.sdf I think it is not a problem with sdf..In my program I call a method that writes a sdf within a swing worker thread.For some reason it doesn't work with any sdf file..


When user clicks a botton it starts a swing worker thread and pass data to that thread..swing worker thread calls CompoundFilter.runFilters()


RunFilters call a private method marvinSDFWrite() which writes the sdf.



I even tried moving the code portion that writes SDF outside the worker thread. same error. I have  no idea what is going on.

ChemAxon 25dcd765a3

19-12-2011 15:20:44

Hi,


So if I understand right the same code snippet just doesn't work in your code. Without knowing your code it is quite hard to tell what can be the problem.


One hint: The molecule is not thread safe by default. So don't modify the molecule during the export.


Can you reproduce the bug outside your swing environment?

User a9d76a1173

19-12-2011 15:34:45










volfi wrote:

Hi,


So if I understand right the same code sniplet just doesn't work in your code. Without knowing your code it is quite hard to tell what can be the problem.


One hint: The molecule is not thread safe by default. So don't modify the molecule during the export.


Can you reproduce the bug outside your swing environment?



I tried moving the code snippet to the main method.. even before I start the GUI thread


 


public static void main(String[] args)  {

        try
        {
        FileInputStream fileInputStream = new FileInputStream("test.sdf");
    FileOutputStream os = new FileOutputStream("out.sdf");
    MolImporter molImporter = new MolImporter(fileInputStream);
    MolExporter molExporter = new MolExporter(os, "sdf");
    Molecule mol;
    while ((mol = molImporter.read()) != null) {
        molExporter.write(mol);
    }
    molImporter.close();
    molExporter.close();  
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
   


same error !


java.lang.NullPointerException
    at chemaxon.struc.MolAtom.hasSMARTSPropsExcluding(MolAtom.java:4034)
    at chemaxon.marvin.io.formats.mdl.MolExport.hasSMARTSOnlyProps(MolExport.java:2833)
    at chemaxon.marvin.io.formats.mdl.MolExport.appendCtabV2(MolExport.java:578)
    at chemaxon.marvin.io.formats.mdl.MolExport.appendCtab(MolExport.java:541)
    at chemaxon.marvin.io.formats.mdl.MolExport.convert(MolExport.java:371)
    at chemaxon.formats.MolExporter.write(MolExporter.java:759)
    at edu.uconn.pharmacy.molfind.Main.main(Main.java:43)

User a9d76a1173

19-12-2011 17:42:10










lochana wrote:










volfi wrote:

Hi,


So if I understand right the same code sniplet just doesn't work in your code. Without knowing your code it is quite hard to tell what can be the problem.


One hint: The molecule is not thread safe by default. So don't modify the molecule during the export.


Can you reproduce the bug outside your swing environment?



I tried moving the code snippet to the main method.. even before I start the GUI thread


 


public static void main(String[] args)  {

        try
        {
        FileInputStream fileInputStream = new FileInputStream("test.sdf");
    FileOutputStream os = new FileOutputStream("out.sdf");
    MolImporter molImporter = new MolImporter(fileInputStream);
    MolExporter molExporter = new MolExporter(os, "sdf");
    Molecule mol;
    while ((mol = molImporter.read()) != null) {
        molExporter.write(mol);
    }
    molImporter.close();
    molExporter.close();  
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
   


same error !


java.lang.NullPointerException
    at chemaxon.struc.MolAtom.hasSMARTSPropsExcluding(MolAtom.java:4034)
    at chemaxon.marvin.io.formats.mdl.MolExport.hasSMARTSOnlyProps(MolExport.java:2833)
    at chemaxon.marvin.io.formats.mdl.MolExport.appendCtabV2(MolExport.java:578)
    at chemaxon.marvin.io.formats.mdl.MolExport.appendCtab(MolExport.java:541)
    at chemaxon.marvin.io.formats.mdl.MolExport.convert(MolExport.java:371)
    at chemaxon.formats.MolExporter.write(MolExporter.java:759)
    at edu.uconn.pharmacy.molfind.Main.main(Main.java:43)



Removing all marvin jar files and adding back fixed the problem !!


volfi, thanks for all the help .. appreciate it !

User 5c60d0332e

18-03-2013 05:23:53

Hi Team,


 


Can any provide information regarding  how to read data from SDF file in java. 


If you have any sample code please share me. 


 


Thanks in advance.


With Regards,


Krishna Bhaskar.

ChemAxon fc046975bc

18-03-2013 08:05:43

Hi Krishna,


You may find information on how to import file with Chemaxon's API in our developer guide, here.


Best Regards,
Peter

User 5c60d0332e

18-03-2013 09:35:14

Hi pszakacs,


I have added marvin.jar to my buildpath, I am not able to find "MolImporter" class. Please provide me the link to down exact jar file.


 


With Regards,


Krishna Bhaskar.


 


 


 

ChemAxon fc046975bc

18-03-2013 09:56:58

The necessary jars can be downlowded from here. After installation the jars are in the generated lib directory. You shoul add MarvinBeans-beans.jar and chemaxon-core.jar as well.


BR,
Peter