import _ from 'lodash' ; import { GenomicLocation } from 'genome-nexus-ts-api-client' ; import { Mutation } from '../model/Mutation' ; export function countMutationsByProteinChange ( mutations ) { const mutationsByProteinChange = _ . groupBy ( mutations , 'proteinChange' ) ; const mutationCountsByProteinChange = _ . map ( mutationsByProteinChange , mutations => ( { proteinChange : mutations [ 0 ] . proteinChange , count : mutations . length , } ) ) ; return _ . orderBy ( mutationCountsByProteinChange , [ 'count' , 'proteinChange' ] , [ 'desc' , 'asc' ] ) ; } export function groupMutationsByProteinStartPos < T extends Mutation > ( mutations ) { const map = { } ; for ( const mutation of mutations ) { const codon = mutation . proteinPosStart ; if ( codon !== undefined && codon !== null ) { map [ codon ] = map [ codon ] || [ ] ; map [ codon ] . push ( mutation ) ; } } return map ; } function findChromosome ( mutation ) { return mutation . chromosome || mutation . chr ; } export function extractGenomicLocation ( mutation ) { const chromosome = findChromosome ( mutation ) ; if ( chromosome && mutation . startPosition && mutation . endPosition && mutation . referenceAllele && mutation . variantAllele ) { return { chromosome : chromosome . replace ( 'chr' , '' ) , start : mutation . startPosition , end : mutation . endPosition , referenceAllele : mutation . referenceAllele , variantAllele : mutation . variantAllele , } ; } else { return undefined ; } } export function generateHgvsgByMutation ( mutation ) { if ( hasValidGenomicLocation ( mutation ) ) { const chromosome = findChromosome ( mutation ) ; if ( mutation . referenceAllele === '-' ) { return `${ chromosome }:g.${ mutation . startPosition }_${ mutation . endPosition }ins${ mutation . variantAllele }` ; } else if ( mutation . variantAllele === '-' ) { if ( mutation . startPosition === mutation . endPosition ) { return `${ chromosome }:g.${ mutation . startPosition }del` ; } return `${ chromosome }:g.${ mutation . startPosition }_${ mutation . endPosition }del` ; } else if ( mutation . referenceAllele ! . length === 1 && mutation . variantAllele ! . length === 1 ) { return `${ chromosome }:g.${ mutation . startPosition }${ mutation . referenceAllele }>${ mutation . variantAllele }` ; } else if ( mutation . referenceAllele ! . length === 1 && mutation . variantAllele ! . length > 1 ) { return `${ chromosome }:g.${ mutation . startPosition }delins${ mutation . variantAllele }` ; } else { return `${ chromosome }:g.${ mutation . startPosition }_${ mutation . endPosition }delins${ mutation . variantAllele }` ; } } return undefined ; } export function hasValidGenomicLocation ( mutation ) { if ( findChromosome ( mutation ) && mutation . startPosition && mutation . startPosition !== - 1 && mutation . endPosition && mutation . endPosition !== - 1 && mutation . referenceAllele && mutation . referenceAllele !== 'NA' && mutation . variantAllele && mutation . variantAllele !== 'NA' ) { if ( mutation . referenceAllele === '-' && mutation . variantAllele === '-' ) { return false ; } return true ; } return false ; } export function genomicLocationString ( genomicLocation ) { return `${ genomicLocation . chromosome },${ genomicLocation . start },${ genomicLocation . end },${ genomicLocation . referenceAllele },${ genomicLocation . variantAllele }` ; } export function uniqueGenomicLocations ( mutations ) { const genomicLocationMap = { } ; mutations . map ( ( mutation ) => { const genomicLocation = extractGenomicLocation ( mutation ) ; if ( genomicLocation ) { genomicLocationMap [ genomicLocationString ( genomicLocation ) ] = genomicLocation ; } } ) ; return _ . values ( genomicLocationMap ) ; } export function normalizeChromosome ( chromosome ) { switch ( chromosome ) { case '23' : { return 'X' ; } case '24' : { return 'Y' ; } default : { return chromosome ; } } }