<commit_before>/*
 * Copyright 2008-2013 Microarray Informatics Team, 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.
 *
 *
 * For further details of the Gene Expression Atlas project, including source code,
 * downloads and documentation, please see:
 *
 * http://gxa.github.com/gxa
 */

package uk.ac.ebi.atlas.search;

import com.google.common.base.Optional;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.solr.common.SolrException;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import uk.ac.ebi.atlas.bioentity.GeneSetUtil;
import uk.ac.ebi.atlas.search.EFO.ConditionSearchEFOExpander;
import uk.ac.ebi.atlas.search.baseline.BaselineExperimentAssayGroup;
import uk.ac.ebi.atlas.search.baseline.BaselineExperimentAssayGroupSearchService;
import uk.ac.ebi.atlas.search.diffanalytics.DiffAnalyticsList;
import uk.ac.ebi.atlas.search.diffanalytics.DiffAnalyticsSearchService;
import uk.ac.ebi.atlas.solr.BioentityProperty;
import uk.ac.ebi.atlas.solr.BioentityType;
import uk.ac.ebi.atlas.solr.query.SolrQueryService;
import uk.ac.ebi.atlas.thirdpartyintegration.EBIGlobalSearchQueryBuilder;
import uk.ac.ebi.atlas.web.DifferentialRequestPreferences;
import uk.ac.ebi.atlas.web.GeneQuerySearchRequestParameters;

import javax.inject.Inject;
import javax.validation.Valid;
import java.util.Set;
import java.util.SortedSet;

import static com.google.common.base.Preconditions.checkArgument;
import static uk.ac.ebi.atlas.search.baseline.BaselineExperimentAssayGroups.*;

@Controller
@Scope("prototype")
public class BioentitiesSearchController {

    private static final Logger LOGGER = Logger.getLogger(BioentitiesSearchController.class);

    private DiffAnalyticsSearchService diffAnalyticsSearchService;
    private BaselineExperimentAssayGroupSearchService baselineExperimentAssayGroupSearchService;

    private EBIGlobalSearchQueryBuilder ebiGlobalSearchQueryBuilder;

    private SolrQueryService solrQueryService;
    private ConditionSearchEFOExpander efoExpander;

    @Inject
    public BioentitiesSearchController(DiffAnalyticsSearchService diffAnalyticsSearchService, BaselineExperimentAssayGroupSearchService baselineExperimentAssayGroupSearchService, EBIGlobalSearchQueryBuilder ebiGlobalSearchQueryBuilder, SolrQueryService solrQueryService, ConditionSearchEFOExpander efoExpander) {
        this.diffAnalyticsSearchService = diffAnalyticsSearchService;
        this.baselineExperimentAssayGroupSearchService = baselineExperimentAssayGroupSearchService;
        this.ebiGlobalSearchQueryBuilder = ebiGlobalSearchQueryBuilder;
        this.solrQueryService = solrQueryService;
        this.efoExpander = efoExpander;
    }

