Import/Export

The import and export types can be defined dynamically and this is really usefull for slightly modified import and export funtionalities, e.g. assigning automatically identifiers, checking for duplicates, ...

Import

Get molecule import classes

Example 9-22. Get molecule import classes

// open file input stream
String inputFile="yourInputFile.sdf";
FileInputStream input = null;
try
{
  input = new FileInputStream(inputFile);
}
catch (Exception ex)
{
  ex.printStackTrace();
  // TODO: handle exception
}
// create simple reader and
// estimate file format by file extension
SimpleReader reader = null;
try
{
  // estimate input file type
  IOType inType = IOTypeHolder.instance().filenameToType(inputFile);
  if (inType == null)
  {
    // TODO: handle unkown input file type
  }
  // open simple reader
  reader = new SimpleReader(input, inType);
}
catch (IOException e)
{
  e.printStackTrace();
  // TODO: handle exception
}

Export

Get molecule export classes

Example 9-23. Get molecule export classes

// open file output stream
String outputFile="yourOutputFile.sdf";
FileOutputStream output = null;
try
{
  output = new FileOutputStream(outputFile);
}
catch (Exception ex)
{
  ex.printStackTrace();
  // TODO: handle exception
}
// create simple writer and
// estimate file format by file extension
SimpleWriter writer = null;
try
{
  // estimate output file type
  IOType outType = IOTypeHolder.instance().filenameToType(outputFile);
  if (outType == null)
  {
    // TODO: handle unkown output file type
  }
  // open simple writer
  writer = new SimpleWriter(output, outType);
}
catch (IOException e)
{
  e.printStackTrace();
  // TODO: handle exception
}

Simple Import/Export pipeline

Create molecule processing pipe

Example 9-24. Create molecule processing pipe

package joelib.test;

// import base classes and the molecule class
import java.io.IOException;
import joelib.data.JOEPairData;
import joelib.io.IOTypeHolder;
import joelib.io.MoleculeIOException;
import joelib.io.SimpleReaderWriterPipe;
import joelib.molecule.JOEMol;
import org.apache.log4j.Category;

public class AddMolID extends SimpleReaderWriterPipe
{
  // initialize logging tool for this class
  private static Category logger = Category.getInstance("joelib.test.AddMolID");

  public AddMolID(String args[]) throws IOException
  {
    // call super class for created pipe by
    // using the command line parameters
    super(args);
  }

  // you will be fored to implement these method by the
  // SimpleReaderWriterPipe parent class
  public void showUsage()
  {
    StringBuffer sb = new StringBuffer();
    String programName = this.getClass().getName();
    sb.append("Usage is :\n");
    sb.append("java -cp . ");
    sb.append(programName);
    sb.append(" [options]");
    sb.append(" <input file>");
    sb.append(" <output file>");
    sb.append("\n where options can be:");
    sb.append(" -i<inputFormat>");
    sb.append(" -o<outputFormat>");
    sb.append("\nSupported molecule types:");
    sb.append(IOTypeHolder.instance().toString());
    System.out.println(sb.toString());
    System.exit(0);
  }

  public void loadWriteAllMolecules()
  {
    logger.info("Start adding molecule identifiers ...");
    for (;;)
    {
      try
      {
        // break, if all molecules were treated
        if (!this.readWriteNext())break;
      }
      catch (IOException e)
      {
        logger.error("IO problems: "+e.getMessage()+" at molecule "+loadedMolecule().getTitle());
      }
      catch (MoleculeIOException e)
      {
        logger.error("Molecule parsing problems: "+e.getMessage()+
                     " at molecule "+loadedMolecule().getTitle());
      }
    }
  }

  // you will be fored to implement these method by the
  // SimpleReaderWriterPipe parent class
  public void molecule2handle(JOEMol mol)
  {
    JOEPairData dp = new JOEPairData();
    dp.setAttribute("ID");
    dp.setValue(Integer.toString(moleculesLoaded()));
    // overwrite previous identifiers
    mol.addData(dp,true);
  }

  public static void main(String[] args)
  {
    AddMolID addMolID = null;
    try
    {
      addMolID = new AddMolID(args);
    }
    catch (IOException e)
    {
      e.printStackTrace();
      System.exit(1);
    }
    addMolID.loadWriteAllMolecules();
  }
}