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.constants.DENOPTIMConstants;
45import denoptim.fitness.IDenoptimDescriptor;
111 private static final String[]
NAMES = {
"SocketProvidedDescriptor"};
134 return new DescriptorSpecification(
"Denoptim source code",
135 this.getClass().getName(), paramID,
"DENOPTIM project");
162 throw new IllegalArgumentException(
"No parameter for name: "+name);
176 if (params.length != 2)
178 throw new IllegalArgumentException(
"SocketProvidedDescriptor only "
179 +
"expects two parameter");
181 if (!(params[0] instanceof String))
183 throw new IllegalArgumentException(
"Parameter is not String ("
184 + params[0].getClass().getName() +
").");
186 if (!(params[1] instanceof Integer))
188 throw new IllegalArgumentException(
"Parameter is not Integer ("
189 + params[0].getClass().getName() +
").");
193 port = (Integer) params[1];
202 Object[] params =
new Object[2];
227 }
catch (IOException e1)
229 throw new IllegalArgumentException(
"Could not connect to socket",e1);
232 Runtime.getRuntime().addShutdownHook(
new Thread(){
public void run(){
235 }
catch (IOException e) { }
238 PrintWriter writerToSocket;
241 OutputStream outputSocket = socket.getOutputStream();
242 writerToSocket =
new PrintWriter(outputSocket,
true);
243 }
catch (IOException e1)
248 }
catch (IOException e)
252 throw new IllegalArgumentException(
"Could not connect to socket",e1);
255 BufferedReader readerFromSocket;
258 InputStream inputFromSocket = socket.getInputStream();
259 readerFromSocket =
new BufferedReader(
260 new InputStreamReader(inputFromSocket));
261 }
catch (IOException e1)
266 }
catch (IOException e)
270 throw new IllegalArgumentException(
"Could not read from socket",e1);
273 JsonObject jsonObj =
new JsonObject();
274 Object smilesProp = mol.getProperty(
"SMILES");
275 if (smilesProp==
null)
280 }
catch (IOException e)
284 throw new IllegalArgumentException(
"AtomContainers fed to "
285 + this.getClass().getName() +
" are expected to contain "
287 +
"', but it was not found.");
289 jsonObj.addProperty(
"SMILES", smilesProp.toString());
290 jsonObj.addProperty(
"version",
version);
293 Gson jsonConverted =
new GsonBuilder().create();
296 writerToSocket.println(jsonConverted.toJson(jsonObj));
299 socket.shutdownOutput();
300 }
catch (IOException e1)
305 }
catch (IOException e)
309 throw new IllegalStateException(
"Could not half-close socket from "
310 + this.getClass().getName(),e1);
313 JsonObject answer =
null;
315 Exception potentialProblem =
null;
317 answer = jsonConverted.fromJson(readerFromSocket.readLine(),
321 double value = Double.parseDouble(
323 result =
new DoubleResult(value);
325 result =
new DoubleResult(Double.NaN);
329 System.err.println(
"ERROR: Socket server replied without "
332 +
"'" +
NAMES[0] +
"'to NaN.");
334 result =
new DoubleResult(Double.NaN);
335 potentialProblem =
new Exception(
336 "Wrong syntax in answer from socket server.");
338 }
catch (JsonSyntaxException | IOException e) {
340 result =
new DoubleResult(Double.NaN);
341 potentialProblem = e;
347 }
catch (IOException e)
366 return new DoubleResultType();
375 return "Socket-provided descriptor";
384 return "We ignore how the descriptor is calculated. We only know we "
385 +
"can ask a socket server for a score. The connection can be "
386 +
"parametrized so that we can define that a server is "
387 +
"reachable at host name <code>" +
PARAMNAMES[0] +
"</code>, "
388 +
"and port <code>" +
PARAMNAMES[1] +
"</code>. By convention, "
389 +
"the communication deploys a JSON string. "
390 +
"Such format is expected for both "
391 +
"the request for a score (the request generated by "
392 +
"DENOPTIM, i.e., the client) and for "
393 +
"the answer to such request (produced by the "
394 +
"server). The requst contains always the SMILES of the "
395 +
"candidate (e.g., "
402 +
"communication protocol will produce a <code>NaN</code> "
412 return new String[] {
"molecular"};
General set of constants used in DENOPTIM.
static final String MOLERRORTAG
SDF tag containing errors during execution of molecule specific tasks.
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...