import { Mutation } from '../model/Mutation' ; import { IMyCancerGenome , IMyCancerGenomeData } from '../model/MyCancerGenome' ; import myCancerGenomeJson from './mycancergenome.json' ; export function getMyCancerGenomeLinks ( mutation , myCancerGenomeData ) { const myCancerGenomes = mutation . gene && mutation . gene . hugoGeneSymbol ? myCancerGenomeData [ mutation . gene . hugoGeneSymbol ] : null ; let links = [ ] ; if ( myCancerGenomes ) { links = filterByAlteration ( mutation , myCancerGenomes ) . map ( ( myCancerGenome ) => myCancerGenome . linkHTML ) ; } return links ; } export function filterByAlteration ( mutation , myCancerGenomes ) { return myCancerGenomes . filter ( ( myCancerGenome ) => { const proteinChangeRegExp = /^[A-Za-z][0-9]+[A-Za-z]/ ; const numericalRegExp = /[0-9]+/ ; const matched = myCancerGenome . alteration . trim ( ) . match ( proteinChangeRegExp ) ; if ( matched && mutation . proteinChange ) { const mutationPos = mutation . proteinChange . match ( numericalRegExp ) ; const alterationPos = myCancerGenome . alteration . match ( numericalRegExp ) ; return ( mutationPos && alterationPos && mutationPos [ 0 ] === alterationPos [ 0 ] ) ; } return false ; } ) ; } /**
 * This is NOT a generic html anchor tag parser. Assuming all MyCancerGenome links have the same simple structure.
 * DOMParser can be used for generic parsing purposes, but it is not compatible with the current test environment.
 * (i.e: DOMParser unit tests always fail, but it works fine in the browser)
 */ export function parseMyCancerGenomeLink ( link ) { const hrefStart = link . indexOf ( '"' ) + 1 ; const hrefEnd = hrefStart + link . slice ( hrefStart ) . indexOf ( '"' ) ; const textStart = link . indexOf ( '>' ) + 1 ; const textEnd = link . indexOf ( '</a>' ) ; const href = link . slice ( hrefStart , hrefEnd ) . trim ( ) ; const text = link . slice ( textStart , textEnd ) . trim ( ) ; if ( href . length > 0 && text . length > 0 ) { return { url : href , text : text , } ; } else { return undefined ; } } export function getMyCancerGenomeData ( ) { const data = myCancerGenomeJson ; return geneToMyCancerGenome ( data ) ; } export function geneToMyCancerGenome ( myCancerGenomes ) { const map = { } ; myCancerGenomes . forEach ( ( myCancerGenome ) => { if ( ! ( myCancerGenome . hugoGeneSymbol in map ) ) { map [ myCancerGenome . hugoGeneSymbol ] = [ ] ; } map [ myCancerGenome . hugoGeneSymbol ] . push ( myCancerGenome ) ; } ) ; return map ; }