    @RequestMapping(value = "/query")
    public String showGeneQueryResultPage(@Valid GeneQuerySearchRequestParameters requestParameters, Model model, BindingResult result) {

        checkArgument(requestParameters.hasGeneQuery() || requestParameters.hasCondition(), "Please specify a gene query or condition!");

        String geneQuery = requestParameters.getGeneQuery().asString().trim();

        String selectedSpecies = "";
        if(requestParameters.hasOrganism()) {
            selectedSpecies = requestParameters.getOrganism().trim();
        }

        if (requestParameters.hasGeneQuery() && !requestParameters.hasCondition()) {
            //If Query just for a single bioentityID
            Optional<String> geneIdRedirectString = getGeneIdRedirectString(geneQuery, selectedSpecies, requestParameters.isExactMatch());
            if (geneIdRedirectString.isPresent()) {
                return geneIdRedirectString.get();
            }
        }

        model.addAttribute("geneQuery", requestParameters.getGeneQuery());

        model.addAttribute("searchDescription", requestParameters.getDescription());

        String condition = efoExpander.addEfoAccessions(requestParameters.getConditionQuery()).asString();

        SortedSet<BaselineExperimentAssayGroup> baselineExperimentAssayGroups = baselineExperimentAssayGroupSearchService.query(geneQuery, condition, selectedSpecies.toLowerCase(), requestParameters.isExactMatch());

        boolean showWidget = hasAllSameSpecies(baselineExperimentAssayGroups) && hasAnyTissueExperiment(baselineExperimentAssayGroups) & !requestParameters.hasCondition();

        if (showWidget) {
            model.addAttribute("widgetHasBaselineProfiles", true);
            model.addAttribute("species", baselineExperimentAssayGroups.iterator().next().getSpecies());

            model.addAttribute("baselineCounts", selectNonTissueExperiments(baselineExperimentAssayGroups));
        } else {
            model.addAttribute("baselineCounts", baselineExperimentAssayGroups);
        }

        // used to populate diff-heatmap-table
        DiffAnalyticsList bioentityExpressions = diffAnalyticsSearchService.fetchTop(geneQuery, condition, selectedSpecies, requestParameters.isExactMatch());

        model.addAttribute("bioentities", bioentityExpressions);

        model.addAttribute("preferences", new DifferentialRequestPreferences());

        model.addAttribute("requestParameters", requestParameters);

        model.addAttribute("exactMatch", requestParameters.isExactMatch());

        String globalSearchTerm = ebiGlobalSearchQueryBuilder.buildGlobalSearchTerm(geneQuery, requestParameters.getConditionQuery());<commit_msg>Model attribute baselineCounts changed to firstBaselineCounts<commit_after>/*
 * Copyright 2008-2013 Microarray Informatics Team, 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.
 *
 *
 * For further details of the Gene Expression Atlas project, including source code,
 * downloads and documentation, please see:
 *
 * http://gxa.github.com/gxa
 */

package uk.ac.ebi.atlas.search;

import com.google.common.base.Optional;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.solr.common.SolrException;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import uk.ac.ebi.atlas.bioentity.GeneSetUtil;
import uk.ac.ebi.atlas.search.EFO.ConditionSearchEFOExpander;
import uk.ac.ebi.atlas.search.baseline.BaselineExperimentAssayGroup;
import uk.ac.ebi.atlas.search.baseline.BaselineExperimentAssayGroupSearchService;
import uk.ac.ebi.atlas.search.diffanalytics.DiffAnalyticsList;
import uk.ac.ebi.atlas.search.diffanalytics.DiffAnalyticsSearchService;
import uk.ac.ebi.atlas.solr.BioentityProperty;
import uk.ac.ebi.atlas.solr.BioentityType;
import uk.ac.ebi.atlas.solr.query.SolrQueryService;
import uk.ac.ebi.atlas.thirdpartyintegration.EBIGlobalSearchQueryBuilder;
import uk.ac.ebi.atlas.web.DifferentialRequestPreferences;
import uk.ac.ebi.atlas.web.GeneQuerySearchRequestParameters;

import javax.inject.Inject;
import javax.validation.Valid;
import java.util.Set;
import java.util.SortedSet;

import static com.google.common.base.Preconditions.checkArgument;
import static uk.ac.ebi.atlas.search.baseline.BaselineExperimentAssayGroups.*;

@Controller
@Scope("prototype")
public class BioentitiesSearchController {

    private static final Logger LOGGER = Logger.getLogger(BioentitiesSearchController.class);

    private DiffAnalyticsSearchService diffAnalyticsSearchService;
    private BaselineExperimentAssayGroupSearchService baselineExperimentAssayGroupSearchService;

    private EBIGlobalSearchQueryBuilder ebiGlobalSearchQueryBuilder;

    private SolrQueryService solrQueryService;
    private ConditionSearchEFOExpander efoExpander;

    @Inject
    public BioentitiesSearchController(DiffAnalyticsSearchService diffAnalyticsSearchService, BaselineExperimentAssayGroupSearchService baselineExperimentAssayGroupSearchService, EBIGlobalSearchQueryBuilder ebiGlobalSearchQueryBuilder, SolrQueryService solrQueryService, ConditionSearchEFOExpander efoExpander) {
        this.diffAnalyticsSearchService = diffAnalyticsSearchService;
        this.baselineExperimentAssayGroupSearchService = baselineExperimentAssayGroupSearchService;
        this.ebiGlobalSearchQueryBuilder = ebiGlobalSearchQueryBuilder;
        this.solrQueryService = solrQueryService;
        this.efoExpander = efoExpander;
    }

    @RequestMapping(value = "/query")
    public String showGeneQueryResultPage(@Valid GeneQuerySearchRequestParameters requestParameters, Model model, BindingResult result) {

        checkArgument(requestParameters.hasGeneQuery() || requestParameters.hasCondition(), "Please specify a gene query or condition!");

        String geneQuery = requestParameters.getGeneQuery().asString().trim();

        String selectedSpecies = "";
        if(requestParameters.hasOrganism()) {
            selectedSpecies = requestParameters.getOrganism().trim();
        }

        if (requestParameters.hasGeneQuery() && !requestParameters.hasCondition()) {
            //If Query just for a single bioentityID
            Optional<String> geneIdRedirectString = getGeneIdRedirectString(geneQuery, selectedSpecies, requestParameters.isExactMatch());
            if (geneIdRedirectString.isPresent()) {
                return geneIdRedirectString.get();
            }
        }

        model.addAttribute("geneQuery", requestParameters.getGeneQuery());

        model.addAttribute("searchDescription", requestParameters.getDescription());

        String condition = efoExpander.addEfoAccessions(requestParameters.getConditionQuery()).asString();

        SortedSet<BaselineExperimentAssayGroup> baselineExperimentAssayGroups = baselineExperimentAssayGroupSearchService.query(geneQuery, condition, selectedSpecies.toLowerCase(), requestParameters.isExactMatch());

        boolean showWidget = hasAllSameSpecies(baselineExperimentAssayGroups) && hasAnyTissueExperiment(baselineExperimentAssayGroups) & !requestParameters.hasCondition();

        if (showWidget) {
            model.addAttribute("widgetHasBaselineProfiles", true);
            model.addAttribute("species", baselineExperimentAssayGroups.iterator().next().getSpecies());

            model.addAttribute("firstBaselineCounts", selectNonTissueExperiments(baselineExperimentAssayGroups));
        } else {
            model.addAttribute("firstBaselineCounts", baselineExperimentAssayGroups);
        }

        // used to populate diff-heatmap-table
        DiffAnalyticsList bioentityExpressions = diffAnalyticsSearchService.fetchTop(geneQuery, condition, selectedSpecies, requestParameters.isExactMatch());

        model.addAttribute("bioentities", bioentityExpressions);

        model.addAttribute("preferences", new DifferentialRequestPreferences());

        model.addAttribute("requestParameters", requestParameters);

        model.addAttribute("exactMatch", requestParameters.isExactMatch());

        String globalSearchTerm = ebiGlobalSearchQueryBuilder.buildGlobalSearchTerm(geneQuery, requestParameters.getConditionQuery());
{'fraction_non_alphanumeric': 0.08356545961002786, 'fraction_numerical': 0.002166511915815537, 'mean_word_length': 5.280369290573372, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5219406', 'n_tokens_mistral': 3430, 'n_tokens_neox': 3276, 'n_words': 644}