src/Controller/MoleculeController.php line 62

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)
  53.     {        
  54.         $result = \App\Api\Operations\MoleculeByPk::execute($uuid)->data->molecule_struct[0];
  55.         // dump("MoleculeByPk", $result);
  56.         return $this->render('molecule/show.html.twig', [
  57.             'molecule' => $result,
  58.         ]); 
  59.     }
  60.     public function genotoxicity(string $uuid): Response
  61.     {
  62.         $genotoxResult = \App\Api\Operations\MoleculeGenotoxicityByPk::execute($uuid)->data->molecule_struct[0];
  63.         $textMiningResult = \App\Api\Operations\MoleculeTextMiningByPk::execute($uuid)->data->molecule_struct[0];
  64.         // dump("MoleculeGenotoxicityByPk", $genotoxResult);
  65.         return $this->render('molecule/tab/_genotox.html.twig', [
  66.             'genotoxResult'     => $genotoxResult,            
  67.             'textMiningResult'  => $textMiningResult
  68.         ]);
  69.     }
  70.     public function sensitization(string $uuid): Response
  71.     {
  72.         $sensitizationResult = \App\Api\Operations\MoleculeSensitizationByPk::execute($uuid)->data->molecule_struct[0];
  73.         return $this->render('molecule/tab/_sensitization.html.twig', [
  74.             'sensitizationResult' => $sensitizationResult,            
  75.         ]);
  76.     }
  77.     public function skinPermeation(string $uuid): Response
  78.     {
  79.         $skinPermeationResult = \App\Api\Operations\MoleculeSkinPermeationByPk::execute($uuid)->data->molecule_struct[0];
  80.         return $this->render('molecule/tab/_skin_permeation.html.twig', [
  81.             'skinPermeationResult' => $skinPermeationResult,            
  82.         ]);
  83.     }
  84.     public function systemicToxicity(string $uuid): Response
  85.     {
  86.         $toxResult = \App\Api\Operations\MoleculeSystemicToxicityByPk::execute($uuid)->data->molecule_struct[0];
  87.         return $this->render('molecule/tab/_systemic_toxicity.html.twig', [
  88.             'toxResult' => $toxResult,            
  89.         ]);
  90.     }
  91.     /**
  92.      * @Route("/molecule/plants/{uuid}", name="app_molecule_plant_list")
  93.      */
  94.     public function plantList(string $uuid): Response
  95.     {
  96.         $plantList = [];
  97.         // $result = \App\Api\Operations\MoleculePlantsByPk::execute($uuid)->data->molecule_struct[0];
  98.         $result = \App\Api\Operations\MoleculePlantDataByPk::execute($uuid)->data->molecule_struct[0];
  99.         foreach($result->molecule_plants as $item)
  100.         {   
  101.             if(!isset($plantList[$item->plant_name]))
  102.             {
  103.                 $plantList[$item->plant_name] = [];   
  104.             }
  105.             $plantList[$item->plant_name][] = $item;
  106.         }
  107.         return $this->render('molecule/tab/_plant.html.twig', [
  108.             'plantList' => $plantList,
  109.             'uuid' => $uuid
  110.         ]);
  111.     }            
  112.     
  113.     /**
  114.      * @Route("/molecule/print/{uuid}", name="app_molecule_print")
  115.      */
  116.     public function print($uuid, \Knp\Snappy\Pdf $knpSnappyPdfSluggerInterface $sluggerKernelInterface $kernel)
  117.     {
  118.         $result_struct = \App\Api\Operations\MoleculeByPk::execute($uuid)->data->molecule_struct[0];
  119.         $filename $slugger->slug($result_struct->molname);
  120.         $html $this->renderView('molecule/pdf.html.twig', ['molecule' => $result_struct]);
  121.         return new PdfResponse(
  122.             $knpSnappyPdf->getOutputFromHtml($html),
  123.             $filename
  124.         );
  125.     }
  126.     /**
  127.      * @Route("depict/{format}/{size}/{ratio}/{smiles}", name="molecule_depict", requirements={"smiles"=".+"}))
  128.      */
  129.     public function depict(KernelInterface $kernelstring $smilesstring $format 'png'int $size 600string $ratio '4:3')
  130.     {
  131.         if(!$smiles){
  132.             return new Response();
  133.         }
  134.         $height $size;
  135.         if($ratio == '4:3')
  136.         {
  137.             $height round(($size 1.33), -1);
  138.         }
  139.         $filepath sprintf('%s%s-%s-%s.%s'$kernel->getProjectDir() .'/public/structures/'hash('md5'$smiles), $size$ratio$format) ;
  140.         if(!file_exists($filepath))
  141.         {
  142.             $args = [
  143.                 'indigo-depict'
  144.                 sprintf('- "%s"'$smiles), 
  145.                 $filepath,  
  146.                 '-dearom'
  147.                 sprintf('-w %s'$size), 
  148.                 sprintf('-h %s'$height), 
  149.                 '-thickness 1.4'
  150.                 '-margins 10 5',
  151.                 '2>&1'
  152.             ];
  153.             exec(implode(' '$args), $ret);  
  154.         }
  155.            
  156.         $response = new BinaryFileResponse($filepath);
  157.         return $response;
  158.     }
  159.     /**
  160.      * @Route("/molecule/depict", name="molecule_depict_old")
  161.      */
  162.     public function indigoDepict(Request $requestKernelInterface $kernel)
  163.     {
  164.         $projectDir $kernel->getProjectDir();
  165.         $smiles $request->get('smiles');
  166.         $size $request->get('size');
  167.         $hash   md5($smiles);
  168.         $indigo_depict_path $projectDir '/../bin';
  169.         $outfile  $projectDir '/public/structures/'.$hash.'.png';
  170.         if($size)
  171.         {
  172.             $outfile $outfile '-' $size;
  173.         }
  174.         if(!file_exists($outfile))
  175.         {
  176.             if($size)
  177.             {
  178.                 $units explode('x'$size);
  179.                 $cmd '/usr/bin/indigo-depict - "'.$smiles.'" '.$outfile.' -arom -w '.$units[0].' -h '.$units[1].' -thickness 1.4 -margins 5 5';
  180.                 $process Process::fromShellCommandline($cmd);
  181.                 $process->run();               
  182.             }else{
  183.                 $cmd '/usr/bin/indigo-depict - "'.$smiles.'" '.$outfile.' -arom -w 900 -h 400 -thickness 1.4 -margins 5 5  2>&1';
  184.                 
  185.                 // dd($cmd);
  186.                 $process Process::fromShellCommandline($cmd);
  187.                 $process->run();
  188.                            
  189.             }
  190.         }
  191.         return $this->render('molecule/depict.html.twig', array('hash' => $hash));
  192.     
  193.     }
  194. }