<?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 Nette\Utils\Paginator;
class SearchController extends AbstractController
{
/**
* @Route("/search", name="app_search")
*/
public function search(Request $request)
{
if($request->query->has('search'))
{
$data = $request->query->all('search');
if(!empty($data['molecule_uuid']))
{
return $this->redirectToRoute('app_molecule_show',['uuid' => $data['molecule_uuid']]);
}
elseif(!empty($data['plant_uuid']))
{
$arr = !empty($data['plant_part']) ? ['slug' => $data['plant_uuid'], "plant_part" => $data['plant_part']] : ['slug' => $data['plant_uuid']];
if(!empty($data['molecular_group']))
{
$arr['molecular_group'] = $data['molecular_group'];
}
return $this->redirectToRoute('app_search_plant', $arr);
}
elseif(!empty($data['molecular_group']))
{
return $this->redirectToRoute('app_search_molecular_group', ['group' => $data['molecular_group']]);
}
}
return $this->redirectToRoute('app_default');
}
/**
* @Route("/search/molecular_group/{group}", name="app_search_molecular_group")
*/
public function searchByMolecularGroup(string $group, Request $request)
{
$limit = 30;
$page = $request->query->get('page', 1);
$offset = ($page-1) * $limit;
$exp = new \App\Api\Types\String_comparison_exp;
$exp->_eq = $group;
$input = new \App\Api\Types\molecule_struct_bool_exp();
$input->molgroup = $exp;
$count = \App\Api\Operations\MoleculeWhereCount::execute($input)->data->molecule_struct_aggregate->aggregate->count;
$result = \App\Api\Operations\MoleculeWhere::execute($input, $limit, $offset)->data->molecule_struct;
$paginator = new Paginator;
$paginator->setItemCount($count);
$paginator->setItemsPerPage($limit);
$paginator->setPage($page);
return $this->render('search/result.html.twig', [
'result' => $result,
'paginator' => $paginator,
'searchLabel' => 'Molecular group',
'searchPattern' => ucfirst($group),
]);
}
/**
* @Route("/search/systemic_toxicity", name="app_search_systemic_toxicity")
*/
public function searchBySystemicToxicity(Request $request)
{
$limit = 30;
$page = $request->query->get('page', 1);
$offset = ($page-1) * $limit;
if($request->query->has('search'))
{
$data = $request->query->all('search');
if(!empty($data['systemic_toxicity']))
{
$scExp = new \App\Api\Types\String_comparison_exp;
$scExp->_eq = $data['systemic_toxicity'];
$stbExp = new \App\Api\Types\systemic_toxicity_bool_exp;
$stbExp->shortcramerclass = $scExp;
$input = new \App\Api\Types\molecule_struct_bool_exp();
$input->systemic_toxicity = $stbExp;
if(!empty($data['molecular_group']))
{
$expStr = new \App\Api\Types\String_comparison_exp;
$expStr->_eq = $data['molecular_group'];
$input->molgroup = $expStr;
}
//dd(\App\Api\Operations\MoleculeWhereCount::execute($input));
$count = \App\Api\Operations\MoleculeWhereCount::execute($input)->data->molecule_struct_aggregate->aggregate->count;
$result = \App\Api\Operations\MoleculeWhere::execute($input, $limit, $offset)->data->molecule_struct;
// dump($result);
$molecularGroups = \App\Api\Operations\MolecularGroup::execute()->data->molecular_group_view;
$paginator = new Paginator;
$paginator->setItemCount($count);
$paginator->setItemsPerPage($limit);
$paginator->setPage($page);
return $this->render('search/result.html.twig', [
'result' => $result,
'paginator' => $paginator,
'searchLabel' => 'Systemic toxicity',
'searchPattern' => sprintf('Cramer class %s',$data['systemic_toxicity']),
'searchValue' => $data['systemic_toxicity'],
'molecularGroups' => $molecularGroups,
'molecularGroup' => !empty($data['molecular_group']) ? $data['molecular_group'] : null
]);
}
}
return $this->redirectToRoute('app_toxicity_search');
}
/**
* @Route("/search/skin-sensitization", name="app_search_skin_sensitization")
*/
public function searchBySkinSensitization(Request $request)
{
$limit = 30;
$page = $request->query->get('page', 1);
$offset = ($page-1) * $limit;
if($request->query->has('search'))
{
$data = $request->query->all('search');
if(!empty($data['skin_sensitization']))
{
$boolExp = new \App\Api\Types\Boolean_comparison_exp;
$boolExp->_eq = trim($data['skin_sensitization']) == 'sensitizer' ? true : false;
$mcbExp = new \App\Api\Types\molecule_criticity_bool_exp;
$mcbExp->is_sensitizer = $boolExp;
$input = new \App\Api\Types\molecule_struct_bool_exp();
$input->criticity = $mcbExp;
if(!empty($data['molecular_group']))
{
$expStr = new \App\Api\Types\String_comparison_exp;
$expStr->_eq = $data['molecular_group'];
$input->molgroup = $expStr;
}
$count = \App\Api\Operations\MoleculeWhereCount::execute($input)->data->molecule_struct_aggregate->aggregate->count;
$result = \App\Api\Operations\MoleculeWhere::execute($input, $limit, $offset)->data->molecule_struct;
$molecularGroups = \App\Api\Operations\MolecularGroup::execute()->data->molecular_group_view;
$paginator = new Paginator;
$paginator->setItemCount($count);
$paginator->setItemsPerPage($limit);
$paginator->setPage($page);
return $this->render('search/result.html.twig', [
'result' => $result,
'paginator' => $paginator,
'searchLabel' => 'Skin sensitization',
'searchPattern' => ucfirst(str_replace('-', '', $data['skin_sensitization'])),
'molecularGroups' => $molecularGroups,
'searchValue' => $data['skin_sensitization'],
'molecularGroup' => !empty($data['molecular_group']) ? $data['molecular_group'] : null
]);
}
}
return $this->redirectToRoute('app_toxicity_search');
}
/**
* @Route("/search/genotoxicity", name="app_search_genotoxicity")
*/
public function searchByGenotoxicity(Request $request)
{
$limit = 30;
$page = $request->query->get('page', 1);
$offset = ($page-1) * $limit;
if($request->query->has('search'))
{
$data = $request->query->all('search');
if(!empty($data['genotoxicity']))
{
$boolExp = new \App\Api\Types\Boolean_comparison_exp;
$mcbExp = new \App\Api\Types\molecule_criticity_bool_exp;
if($data['genotoxicity'] == 'mutagenic')
{
$boolExp->_eq = true;
$mcbExp->is_mutagenic = $boolExp;
}
elseif($data['genotoxicity'] == 'clastogenic')
{
$boolExp->_eq = true;
$mcbExp->is_clastogenic = $boolExp;
}
elseif($data['genotoxicity'] == 'non-genotoxic')
{
$boolExp->_eq = false;
$mcbExp->is_genotoxic = $boolExp;
}
$input = new \App\Api\Types\molecule_struct_bool_exp();
$input->criticity = $mcbExp;
if(!empty($data['molecular_group']))
{
$expStr = new \App\Api\Types\String_comparison_exp;
$expStr->_eq = $data['molecular_group'];
$input->molgroup = $expStr;
}
$count = \App\Api\Operations\MoleculeWhereCount::execute($input)->data->molecule_struct_aggregate->aggregate->count;
$result = \App\Api\Operations\MoleculeWhere::execute($input, $limit, $offset)->data->molecule_struct;
$molecularGroups = \App\Api\Operations\MolecularGroup::execute()->data->molecular_group_view;
$paginator = new Paginator;
$paginator->setItemCount($count);
$paginator->setItemsPerPage($limit);
$paginator->setPage($page);
return $this->render('search/result.html.twig', [
'result' => $result,
'paginator' => $paginator,
'searchLabel' => 'Genotoxicity',
'searchPattern' => ucfirst(str_replace('-', ' ', $data['genotoxicity'])),
'searchValue' => $data['genotoxicity'],
'molecularGroups' => $molecularGroups,
'molecularGroup' => !empty($data['molecular_group']) ? $data['molecular_group'] : null
]);
}
}
return $this->redirectToRoute('app_toxicity_search');
}
/**
* @Route("/search/critical", name="app_search_critical")
*/
public function searchCritical(Request $request)
{
$limit = 30;
$page = $request->query->get('page', 1);
$offset = ($page-1) * $limit;
if($request->query->has('search'))
{
$data = $request->query->all('search');
if(!empty($data['critical']))
{
$boolExp = new \App\Api\Types\Boolean_comparison_exp;
$boolExp->_eq = true;
$mcbExp = new \App\Api\Types\molecule_criticity_bool_exp;
if($data['critical'] == "inrs")
{
$mcbExp->inrs = $boolExp;
}
else if($data['critical'] == "cpr")
{
$mcbExp->scss = $boolExp;
}
else if($data['critical'] == "unitis-allergen")
{
return $this->redirectToRoute('app_plant_unitis_allergen');
}
$input = new \App\Api\Types\molecule_struct_bool_exp();
$input->criticity = $mcbExp;
if(!empty($data['molecular_group']))
{
$expStr = new \App\Api\Types\String_comparison_exp;
$expStr->_eq = $data['molecular_group'];
$input->molgroup = $expStr;
}
$count = \App\Api\Operations\MoleculeWhereCount::execute($input)->data->molecule_struct_aggregate->aggregate->count;
$result = \App\Api\Operations\MoleculeWhere::execute($input, $limit, $offset)->data->molecule_struct;
$molecularGroups = \App\Api\Operations\MolecularGroup::execute()->data->molecular_group_view;
$paginator = new Paginator;
$paginator->setItemCount($count);
$paginator->setItemsPerPage($limit);
$paginator->setPage($page);
return $this->render('search/result.html.twig', [
'result' => $result,
'paginator' => $paginator,
'searchLabel' => 'Allergen',
'searchPattern' => ucfirst(str_replace('-', ' ', $data['critical'])),
'searchValue' => $data['critical'],
'molecularGroups' => $molecularGroups,
'molecularGroup' => !empty($data['molecular_group']) ? $data['molecular_group'] : null
]);
}
}
return $this->redirectToRoute('app_toxicity_search');
}
/**
* @Route("/search/plant/{slug}", name="app_search_plant")
*/
public function searchPlant(Request $request, string $slug)
{
$arr = [];
$plantParts = [];
$limit = 30;
$page = !empty($request->query->get('page')) ? $request->query->get('page') : 1;
$offset = ($page-1) * $limit;
$organs = [];
$molOrgans = [];
$plantPart = !empty($request->query->get('search')['plant_part']) ? $request->query->get('search')['plant_part'] : null;
$molecularGroup = !empty($request->query->get('search')['molecular_group']) ? $request->query->get('search')['molecular_group'] : null;
$organs = \App\Api\Operations\PlantPartByPlant::execute($slug)->data->molecule_plant_data;
$exp = new \App\Api\Types\String_comparison_exp;
$exp->_eq = $slug;
$input = new \App\Api\Types\molecule_plant_data_bool_exp();
$input->plant_name_slug = $exp;
if($plantPart)
{
$expStr = new \App\Api\Types\String_comparison_exp;
$expStr->_eq = $plantPart;
$input->plant_part = $expStr;
}
if($molecularGroup)
{
$inputMolecule = new \App\Api\Types\molecule_struct_bool_exp();
$expStr = new \App\Api\Types\String_comparison_exp;
$expStr->_eq = $molecularGroup;
$inputMolecule->molgroup = $expStr;
$input->molecule = $inputMolecule;
}
// $result = \App\Api\Operations\PlantMoleculeWhere::execute($input, $limit, $offset);
try {
$result = \App\Api\Operations\MoleculePlantDataWhere::execute($input, $limit, $offset);
}catch( \Exception $e)
{
die($e->getMessage());
}
foreach($result->data->molecule_plant_data as $item)
{
if($item->molecule)
{
$molecule = new \stdClass;
$molecule->plantpart = $item->plant_part;
// $molecule->bingo_inchikey = $item->molecule->bingo_inchikey;
// $molecule->inchi = $item->molecule->inchi;
// $molecule->inchikey = $item->molecule->inchikey;
// $molecule->molecularformula = $item->molecule->molecularformula;
// $molecule->molecularweight = $item->molecule->molecularweight;
$molecule->molgroup = $item->molecule->molgroup;
$molecule->molname = $item->molname;
$molecule->smiles = $item->molecule->smiles;
$molecule->uuid = $item->molecule->uuid;
if(!array_key_exists($item->molecule->bingo_inchikey,$molOrgans))
{
$molOrgans[$item->molecule->inchikey] = [];
}
$molOrgans[$item->molecule->inchikey][$item->plant_part] = $item->plant_part;
$arr[] = $molecule;
}
}
$count = $result->data->molecule_plant_data_aggregate->aggregate->count;
$molecularGroups = \App\Api\Operations\MolecularGroup::execute()->data->molecular_group_view;
$paginator = new Paginator;
$paginator->setItemCount($count);
$paginator->setItemsPerPage($limit);
$paginator->setPage($page);
return $this->render('search/result.html.twig', [
'result' => $arr,
'paginator' => $paginator,
'searchLabel' => 'Plant',
'searchPattern' => ucfirst(str_replace('_', ' ', $slug)),
'plantNameSlug' => $slug,
'show_plantpart' => true,
'plantPart' => $plantPart,
'organs' => $organs,
'molecularGroups' => $molecularGroups,
'molOrgans' => $molOrgans,
// 'molGroup' => $molGroup
]);
}
/**
* @Route("/search/eu-regulation", name="app_search_eu_regulation")
*/
public function searchByRegulation(Request $request)
{
$limit = 30;
$page = $request->query->get('page', 1);
$offset = ($page-1) * $limit;
if($request->query->has('search'))
{
$molecularGroups = \App\Api\Operations\MolecularGroup::execute()->data->molecular_group_view;
$data = $request->query->all('search');
if(!empty($data['eu_regulation']))
{
$exp = new \App\Api\Types\String_comparison_exp;
$shperaExp = new \App\Api\Types\sphera_result_bool_exp;
if($data['eu_regulation'] == 'found_in_clp_list')
{
$exp->_eq = 'Yes';
$shperaExp->found_in_clp_list = $exp;
}
elseif($data['eu_regulation'] == 'found_in_annex_list')
{
$exp->_neq = '';
$shperaExp->found_in_annex_list = $exp;
}
$input = new \App\Api\Types\molecule_struct_bool_exp();
$input->sphera_result = $shperaExp;
// dd($input);
if(!empty($data['molecular_group']))
{
$expStr = new \App\Api\Types\String_comparison_exp;
$expStr->_eq = $data['molecular_group'];
$input->molgroup = $expStr;
}
// dd($input);
$count = \App\Api\Operations\MoleculeWhereCount::execute($input)->data->molecule_struct_aggregate->aggregate->count;
$result = \App\Api\Operations\MoleculeWhere::execute($input, $limit, $offset)->data->molecule_struct;
$molecularGroups = \App\Api\Operations\MolecularGroup::execute()->data->molecular_group_view;
$paginator = new Paginator;
$paginator->setItemCount($count);
$paginator->setItemsPerPage($limit);
$paginator->setPage($page);
return $this->render('search/result.html.twig', [
'result' => $result,
'paginator' => $paginator,
'searchLabel' => 'Eu regulation',
'searchPattern' => ucfirst(str_replace('_', ' ', $data['eu_regulation'])),
'molecularGroups' => $molecularGroups,
'searchValue' => $data['eu_regulation'],
'molecularGroup' => !empty($data['molecular_group']) ? $data['molecular_group'] : null,
'molecularGroups' => $molecularGroups
]);
}
}
return $this->redirectToRoute('app_toxicity_search');
}
}