<commit_before>import _ from 'lodash';
import request from 'superagent';
import Response = request.Response;

import {
    AggregatedHotspots,
    EvidenceType,
    generateProteinChangeQuery,
    generateAnnotateStructuralVariantQuery,
    getMyVariantInfoAnnotationsFromIndexedVariantAnnotations,
    IOncoKbData,
    Mutation,
    UniprotFeature,
    UniprotFeatureList,
    uniqueGenomicLocations,
    StructuralVariantType,
} from 'cbioportal-utils';
import {
    AnnotateMutationByProteinChangeQuery,
    AnnotateStructuralVariantQuery,
    CancerGene,
    OncoKbAPI,
    OncoKBInfo,
} from 'oncokb-ts-api-client';
import {
    EnsemblFilter,
    EnsemblTranscript,
    GenomeNexusAPI,
    GenomeNexusAPIInternal,
    GenomicLocation,
    Hotspot,
    PfamDomain,
    PostTranslationalModification,
    VariantAnnotation,
    MyVariantInfo,
} from 'genome-nexus-ts-api-client';

import { MutationMapperDataFetcher } from '../model/MutationMapperDataFetcher';
import {
    DEFAULT_MY_GENE_URL_TEMPLATE,
    DEFAULT_UNIPROT_ID_URL_TEMPLATE,
    fetchVariantAnnotationsIndexedByGenomicLocation,
    getUrl,
    initGenomeNexusClient,
    initGenomeNexusInternalClient,
    initOncoKbClient,
    ONCOKB_DEFAULT_DATA,
} from '../util/DataFetcherUtils';
import { CanonicalMutationType } from 'cbioportal-frontend-commons';

export interface MutationMapperDataFetcherConfig {
    myGeneUrlTemplate?: string;
    uniprotIdUrlTemplate?: string;
    mutationAlignerUrlTemplate?: string;
    cachePostMethodsOnClients?: boolean;
    apiCacheLimit?: number;
    genomeNexusUrl?: string;
    oncoKbUrl?: string;
}

export class DefaultMutationMapperDataFetcher
    implements MutationMapperDataFetcher {
    public oncoKbClient: OncoKbAPI;
    public genomeNexusClient: GenomeNexusAPI;
    public genomeNexusInternalClient: GenomeNexusAPIInternal;

    constructor(
        private config: MutationMapperDataFetcherConfig,
        genomeNexusClient?: Partial<GenomeNexusAPI>,
        genomeNexusInternalClient?: Partial<GenomeNexusAPIInternal>,
        oncoKbClient?: Partial<OncoKbAPI>
    ) {
        this.genomeNexusClient =
            (genomeNexusClient as GenomeNexusAPI) ||
            initGenomeNexusClient(
                config.genomeNexusUrl,
                config.cachePostMethodsOnClients,
                config.apiCacheLimit
            );
        this.genomeNexusInternalClient =
            (genomeNexusInternalClient as GenomeNexusAPIInternal) ||
            initGenomeNexusInternalClient(
                config.genomeNexusUrl,
                config.cachePostMethodsOnClients,
                config.apiCacheLimit
            );
        this.oncoKbClient =
            (oncoKbClient as OncoKbAPI) ||
            initOncoKbClient(
                config.oncoKbUrl,
                config.cachePostMethodsOnClients,
                config.apiCacheLimit
            );
    }

    public async fetchSwissProtAccession(entrezGeneId: number): Promise<any> {
        const myGeneData: Response = await request.get(
            getUrl(
                this.config.myGeneUrlTemplate || DEFAULT_MY_GENE_URL_TEMPLATE,
                { entrezGeneId }
            )
        );
        return JSON.parse(myGeneData.text).uniprot['Swiss-Prot'];
    }

    public async fetchUniprotId(swissProtAccession: string): Promise<string> {
        const uniprotData: Response = await request.get(
            getUrl(
                this.config.uniprotIdUrlTemplate ||
                    DEFAULT_UNIPROT_ID_URL_TEMPLATE,
                { swissProtAccession }
            )
        );
        return uniprotData.text.split('\n')[1];
    }

    public async fetchPfamDomainData(
        pfamAccessions: string[],
        client: GenomeNexusAPI = this.genomeNexusClient
    ): Promise<PfamDomain[]> {
        return await client.fetchPfamDomainsByPfamAccessionPOST({
            pfamAccessions: pfamAccessions,
        });
    }

    public async fetchVariantAnnotationsIndexedByGenomicLocation(
        mutations: Partial<Mutation>[],
        fields: string[] = ['annotation_summary'],
        isoformOverrideSource: string = 'uniprot',
        client: GenomeNexusAPI = this.genomeNexusClient
    ): Promise<{ [genomicLocation: string]: VariantAnnotation }> {
        return await fetchVariantAnnotationsIndexedByGenomicLocation(
            mutations,
            fields,
            isoformOverrideSource,
            client
        );
    }

    public async fetchMyVariantInfoAnnotationsIndexedByGenomicLocation(
        mutations: Partial<Mutation>[],
        isoformOverrideSource: string = 'uniprot',
        client: GenomeNexusAPI = this.genomeNexusClient
    ): Promise<{ [genomicLocation: string]: MyVariantInfo }> {
        const indexedVariantAnnotations = await fetchVariantAnnotationsIndexedByGenomicLocation(
            mutations,
            ['my_variant_info'],
            isoformOverrideSource,<commit_msg>set mutationmapper default to mskcc transcripts<commit_after>import _ from 'lodash';
import request from 'superagent';
import Response = request.Response;

import {
    AggregatedHotspots,
    EvidenceType,
    generateProteinChangeQuery,
    generateAnnotateStructuralVariantQuery,
    getMyVariantInfoAnnotationsFromIndexedVariantAnnotations,
    IOncoKbData,
    Mutation,
    UniprotFeature,
    UniprotFeatureList,
    uniqueGenomicLocations,
    StructuralVariantType,
} from 'cbioportal-utils';
import {
    AnnotateMutationByProteinChangeQuery,
    AnnotateStructuralVariantQuery,
    CancerGene,
    OncoKbAPI,
    OncoKBInfo,
} from 'oncokb-ts-api-client';
import {
    EnsemblFilter,
    EnsemblTranscript,
    GenomeNexusAPI,
    GenomeNexusAPIInternal,
    GenomicLocation,
    Hotspot,
    PfamDomain,
    PostTranslationalModification,
    VariantAnnotation,
    MyVariantInfo,
} from 'genome-nexus-ts-api-client';

import { MutationMapperDataFetcher } from '../model/MutationMapperDataFetcher';
import {
    DEFAULT_MY_GENE_URL_TEMPLATE,
    DEFAULT_UNIPROT_ID_URL_TEMPLATE,
    fetchVariantAnnotationsIndexedByGenomicLocation,
    getUrl,
    initGenomeNexusClient,
    initGenomeNexusInternalClient,
    initOncoKbClient,
    ONCOKB_DEFAULT_DATA,
} from '../util/DataFetcherUtils';
import { CanonicalMutationType } from 'cbioportal-frontend-commons';

export interface MutationMapperDataFetcherConfig {
    myGeneUrlTemplate?: string;
    uniprotIdUrlTemplate?: string;
    mutationAlignerUrlTemplate?: string;
    cachePostMethodsOnClients?: boolean;
    apiCacheLimit?: number;
    genomeNexusUrl?: string;
    oncoKbUrl?: string;
}

export class DefaultMutationMapperDataFetcher
    implements MutationMapperDataFetcher {
    public oncoKbClient: OncoKbAPI;
    public genomeNexusClient: GenomeNexusAPI;
    public genomeNexusInternalClient: GenomeNexusAPIInternal;

    constructor(
        private config: MutationMapperDataFetcherConfig,
        genomeNexusClient?: Partial<GenomeNexusAPI>,
        genomeNexusInternalClient?: Partial<GenomeNexusAPIInternal>,
        oncoKbClient?: Partial<OncoKbAPI>
    ) {
        this.genomeNexusClient =
            (genomeNexusClient as GenomeNexusAPI) ||
            initGenomeNexusClient(
                config.genomeNexusUrl,
                config.cachePostMethodsOnClients,
                config.apiCacheLimit
            );
        this.genomeNexusInternalClient =
            (genomeNexusInternalClient as GenomeNexusAPIInternal) ||
            initGenomeNexusInternalClient(
                config.genomeNexusUrl,
                config.cachePostMethodsOnClients,
                config.apiCacheLimit
            );
        this.oncoKbClient =
            (oncoKbClient as OncoKbAPI) ||
            initOncoKbClient(
                config.oncoKbUrl,
                config.cachePostMethodsOnClients,
                config.apiCacheLimit
            );
    }

    public async fetchSwissProtAccession(entrezGeneId: number): Promise<any> {
        const myGeneData: Response = await request.get(
            getUrl(
                this.config.myGeneUrlTemplate || DEFAULT_MY_GENE_URL_TEMPLATE,
                { entrezGeneId }
            )
        );
        return JSON.parse(myGeneData.text).uniprot['Swiss-Prot'];
    }

    public async fetchUniprotId(swissProtAccession: string): Promise<string> {
        const uniprotData: Response = await request.get(
            getUrl(
                this.config.uniprotIdUrlTemplate ||
                    DEFAULT_UNIPROT_ID_URL_TEMPLATE,
                { swissProtAccession }
            )
        );
        return uniprotData.text.split('\n')[1];
    }

    public async fetchPfamDomainData(
        pfamAccessions: string[],
        client: GenomeNexusAPI = this.genomeNexusClient
    ): Promise<PfamDomain[]> {
        return await client.fetchPfamDomainsByPfamAccessionPOST({
            pfamAccessions: pfamAccessions,
        });
    }

    public async fetchVariantAnnotationsIndexedByGenomicLocation(
        mutations: Partial<Mutation>[],
        fields: string[] = ['annotation_summary'],
        isoformOverrideSource: string = 'mskcc',
        client: GenomeNexusAPI = this.genomeNexusClient
    ): Promise<{ [genomicLocation: string]: VariantAnnotation }> {
        return await fetchVariantAnnotationsIndexedByGenomicLocation(
            mutations,
            fields,
            isoformOverrideSource,
            client
        );
    }

    public async fetchMyVariantInfoAnnotationsIndexedByGenomicLocation(
        mutations: Partial<Mutation>[],
        isoformOverrideSource: string = 'uniprot',
        client: GenomeNexusAPI = this.genomeNexusClient
    ): Promise<{ [genomicLocation: string]: MyVariantInfo }> {
        const indexedVariantAnnotations = await fetchVariantAnnotationsIndexedByGenomicLocation(
            mutations,
            ['my_variant_info'],
            isoformOverrideSource,
{'fraction_non_alphanumeric': 0.07174706120794487, 'fraction_numerical': 0.00020267531414673692, 'mean_word_length': 2.8490639625585024, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '681816', 'n_tokens_mistral': 2969, 'n_tokens_neox': 2843, 'n_words': 448}