<commit_before>package org.mskcc.portal.servlet;

import java.io.IOException;

import java.io.PrintWriter;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Arrays;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mskcc.cgds.model.ClinicalData;
import org.mskcc.portal.model.*;
import org.mskcc.portal.oncoPrintSpecLanguage.OncoPrintLangException;
import org.mskcc.portal.oncoPrintSpecLanguage.ParserOutput;
import org.mskcc.portal.remote.*;
import org.mskcc.portal.util.*;
import org.mskcc.portal.r_bridge.SurvivalPlot;
import org.mskcc.cgds.model.CancerStudy;
import org.mskcc.cgds.model.CaseList;
import org.mskcc.cgds.model.GeneticProfile;
import org.mskcc.cgds.model.GeneticAlterationType;
import org.mskcc.cgds.model.ExtendedMutation;
import org.mskcc.cgds.dao.DaoException;
import org.mskcc.cgds.web_api.GetProfileData;
import org.owasp.validator.html.PolicyException;

/**
 * Central Servlet for building queries.
 */
public class QueryBuilder extends HttpServlet {
    public static final boolean INCLUDE_NETWORKS = true;
    public static final String CGDS_URL_PARAM = "cgds_url";
    public static final String PATHWAY_COMMONS_URL_PARAM = "pathway_commons_url";
    public static final String CANCER_TYPES_INTERNAL = "cancer_types";
    public static final String PROFILE_LIST_INTERNAL = "profile_list";
    public static final String CASE_SETS_INTERNAL = "case_sets";
    public static final String CANCER_STUDY_ID = "cancer_study_id";
    public static final String CLINICAL_DATA_LIST = "clinical_data_list";
    public static final String GENETIC_PROFILE_IDS = "genetic_profile_ids";
    public static final String GENE_SET_CHOICE = "gene_set_choice";
    public static final String CASE_SET_ID = "case_set_id";
    public static final String CASE_IDS = "case_ids";
    public static final String GENE_LIST = "gene_list";
    public static final String ACTION = "action";
    public static final String OUTPUT = "output";
    public static final String FORMAT = "format";
    public static final String PLOT_TYPE = "plot_type";
    public static final String OS_SURVIVAL_PLOT = "os_survival_plot";
    public static final String DFS_SURVIVAL_PLOT = "dfs_survival_plot";
    public static final String XDEBUG = "xdebug";
    public static final String ACTION_SUBMIT = "Submit";
    public static final String STEP2_ERROR_MSG = "step2_error_msg";
    public static final String STEP3_ERROR_MSG = "step3_error_msg";
    public static final String STEP4_ERROR_MSG = "step4_error_msg";
    public static final String MERGED_PROFILE_DATA_INTERNAL = "merged_profile_data";
    public static final String PROFILE_DATA_SUMMARY = "profile_data_summary";
    public static final String WARNING_UNION = "warning_union";
    public static final String DOWNLOAD_LINKS = "download_links";
    public static final String NETWORK = "network";
    public static final String HTML_TITLE = "html_title";
    public static final String TAB_INDEX = "tab_index";
    public static final String TAB_DOWNLOAD = "tab_download";
    public static final String TAB_VISUALIZE = "tab_visualize";
    public static final String USER_ERROR_MESSAGE = "user_error_message";
    public static final String ATTRIBUTE_URL_BEFORE_FORWARDING = "ATTRIBUTE_URL_BEFORE_FORWARDING";
    public static final String Z_SCORE_THRESHOLD = "Z_SCORE_THRESHOLD";
    public static final String MRNA_PROFILES_SELECTED = "MRNA_PROFILES_SELECTED";
    public static final String COMPUTE_LOG_ODDS_RATIO = "COMPUTE_LOG_ODDS_RATIO";
    public static final String MUTATION_MAP = "MUTATION_MAP";
    public static final int MUTATION_DETAIL_LIMIT = 10;
    public static final String MUTATION_DETAIL_LIMIT_REACHED = "MUTATION_DETAIL_LIMIT_REACHED";
    public static final int MAX_NUM_GENES = 100;
    public static final String XDEBUG_OBJECT = "xdebug_object";
    public static final String ONCO_PRINT_HTML = "oncoprint_html";
    public static final String INDEX_PAGE = "index.do";

