<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use Symfony\Component\HttpKernel\KernelInterface;
class PredictionController extends AbstractController
{
/**
* @Route("/prediction/index", name="app_prediction_index")
*/
public function index(): Response
{
return $this->render('prediction/index.html.twig',[
]);
}
/**
* @Route("/predict/{smiles}", name="app_prediction_predict")
*/
public function predictAction($smiles, KernelInterface $kernel)
{
$isFlavonoid = false;
$molGroup = '';
$filesystem = new Filesystem();
$smiles = urldecode($smiles);
try {
$tmpFile = $filesystem->tempnam('/tmp', 'prediction_');
} catch (IOExceptionInterface $exception) {
return new JsonResponse(array('status'=>'error'));
}
//flavo detector
$cmd = sprintf("python3 %s/bin/flavo_detector.py '%s'", $kernel->getProjectDir(), $smiles);
$process = Process::fromShellCommandline($cmd);
$process->setTimeout(600);
$process->run();
$ret = $process->getOutput();
$isFlavonoid = (bool) trim($ret);
//Vega
$prefix = '/usr/local/bin/';
$cmd = sprintf('%sjava -jar %s/bin/NCSTOXVega-0.18/NCSTOXVega.jar -add -smiles "%s" -output %s', $prefix, $kernel->getProjectDir(), $smiles, $tmpFile);
$process = Process::fromShellCommandline($cmd);
$process->setTimeout(600);
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
return new JsonResponse(array('status'=>'error'));
}
$result = file_get_contents($tmpFile);
$array = json_decode($result, true);
// dd('array', $array);
$grouped = $this->groupByEndpoint($array);
//PROCESS II
$cmd = sprintf("python3 %s/bin/molgroup_detector.py '%s'", $kernel->getProjectDir(), $smiles);
$process = Process::fromShellCommandline($cmd);
$process->setTimeout(30);
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
return new JsonResponse(array('status'=>'error'));
}else{
$molGroup = $process->getOutput();
}
$grouped['molGroup'] = trim($molGroup);
$filesystem->remove($tmpFile);
// dump('isFlavonoid', $isFlavonoid);
//process flavo
if($isFlavonoid)
{
try {
$tmpFile = $filesystem->tempnam('/tmp', 'prediction_');
} catch (IOExceptionInterface $exception) {
return new JsonResponse(array('status'=>'error'));
}
$cmd = sprintf('%s/bin/openlogic-openjdk-11.0.23/bin/java -jar %s/bin/InVivoMNflavonoids-0.1/NCSTOXVegaMNFlavonoids.jar -add -smiles "%s" -output %s', $kernel->getProjectDir(), $kernel->getProjectDir(), $smiles, $tmpFile);
$process = Process::fromShellCommandline($cmd);
$process->setTimeout(600);
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
return new JsonResponse(array('status'=>'error'));
}
$resultFlavo = file_get_contents($tmpFile);
$arrayFlavo = json_decode($resultFlavo, true);
$filesystem->remove($tmpFile);
if(!empty($grouped['predictions']['In Vivo Micronucleus']) && !empty($arrayFlavo[0]['predictions']))
{
// dump('oui');
$grouped['predictions']['In Vivo Micronucleus'] = $arrayFlavo[0]['predictions'];
}
}
$html = $this->renderView('prediction/result.html.twig', [
'molGroup' => $molGroup,
'result' => $grouped
]);
return new JsonResponse(array('status'=>'success', 'data'=> array(
"html" => $html,
"errors" => count($array[0]['errors']) > 0 ? true : false
)));
}
protected function groupByEndpoint(array $rawArray)
{
$output = [];
$array = $rawArray[0];
$output["smiles"] = $array['smiles'];
$output["valid"] = $array['valid'];
$output["errors"] = $array['errors'];
$output["predictions"] = [];
$output["predictions"]['Skin Absorption'] = [];
$output["predictions"]['Skin Absorption'][] = $array['predictions'][0];
$output["predictions"]['Skin Absorption'][] = $array['predictions'][1];
$output["predictions"]['Skin Permeation'] = [];
$output["predictions"]['Skin Permeation'][] = $array['predictions'][2];
$output["predictions"]['Skin Permeation'][] = $array['predictions'][3];
$output["predictions"]['Skin Sensitization'] = [];
$output["predictions"]['Skin Sensitization'][] = $array['predictions'][4];
$output["predictions"]['Cramer classification'] = [];
$output["predictions"]['Cramer classification'][] = $array['predictions'][5];
$output["predictions"]['In Vivo Micronucleus'] = [];
$output["predictions"]['In Vivo Micronucleus'][] = $array['predictions'][6];
$output["predictions"]['Mutagenicity'] = [];
$output["predictions"]['Mutagenicity'][] = $array['predictions'][7];
$output["predictions"]['Mutagenicity'][] = $array['predictions'][8];
$output["predictions"]['Mutagenicity'][] = $array['predictions'][9];
$output["predictions"]['Mutagenicity'][] = $array['predictions'][10];
$output["predictions"]['Mutagenicity'][] = $array['predictions'][11];
return $output;
}
}