src/Controller/MoleculeController.php line 121

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\HttpKernel\KernelInterface;
  8. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  9. use Symfony\Component\String\Slugger\SluggerInterface;
  10. use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
  11. use Symfony\Component\Process\Exception\ProcessFailedException;
  12. use Symfony\Component\Process\Process;
  13. class MoleculeController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("/molecule/lookup", name="app_molecule_lookup")
  17.      */
  18.     public function lookup(Request $request): Response
  19.     {   
  20.         $pattern    '';
  21.         $data       = [];
  22.         $mode       'start';
  23.         $content json_decode($request->getContent(), true);
  24.         if(array_key_exists('mode'$content)){
  25.             $mode $content['mode'];
  26.         }
  27.         if(array_key_exists('lookup'$content))
  28.         {   
  29.             
  30.             if($mode == 'start')
  31.             {
  32.                 $pattern trim($content['lookup']) .'%';
  33.             }
  34.             else
  35.             {
  36.                 $pattern '%'trim($content['lookup']) .'%';
  37.             }
  38.         
  39.             $result = \App\Api\Operations\MoleculeLookup::execute($pattern)->data->ncstox_molecule_lookup;
  40.             foreach($result as $item)
  41.             {
  42.                 $data[] = ['id' => $item->uuid'name' => $item->molname];
  43.             }
  44.         }
  45.         return $this->render('default/_lookup_items.html.twig', [
  46.             'data' => $data,
  47.         ]); 
  48.     }
  49.     /**
  50.      * @Route("/molecule/show/{uuid}", name="app_molecule_show")
  51.      */
  52.     public function show(string $uuid): \Symfony\Component\HttpFoundation\Response
  53.     {        
  54.         try {
  55.             $result = \App\Api\Operations\MoleculeByPk::execute($uuid)->data->molecule_struct[0];
  56.         }
  57.         catch(\Exception $e){
  58.             $this->addFlash('error''Molecule not found');
  59.             return $this->redirectToRoute('app_default');
  60.         }
  61.         return $this->render('molecule/show.html.twig', [
  62.             'molecule' => $result,
  63.         ]); 
  64.     }
  65.     public function genotoxicity(string $uuid): Response
  66.     {
  67.         $genotoxResult = \App\Api\Operations\MoleculeGenotoxicityByPk::execute($uuid)->data->molecule_struct[0];
  68.         $textMiningResult = \App\Api\Operations\MoleculeTextMiningByPk::execute($uuid)->data->molecule_struct[0];
  69.         // dump("MoleculeGenotoxicityByPk", $genotoxResult);
  70.         return $this->render('molecule/tab/_genotox.html.twig', [
  71.             'genotoxResult'     => $genotoxResult,            
  72.             'textMiningResult'  => $textMiningResult
  73.         ]);
  74.     }
  75.     public function sensitization(string $uuid): Response
  76.     {
  77.         $sensitizationResult = \App\Api\Operations\MoleculeSensitizationByPk::execute($uuid)->data->molecule_struct[0];
  78.         return $this->render('molecule/tab/_sensitization.html.twig', [
  79.             'sensitizationResult' => $sensitizationResult,            
  80.         ]);
  81.     }
  82.     public function skinPermeation(string $uuid): Response
  83.     {
  84.         $skinPermeationResult = \App\Api\Operations\MoleculeSkinPermeationByPk::execute($uuid)->data->molecule_struct[0];
  85.         return $this->render('molecule/tab/_skin_permeation.html.twig', [
  86.             'skinPermeationResult' => $skinPermeationResult,            
  87.         ]);
  88.     }
  89.     public function systemicToxicity(string $uuid): Response
  90.     {
  91.         $toxResult = \App\Api\Operations\MoleculeSystemicToxicityByPk::execute($uuid)->data->molecule_struct[0];
  92.         return $this->render('molecule/tab/_systemic_toxicity.html.twig', [
  93.             'toxResult' => $toxResult,            
  94.         ]);
  95.     }
  96.     /**
  97.      * @Route("/molecule/plants/{uuid}", name="app_molecule_plant_list")
  98.      */
  99.     public function plantList(string $uuid): Response
  100.     {
  101.         $plantList = [];
  102.         // dd(\App\Api\Operations\MoleculePlantsByPk::execute($uuid));
  103.         $result = \App\Api\Operations\MoleculePlantsByPk::execute($uuid)->data->molecule_struct[0];
  104.         // $result = \App\Api\Operations\MoleculePlantDataByPk::execute($uuid)->data->molecule_struct[0];
  105. /*        foreach($result->molecule_plants as $item)
  106.         {   
  107.             if(!isset($plantList[$item->plant_name]))
  108.             {
  109.                 $plantList[$item->plant_name] = [];   
  110.             }
  111.             $plantList[$item->plant_name][] = $item;
  112.         }*/
  113.         foreach($result->plant_molecule as $item)
  114.         {
  115.             if(!isset($plantList[$item->plantname]))
  116.             {
  117.                 $plantList[$item->plantname] = [];   
  118.             }
  119.             $plantList[$item->plantname][] = $item;
  120.         }
  121.         return $this->render('molecule/tab/_plant.html.twig', [
  122.             'plantList' => $plantList,
  123.             'uuid' => $uuid
  124.         ]);
  125.     }            
  126.     
  127.     /**
  128.      * @Route("/molecule/print/{uuid}", name="app_molecule_print")
  129.      */
  130.     public function print($uuid, \Knp\Snappy\Pdf $knpSnappyPdfSluggerInterface $sluggerKernelInterface $kernel): \Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse
  131.     {
  132.         $result_struct = \App\Api\Operations\MoleculeByPk::execute($uuid)->data->molecule_struct[0];
  133.         $filename $slugger->slug($result_struct->molname);
  134.         $html $this->renderView('molecule/pdf.html.twig', ['molecule' => $result_struct]);
  135.         return new PdfResponse(
  136.             $knpSnappyPdf->getOutputFromHtml($html),
  137.             $filename
  138.         );
  139.     }
  140.     /**
  141.      * @Route("depict/{format}/{size}/{ratio}/{smiles}", name="molecule_depict", requirements={"smiles"=".+"}))
  142.      */
  143.     public function depict(KernelInterface $kernelstring $smilesstring $format 'png'int $size 600string $ratio '4:3')
  144.     {
  145.         if(!$smiles){
  146.             return new Response();
  147.         }
  148.         $height $size;
  149.         if($ratio == '4:3')
  150.         {
  151.             $height round(($size 1.33), -1);
  152.         }
  153.         $filepath sprintf('%s%s-%s-%s.%s'$kernel->getProjectDir() .'/public/structures/'hash('md5'$smiles), $size$ratio$format) ;
  154.         if(!file_exists($filepath))
  155.         {
  156.             $args = [
  157.                 'indigo-depict'
  158.                 sprintf('- "%s"'$smiles), 
  159.                 $filepath,  
  160.                 '-dearom'
  161.                 sprintf('-w %s'$size), 
  162.                 sprintf('-h %s'$height), 
  163.                 '-thickness 1.4'
  164.                 '-margins 10 5',
  165.                 '2>&1'
  166.             ];
  167.             exec(implode(' '$args), $ret);  
  168.         }
  169.            
  170.         $response = new BinaryFileResponse($filepath);
  171.         return $response;
  172.     }
  173.     /**
  174.      * @Route("/molecule/depict", name="molecule_depict_old")
  175.      */
  176.     public function indigoDepict(Request $requestKernelInterface $kernel): \Symfony\Component\HttpFoundation\Response
  177.     {
  178.         $projectDir $kernel->getProjectDir();
  179.         $smiles $request->get('smiles');
  180.         $size $request->get('size');
  181.         $hash   md5($smiles);
  182.         $indigo_depict_path $projectDir '/../bin';
  183.         $outfile  $projectDir '/public/structures/'.$hash.'.png';
  184.         if($size)
  185.         {
  186.             $outfile $outfile '-' $size;
  187.         }
  188.         if(!file_exists($outfile))
  189.         {
  190.             if($size)
  191.             {
  192.                 $units explode('x'$size);
  193.                 $cmd '/usr/bin/indigo-depict - "'.$smiles.'" '.$outfile.' -arom -w '.$units[0].' -h '.$units[1].' -thickness 1.4 -margins 5 5';
  194.                 $process Process::fromShellCommandline($cmd);
  195.                 $process->run();               
  196.             }else{
  197.                 $cmd '/usr/bin/indigo-depict - "'.$smiles.'" '.$outfile.' -arom -w 900 -h 400 -thickness 1.4 -margins 5 5  2>&1';
  198.                 
  199.                 $process Process::fromShellCommandline($cmd);
  200.                 $process->run();
  201.                            
  202.             }
  203.         }
  204.         return $this->render('molecule/depict.html.twig', ['hash' => $hash]);
  205.     
  206.     }
  207. }