<commit_before>package org.mskcc.cbio.oncokb.util;

import org.apache.commons.collections.CollectionUtils;
import org.mskcc.cbio.oncokb.bo.EvidenceBo;
import org.mskcc.cbio.oncokb.model.*;

import java.util.*;
import java.util.logging.Level;

/**
 * Created by Hongxin on 8/10/15.
 */
public class EvidenceUtils {
    private static EvidenceBo evidenceBo = ApplicationContextSingleton.getEvidenceBo();

    /**
     * Remove evidences if its alteration in the alteration list
     *
     * @param evidences
     * @param alterations
     * @return
     */
    public static List<Evidence> removeByAlterations(List<Evidence> evidences, Collection<Alteration> alterations) {
        if (alterations != null) {
            Iterator<Evidence> i = evidences.iterator();
            while (i.hasNext()) {
                Boolean contain = false;
                Evidence evidence = i.next();
                for (Alteration alteration : alterations) {
                    if (alteration != null) {
                        for (Alteration eviAlt : evidence.getAlterations()) {
                            if (eviAlt != null && alteration.equals(eviAlt)) {
                                contain = true;
                                break;
                            }
                        }
                        if (contain) {
                            i.remove();
                            break;
                        }
                    }
                }
            }
        }
        return evidences;
    }

    public static List<Evidence> getRelevantEvidences(
            Query query, String source, String geneStatus,
            List<EvidenceType> evidenceTypes, List<LevelOfEvidence> levelOfEvidences) {
        Gene gene = GeneUtils.getGene(query.getEntrezGeneId(), query.getHugoSymbol());
        if (gene != null) {
            String strEntrezId = Integer.toString(gene.getEntrezGeneId());
            String variantId = query.getQueryId() +
                    (source != null ? ("&" + source) : "") +
                    "&" + evidenceTypes.toString() +
                    (levelOfEvidences == null ? "" : ("&" + levelOfEvidences.toString()));

            if (!CacheUtils.containRelevantEvidences(strEntrezId, variantId)) {
                List<Alteration> relevantAlterations = AlterationUtils.getRelevantAlterations(
                        gene, query.getAlteration(), query.getConsequence(),
                        query.getProteinStart(), query.getProteinEnd());

                List<Evidence> relevantEvidences;
                List<TumorType> relevantTumorTypes = new ArrayList<> ();
                if(query.getTumorType() != null) {
                    relevantTumorTypes = TumorTypeUtils.getTumorTypes(query.getTumorType(), source);
                }
                EvidenceQueryRes evidenceQueryRes = new EvidenceQueryRes();
                evidenceQueryRes.setGene(gene);
                evidenceQueryRes.setQuery(query);
                evidenceQueryRes.setAlterations(relevantAlterations);
                evidenceQueryRes.setTumorTypes(relevantTumorTypes);
                List<EvidenceQueryRes> evidenceQueryResList = new ArrayList<>();
                evidenceQueryResList.add(evidenceQueryRes);
                relevantEvidences = filterEvidence(getEvidence(evidenceQueryResList, evidenceTypes, geneStatus, levelOfEvidences), evidenceQueryRes);

                CacheUtils.setRelevantEvidences(strEntrezId, variantId, relevantEvidences);
            }

            return CacheUtils.getRelevantEvidences(strEntrezId, variantId);
        } else {
            return new ArrayList<>();
        }
    }

    private static List<Evidence> getEvidence(List<Alteration> alterations) {
        if (alterations == null || alterations.size() == 0) {
            return new ArrayList<>();
        }
        return evidenceBo.findEvidencesByAlteration(alterations);
    }

    private static List<Evidence> getEvidence(List<Alteration> alterations, List<EvidenceType> evidenceTypes, List<LevelOfEvidence> levelOfEvidences) {
        if (alterations == null || alterations.size() == 0) {
            return new ArrayList<>();
        }
        if (evidenceTypes == null || evidenceTypes.size() == 0) {
            return getEvidence(alterations);
        }
        if (levelOfEvidences == null || levelOfEvidences.size() == 0) {
            return evidenceBo.findEvidencesByAlteration(alterations, evidenceTypes);
        }else {
            return evidenceBo.findEvidencesByAlterationWithLevels(alterations, evidenceTypes, levelOfEvidences);
        }
    }

