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

import org.apache.commons.lang3.StringUtils;
import org.genome_nexus.ApiClient;
import org.genome_nexus.ApiException;
import org.genome_nexus.client.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.mskcc.cbio.oncokb.apiModels.TranscriptMatchResult;
import org.mskcc.cbio.oncokb.apiModels.TranscriptPair;
import org.mskcc.cbio.oncokb.apiModels.ensembl.Sequence;
import org.mskcc.cbio.oncokb.genomenexus.GNVariantAnnotationType;
import org.mskcc.cbio.oncokb.model.*;
import org.mskcc.cbio.oncokb.model.Gene;
import org.mskcc.cbio.oncokb.model.ReferenceGenome;
import org.mskcc.cbio.oncokb.model.VariantConsequence;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by Hongxin on 6/26/17.
 */
public class GenomeNexusUtils {

    private static final String MSK_ISOFORM_OVERRIDE = "mskcc";

    private static final String ENSEMBL_37_API_URL = "https://grch37.rest.ensembl.org";
    private static final String ENSEMBL_38_API_URL = "https://rest.ensembl.org";

    private static final String GN_37_URL = "https://www.genomenexus.org";
    private static final String GN_38_URL = "https://grch38.genomenexus.org";
    private static final int GN_READ_TIMEOUT_OVERRIDE = 30000;

    public static String getEnsemblSequencePOSTUrl(ReferenceGenome referenceGenome) {
        return getEnsemblAPIUrl(referenceGenome) + "/sequence/id";
    }

    public static TranscriptMatchResult matchTranscript(TranscriptPair transcript, ReferenceGenome referenceGenome, String hugoSymbol) throws ApiException {
        // Find whether both transcript length are the same
        Optional<EnsemblTranscript> _ensemblTranscript = getEnsemblTranscript(hugoSymbol, transcript);
        TranscriptMatchResult transcriptMatchResult = new TranscriptMatchResult();

        if (_ensemblTranscript.isPresent()) {
            transcriptMatchResult.setOriginalEnsemblTranscript(_ensemblTranscript.get());
            Optional<Sequence> _sequence = getProteinSequence(transcript.getReferenceGenome(), _ensemblTranscript.get().getProteinId());
            if (_sequence.isPresent()) {
                List<EnsemblTranscript> targetEnsemblTranscripts = getEnsemblTranscriptList(hugoSymbol, referenceGenome);
                if (targetEnsemblTranscripts.size() == 0) {
                    transcriptMatchResult.setNote("The target reference genome does not have any ensembl transcripts.");
                } else {
                    try {
                        pickEnsemblTranscript(transcriptMatchResult, referenceGenome, targetEnsemblTranscripts, _sequence.get());
                    } catch (Exception exception) {
                        transcriptMatchResult.setNote(exception.getMessage());
                    }
                }
            } else {
                transcriptMatchResult.setNote("The transcript is invalid");
            }
        } else {
            transcriptMatchResult.setNote("The transcript is invalid");
        }
        return transcriptMatchResult;
    }

    private static ApiClient getGNApiClient(String url) {
        ApiClient client = new ApiClient();
        client.setReadTimeout(GN_READ_TIMEOUT_OVERRIDE);
        client.setBasePath(url);
        return client;
    }

    private static EnsemblControllerApi getGNEnsemblControllerApi(String url) {
        return new EnsemblControllerApi(getGNApiClient(url));
    }

    private static AnnotationControllerApi getGNAnnotationControllerApi(String url) {
        return new AnnotationControllerApi(getGNApiClient(url));
    }

    private static String getGenomeNexusUrl(ReferenceGenome referenceGenome) {
        switch (referenceGenome) {
            case GRCh37:
                String grch37Url = PropertiesUtils.getProperties("genome_nexus.grch37.url");
                return StringUtils.isEmpty(grch37Url) ? GN_37_URL : grch37Url;
            case GRCh38:<commit_msg>Fix issue when ensemble transcript does not have a protein id<commit_after>package org.mskcc.cbio.oncokb.util;

import org.apache.commons.lang3.StringUtils;
import org.genome_nexus.ApiClient;
import org.genome_nexus.ApiException;
import org.genome_nexus.client.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.mskcc.cbio.oncokb.apiModels.TranscriptMatchResult;
import org.mskcc.cbio.oncokb.apiModels.TranscriptPair;
import org.mskcc.cbio.oncokb.apiModels.ensembl.Sequence;
import org.mskcc.cbio.oncokb.genomenexus.GNVariantAnnotationType;
import org.mskcc.cbio.oncokb.model.*;
import org.mskcc.cbio.oncokb.model.Gene;
import org.mskcc.cbio.oncokb.model.ReferenceGenome;
import org.mskcc.cbio.oncokb.model.VariantConsequence;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by Hongxin on 6/26/17.
 */
public class GenomeNexusUtils {

    private static final String MSK_ISOFORM_OVERRIDE = "mskcc";

    private static final String ENSEMBL_37_API_URL = "https://grch37.rest.ensembl.org";
    private static final String ENSEMBL_38_API_URL = "https://rest.ensembl.org";

    private static final String GN_37_URL = "https://www.genomenexus.org";
    private static final String GN_38_URL = "https://grch38.genomenexus.org";
    private static final int GN_READ_TIMEOUT_OVERRIDE = 30000;

    public static String getEnsemblSequencePOSTUrl(ReferenceGenome referenceGenome) {
        return getEnsemblAPIUrl(referenceGenome) + "/sequence/id";
    }

    public static TranscriptMatchResult matchTranscript(TranscriptPair transcript, ReferenceGenome referenceGenome, String hugoSymbol) throws ApiException {
        // Find whether both transcript length are the same
        Optional<EnsemblTranscript> _ensemblTranscript = getEnsemblTranscript(hugoSymbol, transcript);
        TranscriptMatchResult transcriptMatchResult = new TranscriptMatchResult();

        if (_ensemblTranscript.isPresent()) {
            transcriptMatchResult.setOriginalEnsemblTranscript(_ensemblTranscript.get());
            Optional<Sequence> _sequence = Optional.empty();
            if (StringUtils.isNotEmpty(_ensemblTranscript.get().getProteinId())) {
                _sequence = getProteinSequence(transcript.getReferenceGenome(), _ensemblTranscript.get().getProteinId());
            }
            if (_sequence.isPresent()) {
                List<EnsemblTranscript> targetEnsemblTranscripts = getEnsemblTranscriptList(hugoSymbol, referenceGenome);
                if (targetEnsemblTranscripts.size() == 0) {
                    transcriptMatchResult.setNote("The target reference genome does not have any ensembl transcripts.");
                } else {
                    try {
                        pickEnsemblTranscript(transcriptMatchResult, referenceGenome, targetEnsemblTranscripts, _sequence.get());
                    } catch (Exception exception) {
                        transcriptMatchResult.setNote(exception.getMessage());
                    }
                }
            } else {
                transcriptMatchResult.setNote("No protein sequence available for transcript " + _ensemblTranscript.get().getTranscriptId());
            }
        } else {
            transcriptMatchResult.setNote("The transcript is invalid");
        }
        return transcriptMatchResult;
    }

    private static ApiClient getGNApiClient(String url) {
        ApiClient client = new ApiClient();
        client.setReadTimeout(GN_READ_TIMEOUT_OVERRIDE);
        client.setBasePath(url);
        return client;
    }

    private static EnsemblControllerApi getGNEnsemblControllerApi(String url) {
        return new EnsemblControllerApi(getGNApiClient(url));
    }

    private static AnnotationControllerApi getGNAnnotationControllerApi(String url) {
        return new AnnotationControllerApi(getGNApiClient(url));
    }

    private static String getGenomeNexusUrl(ReferenceGenome referenceGenome) {
        switch (referenceGenome) {
            case GRCh37:
                String grch37Url = PropertiesUtils.getProperties("genome_nexus.grch37.url");
                return StringUtils.isEmpty(grch37Url) ? GN_37_URL : grch37Url;
            case GRCh38:
{'fraction_non_alphanumeric': 0.08528708133971293, 'fraction_numerical': 0.00909090909090909, 'mean_word_length': 3.3706220595922636, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 8, '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': '6440382', 'n_tokens_mistral': 2479, 'n_tokens_neox': 2329, 'n_words': 444}