<commit_before>package org.intermine.bio.web.struts;

/*
 * Copyright (C) 2002-2011 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.tiles.ComponentContext;
import org.apache.struts.tiles.actions.TilesAction;
import org.intermine.api.InterMineAPI;
import org.intermine.api.profile.Profile;
import org.intermine.api.query.PathQueryExecutor;
import org.intermine.api.results.ExportResultsIterator;
import org.intermine.api.results.ResultElement;
import org.intermine.bio.web.logic.CytoscapeInteractionDBQuerier;
import org.intermine.bio.web.logic.CytoscapeInteractionDataGenerator;
import org.intermine.bio.web.logic.CytoscapeInteractionUtil;
import org.intermine.bio.web.model.CytoscapeNetworkEdgeData;
import org.intermine.bio.web.model.CytoscapeNetworkNodeData;
import org.intermine.metadata.Model;
import org.intermine.model.InterMineObject;
import org.intermine.model.bio.Gene;
import org.intermine.model.bio.Protein;
import org.intermine.util.StringUtil;
import org.intermine.web.logic.session.SessionMethods;

/**
 * Set up interaction network data for the cytoscapeInteractionsDisplayer.jsp
 *
 * @author <NAME>
 * @author <NAME>
 *
 */
public class CytoscapeInteractionsController extends TilesAction
{
    @SuppressWarnings("unused")
    private static final Logger LOG = Logger.getLogger(CytoscapeInteractionsController.class);

    private Set<CytoscapeNetworkNodeData> interactionNodeSet =
        new LinkedHashSet<CytoscapeNetworkNodeData>();
    private Set<CytoscapeNetworkEdgeData> interactionEdgeSet =
        new HashSet<CytoscapeNetworkEdgeData>();

    private Set<Integer> geneIdSet = new HashSet<Integer>(); // asset contains gene object store ids

