-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmonte3.R
268 lines (254 loc) · 10.9 KB
/
nmonte3.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#*******************************************************
# R script nMonte.R
# Find mixture composition which minimizes
# the averaged genetic distance to target.
# Penalizing of distant admixtures.
# Activate with: source('nMonte3_temp.R')
# Use: getMonte(datafile, targetfile, pen=0.01);
# both files should be comma-separated csv.
# Utilities:
# subset_data(): Collecting rows from datasheet
# aggr_pops(): Average populations
# tab2comma(): tab-separated to comma-separated
# last modified: headStrings
# v10.4 Huijbregts 8 jan 2018
# updated comments tipirneni
# updated added proxy lib added options for comparision tipirneni
#*******************************************************
library(proxy)
# default global constants
batch_def = 500 # default rows of sample randomly drawn from data file
cycles_def = 1000 # default number of cycles
pen_def = 0.009 # default penalty
# START OF GETMONTE FUNCTION
getMonte <- function(datafile,targetfile,
omit='',Nbatch=batch_def,Ncycles=cycles_def,save=F,pen=pen_def) {
# define AlGORITHM embedded function
do_algorithm <- function(selection, targ) {
mySel <- as.matrix(selection, rownames.force = NA)
myTg <- as.matrix(targ, rownames.force = NA)
dif2targ <- sweep(mySel, 2, myTg, '-') # data - target
Ndata <- nrow(dif2targ)
kcol <- ncol(dif2targ)
rowLabels <- rownames(dif2targ)
print(paste('dimension dif2targ=',dim(dif2targ),sep=' '))
write('',stdout())
# preallocate data
matPop <- matrix(NA_integer_, Nbatch, 1, byrow=T)
dumPop <- matrix(NA_integer_, Nbatch, 1, byrow=T)
matAdmix <- matrix(NA_real_, Nbatch, kcol, byrow=T)
dumAdmix <- matrix(NA_real_, Nbatch, kcol, byrow=T)
print(paste('dimension matadmin=',dim(matAdmix),sep=' '))
write('',stdout())
print(paste('dimension matpop=',dim(matPop),sep=' '))
write('',stdout())
matPop <- sample(1:Ndata,Nbatch,replace=T)
#print(paste('dimension matpop=',dim(matPop),sep=' '))
write('',stdout())
# fill matPop with random row numbers 1:Ndata from datafile
matAdmix <- dif2targ[matPop,]
# print(paste('dimension matadmin=',dim(matAdmix),sep=' '))
write('',stdout())
# iniatialize objective function
colM1 <- colMeans(matAdmix)
eval1 <- (1+pen) * sum(colM1^2)
# Ncycles iterations
for (c in 1:Ncycles) {
# fill batch data
dumPop <- sample(1:Ndata, Nbatch, replace=T)
dumAdmix <- dif2targ[dumPop,]
# loop thru batch
# penalty is squared distance of sample to target
# objective function =
# squared dist of batch mean to target + coef*penalty
# minimize objective function
for (b in 1:Nbatch) {
# test alternative pop
store <- matAdmix[b,]
matAdmix[b,] <- dumAdmix[b,]
#print(paste('dimension b=',store,sep=' _'))
#print(b)
#print(paste('\tdimension matb at =',matAdmix[b,],sep=' - '))
colM2 <- colMeans(matAdmix)
# check delta value of this batch number row vs colmeans of diff matrix, if lower use it
# TODO add neg penalty for uneven matches
eval2 <- sum(colM2^2) + pen*sum(matAdmix[b, ]^2)
# conditional adjust
if (eval2 <= eval1) {
matPop[b] <- dumPop[b]
colM1 <- colM2
eval1 <- eval2
} else {matAdmix[b,] <- store}
} # end batch
} # end cycles
# Collect output
# get fit of target
fitted <- t(colMeans(matAdmix) + myTg[1,])
# collect sampled populations
# split labels of reference samples
popl <- headStrings(rowLabels[matPop], mySep=':')
populations <- factor(popl)
#pop2 <- headStrings2(rowLabels[matPop], mySep=':')
pop2pop <- unique(rowLabels[matPop])
# return list of 2 objects
return(list('estimated'=fitted, 'pops'=populations, 'popsval'=pop2pop))
} # end do_algorithm
# define OUTPUT embedded function
# except pop correlations
do_output <- function(estim, pops){
# set stdOut to sinkFile
if (save!=F) {
sinkFile <- nameIsFree(save)
sink(sinkFile, append=T, split=T)
}
print(paste('penalty=',pen,sep=' '))
print(paste('Ncycles=',Ncycles,sep=' '))
# print target and estimation by col
dif <- estim - myTarget
matrix_out <- rbind(myTarget, estim, dif)
rownames(matrix_out)[2:3] <- c('fitted','dif')
print(matrix_out)
# distance
dist1_2 <- sqrt(sum(dif^2))
dist1_2 <- dist1_2/PCT
print(paste('distance%=',round(100*dist1_2,4),sep=''))
write('',stdout())
# table percentages by pop
tgname <- row.names(myTarget)[1]
write(paste('\t',tgname),stdout())
write('',stdout())
tb <- table(pops)
tb <- tb[order(tb, decreasing=T)]
tb <- as.matrix(100*tb/Nbatch)
write.table(tb,sep=',',quote=F,col.names=F,dec='.')
# reset sinkFile to stdOut
if (save!=F) {sink()}
} # end do_output
# MAIN code of getMonte
# set environment for embedded functions
# proces input
tempData <- read.csv(datafile, head=T, row.names=1, stringsAsFactors=T, na.strings=c('',' ','NA'))
myData <- tempData[rownames(tempData)!=omit,]
myTarget <- read.csv(targetfile, head=T, row.names=1)
check_formats(myData, myTarget)
check_omit(myData, omit) # single item distances
PCT <- ifelse(max(myData[1, ]>2), 100, 1)
print('1. CLOSEST SINGLE ITEM DISTANCE% canberra')
print(nearestItems(myData, myTarget))
cat('\n')
# full table nMonte
print('2. FULL TABLE nMONTE')
results <- do_algorithm(myData, myTarget)
fitted <- results$estimated
populations <- results$pops
cat('- Nearest populations -\n')
print(paste(results$popsval, collapse=" "))
cat('- - -\n')
do_output(fitted, populations)
cat('\n')
#print('CORRELATION OF ADMIXTURE POPULATIONS')
#tb <- table(populations)
#selFinal <- names(tb[tb>0])
#adFinal <- myData[selFinal,,drop=F]
## catch error
#if (nrow(adFinal)==1) {print('Only 1 population, no correlations.')}
#else {
# corPops <- cor(t(adFinal))
# round(corPops, digits=2)
#}
} # end getMonte function
#===================================utilities===================================
#-------------------------------------------------------------------------------
# function subset_data()
# utility for selecting rows from datasheet
# Use: subset_data('DavidMadeThis.csv', 'IselectedThis.csv' ,'Abkhasian', 'Adygei', 'Afanasievo', 'Altai_IA')
# In: name of primary dataFile, name of output file, list of selected pops from primary datafile
# Out: secondary datasheet with selected subset
# USE WITH ONE SELECTED POPULATION TO CREATE TARGET FILE
# Error message 1: non-existence or misspelled name of selected pop
# Error message 2: no pops selected
# Error message 3: output file exists, choose new name
#-------------------------------------------------------------------------------
subset_data <- function(dataFile, saveFile, ...) {
input <- read.csv(dataFile, head=T, row.names=1, stringsAsFactor=F)
selection <- list(...)
selError <- selection[!selection %in% rownames(input)]
# test selection
if (length(selError)>0) {
cat(paste(selError,' is not a valid rowname\n',sep='')) }
# output
else {output <- input[rownames(input) %in% selection,]
print(output) # print to screen
write.csv(output, nameIsFree(saveFile), quote=F) # save to file
}
}
#-------------------------------------------------------------------------------
# function aggr_pops()
# In the files of Davidski rowlabel has the form 'pop:ID'
# This function drops the part after the colon
# and collects the mean of the pop before the colon.
# Use for mean: aggr_pops(fileName)
# Use for median: aggr_pops(fileName, myFunc=median)
#-------------------------------------------------------------------------------
aggr_pops <- function(fileName, myFunc=mean) {
myData <- read.csv(fileName, head=T, row.names=1, stringsAsFactors=FALSE)
splitted <- headStrings(rownames(myData), mySep=':')
aggrData <- aggregate(myData, by=list(splitted), myFunc)
temp <- as.matrix(aggrData[,-1]); rownames(temp) <- aggrData[,1]
return(round(temp, 7))
}
#-------------------------------------------------------------------------------
# function tab2comma()
# Convert tab-separated csv to comma-separated csv
# Use: tab2comma(tabFile,commaFile)
#-------------------------------------------------------------------------------
tab2comma <- function(tabFile,commaFile) {
data <- read.csv(tabFile, head=T, sep='\t', row.names=1, stringsAsFactor=F)
nameIsFree(commaFile)
write.csv(data, commaFile, row.names=T)
}
#-------------------------------------------------------------------------------
# function nearestItems()
# Find n best matches.
# Use: inData <- read file; inTarget <- read target
# nearestItems(inData, inTarget, maxFits=8)
# This is not the nearest neighbor algorithm;
# when the number of items is smaller than maxFits,
# functions returns all the items.
# euclidean
#-------------------------------------------------------------------------------
nearestItems <- function(inData, inTarget, maxFits=200) {
totArr <- rbind(inTarget, inData)
distMat <- as.matrix(dist(totArr, method='canberra'))#canberra soergel euclidean
dist1 <- distMat[,1]
sortDist <- dist1[order(dist1)]
nFits <- min(nrow(inData), maxFits)
return(sortDist[2:(nFits+1)])
}
#==================================internal stuff===============================
# split head from vector of strings, out vector of heads
headStrings <- function(strVec, mySep=':') {
unlist(lapply(strsplit(strVec, mySep), function(x) x[1]))
}
headStrings2 <- function(strVec, mySep=':') {
unlist(lapply(strsplit(strVec, mySep), function(x) x[2]))
}
nameIsFree <- function(newFile) {
while (file.exists(newFile)) {
newFile <- readline('select new filename for saving (without quotes): ')
}
return(newFile)
}
check_formats <- function(sheet, target) {
if (any(is.na(sheet))) {err_row <- as.integer(which(rowSums(is.na(sheet))>0))
print(sheet[err_row, ])
stop(paste('Missing value in row ',err_row))}
if (!identical(colnames(sheet), colnames(target)))
{print(colnames(sheet)); print(colnames(target))
stop('Colnames input not identical')}
}
check_omit <- function(sheet, dropInfo) {
if (dropInfo != '' & !dropInfo %in% rownames(sheet)) {
print('!!!! WARNING: Request to omit unknown popName. !!!!')
}
}