19package denoptim.fitness.descriptors;
21import java.io.BufferedReader;
22import java.io.IOException;
23import java.io.InputStream;
24import java.io.InputStreamReader;
25import java.io.OutputStream;
26import java.io.PrintWriter;
27import java.net.Socket;
29import org.openscience.cdk.exception.CDKException;
30import org.openscience.cdk.interfaces.IAtomContainer;
31import org.openscience.cdk.qsar.AbstractMolecularDescriptor;
32import org.openscience.cdk.qsar.DescriptorSpecification;
33import org.openscience.cdk.qsar.DescriptorValue;
34import org.openscience.cdk.qsar.IMolecularDescriptor;
35import org.openscience.cdk.qsar.result.DoubleResult;
36import org.openscience.cdk.qsar.result.DoubleResultType;
37import org.openscience.cdk.qsar.result.IDescriptorResult;
39import com.google.gson.Gson;
40import com.google.gson.GsonBuilder;
41import com.google.gson.JsonObject;
42import com.google.gson.JsonSyntaxException;
44import denoptim.fitness.IDenoptimDescriptor;
110 private static final String[]
NAMES = {
"SocketProvidedDescriptor"};
133 return new DescriptorSpecification(
"Denoptim source code",
134 this.getClass().getName(), paramID,
"DENOPTIM project");
161 throw new IllegalArgumentException(
"No parameter for name: "+name);
175 if (params.length != 2)
177 throw new IllegalArgumentException(
"SocketProvidedDescriptor only "
178 +
"expects two parameter");
180 if (!(params[0] instanceof String))
182 throw new IllegalArgumentException(
"Parameter is not String ("
183 + params[0].getClass().getName() +
").");
185 if (!(params[1] instanceof Integer))
187 throw new IllegalArgumentException(
"Parameter is not Integer ("
188 + params[0].getClass().getName() +
").");
192 port = (Integer) params[1];
201 Object[] params =
new Object[2];
226 }
catch (IOException e1)
228 throw new IllegalArgumentException(
"Could not connect to socket",e1);
231 Runtime.getRuntime().addShutdownHook(
new Thread(){
public void run(){
234 }
catch (IOException e) { }
237 PrintWriter writerToSocket;
240 OutputStream outputSocket = socket.getOutputStream();
241 writerToSocket =
new PrintWriter(outputSocket,
true);
242 }
catch (IOException e1)
247 }
catch (IOException e)
251 throw new IllegalArgumentException(
"Could not connect to socket",e1);
254 BufferedReader readerFromSocket;
257 InputStream inputFromSocket = socket.getInputStream();
258 readerFromSocket =
new BufferedReader(
259 new InputStreamReader(inputFromSocket));
260 }
catch (IOException e1)
265 }
catch (IOException e)
269 throw new IllegalArgumentException(
"Could not read from socket",e1);
272 JsonObject jsonObj =
new JsonObject();
273 Object smilesProp = mol.getProperty(
"SMILES");
274 if (smilesProp==
null)
279 }
catch (IOException e)
283 throw new IllegalArgumentException(
"AtomContainers fed to "
284 + this.getClass().getName() +
" are expected to contain "
286 +
"', but it was not found.");
288 jsonObj.addProperty(
"SMILES", smilesProp.toString());
289 jsonObj.addProperty(
"version",
version);
292 Gson jsonConverted =
new GsonBuilder().create();
295 writerToSocket.println(jsonConverted.toJson(jsonObj));
298 socket.shutdownOutput();
299 }
catch (IOException e1)
304 }
catch (IOException e)
308 throw new IllegalStateException(
"Could not half-close socket from "
309 + this.getClass().getName(),e1);
312 JsonObject answer =
null;
315 answer = jsonConverted.fromJson(readerFromSocket.readLine(),
319 double value = Double.parseDouble(
321 result =
new DoubleResult(value);
324 result =
new DoubleResult(Double.NaN);
326 System.err.println(
"WARNING: Socket server replied without "
329 +
"'" +
NAMES[0] +
"'to NaN.");
330 result =
new DoubleResult(Double.NaN);
332 }
catch (JsonSyntaxException | IOException e) {
334 result =
new DoubleResult(Double.NaN);
340 }
catch (IOException e)
358 return new DoubleResultType();
367 return "Socket-provided descriptor";
376 return "We ignore how the descriptor is calculated. We only know we "
377 +
"can ask a socket server for a score. The connection can be "
378 +
"parametrized so that we can define that a server is "
379 +
"reachable at host name <code>" +
PARAMNAMES[0] +
"</code>, "
380 +
"and port <code>" +
PARAMNAMES[1] +
"</code>. By convention, "
381 +
"the communication deploys a JSON string. "
382 +
"Such format is expected for both "
383 +
"the request for a score (the request generated by "
384 +
"DENOPTIM, i.e., the client) and for "
385 +
"the answer to such request (produced by the "
386 +
"server). The requst contains always the SMILES of the "
387 +
"candidate (e.g., "
394 +
"communication protocol will produce a <code>NaN</code> "
404 return new String[] {
"molecular"};
Sends the request to produce a numerical descriptor to a defined socket and receives back the respons...
final int version
Version identifier.
void setParameters(Object[] params)
Set the parameters attributes.
DescriptorValue calculate(IAtomContainer mol)
DescriptorSpecification getSpecification()
Get the specification attribute of socket-based descriptor provider.
SocketProvidedDescriptor()
Constructor for a SocketProvidedDescriptor object.
Integer port
The identifier of the port used to communicate with the socket server.
String[] getParameterNames()
Gets the parameterNames attribute of the TanimotoMolSimilarity object.
static final String[] PARAMNAMES
Name of the input parameters.
String hostname
The name of the host or ID address used to communicate with the socket server.
static final String KEYJSONMEMBERSCORE
The key of the JSON member defining the score/s for the descriptor calculated.
static final String KEYJSONMEMBERSMILES
The key of the JSON member defining the SMILES of the candidate for which the socket server should pr...
Object getParameterType(String name)
static final String[] NAMES
NAme of the descriptor produced by this class.
String[] getDictionaryClass()
Get the classification of this descriptor.A descriptor can belong to one or more classes simultaneous...
String[] getDescriptorNames()
String getDictionaryTitle()
Gets the title of this descriptor as it should be in the dictionary.the title
static final String KEYJSONMEMBERERR
The key of the JSON member defining an error in the calculation of the score.
String getDictionaryDefinition()
Get a string that describes the descriptor in detail.Might contain mathematical formulation....
IDescriptorResult getDescriptorResultType()
This interface forces descriptors that are not defined in the CDK ontology to provide information tha...