    /**
     * {@inheritDoc}
     */
    @Override
    public ActionForward execute(ComponentContext context,
            ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        HttpSession session = request.getSession(); // Get HttpSession
        final InterMineAPI im = SessionMethods.getInterMineAPI(session); // Get InterMineAPI
        Model model = im.getModel(); // Get Model
        Profile profile = SessionMethods.getProfile(session); // Get Profile
        PathQueryExecutor executor = im.getPathQueryExecutor(profile); // Get PathQueryExecutor

        Map<String, Set<String>> interactionInfoMap = CytoscapeInteractionUtil
                .getInteractionInfo(model, executor);

        if (interactionInfoMap == null) {
            String dataNotIncludedMessage = "Interaction data is not included.";
            request.setAttribute("dataNotIncludedMessage", dataNotIncludedMessage);
            return null;
        }

        InterMineObject object = (InterMineObject) request
                .getAttribute("object"); // Get object from request

        String theNetwork = new String(); // Network data as a string in different formats

        CytoscapeInteractionDataGenerator dataGen = new CytoscapeInteractionDataGenerator();

        // Whether the object is a Gene or Protein
        if (object instanceof Protein) {
            Protein protein = (Protein) object;
            // TODO In most cases, there is only 1 gene in the collect, but rare cases with more
            // than 1, any examples?
            Set<Gene> genes = protein.getGenes();

            // TODO How to handle a protein with two or more genes - generate several networks?
            // for (Gene gene : genes) {
            Gene gene = genes.iterator().next();
//            Set<CytoscapeNetworkEdgeData> interactionEdgeSubSet =
//                new HashSet<CytoscapeNetworkEdgeData>();
//            Set<CytoscapeNetworkNodeData> interactionNodeSubSet =
//                new HashSet<CytoscapeNetworkNodeData>();

            // Check if interaction data available for the organism
            String orgName = gene.getOrganism().getName();
            if (!interactionInfoMap.containsKey(orgName)) {
                String orgWithNoDataMessage = "Interaction data is not availiable for "
                        + orgName + " genes";
                request.setAttribute("orgWithNoDataMessage", orgWithNoDataMessage);
                return null;
            }

            ExportResultsIterator rawIntData = queryInteractionRawDataFromDB(gene, model, executor);

            if (rawIntData == null) {
                String dataSourceStr = StringUtil.join(interactionInfoMap.get(orgName), ",");

                String geneWithNoDatasourceMessage = "Interaction data is not availiable for "
                        + gene.getSymbol() + " (" + gene.getPrimaryIdentifier()
                        + ") from data sources: " + dataSourceStr;
                request.setAttribute("geneWithNoDatasourceMessage", geneWithNoDatasourceMessage);
                return null;
            }

            // Add the interaction network for this gene to the whole network
            prepareNetworkData(rawIntData);

//            interactionEdgeSet.addAll(interactionEdgeSubSet);
//            interactionNodeSet.addAll(interactionNodeSubSet);

            //}
            // TODO should be a set of hub-genes
            request.setAttribute("hubGene", gene.getPrimaryIdentifier());
        } else if (object instanceof Gene) {
            Gene gene = (Gene) object;

            // Check if interaction data available for the organism
            String orgName = gene.getOrganism().getName();
            if (!interactionInfoMap.containsKey(orgName)) {
                String orgWithNoDataMessage = "Interaction data is not availiable for "
                        + orgName + " genes";
                request.setAttribute("orgWithNoDataMessage", orgWithNoDataMessage);
                return null;
            }

            ExportResultsIterator rawIntData = queryInteractionRawDataFromDB(gene, model, executor);

            if (rawIntData == null) {
                String dataSourceStr = StringUtil.join(interactionInfoMap.get(orgName), ",");

                String geneWithNoDatasourceMessage = "Interaction data is not availiable for "<commit_msg>Update interactions message.<commit_after>package org.intermine.bio.web.struts;

/*
 * Copyright (C) 2002-2011 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.tiles.ComponentContext;
import org.apache.struts.tiles.actions.TilesAction;
import org.intermine.api.InterMineAPI;
import org.intermine.api.profile.Profile;
import org.intermine.api.query.PathQueryExecutor;
import org.intermine.api.results.ExportResultsIterator;
import org.intermine.api.results.ResultElement;
import org.intermine.bio.web.logic.CytoscapeInteractionDBQuerier;
import org.intermine.bio.web.logic.CytoscapeInteractionDataGenerator;
import org.intermine.bio.web.logic.CytoscapeInteractionUtil;
import org.intermine.bio.web.model.CytoscapeNetworkEdgeData;
import org.intermine.bio.web.model.CytoscapeNetworkNodeData;
import org.intermine.metadata.Model;
import org.intermine.model.InterMineObject;
import org.intermine.model.bio.Gene;
import org.intermine.model.bio.Protein;
import org.intermine.util.StringUtil;
import org.intermine.web.logic.session.SessionMethods;

/**
 * Set up interaction network data for the cytoscapeInteractionsDisplayer.jsp
 *
 * @author <NAME>
 * @author <NAME>
 *
 */
public class CytoscapeInteractionsController extends TilesAction
{
    @SuppressWarnings("unused")
    private static final Logger LOG = Logger.getLogger(CytoscapeInteractionsController.class);

    private Set<CytoscapeNetworkNodeData> interactionNodeSet =
        new LinkedHashSet<CytoscapeNetworkNodeData>();
    private Set<CytoscapeNetworkEdgeData> interactionEdgeSet =
        new HashSet<CytoscapeNetworkEdgeData>();

    private Set<Integer> geneIdSet = new HashSet<Integer>(); // asset contains gene object store ids

    /**
     * {@inheritDoc}
     */
    @Override
    public ActionForward execute(ComponentContext context,
            ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        HttpSession session = request.getSession(); // Get HttpSession
        final InterMineAPI im = SessionMethods.getInterMineAPI(session); // Get InterMineAPI
        Model model = im.getModel(); // Get Model
        Profile profile = SessionMethods.getProfile(session); // Get Profile
        PathQueryExecutor executor = im.getPathQueryExecutor(profile); // Get PathQueryExecutor

        Map<String, Set<String>> interactionInfoMap = CytoscapeInteractionUtil
                .getInteractionInfo(model, executor);

        if (interactionInfoMap == null) {
            String dataNotIncludedMessage = "Interaction data is not included.";
            request.setAttribute("dataNotIncludedMessage", dataNotIncludedMessage);
            return null;
        }

        InterMineObject object = (InterMineObject) request
                .getAttribute("object"); // Get object from request

        String theNetwork = new String(); // Network data as a string in different formats

        CytoscapeInteractionDataGenerator dataGen = new CytoscapeInteractionDataGenerator();

        // Whether the object is a Gene or Protein
        if (object instanceof Protein) {
            Protein protein = (Protein) object;
            // TODO In most cases, there is only 1 gene in the collect, but rare cases with more
            // than 1, any examples?
            Set<Gene> genes = protein.getGenes();

            // TODO How to handle a protein with two or more genes - generate several networks?
            // for (Gene gene : genes) {
            Gene gene = genes.iterator().next();
//            Set<CytoscapeNetworkEdgeData> interactionEdgeSubSet =
//                new HashSet<CytoscapeNetworkEdgeData>();
//            Set<CytoscapeNetworkNodeData> interactionNodeSubSet =
//                new HashSet<CytoscapeNetworkNodeData>();

            // Check if interaction data available for the organism
            String orgName = gene.getOrganism().getName();
            if (!interactionInfoMap.containsKey(orgName)) {
                String orgWithNoDataMessage = "No interaction data found for "
                        + orgName + " genes";
                request.setAttribute("orgWithNoDataMessage", orgWithNoDataMessage);
                return null;
            }

            ExportResultsIterator rawIntData = queryInteractionRawDataFromDB(gene, model, executor);

            if (rawIntData == null) {
                String dataSourceStr = StringUtil.join(interactionInfoMap.get(orgName), ",");

                String geneWithNoDatasourceMessage = "No interaction data found for "
                        + gene.getSymbol() + " (" + gene.getPrimaryIdentifier()
                        + ") from data sources: " + dataSourceStr;
                request.setAttribute("geneWithNoDatasourceMessage", geneWithNoDatasourceMessage);
                return null;
            }

            // Add the interaction network for this gene to the whole network
            prepareNetworkData(rawIntData);

//            interactionEdgeSet.addAll(interactionEdgeSubSet);
//            interactionNodeSet.addAll(interactionNodeSubSet);

            //}
            // TODO should be a set of hub-genes
            request.setAttribute("hubGene", gene.getPrimaryIdentifier());
        } else if (object instanceof Gene) {
            Gene gene = (Gene) object;

            // Check if interaction data available for the organism
            String orgName = gene.getOrganism().getName();
            if (!interactionInfoMap.containsKey(orgName)) {
                String orgWithNoDataMessage = "No interaction data found for "
                        + orgName + " genes";
                request.setAttribute("orgWithNoDataMessage", orgWithNoDataMessage);
                return null;
            }

            ExportResultsIterator rawIntData = queryInteractionRawDataFromDB(gene, model, executor);

            if (rawIntData == null) {
                String dataSourceStr = StringUtil.join(interactionInfoMap.get(orgName), ",");

                String geneWithNoDatasourceMessage = "No interaction data found for "
{'fraction_non_alphanumeric': 0.08658868535275509, 'fraction_numerical': 0.001618480100051497, 'mean_word_length': 2.8951289398280804, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '20344', 'n_tokens_mistral': 3578, 'n_tokens_neox': 3447, 'n_words': 872}