    private ServletXssUtil servletXssUtil;

    /**
     * Initializes the servlet.
     *
     * @throws ServletException Serlvet Init Error.
     */
    public void init() throws ServletException {
        super.init();
        String cgdsUrl = getInitParameter(CGDS_URL_PARAM);
        System.out.println ("Init Query Builder with CGDS URL:  " + cgdsUrl);
        GlobalProperties.setCgdsUrl(cgdsUrl);
		String pathwayCommonsUrl = getInitParameter(PATHWAY_COMMONS_URL_PARAM);
        System.out.println ("Init Query Builder with PathwayCommons URL:  " + pathwayCommonsUrl);
        GlobalProperties.setPathwayCommonsUrl(pathwayCommonsUrl);
        try {
            servletXssUtil = ServletXssUtil.getInstance();
        } catch (PolicyException e) {
            throw new ServletException (e);
        }
    }

    /**
     * Handles HTTP GET Request.
     *
     * @param httpServletRequest  Http Servlet Request Object.
     * @param httpServletResponse Http Servelt Response Object.
     * @throws ServletException Servlet Error.
     * @throws IOException      IO Error.
     */
    protected void doGet(HttpServletRequest httpServletRequest,
                         HttpServletResponse httpServletResponse) throws ServletException,
            IOException {
        doPost(httpServletRequest, httpServletResponse);
    }

