Skip to content

library

stefhk3 edited this page Nov 6, 2020 · 14 revisions

The javatools parser and writer can be used as libraries in other Java programs to read or write NMReDATA files. The relevant classes are in the lib subproject within the repository. A compiled version can be downloaded from the latest release[downloaded here]. The lib subproject is using maven, dependencies are defined in the pom.xml. Note that the JCampParser.jar is part of the project since it has changes compared to the versions available in maven repositories. You should use maven to build the project and either use maven to include it into your project or use the jar file created.

The reader/writer uses the classes from the jcamp dx project and the Chemistry Development Kit as an object model, so NMReDATA files are read into those classes and written from them. For reading an NMReDATA file, example code is this:

NmredataReader reader = new NmredataReader(new FileInputStream(args[0]));
NmreData data = reader.read();

The NmreData object then contains an IAtomContainer (CDK) object and a list of Spectrum (jcamp-dx) objects. Usage of those objects is demonstrated by this code from the javatools test package:

    Assert.assertEquals(7, data.getMolecule().getAtomCount());
    Assert.assertEquals(7, data.getMolecule().getBondCount());
    Assert.assertEquals(6, data.getSpectra().size());
    Assert.assertEquals(3, ((NMRSpectrum)data.getSpectra().get(0)).getPeakTable().length);
    Assert.assertEquals(3, ((AtomReference)((NMRSpectrum)data.getSpectra().get(0)).getAssignments()[2].getTargets()[0]).getAtomNumber());
    Assert.assertEquals(5, ((AtomReference)((NMRSpectrum)data.getSpectra().get(0)).getAssignments()[2].getTargets()[1]).getAtomNumber());

For writing an NMReDATA file, you need to build an appropriate NmreData object and write this out. Some example operations for building the object follow here:

    NmreData data=new NmreData();
    String solvent="CDCl3";
    data.setSolvent(solvent);
    double freq=150;
    IAtomContainer mol=<your structure>;
    IOrderedDataArray1D x = new OrderedArrayData(new double[]{20,30}, xUnit);//Those are the shift positions
    IDataArray1D y = new ArrayData(new double[]{.5,1}, yUnit);//Those are the intensities of the peaks
    NMRSpectrum nmrspectrumc = new NMRSpectrum(x, y, "13C", freq, reference, false, JCAMPReader.RELAXED);
    nmrspectrumc.setPeakTable(peaks1d);
    Assignment[] assignmentslocal=new Assignment[2];//These are the assignments, one for each peak
    assignmentslocal[0]=new Assignment(new Pattern(20, Multiplicity.UNKNOWN), new IAssignmentTarget[]{new AtomReference(null, 0)});//The pattern gives the x position the assignment is for, the 0 is the atom number in the IAtomContainer object the peak is assigned to
    assignmentslocal[1]=new Assignment(new Pattern(30, Multiplicity.UNKNOWN), new IAssignmentTarget[]{new AtomReference(null, 1)});//The pattern gives the x position the assignment is for, the 1 is the atom number in the IAtomContainer object the peak is assigned to
    nmrspectrumc.setAssignments(assignmentslocal);
    data.addSpectrum(nmrspectrumc);`

The NmreData object can then be written like this:

PrintWriter out=<whatever desired>
NmredataWriter writer=new NmredataWriter(out);
writer.write(data, NmredataVersion.ONEPOINTONE);
writer.close();

Notice that you can write to version 1.0, 1.1, or 2.0, whereas when reading the file, the version comes from the files (and can be checked using getVersion() of NmreData).

Clone this wiki locally