Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. /** START OF DATA SECTION */
  2.  
  3. const mutations = {
  4. "patientA": {
  5. // key is the location, value is the amount of mutations there
  6. "KRAS/10394954": 5,
  7. "KRAS/3958838": 20,
  8. "TP53/94959003": 10,
  9. "TP53/12931920": 2,
  10. "NRAS/399322": 75,
  11. },
  12. "patientB": {
  13. "KRAS/10394954": 31,
  14. "KRAS/3958838": 122,
  15. "TP53/94959003": 45,
  16. "TP53/12931920": 11,
  17. "NRAS/399322": 52,
  18. },
  19. "patientC": {
  20. "KRAS/10394954": 10,
  21. "KRAS/3958838": 74,
  22. "TP53/94959003": 4,
  23. "TP53/12931920": 0,
  24. "NRAS/399322": 39,
  25. "NRAS/19929330": 20,
  26. },
  27. "patientD": {
  28. "KRAS/10394954": 7,
  29. "KRAS/3958838": 3,
  30. "TP53/94959003": 11,
  31. "TP53/12931920": 92,
  32. "NRAS/399322": 0,
  33. "NRAS/19929330": 3,
  34. },
  35. "patientE": {
  36. "KRAS/10394954": 7,
  37. "KRAS/3958838": 3,
  38. "TP53/94959003": 11,
  39. "TP53/12931920": 92,
  40. "NRAS/399322": 0,
  41. "NRAS/19929330": 3,
  42. }
  43. }
  44.  
  45. const metadata = {
  46. "patientA": "cancer",
  47. "patientB": "cancer",
  48. "patientC": "control",
  49. "patientD": "cancer",
  50. "patientE": "control"
  51. };
  52.  
  53. /** END OF DATA SECTION */
  54.  
  55. function getSensitivity(plainPatients, overallCancerCount){
  56. const sortedPatients = sortPatients(plainPatients)
  57. console.log('getSensitivity:: sortedPatients', sortedPatients.map(p => p.pateintId));
  58. const topPatientsFromLeft = sortedPatients.slice(0, overallCancerCount);
  59. // = True positive (?)
  60. const subsetCancerCount = topPatientsFromLeft.filter(p => p.diagonse === 'cancer').length;
  61. console.log('getSensitivity:: First top pateints', topPatientsFromLeft);
  62. const sensitivity = subsetCancerCount / overallCancerCount;
  63. console.log('getSpecificity:: result value - ', sensitivity);
  64. return sensitivity;
  65. }
  66.  
  67. function getSpecificity(plainPatients, overallControlCounter){
  68. const sortedPatients = sortPatients(plainPatients);
  69. const patientsAmount = sortedPatients.length;
  70. const topPatientsFromRight = sortedPatients.slice(patientsAmount - overallControlCounter, patientsAmount + 1);
  71. // = True negative (?)
  72. const subControlsCount = topPatientsFromRight.filter(p => p.diagonse === "control").length;
  73. console.log('getSpecificity:: First top pateints', topPatientsFromRight);
  74. const specificity = subControlsCount / overallControlCounter;
  75. console.log('getSpecificity:: result value - ', specificity);
  76. return specificity;
  77. }
  78.  
  79. /**
  80. * DONE ?
  81. */
  82. function sumMutations(mutationsObj){
  83. const mutationsArr = Object.values(mutationsObj);
  84. return mutationsArr.reduce((sum, m) => m + sum, 0);
  85. }
  86.  
  87. /**
  88. * DONE ?
  89. */
  90. function sortPatients(patients){
  91. return patients.sort((p1, p2) => {
  92. const sum1 = sumMutations(p1.mutations);
  93. const sum2 = sumMutations(p2.mutations);
  94. return sum1 - sum2;
  95. });
  96. }
  97.  
  98. /**
  99. * Helper function for conveting given data to a more
  100. * cormftable format
  101. */
  102. function transformMutations(mutations){
  103. const trasformedResult = [];
  104. let tempDiagnose = null;
  105. for (let [key, value] of Object.entries(mutations)) {
  106. tempDiagnose = metadata[key];
  107. trasformedResult.push({
  108. pateintId: key,
  109. diagonse: tempDiagnose,
  110. mutations: {...value}
  111. });
  112. }
  113. return trasformedResult;
  114. }
  115.  
  116. function getIdealLocationsCombination(){
  117. const trasnformedArr = transformMutations(mutations);
  118. const locationsSet = new Set();
  119. let overallCancerCount = 0;
  120. let overallControlCounter = 0;
  121.  
  122. for(let p of trasnformedArr){
  123. for (let [key, value] of Object.entries(p.mutations)) {
  124. if(!locationsSet.has(key)){
  125. locationsSet.add(key);
  126. }
  127. }
  128. if(p.diagonse === 'cancer'){
  129. overallCancerCount++;
  130. } else {
  131. overallControlCounter++;
  132. }
  133. }
  134.  
  135. // Sorting locations in decending order according to thier occurence
  136. const sortedLocationBySum = Array.from(locationsSet).sort((loc1, loc2) => {
  137. const sum1 = trasnformedArr.reduce((sum, p) => sum + p.mutations[loc1],0);
  138. const sum2 = trasnformedArr.reduce((sum,p) => sum + p.mutations[loc2],0);
  139.  
  140. return sum1 - sum2;
  141. });
  142.  
  143. let updatedData = [...trasnformedArr];
  144. const exculdedLocations = [];
  145. let tSensitivity = 0;
  146. let tSpecificity = 0;
  147. let currentLocaiton = null;
  148.  
  149. let bestSensitivity = getSensitivity(updatedData, overallCancerCount);
  150. let bestSpecificity = getSpecificity(updatedData, overallControlCounter);
  151.  
  152. for(let i = 0; i < sortedLocationBySum.length; i++){
  153. currentLocation = sortedLocationBySum[i];
  154. for(let p of updatedData){
  155. delete p.mutations[currentLocation];
  156. }
  157.  
  158. console.log('getIdealLocationsCombination:: currentLocaiton', currentLocation);
  159. tSensitivity = getSensitivity(updatedData, overallCancerCount);
  160. console.log(`getIdealLocationsCombination:: sensitivity - ${tSensitivity}`);
  161. tSpecificity = getSpecificity(updatedData, overallControlCounter);
  162. console.log(`getIdealLocationsCombination:: specificity - ${tSpecificity}`);
  163.  
  164. if(i !== sortedLocationBySum.length - 1 && tSensitivity >= bestSensitivity && tSpecificity >= bestSpecificity){
  165. exculdedLocations.push(currentLocation);
  166. bestSensitivity = tSensitivity;
  167. bestSpecificity = tSpecificity;
  168. } else {
  169. break;
  170. }
  171. }
  172.  
  173.  
  174. console.log('Exculde this location to get the ideal resutls', exculdedLocations);
  175. return exculdedLocations;
  176. }
  177.  
  178. getIdealLocationsCombination();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement