Diff of /Analysis_2.R [000000] .. [b8d756]

Switch to unified view

a b/Analysis_2.R
1
library(openxlsx)
2
library(plyr)
3
library(dplyr)
4
5
pt = read.xlsx("MEGA PT Data for Lu%2c August 21%2c 2017.xlsx", sheet = 1)
6
pt$mutID = paste(pt$Chrom, pt$Position,pt$BaseFrom,pt$BaseTo)
7
pt$Matched.Plasma = as.character(pt$Matched.Plasma)
8
9
10
cancer = c("CRC","Lung","Breast","Pancreas","Ovarian","Esophagus","Liver","Stomach","Small Intestine","Gastric","Ovary","pancreas")
11
12
uidThr=200
13
## threshold for omega value
14
thres = 1.9
15
16
final_cv = data.frame()
17
fp_cv = data.frame()
18
19
for(m in 1:10){
20
  allResults = c()
21
  cv = matrix(0, nrow = 10, ncol = 3)
22
  colnames(cv) = c("FP","Sens", "Con")
23
  
24
  for(c in 1:10){
25
    
26
    load(paste0("PvalueRatio1209cv_5perc_bl_",m,"_", c,"_1208data.rda"))
27
28
    result$fail = (result$UID1<uidThr)+(result$UID2<uidThr)+(result$UID3<uidThr)+
29
      (result$UID4<uidThr)+(result$UID5<uidThr)+(result$UID6<uidThr)
30
    result = result[result$fail<=2,]
31
    
32
    ## difference between average MAF in the test and the max MAF in the normal controls
33
    result$diff = result$aveMAF-result$max
34
    result$diff_r = (result$aveMAF-result$max)/(result$max+10e-6)
35
    result$PT.Avg.MAF[is.na(result$PT.Avg.MAF)] = -1
36
    result$PT.Avg.MAF = as.numeric(result$PT.Avg.MAF)
37
    
38
    ratio = matrix(c(result$r1, result$r2,result$r3,result$r4, result$r5,result$r6), nrow = nrow(result), ncol = 6)
39
    uid = matrix(c(result$UID1,result$UID2, result$UID3,result$UID4,result$UID5,result$UID6), nrow = nrow(result), ncol = 6)
40
    
41
    order.ratio = t(apply(ratio, 1, order))
42
    
43
    for(i in 1:nrow(ratio)){
44
      ratio[i,] = ratio[i,][order.ratio[i,]]
45
      uid[i,] = uid[i,][order.ratio[i,]]
46
    }
47
    
48
    uid[uid<uidThr] = 0
49
    
50
    ## eliminate the min and max wells in the testSet
51
    result$omega = rowSums(log(ratio[,-c(1,6)])*uid[,-c(1,6)])/rowSums(uid[,-c(1,6)])
52
53
    result$omega[is.na(result$omega)] = Inf
54
    result$class = FALSE
55
    result$class = (result$omega>=thres)
56
    result$iteration = m
57
    result$fold = c
58
    ## normal plasmas in the testSet
59
    nltemp = setdiff(nlpls,nlt)
60
    
61
    ## summarizing results
62
    summ = ddply(result, .(Template,Sample.Category), summarise, PT = max(PT.Avg.MAF*class), class = max(class))
63
    
64
    cv[c,1] = sum(summ$class[summ$Template %in% nltemp])/length(nltemp)
65
    cv[c,2] = sum(summ$class[summ$Sample.Category %in% cancer])/nrow(summ[summ$Sample.Category %in% cancer,])
66
    
67
    check = summ[summ$class==TRUE & (summ$Sample.Category %in% cancer) & (summ$Template %in% pt$Matched.Plasma),]
68
    cv[c,3] = sum(check$PT>=1)/sum(check$PT>=0)
69
    
70
    allResults = rbind(allResults,result)
71
    
72
  }
73
  save(allResults, file = paste0("allResults_fromLuMethod_",m,"_20171209.rda"))
74
75
  print(m)
76
  cv = data.frame(cv)
77
  final_cv = rbind.data.frame(final_cv,cv)
78
}
79
80
81
82
sampleTable_ALL = data.frame()
83
for(m in 1:10){
84
  load(file = paste0("allResults_fromLuMethod_",m,"_20171209.rda"))
85
  
86
  # subset by sample omega values and mutations
87
  sampleOmegaList = tapply(allResults[,'omega'], INDEX = allResults[,'Template'], FUN = function(x)return(x))
88
  sampleMutList = tapply(allResults[,'mutID'], INDEX = allResults[,'Template'], FUN = function(x)return(x))
89
  sampleCosmicList = tapply(allResults[,'CosmicCount'], INDEX = allResults[,'Template'], FUN = function(x)return(x))
90
  sampleGeneList = tapply(allResults[,'Gene'], INDEX = allResults[,'Template'], FUN = function(x)return(x))
91
  sampleAmpList = tapply(allResults[,"ampMatchName"], INDEX = allResults[,'Template'], FUN = function(x)return(x))
92
  
93
  # find max value
94
  sampleMaxS = unlist(tapply(allResults[,'omega'], INDEX = allResults[,'Template'], FUN = which.max, simplify = T))
95
  
96
  maxS = sapply(names(sampleMaxS), function(i) sampleOmegaList[[i]][sampleMaxS[i]])
97
  maxSmut = sapply(names(sampleMaxS), function(i) sampleMutList[[i]][sampleMaxS[i]])
98
  cosm = sapply(names(sampleMaxS), function(i) sampleCosmicList[[i]][sampleMaxS[i]])
99
  gene = sapply(names(sampleMaxS), function(i) sampleGeneList[[i]][sampleMaxS[i]])
100
  amp = sapply(names(sampleMaxS), function(i) sampleAmpList[[i]][sampleMaxS[i]])
101
  
102
  
103
  sampleTable = allResults[,c('Template','Sample.Category',"iteration","fold")]
104
  sampleTable = sampleTable[!duplicated(sampleTable[,1]),]
105
  rownames(sampleTable) = sampleTable[,1]
106
  sampleTable = cbind(sampleTable,maxOmega = maxS[rownames(sampleTable)], maxO_mut = maxSmut[rownames(sampleTable)],
107
                      CosmicCount = cosm[rownames(sampleTable)], gene = gene[rownames(sampleTable)], ampMatchName = amp[rownames(sampleTable)])
108
  
109
  sampleTable$remove = paste(sampleTable$Template,sampleTable$maxO_mut)
110
  sampleTable = sampleTable[,-1]
111
  
112
  uid_matrix = allResults[,c(1:51)]
113
  uid_matrix_subset = uid_matrix[match(sampleTable$remove,uid_matrix$remove),]
114
  
115
  sampleTable = cbind(sampleTable,uid_matrix_subset)
116
  
117
  #save(sampleTable, file = 'omegaPerSample_20171019.rda')
118
  
119
  # add maximum diff and diff_r per sample
120
  diff = unlist(tapply(allResults[,'diff'], INDEX = allResults[,'Template'], FUN = max, na.rm = T, simplify = T))
121
  diff_r = unlist(tapply(allResults[,'diff_r'], INDEX = allResults[,'Template'], FUN = max, na.rm = T, simplify = T))
122
  sampleTable_diff = cbind(sampleTable,maxDiff = diff[rownames(sampleTable)],maxDiff_r = diff_r[rownames(sampleTable)])
123
  sampleTable_ALL = rbind.data.frame(sampleTable_ALL, sampleTable_diff)
124
}
125
save(sampleTable_ALL, file = 'maxValuesPerSample_20171209_FORJosh.rda')
126
127