View Javadoc

1   package uk.ac.cam.spectra.nmr;
2   
3   import java.io.File;
4   import java.io.FileReader;
5   import java.io.IOException;
6   import java.util.regex.Matcher;
7   import java.util.regex.Pattern;
8   
9   import nu.xom.Document;
10  
11  import org.apache.log4j.Logger;
12  import org.xmlcml.cml.base.CMLException;
13  import org.xmlcml.cml.legacy.molecule.MDLConverter;
14  
15  import uk.ac.cam.spectra.DataFileType;
16  import uk.ac.cam.spectra.DataProcess;
17  import uk.ac.cam.spectra.Validation;
18  
19  import com.hp.hpl.jena.rdf.model.Model;
20  import com.hp.hpl.jena.rdf.model.ModelFactory;
21  
22  /**
23   * SPECTRa data type bean for handling MDL Mol files, adapting the CML
24   * Legacy2CML library.
25   * 
26   * @author ojd20
27   * 
28   */
29  public class MDLMol implements DataFileType {
30  
31      /**
32       * 
33       */
34      private static final long serialVersionUID = 8458730310950710746L;
35  
36      private static final String CML_XML_SUFFIX = ".cml.xml";
37  
38      private static final Pattern MOLFILE_NAME_PATTERN = Pattern
39              .compile("^(.*)\\.mol$");
40  
41      private static final Logger LOG = Logger.getLogger(MDLMol.class);
42  
43      /**
44       * 
45       */
46      public ValidationAndCml convertAndValidate(File file) {
47          ValidationAndCml vac = new ValidationAndCml();
48          FileReader fr = null;
49          try {
50              MDLConverter converter = new MDLConverter();
51              fr = new FileReader(file);
52              vac.cml = new Document(converter.readMOL(fr));
53          } catch (IOException e) {
54              LOG
55                      .error("Problem reading from " + file + ": "
56                              + e.getMessage(), e);
57              throw new RuntimeException("Problem reading from " + file + ": "
58                      + e.getMessage(), e);
59          } catch (CMLException e) {
60              vac.validation.getErrors().add(e.getMessage());
61          } finally {
62              if (fr != null) {
63                  try {
64                      fr.close();
65                  } catch (IOException e) {
66                      LOG.warn(e);
67                  }
68              }
69          }
70          return vac;
71      }
72  
73      /**
74       * Not expecting much useful here.
75       */
76      public Model extractMetadata(File file, String packageUri) {
77          return ModelFactory.createDefaultModel();
78      }
79  
80      public Document convert(File file) {
81          FileReader fr = null;
82          try {
83              MDLConverter converter = new MDLConverter();
84              fr = new FileReader(file);
85              return new Document(converter.readMOL(fr));
86          } catch (IOException e) {
87              LOG
88                      .error("Problem reading from " + file + ": "
89                              + e.getMessage(), e);
90              throw new RuntimeException("Problem reading from " + file + ": "
91                      + e.getMessage(), e);
92          } catch (CMLException e) {
93              LOG
94                      .error("Problem reading from " + file + ": "
95                              + e.getMessage(), e);
96              throw new RuntimeException("Problem reading from " + file + ": "
97                      + e.getMessage(), e);
98          } finally {
99              if (fr != null) {
100                 try {
101                     fr.close();
102                 } catch (IOException e) {
103                     LOG.warn(e);
104                 }
105             }
106         }
107     }
108 
109     public String convertName(String fileName) {
110         Matcher m = MOLFILE_NAME_PATTERN.matcher(fileName);
111         return (m.matches() ? m.group(1) : fileName) + CML_XML_SUFFIX;
112     }
113 
114     public String getMimeType() {
115         return "chemical/x-mdl-molfile";
116     }
117 
118     public void process(DataProcess process) {
119         if (process.getPerformConversion() || process.getPerformValidation()) {
120             ValidationAndCml vac = convertAndValidate(process.getSourceFile());
121             process.setConvertedCml(vac.cml);
122             process.setValidation(vac.validation);
123         }
124         if (process.getPerformExtraction()) {
125             process.setExtractedMetadata(extractMetadata(process
126                     .getSourceFile(), process.getMetadataPackageUri()));
127         }
128     }
129 
130 }
131 
132 class ValidationAndCml {
133     Validation validation = new Validation();
134 
135     Document cml;
136 }