    /**
     * Handles HTTP POST Request.
     *
     * @param httpServletRequest  Http Servlet Request Object.
     * @param httpServletResponse Http Servelt Response Object.
     * @throws ServletException Servlet Error.
     * @throws IOException      IO Error.
     */
    protected void doPost(HttpServletRequest httpServletRequest,
                          HttpServletResponse httpServletResponse) throws ServletException,
            IOException {
        XDebug xdebug = new XDebug( httpServletRequest );
        xdebug.startTimer();

        xdebug.logMsg(this, "Attempting to initiate new user query.");
        
        if (httpServletRequest.getRequestURL() != null) {
            httpServletRequest.setAttribute(ATTRIBUTE_URL_BEFORE_FORWARDING,
                                            httpServletRequest.getRequestURL().toString());
        }

        //  Get User Selected Action
        String action = servletXssUtil.getCleanInput (httpServletRequest, ACTION);

        //  Get User Selected Cancer Type
        String cancerTypeId = servletXssUtil.getCleanInput(httpServletRequest, CANCER_STUDY_ID);

        //  Get User Selected Genetic Profiles
        String geneticProfileIds[] = httpServletRequest.getParameterValues(GENETIC_PROFILE_IDS);
        HashSet<String> geneticProfileIdSet = new HashSet<String>();
        if (geneticProfileIds != null && geneticProfileIds.length > 0) {
            for (String geneticProfileIdDirty : geneticProfileIds) {
                String geneticProfileIdClean = servletXssUtil.getCleanInput(geneticProfileIdDirty);
                geneticProfileIdSet.add(geneticProfileIdClean);
            }
        }
        httpServletRequest.setAttribute(GENETIC_PROFILE_IDS, geneticProfileIdSet);

        //  Get User Defined Gene List
        String geneList = servletXssUtil.getCleanInput (httpServletRequest, GENE_LIST);
        xdebug.logMsg(this, "Gene List is set to:  " + geneList);

        //  Get all Cancer Types
        try {
            ArrayList<CancerStudy> cancerStudyList = GetCancerTypes.getCancerStudies();

            if (cancerTypeId == null) {
                cancerTypeId = cancerStudyList.get(0).getCancerStudyStableId();
            }
            
            httpServletRequest.setAttribute(CANCER_STUDY_ID, cancerTypeId);
            httpServletRequest.setAttribute(CANCER_TYPES_INTERNAL, cancerStudyList);

            //  Get Genetic Profiles for Selected Cancer Type
            ArrayList<GeneticProfile> profileList = GetGeneticProfiles.getGeneticProfiles
                (cancerTypeId);
            httpServletRequest.setAttribute(PROFILE_LIST_INTERNAL, profileList);

            //  Get Case Sets for Selected Cancer Type
            xdebug.logMsg(this, "Using Cancer Study ID:  " + cancerTypeId);
            ArrayList<CaseList> caseSets = GetCaseSets.getCaseSets(cancerTypeId);
            xdebug.logMsg(this, "Total Number of Case Sets:  " + caseSets.size());
            CaseList caseSet = new CaseList();
            caseSet.setName("User-defined Case List");
            caseSet.setDescription("User defined case list.");
            caseSet.setStableId("-1");
            caseSets.add(caseSet);
            httpServletRequest.setAttribute(CASE_SETS_INTERNAL, caseSets);

            //  Get User Selected Case Set
            String caseSetId = servletXssUtil.getCleanInput(httpServletRequest, CASE_SET_ID);
            if (caseSetId != null) {
                httpServletRequest.setAttribute(CASE_SET_ID, caseSetId);
            } else {
                if (caseSets.size() > 0) {
                    CaseList zeroSet = caseSets.get(0);
                    httpServletRequest.setAttribute(CASE_SET_ID, zeroSet.getStableId());
                }
            }
            String caseIds = servletXssUtil.getCleanInput(httpServletRequest, CASE_IDS);
            httpServletRequest.setAttribute(XDEBUG_OBJECT, xdebug);

            boolean errorsExist = validateForm(action, profileList, geneticProfileIdSet, geneList,
                                               caseSetId, caseIds, httpServletRequest);
            if (action != null && action.equals(ACTION_SUBMIT) && (!errorsExist)) {

                processData(geneticProfileIdSet, profileList, geneList, caseSetId,
                            caseIds, caseSets, getServletContext(), httpServletRequest,
                            httpServletResponse, xdebug);
            } else {
                RequestDispatcher dispatcher =
                    getServletContext().getRequestDispatcher("/WEB-INF/jsp/index.jsp");
                dispatcher.forward(httpServletRequest, httpServletResponse);
            }
        } catch (RemoteException e) {
            xdebug.logMsg(this, "Got Remote Exception:  " + e.getMessage());
            forwardToErrorPage(httpServletRequest, httpServletResponse,
                               "The Cancer Genomics Data Server is not currently "
                               + "available. <br/><br/>Please check back later.", xdebug);
        } catch (DaoException e) {
            xdebug.logMsg(this, "Got Database Exception:  " + e.getMessage());
            forwardToErrorPage(httpServletRequest, httpServletResponse,
                               "The Cancer Genomics Data Server is not currently "
                               + "available. <br/><br/>Please check back later.", xdebug);
        }
    }

