summaryrefslogtreecommitdiff
path: root/java/CdkDescriptors.java
blob: 092e986e045f839fb0d628f89c3ae26e932260d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.*;
import java.io.*;
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.io.iterator.IteratingMDLReader;
import org.openscience.cdk.qsar.*;
import org.openscience.cdk.qsar.DescriptorValue;

class CdkDescriptors {
  public static void main(String[] args) {

    // parse command line arguments > 1 (descriptors)
    DescriptorEngine engine;
    List<String> classNames = new ArrayList<String>();
    for (int i =1; i < args.length; i++) {
      classNames.add("org.openscience.cdk.qsar.descriptors.molecular." + args[i] + "Descriptor");
    }
    engine = new DescriptorEngine(classNames);
    List<IDescriptor> instances =  engine.instantiateDescriptors(classNames);
    List<DescriptorSpecification> specs = engine.initializeSpecifications(instances);
    engine.setDescriptorInstances(instances);
    engine.setDescriptorSpecifications(specs);

    try {
      BufferedReader br = new BufferedReader(new FileReader(args[0]));
      PrintWriter yaml = new PrintWriter(new FileWriter(args[0]+"cdk.yaml"));
      // parse 3d sdf from file and calculate descriptors
      IteratingMDLReader reader = new IteratingMDLReader( br, DefaultChemObjectBuilder.getInstance());
      int c = 0;
      while (reader.hasNext()) {
        try {
          System.out.println("computing "+(args.length-1)+" descriptors for compound "+(++c));
          IMolecule molecule = (IMolecule)reader.next();
          engine.process(molecule);
          Map<Object,Object> properties = molecule.getProperties();
          Boolean first = true;
          for (Map.Entry<Object, Object> entry : properties.entrySet()) {
            try {
              if ((entry.getKey() instanceof DescriptorSpecification) && (entry.getValue() instanceof DescriptorValue)) {
                DescriptorSpecification property = (DescriptorSpecification)entry.getKey();
                DescriptorValue value = (DescriptorValue)entry.getValue();
                String[] values = value.getValue().toString().split(",");
                for (int i = 0; i < values.length; i++) {
                  if (first) { yaml.print("- "); first = false; }
                  else { yaml.print("  "); }
                  String cdk_class = property.getImplementationTitle();
                  String name = cdk_class.substring(cdk_class.lastIndexOf(".")+1).replace("Descriptor","");
                  yaml.println("Cdk." + name + "." + value.getNames()[i] + ": " + values[i]);
                }
                
              }
            }
            catch (ClassCastException e) { } // sdf properties are stored as molecules properties (strings), ignore them
            catch (Exception e) { e.printStackTrace(); } // output nothing to yaml
          }
        }
        catch (Exception e) {
          yaml.println("- {}");
          e.printStackTrace();
          continue;
        }
      }
      yaml.close();
    }
    catch (Exception e) { e.printStackTrace(); }
  }
}