package org.mskcc.cbio.oncokb.util;

import org.apache.commons.lang3.StringUtils;
import org.mskcc.cbio.oncokb.bo.GeneBo;
import org.mskcc.cbio.oncokb.model.Gene;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by hongxinzhang on 4/5/16.
 */
public class GeneUtils {
    public static Gene getGene(String gene) {
        if (StringUtils.isNumeric(gene)) {
            return getGeneByEntrezId(Integer.parseInt(gene));
        } else {
            return getGeneByHugoSymbol(gene);
        }
    }

    public static Gene getGeneByHugoSymbol(String hugoSymbol) {
        if (hugoSymbol != null) {
            hugoSymbol = hugoSymbol.toUpperCase();
            GeneBo geneBo = ApplicationContextSingleton.getGeneBo();
            if (CacheUtils.isEnabled()) {
                Gene gene = CacheUtils.getGeneByHugoSymbol(hugoSymbol);
                if (gene == null) {
                    gene = geneBo.findGeneByAlias(hugoSymbol);
                }
                return gene;
            } else {
                return geneBo.findGeneByHugoSymbol(hugoSymbol);
            }
        }
        return null;
    }

    public static Gene getGeneByEntrezId(Integer entrezId) {
        if (entrezId != null) {
            GeneBo geneBo = ApplicationContextSingleton.getGeneBo();
            if (CacheUtils.isEnabled()) {
                if (CacheUtils.getGeneByEntrezId(entrezId) == null) {
                    CacheUtils.setGeneByEntrezId(geneBo.findGeneByEntrezGeneId(entrezId));
                }
                return CacheUtils.getGeneByEntrezId(entrezId);
            } else {
                return geneBo.findGeneByEntrezGeneId(entrezId);
            }
        }
        return null;
    }

    public static Set<Gene> searchGene(String keywords) {
        Set<Gene> genes = new HashSet<>();
        if (keywords != null && keywords != "") {
            Set<Gene> allGenes = GeneUtils.getAllGenes();
            if (org.apache.commons.lang3.math.NumberUtils.isNumber(keywords)) {
                for (Gene gene : allGenes) {
                    String entrezId = Integer.toString(gene.getEntrezGeneId());
                    if (entrezId.contains(keywords)) {
                        genes.add(gene);
                    }
                }
            } else {
                for (Gene gene : allGenes) {
                    String hugoSymbol = gene.getHugoSymbol();
                    if (StringUtils.containsIgnoreCase(hugoSymbol, keywords)) {
                        genes.add(gene);
                    } else {
                        for (String alias : gene.getGeneAliases()) {
                            if (StringUtils.containsIgnoreCase(alias, keywords)) {
                                genes.add(gene);
                                break;
                            }
                        }
                    }
                }
            }
        }

        return genes;
    }

    public static Set<Gene> getAllGenes() {
        if (CacheUtils.isEnabled()) {
            return CacheUtils.getAllGenes();
        } else {
            GeneBo geneBo = ApplicationContextSingleton.getGeneBo();
            return new HashSet<>(geneBo.findAll());
        }
    }

    public static Boolean isSameGene(Integer entrezGeneId, String hugoSymbol) {
        Boolean flag = false;
        Gene entrezGene = null;
        Gene hugoGene = null;
        if (entrezGeneId != null) {
            entrezGene = GeneUtils.getGeneByEntrezId(entrezGeneId);
        }

        if (hugoSymbol != null) {
            hugoGene = GeneUtils.getGeneByHugoSymbol(hugoSymbol);
        }

        if (entrezGene != null && hugoGene != null && entrezGene.equals(hugoGene)) {
            flag = true;
        }
        return flag;
    }
}

@@ -905,25 +905,17 @@
 ene = ge
-neBo.find
+t
 GeneByAl
@@ -1574,32 +1574,32 @@
        %7D else %7B%0A
-
                 
@@ -1690,32 +1690,963 @@
 rn null;%0A    %7D%0A%0A
+    public static Gene getGeneByAlias(String geneAlias) %7B%0A        if (geneAlias != null) %7B%0A            GeneBo geneBo = ApplicationContextSingleton.getGeneBo();%0A            if (CacheUtils.isEnabled()) %7B%0A                Set%3CGene%3E genes = getAllGenes();%0A                Set%3CGene%3E matches = new HashSet%3C%3E();%0A                for (Gene gene : genes) %7B%0A                    for (String alias : gene.getGeneAliases()) %7B%0A                        if (alias.equals(geneAlias)) %7B%0A                            matches.add(gene);%0A                            break;%0A                        %7D%0A                    %7D%0A                %7D%0A                if (matches.isEmpty() %7C%7C matches.size() %3E 1) %7B%0A                    return null;%0A                %7D else %7B%0A                    matches.iterator().next();%0A                %7D%0A            %7D else %7B%0A                return geneBo.findGeneByAlias(geneAlias);%0A            %7D%0A        %7D%0A        return null;%0A    %7D%0A%0A
     public stati

Add getGeneByAlias in GeneUtils