findListOfCompoundsJSN method
Future
findListOfCompoundsJSN(
- String applicationDescription
)
Implementation
Future findListOfCompoundsJSN(String applicationDescription) async {
try {
// Step 1: Get relevant SMILES patterns from AI
final smilesList = await getRelevantSmiles(applicationDescription);
// Step 2: Search PubChem for each SMILES pattern
final Set<int> uniqueCids = {};
for (String smiles in smilesList) {
try {
final cids = await getSubstructureCids(smiles);
uniqueCids.addAll(cids.take(maxResultsPerSmiles));
} catch (e) {
// Optionally handle or log errors from getSubstructureCids
print('Error fetching CIDs for SMILES $smiles: $e');
}
}
if (uniqueCids.isEmpty) {
return jsonEncode({
'query_smiles': smilesList,
'results': [],
'message': 'No compounds found for the generated SMILES patterns.'
});
}
// Step 3: Fetch properties for collected CIDs
final List<Map<String, dynamic>> compoundPropertiesList = [];
for (int cid in uniqueCids.take(10)) {
// Limiting to top 10 unique CIDs
try {
compoundPropertiesList.add(await getCompoundProperties(cid));
} catch (e) {
// Include error information for specific CIDs in the JSON response
compoundPropertiesList.add({
'cid': cid,
'error': 'Failed to fetch properties: ${e.toString()}'
});
}
}
// Prepare the data for JSON encoding
final Map<String, dynamic> jsonData = {
'query_application_description': applicationDescription,
'generated_smiles_patterns': smilesList,
'retrieved_compounds': compoundPropertiesList
};
return jsonEncode(jsonData);
} catch (e) {
// Return a JSON formatted error message
return jsonEncode(
{'error': 'An overall error occurred: ${e.toString()}'});
}
}