    private static List<Evidence> getEvidence(List<Alteration> alterations, List<EvidenceType> evidenceTypes, List<TumorType> tumorTypes, List<LevelOfEvidence> levelOfEvidences) {
        if (alterations == null || alterations.size() == 0) {
            return new ArrayList<>();
        }
        if (evidenceTypes == null || evidenceTypes.size() == 0) {
            return getEvidence(alterations);
        }
        if (tumorTypes == null || tumorTypes.size() == 0) {
            return getEvidence(alterations, evidenceTypes, levelOfEvidences);
        }
        if (levelOfEvidences == null || levelOfEvidences.size() == 0) {
            return evidenceBo.findEvidencesByAlteration(alterations, evidenceTypes, tumorTypes);
        }else {
            return evidenceBo.findEvidencesByAlteration(alterations, evidenceTypes, tumorTypes, levelOfEvidences);
        }
    }

    public static List<Evidence> getEvidence(List<EvidenceQueryRes> queries, List<EvidenceType> evidenceTypes, String geneStatus, List<LevelOfEvidence> levelOfEvidences) {
        List<Evidence> evidences = new ArrayList<>();
        List<EvidenceType> filteredETs = new ArrayList<>();

        Map<Integer, Gene> genes = new HashMap<>(); //Get gene evidences
        Map<Integer, Alteration> alterations = new HashMap<>();
        Map<Integer, Alteration> alterationsME = new HashMap<>(); //Mutation effect only
        Map<String, TumorType> tumorTypes = new HashMap<>();

        for (EvidenceQueryRes query : queries) {
            if (query.getGene() != null) {
                int entrezGeneId = query.getGene().getEntrezGeneId();
                if (!genes.containsKey(entrezGeneId)) {
                    genes.put(entrezGeneId, query.getGene());
                }

                for (Alteration alt : query.getAlterations()) {
                    int altId = alt.getAlterationId();

                    if (geneStatus == null || geneStatus == "") {
                        geneStatus = "all";
                    }
                    geneStatus = geneStatus.toLowerCase();
                    if (geneStatus.equals("all") || query.getGene().getStatus().toLowerCase().equals(geneStatus)) {
                        if (!alterations.containsKey(altId)) {
                            alterations.put(altId, alt);
                        }

                        for (TumorType tumorType : query.getTumorTypes()) {<commit_msg>Open all genes<commit_after>package org.mskcc.cbio.oncokb.util;

import org.apache.commons.collections.CollectionUtils;
import org.mskcc.cbio.oncokb.bo.EvidenceBo;
import org.mskcc.cbio.oncokb.model.*;

import java.util.*;
import java.util.logging.Level;

/**
 * Created by Hongxin on 8/10/15.
 */
public class EvidenceUtils {
    private static EvidenceBo evidenceBo = ApplicationContextSingleton.getEvidenceBo();

    /**
     * Remove evidences if its alteration in the alteration list
     *
     * @param evidences
     * @param alterations
     * @return
     */
    public static List<Evidence> removeByAlterations(List<Evidence> evidences, Collection<Alteration> alterations) {
        if (alterations != null) {
            Iterator<Evidence> i = evidences.iterator();
            while (i.hasNext()) {
                Boolean contain = false;
                Evidence evidence = i.next();
                for (Alteration alteration : alterations) {
                    if (alteration != null) {
                        for (Alteration eviAlt : evidence.getAlterations()) {
                            if (eviAlt != null && alteration.equals(eviAlt)) {
                                contain = true;
                                break;
                            }
                        }
                        if (contain) {
                            i.remove();
                            break;
                        }
                    }
                }
            }
        }
        return evidences;
    }

    public static List<Evidence> getRelevantEvidences(
            Query query, String source, String geneStatus,
            List<EvidenceType> evidenceTypes, List<LevelOfEvidence> levelOfEvidences) {
        Gene gene = GeneUtils.getGene(query.getEntrezGeneId(), query.getHugoSymbol());
        if (gene != null) {
            String strEntrezId = Integer.toString(gene.getEntrezGeneId());
            String variantId = query.getQueryId() +
                    (source != null ? ("&" + source) : "") +
                    "&" + evidenceTypes.toString() +
                    (levelOfEvidences == null ? "" : ("&" + levelOfEvidences.toString()));

            if (!CacheUtils.containRelevantEvidences(strEntrezId, variantId)) {
                List<Alteration> relevantAlterations = AlterationUtils.getRelevantAlterations(
                        gene, query.getAlteration(), query.getConsequence(),
                        query.getProteinStart(), query.getProteinEnd());

                List<Evidence> relevantEvidences;
                List<TumorType> relevantTumorTypes = new ArrayList<> ();
                if(query.getTumorType() != null) {
                    relevantTumorTypes = TumorTypeUtils.getTumorTypes(query.getTumorType(), source);
                }
                EvidenceQueryRes evidenceQueryRes = new EvidenceQueryRes();
                evidenceQueryRes.setGene(gene);
                evidenceQueryRes.setQuery(query);
                evidenceQueryRes.setAlterations(relevantAlterations);
                evidenceQueryRes.setTumorTypes(relevantTumorTypes);
                List<EvidenceQueryRes> evidenceQueryResList = new ArrayList<>();
                evidenceQueryResList.add(evidenceQueryRes);
                relevantEvidences = filterEvidence(getEvidence(evidenceQueryResList, evidenceTypes, geneStatus, levelOfEvidences), evidenceQueryRes);

                CacheUtils.setRelevantEvidences(strEntrezId, variantId, relevantEvidences);
            }

            return CacheUtils.getRelevantEvidences(strEntrezId, variantId);
        } else {
            return new ArrayList<>();
        }
    }

    private static List<Evidence> getEvidence(List<Alteration> alterations) {
        if (alterations == null || alterations.size() == 0) {
            return new ArrayList<>();
        }
        return evidenceBo.findEvidencesByAlteration(alterations);
    }

    private static List<Evidence> getEvidence(List<Alteration> alterations, List<EvidenceType> evidenceTypes, List<LevelOfEvidence> levelOfEvidences) {
        if (alterations == null || alterations.size() == 0) {
            return new ArrayList<>();
        }
        if (evidenceTypes == null || evidenceTypes.size() == 0) {
            return getEvidence(alterations);
        }
        if (levelOfEvidences == null || levelOfEvidences.size() == 0) {
            return evidenceBo.findEvidencesByAlteration(alterations, evidenceTypes);
        }else {
            return evidenceBo.findEvidencesByAlterationWithLevels(alterations, evidenceTypes, levelOfEvidences);
        }
    }

    private static List<Evidence> getEvidence(List<Alteration> alterations, List<EvidenceType> evidenceTypes, List<TumorType> tumorTypes, List<LevelOfEvidence> levelOfEvidences) {
        if (alterations == null || alterations.size() == 0) {
            return new ArrayList<>();
        }
        if (evidenceTypes == null || evidenceTypes.size() == 0) {
            return getEvidence(alterations);
        }
        if (tumorTypes == null || tumorTypes.size() == 0) {
            return getEvidence(alterations, evidenceTypes, levelOfEvidences);
        }
        if (levelOfEvidences == null || levelOfEvidences.size() == 0) {
            return evidenceBo.findEvidencesByAlteration(alterations, evidenceTypes, tumorTypes);
        }else {
            return evidenceBo.findEvidencesByAlteration(alterations, evidenceTypes, tumorTypes, levelOfEvidences);
        }
    }

    public static List<Evidence> getEvidence(List<EvidenceQueryRes> queries, List<EvidenceType> evidenceTypes, String geneStatus, List<LevelOfEvidence> levelOfEvidences) {
        List<Evidence> evidences = new ArrayList<>();
        List<EvidenceType> filteredETs = new ArrayList<>();

        Map<Integer, Gene> genes = new HashMap<>(); //Get gene evidences
        Map<Integer, Alteration> alterations = new HashMap<>();
        Map<Integer, Alteration> alterationsME = new HashMap<>(); //Mutation effect only
        Map<String, TumorType> tumorTypes = new HashMap<>();

        for (EvidenceQueryRes query : queries) {
            if (query.getGene() != null) {
                int entrezGeneId = query.getGene().getEntrezGeneId();
                if (!genes.containsKey(entrezGeneId)) {
                    genes.put(entrezGeneId, query.getGene());
                }

                for (Alteration alt : query.getAlterations()) {
                    int altId = alt.getAlterationId();

//                    if (geneStatus == null || geneStatus == "") {
                        geneStatus = "all";
//                    }
                    geneStatus = geneStatus.toLowerCase();
                    if (geneStatus.equals("all") || query.getGene().getStatus().toLowerCase().equals(geneStatus)) {
                        if (!alterations.containsKey(altId)) {
                            alterations.put(altId, alt);
                        }

                        for (TumorType tumorType : query.getTumorTypes()) {
{'fraction_non_alphanumeric': 0.10459565372283577, 'fraction_numerical': 0.0018525115781973637, 'mean_word_length': 2.084157328059767, 'pattern_counts': {'":': 0, '<': 91, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7391178', 'n_tokens_mistral': 3869, 'n_tokens_neox': 3447, 'n_words': 714}