<commit_before>/*******************************************************************************
 * Copyright 2015 EMBL - European Bioinformatics Institute
 *
 * Licensed under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the
 * License.
 *******************************************************************************/
package uk.ac.ebi.phenotype.web.controller;

import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.mousephenotype.cda.enumerations.SexType;
import org.mousephenotype.cda.enumerations.ZygosityType;
import org.mousephenotype.cda.solr.service.AnatomyService;
import org.mousephenotype.cda.solr.service.ExpressionService;
import org.mousephenotype.cda.solr.service.GeneService;
import org.mousephenotype.cda.solr.service.ImageService;
import org.mousephenotype.cda.solr.service.dto.AnatomyDTO;
import org.mousephenotype.cda.solr.service.dto.GeneDTO;
import org.mousephenotype.cda.solr.service.dto.ImageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

@Controller
public class ImageComparatorController {

	@Autowired
	ImageService imageService;

	@Autowired
	ExpressionService expressionService;

	@Autowired
	GeneService geneService;
	
	@Autowired
	AnatomyService anatomyService;

	
	@RequestMapping("/imageComparator")
	public String imageCompBrowser( @RequestParam(value = "acc", required=false)  String acc,
				@RequestParam(value = "parameter_stable_id", required=false)  String parameterStableId,
				@RequestParam(value = "parameter_association_value", required=false)  String parameterAssociationValue,
				@RequestParam(value = "anatomy_id", required=false)  String anatomyId,
				@RequestParam(value = "anatomy_term", required=false)  String anatomyTerm,
				@RequestParam(value = "gender", required=false) String gender,
				@RequestParam(value = "zygosity", required=false) String zygosity,
				@RequestParam(value="mediaType", required=false) String mediaType,
				@RequestParam(value="colony_id", required=false) String colonyId,
				@RequestParam(value="mp_id", required=false) String mpId,
				Model model, HttpServletRequest request)
			throws SolrServerException, IOException {
		System.out.println("calling image imageComparator");
		
		// good example url with control and experimental images
		// http://localhost:8080/phenotype-archive/imagePicker/MGI:2669829/IMPC_EYE_050_001
		//changed to http://localhost:8080/phenotype-archive/imageComparator?acc=MGI:2669829&parameter_stable_id=IMPC_EYE_050_001
		//in anatomy pages we have links like this that need to be supported
		//http://localhost:8080/phenotype-archive/impcImages/images?q=*:*&defType=edismax&wt=json&fq=(anatomy_id:%22EMAPA:16105%22%20OR%20selected_top_level_anatomy_id:%22EMAPA:16105%22%20OR%20intermediate_anatomy_id:%22EMAPA:16105%22)%20%20AND%20gene_symbol:Ap4e1%20AND%20parameter_name:%22LacZ%20images%20wholemount%22%20AND%20parameter_association_value:%22ambiguous%22&title=gene%20Ap4e1%20with%20ambiguous%20in%20heart
		//http://localhost:8080/phenotype-archive/imageCompara?anatomy_id:%22EMAPA:16105%22&gene_symbol:Ap4e1&parameter_name:%22LacZ%20images%20wholemount%22&parameter_association_value:%22ambiguous%22
		//example link from gene page phenotype table that should work through this method with mpId and colonyId
		//http://localhost:8080/phenotype-archive/imageComparator?acc=MGI:1913955&mpId=MP:0000572&colony_id=RIKEN_EPD0296_2_A10
		
		if(mediaType!=null) System.out.println("mediaType= "+mediaType);
		// get experimental images
		// we will also want to call the getControls method and display side by
		// side
		if(StringUtils.isEmpty(anatomyId) && !StringUtils.isEmpty(anatomyTerm) && !anatomyTerm.contains("Unassigned")){
			try {
				AnatomyDTO anatomyDto = anatomyService.getTermByName(anatomyTerm);
				anatomyId=anatomyDto.getAnatomyId();
			} catch (Exception e) {
				//if anatomy id is not found it will be set to null and carry on.
				e.printStackTrace();
			}
		}
		List<ImageDTO> mutants=new ArrayList<>();
		QueryResponse responseExperimental = imageService
				.getImagesForGeneByParameter(acc, parameterStableId,"experimental", Integer.MAX_VALUE, 
						null, null, null, anatomyId, parameterAssociationValue, mpId, colonyId);
		ImageDTO imgDoc =null;
		if (responseExperimental != null && responseExperimental.getResults().size()>0) {
			//mutants=responseExperimental.getResults();
			mutants=responseExperimental.getBeans(ImageDTO.class);
			
			imgDoc = mutants.get(0);
		}
		
		int numberOfControlsPerSex = 100;
		// int daysEitherSide = 30;// get a month either side
		SexType sexType=null;
		if(gender!=null && !gender.equals("all")){
			sexType = getSexTypesForFilter(gender);
		}
		
		
		//this filters controls by the sex and things like procedure and phenotyping center - based on first image - this may not be a good idea - there maybe multiple phenotyping centers for a procedure which woudln't show???
		List<ImageDTO> controls=null;
		if(imgDoc!=null){
		controls = filterControlsBySexAndOthers(imgDoc, numberOfControlsPerSex, sexType, parameterStableId, anatomyId);
		}
		List<ImageDTO> filteredMutants = filterMutantsBySex(mutants, imgDoc, sexType);
		
		List<ZygosityType> zygosityTypes=null;
		if(zygosity!=null && !zygosity.equals("all")){
			zygosityTypes=getZygosityTypesForFilter(zygosity);
			//only filter mutants by zygosity as all controls are homs.
			filteredMutants=filterImagesByZygosity(filteredMutants, zygosityTypes);
		}
		
		boolean federated=this.isFederated(filteredMutants);
		

		
		

		this.addGeneToPage(acc, model);
		model.addAttribute("mediaType", mediaType);
		model.addAttribute("sexTypes",SexType.values());
		model.addAttribute("zygTypes",ZygosityType.values());<commit_msg>reduced the number of controls in comparator<commit_after>/*******************************************************************************
 * Copyright 2015 EMBL - European Bioinformatics Institute
 *
 * Licensed under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the
 * License.
 *******************************************************************************/
package uk.ac.ebi.phenotype.web.controller;

import org.apache.commons.lang3.StringUtils;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.mousephenotype.cda.enumerations.SexType;
import org.mousephenotype.cda.enumerations.ZygosityType;
import org.mousephenotype.cda.solr.service.AnatomyService;
import org.mousephenotype.cda.solr.service.ExpressionService;
import org.mousephenotype.cda.solr.service.GeneService;
import org.mousephenotype.cda.solr.service.ImageService;
import org.mousephenotype.cda.solr.service.dto.AnatomyDTO;
import org.mousephenotype.cda.solr.service.dto.GeneDTO;
import org.mousephenotype.cda.solr.service.dto.ImageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

@Controller
public class ImageComparatorController {

	@Autowired
	ImageService imageService;

	@Autowired
	ExpressionService expressionService;

	@Autowired
	GeneService geneService;
	
	@Autowired
	AnatomyService anatomyService;

	
	@RequestMapping("/imageComparator")
	public String imageCompBrowser( @RequestParam(value = "acc", required=false)  String acc,
				@RequestParam(value = "parameter_stable_id", required=false)  String parameterStableId,
				@RequestParam(value = "parameter_association_value", required=false)  String parameterAssociationValue,
				@RequestParam(value = "anatomy_id", required=false)  String anatomyId,
				@RequestParam(value = "anatomy_term", required=false)  String anatomyTerm,
				@RequestParam(value = "gender", required=false) String gender,
				@RequestParam(value = "zygosity", required=false) String zygosity,
				@RequestParam(value="mediaType", required=false) String mediaType,
				@RequestParam(value="colony_id", required=false) String colonyId,
				@RequestParam(value="mp_id", required=false) String mpId,
				Model model, HttpServletRequest request)
			throws SolrServerException, IOException {
		System.out.println("calling image imageComparator");
		
		// good example url with control and experimental images
		// http://localhost:8080/phenotype-archive/imagePicker/MGI:2669829/IMPC_EYE_050_001
		//changed to http://localhost:8080/phenotype-archive/imageComparator?acc=MGI:2669829&parameter_stable_id=IMPC_EYE_050_001
		//in anatomy pages we have links like this that need to be supported
		//http://localhost:8080/phenotype-archive/impcImages/images?q=*:*&defType=edismax&wt=json&fq=(anatomy_id:%22EMAPA:16105%22%20OR%20selected_top_level_anatomy_id:%22EMAPA:16105%22%20OR%20intermediate_anatomy_id:%22EMAPA:16105%22)%20%20AND%20gene_symbol:Ap4e1%20AND%20parameter_name:%22LacZ%20images%20wholemount%22%20AND%20parameter_association_value:%22ambiguous%22&title=gene%20Ap4e1%20with%20ambiguous%20in%20heart
		//http://localhost:8080/phenotype-archive/imageCompara?anatomy_id:%22EMAPA:16105%22&gene_symbol:Ap4e1&parameter_name:%22LacZ%20images%20wholemount%22&parameter_association_value:%22ambiguous%22
		//example link from gene page phenotype table that should work through this method with mpId and colonyId
		//http://localhost:8080/phenotype-archive/imageComparator?acc=MGI:1913955&mpId=MP:0000572&colony_id=RIKEN_EPD0296_2_A10
		
		if(mediaType!=null) System.out.println("mediaType= "+mediaType);
		// get experimental images
		// we will also want to call the getControls method and display side by
		// side
		if(StringUtils.isEmpty(anatomyId) && !StringUtils.isEmpty(anatomyTerm) && !anatomyTerm.contains("Unassigned")){
			try {
				AnatomyDTO anatomyDto = anatomyService.getTermByName(anatomyTerm);
				anatomyId=anatomyDto.getAnatomyId();
			} catch (Exception e) {
				//if anatomy id is not found it will be set to null and carry on.
				e.printStackTrace();
			}
		}
		List<ImageDTO> mutants=new ArrayList<>();
		QueryResponse responseExperimental = imageService
				.getImagesForGeneByParameter(acc, parameterStableId,"experimental", Integer.MAX_VALUE, 
						null, null, null, anatomyId, parameterAssociationValue, mpId, colonyId);
		ImageDTO imgDoc =null;
		if (responseExperimental != null && responseExperimental.getResults().size()>0) {
			//mutants=responseExperimental.getResults();
			mutants=responseExperimental.getBeans(ImageDTO.class);
			
			imgDoc = mutants.get(0);
		}
		
		int numberOfControlsPerSex = 25;
		// int daysEitherSide = 30;// get a month either side
		SexType sexType=null;
		if(gender!=null && !gender.equals("all")){
			sexType = getSexTypesForFilter(gender);
		}
		
		
		//this filters controls by the sex and things like procedure and phenotyping center - based on first image - this may not be a good idea - there maybe multiple phenotyping centers for a procedure which woudln't show???
		List<ImageDTO> controls=null;
		if(imgDoc!=null){
		controls = filterControlsBySexAndOthers(imgDoc, numberOfControlsPerSex, sexType, parameterStableId, anatomyId);
		}
		List<ImageDTO> filteredMutants = filterMutantsBySex(mutants, imgDoc, sexType);
		
		List<ZygosityType> zygosityTypes=null;
		if(zygosity!=null && !zygosity.equals("all")){
			zygosityTypes=getZygosityTypesForFilter(zygosity);
			//only filter mutants by zygosity as all controls are homs.
			filteredMutants=filterImagesByZygosity(filteredMutants, zygosityTypes);
		}
		
		boolean federated=this.isFederated(filteredMutants);
		

		
		

		this.addGeneToPage(acc, model);
		model.addAttribute("mediaType", mediaType);
		model.addAttribute("sexTypes",SexType.values());
		model.addAttribute("zygTypes",ZygosityType.values());
{'fraction_non_alphanumeric': 0.12488835963084251, 'fraction_numerical': 0.02686811551056862, 'mean_word_length': 6.090765171503958, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7253636', 'n_tokens_mistral': 4656, 'n_tokens_neox': 4000, 'n_words': 845}