src/Controller/PredictionController.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\Filesystem\Filesystem;
  8. use Symfony\Component\Process\Exception\ProcessFailedException;
  9. use Symfony\Component\Process\Process;
  10. use Symfony\Component\HttpKernel\KernelInterface;
  11. class PredictionController extends AbstractController
  12. {
  13.     /**
  14.      * @Route("/prediction/index", name="app_prediction_index")
  15.      */
  16.     public function index(): Response
  17.     {
  18.         return $this->render('prediction/index.html.twig',[
  19.         ]);
  20.     }
  21.     /**
  22.      * @Route("/predict/{smiles}", name="app_prediction_predict")
  23.      */
  24.     public function predictAction($smilesKernelInterface $kernel)
  25.     {
  26.         $isFlavonoid    false;
  27.         $molGroup       '';
  28.         $filesystem     = new Filesystem();
  29.         $smiles         urldecode($smiles);
  30.         try {
  31.             $tmpFile $filesystem->tempnam('/tmp''prediction_');
  32.         } catch (IOExceptionInterface $exception) {
  33.             return new JsonResponse(array('status'=>'error'));          
  34.         }
  35.         //flavo detector
  36.         $cmd sprintf("python3 %s/bin/flavo_detector.py '%s'"$kernel->getProjectDir(), $smiles);
  37.         $process Process::fromShellCommandline($cmd);
  38.         $process->setTimeout(600);
  39.         $process->run();
  40.         $ret =  $process->getOutput();
  41.         $isFlavonoid = (bool) trim($ret);
  42.         //Vega
  43.         $prefix '/usr/local/bin/';
  44.         $cmd sprintf('%sjava -jar %s/bin/NCSTOXVega-0.18/NCSTOXVega.jar -add -smiles "%s"  -output %s'$prefix$kernel->getProjectDir(), $smiles$tmpFile);
  45.         $process Process::fromShellCommandline($cmd);
  46.         $process->setTimeout(600);
  47.         $process->run();
  48.         if (!$process->isSuccessful()) {
  49.             throw new ProcessFailedException($process);
  50.             return new JsonResponse(array('status'=>'error'));          
  51.         }        
  52.         $result file_get_contents($tmpFile);
  53.         $array json_decode($resulttrue);
  54.         // dd('array', $array);
  55.         $grouped $this->groupByEndpoint($array);
  56.         //PROCESS II
  57.         $cmd sprintf("python3 %s/bin/molgroup_detector.py '%s'"$kernel->getProjectDir(), $smiles);
  58.         $process Process::fromShellCommandline($cmd);
  59.         $process->setTimeout(30);
  60.         $process->run();
  61.         if (!$process->isSuccessful()) {
  62.             throw new ProcessFailedException($process);
  63.             return new JsonResponse(array('status'=>'error'));          
  64.         }else{
  65.             $molGroup $process->getOutput();
  66.         }
  67.         $grouped['molGroup'] = trim($molGroup);
  68.         $filesystem->remove($tmpFile);
  69.         // dump('isFlavonoid', $isFlavonoid);
  70.         //process flavo
  71.         if($isFlavonoid)
  72.         {
  73.             try {
  74.                 $tmpFile $filesystem->tempnam('/tmp''prediction_');
  75.             } catch (IOExceptionInterface $exception) {
  76.                 return new JsonResponse(array('status'=>'error'));          
  77.             }
  78.             $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);
  79.             $process Process::fromShellCommandline($cmd);
  80.             $process->setTimeout(600);
  81.             $process->run();
  82.             if (!$process->isSuccessful()) {
  83.                 throw new ProcessFailedException($process);
  84.                 return new JsonResponse(array('status'=>'error'));          
  85.             }        
  86.             $resultFlavo file_get_contents($tmpFile);
  87.             $arrayFlavo json_decode($resultFlavotrue);
  88.             $filesystem->remove($tmpFile);
  89.             if(!empty($grouped['predictions']['In Vivo Micronucleus']) && !empty($arrayFlavo[0]['predictions']))
  90.             {
  91.                 // dump('oui');
  92.                 $grouped['predictions']['In Vivo Micronucleus'] = $arrayFlavo[0]['predictions'];
  93.             }
  94.             
  95.         }
  96.         $html $this->renderView('prediction/result.html.twig', [
  97.             'molGroup' => $molGroup,
  98.             'result' => $grouped
  99.         ]);
  100.         return new JsonResponse(array('status'=>'success''data'=> array(
  101.             "html"     => $html,
  102.             "errors"   => count($array[0]['errors']) > true false 
  103.         )));
  104.     }
  105.     protected function groupByEndpoint(array $rawArray)
  106.     {
  107.         $output = [];
  108.         $array $rawArray[0];
  109.         $output["smiles"] =  $array['smiles'];
  110.         $output["valid"]  =  $array['valid'];
  111.         $output["errors"] =  $array['errors'];
  112.         $output["predictions"] = [];
  113.         $output["predictions"]['Skin Absorption']   = [];
  114.         $output["predictions"]['Skin Absorption'][] = $array['predictions'][0];
  115.         $output["predictions"]['Skin Absorption'][] = $array['predictions'][1];
  116.         $output["predictions"]['Skin Permeation']   = [];
  117.         $output["predictions"]['Skin Permeation'][] = $array['predictions'][2];
  118.         $output["predictions"]['Skin Permeation'][] = $array['predictions'][3];
  119.         $output["predictions"]['Skin Sensitization']   = [];
  120.         $output["predictions"]['Skin Sensitization'][] = $array['predictions'][4];
  121.         $output["predictions"]['Cramer classification']   = [];
  122.         $output["predictions"]['Cramer classification'][] = $array['predictions'][5];
  123.         $output["predictions"]['In Vivo Micronucleus']   = [];
  124.         $output["predictions"]['In Vivo Micronucleus'][] = $array['predictions'][6];
  125.         $output["predictions"]['Mutagenicity']   = [];
  126.         $output["predictions"]['Mutagenicity'][] = $array['predictions'][7];
  127.         $output["predictions"]['Mutagenicity'][] = $array['predictions'][8];
  128.         $output["predictions"]['Mutagenicity'][] = $array['predictions'][9];
  129.         $output["predictions"]['Mutagenicity'][] = $array['predictions'][10];
  130.         $output["predictions"]['Mutagenicity'][] = $array['predictions'][11];
  131.         return $output;
  132.     }
  133. }