<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\String\Slugger\SluggerInterface;
use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
class MoleculeController extends AbstractController
{
/**
* @Route("/molecule/lookup", name="app_molecule_lookup")
*/
public function lookup(Request $request): Response
{
$pattern = '';
$data = [];
$mode = 'start';
$content = json_decode($request->getContent(), true);
if(array_key_exists('mode', $content)){
$mode = $content['mode'];
}
if(array_key_exists('lookup', $content))
{
if($mode == 'start')
{
$pattern = trim($content['lookup']) .'%';
}
else
{
$pattern = '%'. trim($content['lookup']) .'%';
}
$result = \App\Api\Operations\MoleculeLookup::execute($pattern)->data->ncstox_molecule_lookup;
foreach($result as $item)
{
$data[] = ['id' => $item->uuid, 'name' => $item->molname];
}
}
return $this->render('default/_lookup_items.html.twig', [
'data' => $data,
]);
}
/**
* @Route("/molecule/show/{uuid}", name="app_molecule_show")
*/
public function show(string $uuid)
{
$result = \App\Api\Operations\MoleculeByPk::execute($uuid)->data->molecule_struct[0];
// dump("MoleculeByPk", $result);
return $this->render('molecule/show.html.twig', [
'molecule' => $result,
]);
}
public function genotoxicity(string $uuid): Response
{
$genotoxResult = \App\Api\Operations\MoleculeGenotoxicityByPk::execute($uuid)->data->molecule_struct[0];
$textMiningResult = \App\Api\Operations\MoleculeTextMiningByPk::execute($uuid)->data->molecule_struct[0];
// dump("MoleculeGenotoxicityByPk", $genotoxResult);
return $this->render('molecule/tab/_genotox.html.twig', [
'genotoxResult' => $genotoxResult,
'textMiningResult' => $textMiningResult
]);
}
public function sensitization(string $uuid): Response
{
$sensitizationResult = \App\Api\Operations\MoleculeSensitizationByPk::execute($uuid)->data->molecule_struct[0];
return $this->render('molecule/tab/_sensitization.html.twig', [
'sensitizationResult' => $sensitizationResult,
]);
}
public function skinPermeation(string $uuid): Response
{
$skinPermeationResult = \App\Api\Operations\MoleculeSkinPermeationByPk::execute($uuid)->data->molecule_struct[0];
return $this->render('molecule/tab/_skin_permeation.html.twig', [
'skinPermeationResult' => $skinPermeationResult,
]);
}
public function systemicToxicity(string $uuid): Response
{
$toxResult = \App\Api\Operations\MoleculeSystemicToxicityByPk::execute($uuid)->data->molecule_struct[0];
return $this->render('molecule/tab/_systemic_toxicity.html.twig', [
'toxResult' => $toxResult,
]);
}
/**
* @Route("/molecule/plants/{uuid}", name="app_molecule_plant_list")
*/
public function plantList(string $uuid): Response
{
$plantList = [];
// $result = \App\Api\Operations\MoleculePlantsByPk::execute($uuid)->data->molecule_struct[0];
$result = \App\Api\Operations\MoleculePlantDataByPk::execute($uuid)->data->molecule_struct[0];
foreach($result->molecule_plants as $item)
{
if(!isset($plantList[$item->plant_name]))
{
$plantList[$item->plant_name] = [];
}
$plantList[$item->plant_name][] = $item;
}
return $this->render('molecule/tab/_plant.html.twig', [
'plantList' => $plantList,
'uuid' => $uuid
]);
}
/**
* @Route("/molecule/print/{uuid}", name="app_molecule_print")
*/
public function print($uuid, \Knp\Snappy\Pdf $knpSnappyPdf, SluggerInterface $slugger, KernelInterface $kernel)
{
$result_struct = \App\Api\Operations\MoleculeByPk::execute($uuid)->data->molecule_struct[0];
$filename = $slugger->slug($result_struct->molname);
$html = $this->renderView('molecule/pdf.html.twig', ['molecule' => $result_struct]);
return new PdfResponse(
$knpSnappyPdf->getOutputFromHtml($html),
$filename
);
}
/**
* @Route("depict/{format}/{size}/{ratio}/{smiles}", name="molecule_depict", requirements={"smiles"=".+"}))
*/
public function depict(KernelInterface $kernel, string $smiles, string $format = 'png', int $size = 600, string $ratio = '4:3')
{
if(!$smiles){
return new Response();
}
$height = $size;
if($ratio == '4:3')
{
$height = round(($size / 1.33), -1);
}
$filepath = sprintf('%s%s-%s-%s.%s', $kernel->getProjectDir() .'/public/structures/', hash('md5', $smiles), $size, $ratio, $format) ;
if(!file_exists($filepath))
{
$args = [
'indigo-depict',
sprintf('- "%s"', $smiles),
$filepath,
'-dearom',
sprintf('-w %s', $size),
sprintf('-h %s', $height),
'-thickness 1.4',
'-margins 10 5',
'2>&1'
];
exec(implode(' ', $args), $ret);
}
$response = new BinaryFileResponse($filepath);
return $response;
}
/**
* @Route("/molecule/depict", name="molecule_depict_old")
*/
public function indigoDepict(Request $request, KernelInterface $kernel)
{
$projectDir = $kernel->getProjectDir();
$smiles = $request->get('smiles');
$size = $request->get('size');
$hash = md5($smiles);
$indigo_depict_path = $projectDir . '/../bin';
$outfile = $projectDir . '/public/structures/'.$hash.'.png';
if($size)
{
$outfile = $outfile . '-' . $size;
}
if(!file_exists($outfile))
{
if($size)
{
$units = explode('x', $size);
$cmd = '/usr/bin/indigo-depict - "'.$smiles.'" '.$outfile.' -arom -w '.$units[0].' -h '.$units[1].' -thickness 1.4 -margins 5 5';
$process = Process::fromShellCommandline($cmd);
$process->run();
}else{
$cmd = '/usr/bin/indigo-depict - "'.$smiles.'" '.$outfile.' -arom -w 900 -h 400 -thickness 1.4 -margins 5 5 2>&1';
// dd($cmd);
$process = Process::fromShellCommandline($cmd);
$process->run();
}
}
return $this->render('molecule/depict.html.twig', array('hash' => $hash));
}
}