    /**
     * process a good request
     * 
    */
    private void processData(HashSet<String> geneticProfileIdSet,
                                ArrayList<GeneticProfile> profileList,
                                String geneListStr,
                                String caseSetId, String caseIds,
                                ArrayList<CaseList> caseSetList,
                                ServletContext servletContext, HttpServletRequest request,
                                HttpServletResponse response,
                                XDebug xdebug)
            throws IOException, ServletException, DaoException {

       // parse geneList, written in the OncoPrintSpec language (except for changes by XSS clean)
       double zScore = ZScoreUtil.getZScore(geneticProfileIdSet, profileList, request);
       ParserOutput theOncoPrintSpecParserOutput =
               OncoPrintSpecificationDriver.callOncoPrintSpecParserDriver( geneListStr,
                geneticProfileIdSet, profileList, zScore );
       
        ArrayList<String> geneList = new ArrayList<String>();
        geneList.addAll( theOncoPrintSpecParserOutput.getTheOncoPrintSpecification().listOfGenes());
        ArrayList<String> tempGeneList = new ArrayList<String>();
        for (String gene : geneList){
            tempGeneList.add(gene);
        }
        geneList = tempGeneList;
        request.setAttribute(GENE_LIST, geneList);

        xdebug.logMsg(this, "Using gene list geneList.toString():  " + geneList.toString());
        HashSet<String> setOfCaseIds = null;
        if (!caseSetId.equals("-1")) {
            for (CaseList caseSet : caseSetList) {
                if (caseSet.getStableId().equals(caseSetId)) {
                    caseIds = caseSet.getCaseListAsString();
                    setOfCaseIds = new HashSet<String>(caseSet.getCaseList());
                    caseSet.getCaseList();
                }
            }
        }
            else {
            String[] caseIdSplit = caseIds.split("\\s+");
            setOfCaseIds = new HashSet<String>();
            for (String caseID : caseIdSplit){
                if (null != caseID){
                   setOfCaseIds.add(caseID);
                }
            }
        }
        
        request.setAttribute(CASE_IDS, caseIds);

        Iterator<String> profileIterator = geneticProfileIdSet.iterator();
        ArrayList<ProfileData> profileDataList = new ArrayList<ProfileData>();

        Set<String> warningUnion = new HashSet<String>();
        ArrayList<DownloadLink> downloadLinkSet = new ArrayList<DownloadLink>();
        ArrayList<ExtendedMutation> mutationList = new ArrayList<ExtendedMutation>();

        while (profileIterator.hasNext()) {<commit_msg>added customcase documentation to querybuilder<commit_after>package org.mskcc.portal.servlet;

import java.io.IOException;

import java.io.PrintWriter;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Arrays;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mskcc.cgds.model.ClinicalData;
import org.mskcc.portal.model.*;
import org.mskcc.portal.oncoPrintSpecLanguage.OncoPrintLangException;
import org.mskcc.portal.oncoPrintSpecLanguage.ParserOutput;
import org.mskcc.portal.remote.*;
import org.mskcc.portal.util.*;
import org.mskcc.portal.r_bridge.SurvivalPlot;
import org.mskcc.cgds.model.CancerStudy;
import org.mskcc.cgds.model.CaseList;
import org.mskcc.cgds.model.GeneticProfile;
import org.mskcc.cgds.model.GeneticAlterationType;
import org.mskcc.cgds.model.ExtendedMutation;
import org.mskcc.cgds.dao.DaoException;
import org.mskcc.cgds.web_api.GetProfileData;
import org.owasp.validator.html.PolicyException;

/**
 * Central Servlet for building queries.
 */
public class QueryBuilder extends HttpServlet {
    public static final boolean INCLUDE_NETWORKS = true;
    public static final String CGDS_URL_PARAM = "cgds_url";
    public static final String PATHWAY_COMMONS_URL_PARAM = "pathway_commons_url";
    public static final String CANCER_TYPES_INTERNAL = "cancer_types";
    public static final String PROFILE_LIST_INTERNAL = "profile_list";
    public static final String CASE_SETS_INTERNAL = "case_sets";
    public static final String CANCER_STUDY_ID = "cancer_study_id";
    public static final String CLINICAL_DATA_LIST = "clinical_data_list";
    public static final String GENETIC_PROFILE_IDS = "genetic_profile_ids";
    public static final String GENE_SET_CHOICE = "gene_set_choice";
    public static final String CASE_SET_ID = "case_set_id";
    public static final String CASE_IDS = "case_ids";
    public static final String GENE_LIST = "gene_list";
    public static final String ACTION = "action";
    public static final String OUTPUT = "output";
    public static final String FORMAT = "format";
    public static final String PLOT_TYPE = "plot_type";
    public static final String OS_SURVIVAL_PLOT = "os_survival_plot";
    public static final String DFS_SURVIVAL_PLOT = "dfs_survival_plot";
    public static final String XDEBUG = "xdebug";
    public static final String ACTION_SUBMIT = "Submit";
    public static final String STEP2_ERROR_MSG = "step2_error_msg";
    public static final String STEP3_ERROR_MSG = "step3_error_msg";
    public static final String STEP4_ERROR_MSG = "step4_error_msg";
    public static final String MERGED_PROFILE_DATA_INTERNAL = "merged_profile_data";
    public static final String PROFILE_DATA_SUMMARY = "profile_data_summary";
    public static final String WARNING_UNION = "warning_union";
    public static final String DOWNLOAD_LINKS = "download_links";
    public static final String NETWORK = "network";
    public static final String HTML_TITLE = "html_title";
    public static final String TAB_INDEX = "tab_index";
    public static final String TAB_DOWNLOAD = "tab_download";
    public static final String TAB_VISUALIZE = "tab_visualize";
    public static final String USER_ERROR_MESSAGE = "user_error_message";
    public static final String ATTRIBUTE_URL_BEFORE_FORWARDING = "ATTRIBUTE_URL_BEFORE_FORWARDING";
    public static final String Z_SCORE_THRESHOLD = "Z_SCORE_THRESHOLD";
    public static final String MRNA_PROFILES_SELECTED = "MRNA_PROFILES_SELECTED";
    public static final String COMPUTE_LOG_ODDS_RATIO = "COMPUTE_LOG_ODDS_RATIO";
    public static final String MUTATION_MAP = "MUTATION_MAP";
    public static final int MUTATION_DETAIL_LIMIT = 10;
    public static final String MUTATION_DETAIL_LIMIT_REACHED = "MUTATION_DETAIL_LIMIT_REACHED";
    public static final int MAX_NUM_GENES = 100;
    public static final String XDEBUG_OBJECT = "xdebug_object";
    public static final String ONCO_PRINT_HTML = "oncoprint_html";
    public static final String INDEX_PAGE = "index.do";

    private ServletXssUtil servletXssUtil;

    /**
     * Initializes the servlet.
     *
     * @throws ServletException Serlvet Init Error.
     */
    public void init() throws ServletException {
        super.init();
        String cgdsUrl = getInitParameter(CGDS_URL_PARAM);
        System.out.println ("Init Query Builder with CGDS URL:  " + cgdsUrl);
        GlobalProperties.setCgdsUrl(cgdsUrl);
		String pathwayCommonsUrl = getInitParameter(PATHWAY_COMMONS_URL_PARAM);
        System.out.println ("Init Query Builder with PathwayCommons URL:  " + pathwayCommonsUrl);
        GlobalProperties.setPathwayCommonsUrl(pathwayCommonsUrl);
        try {
            servletXssUtil = ServletXssUtil.getInstance();
        } catch (PolicyException e) {
            throw new ServletException (e);
        }
    }

    /**
     * Handles HTTP GET Request.
     *
     * @param httpServletRequest  Http Servlet Request Object.
     * @param httpServletResponse Http Servelt Response Object.
     * @throws ServletException Servlet Error.
     * @throws IOException      IO Error.
     */
    protected void doGet(HttpServletRequest httpServletRequest,
                         HttpServletResponse httpServletResponse) throws ServletException,
            IOException {
        doPost(httpServletRequest, httpServletResponse);
    }

    /**
     * Handles HTTP POST Request.
     *
     * @param httpServletRequest  Http Servlet Request Object.
     * @param httpServletResponse Http Servelt Response Object.
     * @throws ServletException Servlet Error.
     * @throws IOException      IO Error.
     */
    protected void doPost(HttpServletRequest httpServletRequest,
                          HttpServletResponse httpServletResponse) throws ServletException,
            IOException {
        XDebug xdebug = new XDebug( httpServletRequest );
        xdebug.startTimer();

        xdebug.logMsg(this, "Attempting to initiate new user query.");
        
        if (httpServletRequest.getRequestURL() != null) {
            httpServletRequest.setAttribute(ATTRIBUTE_URL_BEFORE_FORWARDING,
                                            httpServletRequest.getRequestURL().toString());
        }

        //  Get User Selected Action
        String action = servletXssUtil.getCleanInput (httpServletRequest, ACTION);

        //  Get User Selected Cancer Type
        String cancerTypeId = servletXssUtil.getCleanInput(httpServletRequest, CANCER_STUDY_ID);

        //  Get User Selected Genetic Profiles
        String geneticProfileIds[] = httpServletRequest.getParameterValues(GENETIC_PROFILE_IDS);
        HashSet<String> geneticProfileIdSet = new HashSet<String>();
        if (geneticProfileIds != null && geneticProfileIds.length > 0) {
            for (String geneticProfileIdDirty : geneticProfileIds) {
                String geneticProfileIdClean = servletXssUtil.getCleanInput(geneticProfileIdDirty);
                geneticProfileIdSet.add(geneticProfileIdClean);
            }
        }
        httpServletRequest.setAttribute(GENETIC_PROFILE_IDS, geneticProfileIdSet);

        //  Get User Defined Gene List
        String geneList = servletXssUtil.getCleanInput (httpServletRequest, GENE_LIST);
        xdebug.logMsg(this, "Gene List is set to:  " + geneList);

        //  Get all Cancer Types
        try {
            ArrayList<CancerStudy> cancerStudyList = GetCancerTypes.getCancerStudies();

            if (cancerTypeId == null) {
                cancerTypeId = cancerStudyList.get(0).getCancerStudyStableId();
            }
            
            httpServletRequest.setAttribute(CANCER_STUDY_ID, cancerTypeId);
            httpServletRequest.setAttribute(CANCER_TYPES_INTERNAL, cancerStudyList);

            //  Get Genetic Profiles for Selected Cancer Type
            ArrayList<GeneticProfile> profileList = GetGeneticProfiles.getGeneticProfiles
                (cancerTypeId);
            httpServletRequest.setAttribute(PROFILE_LIST_INTERNAL, profileList);

            //  Get Case Sets for Selected Cancer Type
            xdebug.logMsg(this, "Using Cancer Study ID:  " + cancerTypeId);
            ArrayList<CaseList> caseSets = GetCaseSets.getCaseSets(cancerTypeId);
            xdebug.logMsg(this, "Total Number of Case Sets:  " + caseSets.size());
            CaseList caseSet = new CaseList();
            caseSet.setName("User-defined Case List");
            caseSet.setDescription("User defined case list.");
            caseSet.setStableId("-1");
            caseSets.add(caseSet);
            httpServletRequest.setAttribute(CASE_SETS_INTERNAL, caseSets);

            //  Get User Selected Case Set
            String caseSetId = servletXssUtil.getCleanInput(httpServletRequest, CASE_SET_ID);
            if (caseSetId != null) {
                httpServletRequest.setAttribute(CASE_SET_ID, caseSetId);
            } else {
                if (caseSets.size() > 0) {
                    CaseList zeroSet = caseSets.get(0);
                    httpServletRequest.setAttribute(CASE_SET_ID, zeroSet.getStableId());
                }
            }
            String caseIds = servletXssUtil.getCleanInput(httpServletRequest, CASE_IDS);
            httpServletRequest.setAttribute(XDEBUG_OBJECT, xdebug);

            boolean errorsExist = validateForm(action, profileList, geneticProfileIdSet, geneList,
                                               caseSetId, caseIds, httpServletRequest);
            if (action != null && action.equals(ACTION_SUBMIT) && (!errorsExist)) {

                processData(geneticProfileIdSet, profileList, geneList, caseSetId,
                            caseIds, caseSets, getServletContext(), httpServletRequest,
                            httpServletResponse, xdebug);
            } else {
                RequestDispatcher dispatcher =
                    getServletContext().getRequestDispatcher("/WEB-INF/jsp/index.jsp");
                dispatcher.forward(httpServletRequest, httpServletResponse);
            }
        } catch (RemoteException e) {
            xdebug.logMsg(this, "Got Remote Exception:  " + e.getMessage());
            forwardToErrorPage(httpServletRequest, httpServletResponse,
                               "The Cancer Genomics Data Server is not currently "
                               + "available. <br/><br/>Please check back later.", xdebug);
        } catch (DaoException e) {
            xdebug.logMsg(this, "Got Database Exception:  " + e.getMessage());
            forwardToErrorPage(httpServletRequest, httpServletResponse,
                               "The Cancer Genomics Data Server is not currently "
                               + "available. <br/><br/>Please check back later.", xdebug);
        }
    }

    /**
     * process a good request
     * 
    */
    private void processData(HashSet<String> geneticProfileIdSet,
                                ArrayList<GeneticProfile> profileList,
                                String geneListStr,
                                String caseSetId, String caseIds,
                                ArrayList<CaseList> caseSetList,
                                ServletContext servletContext, HttpServletRequest request,
                                HttpServletResponse response,
                                XDebug xdebug)
            throws IOException, ServletException, DaoException {

       // parse geneList, written in the OncoPrintSpec language (except for changes by XSS clean)
       double zScore = ZScoreUtil.getZScore(geneticProfileIdSet, profileList, request);
       ParserOutput theOncoPrintSpecParserOutput =
               OncoPrintSpecificationDriver.callOncoPrintSpecParserDriver( geneListStr,
                geneticProfileIdSet, profileList, zScore );
       
        ArrayList<String> geneList = new ArrayList<String>();
        geneList.addAll( theOncoPrintSpecParserOutput.getTheOncoPrintSpecification().listOfGenes());
        ArrayList<String> tempGeneList = new ArrayList<String>();
        for (String gene : geneList){
            tempGeneList.add(gene);
        }
        geneList = tempGeneList;
        request.setAttribute(GENE_LIST, geneList);

        xdebug.logMsg(this, "Using gene list geneList.toString():  " + geneList.toString());
        HashSet<String> setOfCaseIds = null;
        if (!caseSetId.equals("-1")) {
            for (CaseList caseSet : caseSetList) {
                if (caseSet.getStableId().equals(caseSetId)) {
                    caseIds = caseSet.getCaseListAsString();
                    setOfCaseIds = new HashSet<String>(caseSet.getCaseList());
                    caseSet.getCaseList();
                }
            }
        }
        //if user specifies specific cases, add these to hashset, and send to GetMutationData
            else {
            String[] caseIdSplit = caseIds.split("\\s+");
            setOfCaseIds = new HashSet<String>();
            for (String caseID : caseIdSplit){
                if (null != caseID){
                   setOfCaseIds.add(caseID);
                }
            }
        }
        
        request.setAttribute(CASE_IDS, caseIds);

        Iterator<String> profileIterator = geneticProfileIdSet.iterator();
        ArrayList<ProfileData> profileDataList = new ArrayList<ProfileData>();

        Set<String> warningUnion = new HashSet<String>();
        ArrayList<DownloadLink> downloadLinkSet = new ArrayList<DownloadLink>();
        ArrayList<ExtendedMutation> mutationList = new ArrayList<ExtendedMutation>();

        while (profileIterator.hasNext()) {
{'fraction_non_alphanumeric': 0.0789921442276364, 'fraction_numerical': 0.0012308583426854432, 'mean_word_length': 2.738024357239513, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1558658', 'n_tokens_mistral': 7546, 'n_tokens_neox': 7356, 'n_words': 1719}