From 9a5b7b1bfb352b3a6555babe8bc2344414f66185 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Sat, 15 Aug 2015 11:52:18 +0200 Subject: java libraries added --- .gitignore | 3 + java/.gitignore | 2 + java/CdkDescriptorInfo.class | Bin 0 -> 1705 bytes java/CdkDescriptorInfo.java | 22 +++++++ java/CdkDescriptors.class | Bin 0 -> 6167 bytes java/CdkDescriptors.java | 141 ++++++++++++++++++++++++++++++++++++++++ java/Jmol.jar | Bin 0 -> 3422261 bytes java/JoelibDescriptorInfo.class | Bin 0 -> 1039 bytes java/JoelibDescriptorInfo.java | 15 +++++ java/JoelibDescriptors.class | Bin 0 -> 2578 bytes java/JoelibDescriptors.java | 60 +++++++++++++++++ java/Rakefile | 15 +++++ java/cdk-1.4.19.jar | Bin 0 -> 16877592 bytes java/joelib2.jar | Bin 0 -> 1866172 bytes java/log4j.jar | Bin 0 -> 391834 bytes lazar.gemspec | 2 +- 16 files changed, 259 insertions(+), 1 deletion(-) create mode 100644 java/.gitignore create mode 100644 java/CdkDescriptorInfo.class create mode 100644 java/CdkDescriptorInfo.java create mode 100644 java/CdkDescriptors.class create mode 100644 java/CdkDescriptors.java create mode 100644 java/Jmol.jar create mode 100644 java/JoelibDescriptorInfo.class create mode 100644 java/JoelibDescriptorInfo.java create mode 100644 java/JoelibDescriptors.class create mode 100644 java/JoelibDescriptors.java create mode 100644 java/Rakefile create mode 100644 java/cdk-1.4.19.jar create mode 100644 java/joelib2.jar create mode 100644 java/log4j.jar diff --git a/.gitignore b/.gitignore index 8f883a0..926a09d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +last-utils +libfminer +test/fminer_debug.txt Gemfile.lock *.gem .bundle diff --git a/java/.gitignore b/java/.gitignore new file mode 100644 index 0000000..533ae63 --- /dev/null +++ b/java/.gitignore @@ -0,0 +1,2 @@ +bak +docs diff --git a/java/CdkDescriptorInfo.class b/java/CdkDescriptorInfo.class new file mode 100644 index 0000000..9b9408e Binary files /dev/null and b/java/CdkDescriptorInfo.class differ diff --git a/java/CdkDescriptorInfo.java b/java/CdkDescriptorInfo.java new file mode 100644 index 0000000..73a65ac --- /dev/null +++ b/java/CdkDescriptorInfo.java @@ -0,0 +1,22 @@ +import java.util.*; +import org.openscience.cdk.qsar.descriptors.molecular.*; +import org.openscience.cdk.qsar.*; + +class CdkDescriptorInfo { + public static void main(String[] args) { + + DescriptorEngine engine = new DescriptorEngine(DescriptorEngine.MOLECULAR); + + for (Iterator it = engine.getDescriptorInstances().iterator(); it.hasNext(); ) { + IDescriptor descriptor = it.next(); + String cdk_class = descriptor.getClass().toString().replaceAll("class ",""); + System.out.println("- :java_class: \""+cdk_class+"\""); + String description = engine.getDictionaryDefinition(cdk_class).replaceAll("^\\s+", "" ).replaceAll("\\s+$", "").replaceAll("\\s+", " "); + System.out.println(" :description: \""+description+"\""); + System.out.println(" :names:"); + for (String name : descriptor.getDescriptorNames()) { + System.out.println(" - \""+name+"\""); + } + } + } +} diff --git a/java/CdkDescriptors.class b/java/CdkDescriptors.class new file mode 100644 index 0000000..49bfaf2 Binary files /dev/null and b/java/CdkDescriptors.class differ diff --git a/java/CdkDescriptors.java b/java/CdkDescriptors.java new file mode 100644 index 0000000..1236240 --- /dev/null +++ b/java/CdkDescriptors.java @@ -0,0 +1,141 @@ +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; +import org.openscience.cdk.aromaticity.CDKHueckelAromaticityDetector; +import org.openscience.cdk.tools.manipulator.AtomContainerManipulator; +import org.openscience.cdk.exception.NoSuchAtomTypeException; + +class CdkDescriptors { + public static void main(String[] args) { + + if (args==null || args.length<2) { + System.err.println("required params: ..."); + System.exit(1); + } + if (! new File(args[0]).exists()){ + System.err.println("file not found "+args[0]); + System.exit(1); + } + + // command line descriptor params can be either "descriptorName" or "descriptorValueName" + // terminology: + // A descriptor can calculate serveral values, e.g., ALOGP produces ALOGP.ALogP, ALOGP.ALogp2, ALOGP.AMR + // "descriptorName" ALOGP + // "valueName" AMR + // "descriptorValueName" ALOGP.AMR + DescriptorEngine engine; + Set classNames = new LinkedHashSet(); // descriptors to be computed + Set descriptorNames = new LinkedHashSet(); // all values of this descriptor will be printed + Set descriptorValueNames = new LinkedHashSet(); // only these values of a descriptor will be printed + for (int i =1; i < args.length; i++) { + String descriptorName; + if (args[i].indexOf(".")!=-1) { + descriptorValueNames.add(args[i]); + descriptorName = args[i].substring(0,args[i].indexOf(".")); + } + else { + descriptorNames.add(args[i]); + descriptorName = args[i]; + } + classNames.add(getDescriptorClassName(descriptorName)); + } + + engine = new DescriptorEngine(new ArrayList(classNames)); + List instances = engine.instantiateDescriptors(new ArrayList(classNames)); + List 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(); + molecule = (IMolecule) AtomContainerManipulator.removeHydrogens(molecule); + try { + AtomContainerManipulator.percieveAtomTypesAndConfigureAtoms(molecule); + } + catch (NoSuchAtomTypeException e) { + e.printStackTrace(); + } + CDKHueckelAromaticityDetector.detectAromaticity(molecule); + + engine.process(molecule); + Map properties = molecule.getProperties(); + Boolean first = true; + for (Map.Entry 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++) { + String cdk_class = property.getImplementationTitle(); + String descriptorName = cdk_class.substring(cdk_class.lastIndexOf(".")+1).replace("Descriptor",""); + String descriptorValueName = descriptorName + "." + value.getNames()[i]; + if (descriptorNames.contains(descriptorName) || descriptorValueNames.contains(descriptorValueName)) { + if (first) { yaml.print("- "); first = false; } + else { yaml.print(" "); } + yaml.println("Cdk." + descriptorValueName + ": " + 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(); } + } + + + /** HACK to find the class for a descriptor + * problem: Descriptor is not always at the end of the class (APolDescriptor), but may be in the middle (AutocorrelationDescriptorPolarizability) + * this method makes a class-lookup using trial and error */ + static String getDescriptorClassName(String descriptorName) { + String split = splitCamelCase(descriptorName)+" "; // space mark possible positions for 'Descriptor' + for(int i = split.length()-1; i>0; i--) { + if (split.charAt(i)==' ') { // iterate over all spaces, starting with the trailing one + String test = split.substring(0,i)+"Descriptor"+split.substring(i+1,split.length()); // replace current space with 'Descriptor' .. + test = test.replaceAll("\\s",""); // .. and remove other spaces + String className = "org.openscience.cdk.qsar.descriptors.molecular." + test; + try { + Class.forName(className); + return className; + } catch (ClassNotFoundException e) {} + } + } + System.err.println("Descriptor not found: "+descriptorName); + System.exit(1); + return null; + } + + /** inserts space in between camel words */ + static String splitCamelCase(String s) { + return s.replaceAll( + String.format("%s|%s|%s", + "(?<=[A-Z])(?=[A-Z][a-z])", + "(?<=[^A-Z])(?=[A-Z])", + "(?<=[A-Za-z])(?=[^A-Za-z])" + ), + " " + ); + } +} diff --git a/java/Jmol.jar b/java/Jmol.jar new file mode 100644 index 0000000..2c7e5aa Binary files /dev/null and b/java/Jmol.jar differ diff --git a/java/JoelibDescriptorInfo.class b/java/JoelibDescriptorInfo.class new file mode 100644 index 0000000..0ee67bf Binary files /dev/null and b/java/JoelibDescriptorInfo.class differ diff --git a/java/JoelibDescriptorInfo.java b/java/JoelibDescriptorInfo.java new file mode 100644 index 0000000..851d650 --- /dev/null +++ b/java/JoelibDescriptorInfo.java @@ -0,0 +1,15 @@ +import joelib2.feature.FeatureHelper; + +class JoelibDescriptorInfo { + public static void main(String[] args) { + FeatureHelper helper = FeatureHelper.instance(); + System.out.println("---"); // document separator for Joelib debug messages + for (Object feature : helper.getNativeFeatures() ) { + System.out.println("- :java_class: \""+feature.toString()+"\""); + // methods for accessing feature descriptions e.g. with + // FeatureFactory.instance().getFeature(feature.toString()).getDescription().getText() or + // FeatureFactory.instance().getFeature(feature.toString()).getDescription().getHtml() + // are defunct + } + } +} diff --git a/java/JoelibDescriptors.class b/java/JoelibDescriptors.class new file mode 100644 index 0000000..d88ac63 Binary files /dev/null and b/java/JoelibDescriptors.class differ diff --git a/java/JoelibDescriptors.java b/java/JoelibDescriptors.java new file mode 100644 index 0000000..e90e35f --- /dev/null +++ b/java/JoelibDescriptors.java @@ -0,0 +1,60 @@ +import java.util.*; +import java.io.*; +import joelib2.feature.Feature; +import joelib2.feature.FeatureHelper; +import joelib2.feature.FeatureFactory; +import joelib2.feature.FeatureResult; +import joelib2.io.BasicIOType; +import joelib2.io.BasicIOTypeHolder; +import joelib2.io.BasicReader; +import joelib2.io.MoleculeFileHelper; +import joelib2.io.MoleculeFileIO; +import joelib2.io.MoleculeIOException; +import joelib2.molecule.BasicConformerMolecule; + +class JoelibDescriptors { + public static void main(String[] args) { + + String[] features = null; + features = new String[args.length-1]; + System.arraycopy(args,1,features,0,args.length-1); + + FeatureFactory factory = FeatureFactory.instance(); + MoleculeFileIO loader = null; + String line = new String(); + String sdf = new String(); + try { + // parse 3d sdf from file and calculate descriptors + InputStream is = new FileInputStream(args[0]); + PrintWriter yaml = new PrintWriter(new FileWriter(args[0]+"joelib.yaml")); + BasicIOType inType = BasicIOTypeHolder.instance().getIOType("SDF"); + loader = MoleculeFileHelper.getMolReader(is, inType); + BasicConformerMolecule mol = new BasicConformerMolecule(inType, inType); + while (true) { + try { + Boolean success = loader.read(mol); + if (!success) { break; } // last molecule + for (int i =0; i < features.length; i++) { + String name = "joelib2.feature.types." + features[i]; + Feature feature = factory.getFeature(name); + FeatureResult result = feature.calculate(mol); + if (i == 0) { yaml.print("- "); } + else { yaml.print(" "); } + yaml.print( "Joelib."+features[i]+": " ); + yaml.println( result.toString() ); + } + + } + catch (Exception e) { + System.err.println(e.toString()); + e.printStackTrace(); + } + } + yaml.close(); + } + catch (Exception e) { + System.err.println(e.toString()); + e.printStackTrace(); + } + } +} diff --git a/java/Rakefile b/java/Rakefile new file mode 100644 index 0000000..a865528 --- /dev/null +++ b/java/Rakefile @@ -0,0 +1,15 @@ +# Java class, classpath +java_classes = [ + ["CdkDescriptors", "cdk-1.4.19.jar"], + ["CdkDescriptorInfo", "cdk-1.4.19.jar"], + ["JoelibDescriptors", "joelib2.jar:."], + ["JoelibDescriptorInfo", "joelib2.jar:."], +] + +task :default => java_classes.collect{|c| "#{c.first}.class"} + +java_classes.each do |c| + file "#{c.first}.class" => "#{c.first}.java" do + puts `javac -classpath #{c.last} #{c.first}.java` + end +end diff --git a/java/cdk-1.4.19.jar b/java/cdk-1.4.19.jar new file mode 100644 index 0000000..3281c87 Binary files /dev/null and b/java/cdk-1.4.19.jar differ diff --git a/java/joelib2.jar b/java/joelib2.jar new file mode 100644 index 0000000..fae8d3c Binary files /dev/null and b/java/joelib2.jar differ diff --git a/java/log4j.jar b/java/log4j.jar new file mode 100644 index 0000000..c930a6a Binary files /dev/null and b/java/log4j.jar differ diff --git a/lazar.gemspec b/lazar.gemspec index 8da29b7..6fe3f5b 100644 --- a/lazar.gemspec +++ b/lazar.gemspec @@ -22,7 +22,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency "bundler" s.add_runtime_dependency "rest-client" s.add_runtime_dependency 'nokogiri' - s.add_runtime_dependency "openbabel" + #s.add_runtime_dependency "openbabel" s.add_runtime_dependency 'rserve-client' s.add_runtime_dependency "mongoid", '~> 5.0beta' -- cgit v1.2.3