Reputation: 869
I would like to calculate the distance between the markers (Name
) in a given chromosome (Chr
). The objects dist1.alldown
(distance downstream) and dist1.allup
(distance upstream) have exactly what I want. However, the below script is computationally inefficient (my real data could contain a milion of markers and this loop is time consuming).
df <- 'Name Chr Position
GGaluGA001820 chr1 34388
Gga_rs16686671 chr1 67781
GGaluGA001841 chr1 80477
Gga_rs15995401 chr1 111556
Gga_rs15995393 chr1 112481
GGaluGA001890 chr1 149690
GGaluGA001902 chr1 176450
Gga_rs14688751 chr1 185573
GGaluGA001921 chr1 202425
GGaluGA001945 chr1 235155'
df <- read.table(text=df, header=T)
probes <- df
probes.split <- split(probes, probes$Chr)
####### Loop to infer distance upstream #####
{dist1.all <- NULL
for(k in 1:length(probes.split)){
probescx <- probes.split[[k]]
probescx <- probescx[order(probescx$Position, decreasing=F),]
for(i in 1:nrow(probescx)){
v <- vector()
v[k] <- k^2; print(paste(k,i))
rowx <- probescx[i,]
rowxm1 <- probescx[i-1,]
if(nrow(rowxm1) > 0){
lab <- rowx[1,1:2]
dist1 <- rowx[1,3] - rowxm1[1,3]
dist1 <- as.data.frame(dist1)
dist1 <- cbind(lab, dist1)
dist1.all <- rbind(dist1.all, dist1)
}
}
}
}
### Save a different object
dist1.allup <- dist1.all
##background of up object
dist1.allupback <- dist1.allup
### Loop to infer distance downstream
{dist1.all <- NULL
for(k in 1:length(probes.split)){
probescx <- probes.split[[k]]
probescx <- probescx[order(probescx$Position, decreasing=F),]
for(i in 1:nrow(probescx)){
v <- vector()
v[k] <- k^2; print(paste(k,i))
rowx <- probescx[i,]
rowxm1 <- probescx[i+1,]
if(nrow(rowxm1) > 0){
lab <- rowx[1,1:2]
dist1 <- rowx[1,3] - rowxm1[1,3]
dist1 <- as.data.frame(dist1)
dist1 <- cbind(lab, dist1)
dist1.all <- rbind(dist1.all, dist1)
}
}
}
}
### Save a different object
dist1.alldown <- dist1.all
##background of down object
dist1.alldownback <- dist1.alldown
## Turn distance in positive integers
dist1.alldown$dist1 <- dist1.alldown$dist1 * -1
Some ideas or known tools to obtain an efficiently approach? Thank you!
Upvotes: 2
Views: 58
Reputation: 6372
Lets simplify your data a bit. You have:
> df
Name Chr Position
1 GGaluGA001820 chr1 34388
2 Gga_rs16686671 chr1 67781
3 GGaluGA001841 chr1 80477
4 Gga_rs15995401 chr1 111556
5 Gga_rs15995393 chr1 112481
6 GGaluGA001890 chr1 149690
7 GGaluGA001902 chr1 176450
8 Gga_rs14688751 chr1 185573
9 GGaluGA001921 chr1 202425
10 GGaluGA001945 chr1 235155
Based on
> dist1.allup
Name Chr dist1
2 Gga_rs16686671 chr1 33393
3 GGaluGA001841 chr1 12696
4 Gga_rs15995401 chr1 31079
5 Gga_rs15995393 chr1 925
6 GGaluGA001890 chr1 37209
7 GGaluGA001902 chr1 26760
8 Gga_rs14688751 chr1 9123
9 GGaluGA001921 chr1 16852
10 GGaluGA001945 chr1 32730
You're looking for the rowwise distance between markers (I.e. GGalu -> Gga_rs, Gga_rs -> GGalu).
The most straightforward way to do this (and very computationally quick) would be with data.table
.
First, set to a data table
library(data.table)
setDT(df)
Then, order your data so that you have consecutive markers (your data may already be like this, but good to make sure:
df <- df[order(Chr,Position)]
Then, create offsetting data for Chr, Name and Position:
df[, ChrN := Chr[.I + 1]]
df[, NameN := Name[.I + 1]]
df[, PosN := Position[.I + 1]]
We only want to compare on the same chromosome:
df <- df[Chr == ChrN]
And now we can calculate the distances
df[, list(NameFrom = Name, NameTo = NameN, Chr, dist = PosN - Position)]
As this is vectorized, and uses in memory operations, it should be much quicker than the looping approach above.
For all.down, use:
df <- df[-order(Chr,Position)]
and
df[, list(NameFrom = Name, NameTo = NameN, Chr, dist = PosN - Position)]
becomes
df[, list(NameFrom = Name, NameTo = NameN, Chr, dist = Position - PosN)]
Upvotes: 1