Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** START OF DATA SECTION */
- const mutations = {
- "patientA": {
- // key is the location, value is the amount of mutations there
- "KRAS/10394954": 5,
- "KRAS/3958838": 20,
- "TP53/94959003": 10,
- "TP53/12931920": 2,
- "NRAS/399322": 75,
- },
- "patientB": {
- "KRAS/10394954": 31,
- "KRAS/3958838": 122,
- "TP53/94959003": 45,
- "TP53/12931920": 11,
- "NRAS/399322": 52,
- },
- "patientC": {
- "KRAS/10394954": 10,
- "KRAS/3958838": 74,
- "TP53/94959003": 4,
- "TP53/12931920": 0,
- "NRAS/399322": 39,
- "NRAS/19929330": 20,
- },
- "patientD": {
- "KRAS/10394954": 7,
- "KRAS/3958838": 3,
- "TP53/94959003": 11,
- "TP53/12931920": 92,
- "NRAS/399322": 0,
- "NRAS/19929330": 3,
- },
- "patientE": {
- "KRAS/10394954": 7,
- "KRAS/3958838": 3,
- "TP53/94959003": 11,
- "TP53/12931920": 92,
- "NRAS/399322": 0,
- "NRAS/19929330": 3,
- }
- }
- const metadata = {
- "patientA": "cancer",
- "patientB": "cancer",
- "patientC": "control",
- "patientD": "cancer",
- "patientE": "control"
- };
- /** END OF DATA SECTION */
- function getSensitivity(plainPatients, overallCancerCount){
- const sortedPatients = sortPatients(plainPatients)
- console.log('getSensitivity:: sortedPatients', sortedPatients.map(p => p.pateintId));
- const topPatientsFromLeft = sortedPatients.slice(0, overallCancerCount);
- // = True positive (?)
- const subsetCancerCount = topPatientsFromLeft.filter(p => p.diagonse === 'cancer').length;
- console.log('getSensitivity:: First top pateints', topPatientsFromLeft);
- const sensitivity = subsetCancerCount / overallCancerCount;
- console.log('getSpecificity:: result value - ', sensitivity);
- return sensitivity;
- }
- function getSpecificity(plainPatients, overallControlCounter){
- const sortedPatients = sortPatients(plainPatients);
- const patientsAmount = sortedPatients.length;
- const topPatientsFromRight = sortedPatients.slice(patientsAmount - overallControlCounter, patientsAmount + 1);
- // = True negative (?)
- const subControlsCount = topPatientsFromRight.filter(p => p.diagonse === "control").length;
- console.log('getSpecificity:: First top pateints', topPatientsFromRight);
- const specificity = subControlsCount / overallControlCounter;
- console.log('getSpecificity:: result value - ', specificity);
- return specificity;
- }
- /**
- * DONE ?
- */
- function sumMutations(mutationsObj){
- const mutationsArr = Object.values(mutationsObj);
- return mutationsArr.reduce((sum, m) => m + sum, 0);
- }
- /**
- * DONE ?
- */
- function sortPatients(patients){
- return patients.sort((p1, p2) => {
- const sum1 = sumMutations(p1.mutations);
- const sum2 = sumMutations(p2.mutations);
- return sum1 - sum2;
- });
- }
- /**
- * Helper function for conveting given data to a more
- * cormftable format
- */
- function transformMutations(mutations){
- const trasformedResult = [];
- let tempDiagnose = null;
- for (let [key, value] of Object.entries(mutations)) {
- tempDiagnose = metadata[key];
- trasformedResult.push({
- pateintId: key,
- diagonse: tempDiagnose,
- mutations: {...value}
- });
- }
- return trasformedResult;
- }
- function getIdealLocationsCombination(){
- const trasnformedArr = transformMutations(mutations);
- const locationsSet = new Set();
- let overallCancerCount = 0;
- let overallControlCounter = 0;
- for(let p of trasnformedArr){
- for (let [key, value] of Object.entries(p.mutations)) {
- if(!locationsSet.has(key)){
- locationsSet.add(key);
- }
- }
- if(p.diagonse === 'cancer'){
- overallCancerCount++;
- } else {
- overallControlCounter++;
- }
- }
- // Sorting locations in decending order according to thier occurence
- const sortedLocationBySum = Array.from(locationsSet).sort((loc1, loc2) => {
- const sum1 = trasnformedArr.reduce((sum, p) => sum + p.mutations[loc1],0);
- const sum2 = trasnformedArr.reduce((sum,p) => sum + p.mutations[loc2],0);
- return sum1 - sum2;
- });
- let updatedData = [...trasnformedArr];
- const exculdedLocations = [];
- let tSensitivity = 0;
- let tSpecificity = 0;
- let currentLocaiton = null;
- let bestSensitivity = getSensitivity(updatedData, overallCancerCount);
- let bestSpecificity = getSpecificity(updatedData, overallControlCounter);
- for(let i = 0; i < sortedLocationBySum.length; i++){
- currentLocation = sortedLocationBySum[i];
- for(let p of updatedData){
- delete p.mutations[currentLocation];
- }
- console.log('getIdealLocationsCombination:: currentLocaiton', currentLocation);
- tSensitivity = getSensitivity(updatedData, overallCancerCount);
- console.log(`getIdealLocationsCombination:: sensitivity - ${tSensitivity}`);
- tSpecificity = getSpecificity(updatedData, overallControlCounter);
- console.log(`getIdealLocationsCombination:: specificity - ${tSpecificity}`);
- if(i !== sortedLocationBySum.length - 1 && tSensitivity >= bestSensitivity && tSpecificity >= bestSpecificity){
- exculdedLocations.push(currentLocation);
- bestSensitivity = tSensitivity;
- bestSpecificity = tSpecificity;
- } else {
- break;
- }
- }
- console.log('Exculde this location to get the ideal resutls', exculdedLocations);
- return exculdedLocations;
- }
- getIdealLocationsCombination();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement