haplotype_distance_between_within_populations_and_groups
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
haplotype_distance_between_within_populations_and_groups [2008/01/22 11:34] – created heidi | haplotype_distance_between_within_populations_and_groups [2008/07/22 13:31] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 2: | Line 2: | ||
* file: matrix_multiplePlots.r | * file: matrix_multiplePlots.r | ||
* document page: 25 | * document page: 25 | ||
+ | |||
+ | \\ | ||
+ | **code:** | ||
+ | < | ||
+ | #----read data------------------------------------------------------------------ | ||
+ | #----read haplotype list---- | ||
+ | Data <- read.table(" | ||
+ | |||
+ | Row <- nrow(Data) | ||
+ | Columns <- Row | ||
+ | |||
+ | |||
+ | #----read data row by row:---- | ||
+ | x <- 0 | ||
+ | n <- 1 | ||
+ | |||
+ | DistanceMatrix <- as.matrix(scan(" | ||
+ | HapDistanceMatrix_betweenPop.txt", | ||
+ | DistanceMatrix <- cbind(DistanceMatrix, | ||
+ | DistanceMatrix <- DistanceMatrix[, | ||
+ | |||
+ | n <- n + 1 | ||
+ | x <- x + 1 | ||
+ | |||
+ | while(n< | ||
+ | nextrow <- as.matrix(scan(" | ||
+ | | ||
+ | nextrow <- cbind(t(nextrow), | ||
+ | nextrow <- nextrow[, | ||
+ | |||
+ | DistanceMatrix <- rbind(DistanceMatrix, | ||
+ | |||
+ | n <- n + 1 | ||
+ | x <- x + 1 | ||
+ | } | ||
+ | |||
+ | |||
+ | #----select the data of DistanceMatrix------------------------------------------ | ||
+ | dimnames(DistanceMatrix) <- list(Data[, | ||
+ | |||
+ | # | ||
+ | Pop1 <- read.table(" | ||
+ | Pop1 <- as.character(Pop1[, | ||
+ | DistanceMatrixPop1 <- DistanceMatrix[Pop1, | ||
+ | |||
+ | # | ||
+ | Pop2 <- read.table(" | ||
+ | Pop2 <- as.character(Pop2[, | ||
+ | DistanceMatrixPop2 <- DistanceMatrix[Pop2, | ||
+ | |||
+ | #--between population1/ | ||
+ | #whole DistanceMatrix | ||
+ | wholeDistanceMatrix <- DistanceMatrix | ||
+ | x <- 1 | ||
+ | while(x <= ncol(wholeDistanceMatrix)){ | ||
+ | |||
+ | twholeDistanceMatrix <- t(wholeDistanceMatrix) | ||
+ | wholeDistanceMatrix[x, | ||
+ | |||
+ | x <- x + 1 | ||
+ | } | ||
+ | wholeDistanceMatrixBetween <- wholeDistanceMatrix[Pop2, | ||
+ | |||
+ | |||
+ | # | ||
+ | DistanceMatrixUp <- cbind(DistanceMatrixPop1, | ||
+ | DistanceMatrixLo <- cbind(wholeDistanceMatrixBetween, | ||
+ | DistanceMatrixTogether <- rbind(DistanceMatrixUp, | ||
+ | |||
+ | |||
+ | #----whole DistanceMatrix both Populations together: | ||
+ | Pop_together <- c(Pop1, Pop2) | ||
+ | |||
+ | wholeDistanceMatrixTogether <- wholeDistanceMatrix[Pop_together, | ||
+ | |||
+ | |||
+ | #----Mirror matrix (left-right)---- | ||
+ | mirror.matrix <- function(x) { | ||
+ | xx <- as.data.frame(x); | ||
+ | xx <- rev(xx); | ||
+ | xx <- as.matrix(xx); | ||
+ | xx; | ||
+ | } | ||
+ | |||
+ | #----Rotate matrix 270 clockworks---- | ||
+ | rotate270.matrix <- function(x) { | ||
+ | mirror.matrix(t(x)) | ||
+ | } | ||
+ | |||
+ | DistanceMatrixTogether <- rotate270.matrix(DistanceMatrixTogether) | ||
+ | |||
+ | |||
+ | #----draw matrix---------------------------------------------------------------- | ||
+ | windows() | ||
+ | library(fields) | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | a <- ncol(DistanceMatrixTogether) | ||
+ | b <- nrow(DistanceMatrixTogether) | ||
+ | |||
+ | x <- c(1:a) | ||
+ | y <- c(1:b) | ||
+ | |||
+ | png(" | ||
+ | op <- par(mfrow=c(2, | ||
+ | |||
+ | #----matrix one---- | ||
+ | op1 <- par(mfg=c(1, | ||
+ | image(x, | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | par(op1) | ||
+ | |||
+ | #----matrix two---- | ||
+ | op2 <- par(mfg=c(2, | ||
+ | image(x,y, wholeDistanceMatrixTogether, | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | box() | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | par(op2) | ||
+ | |||
+ | #----matrix three---- | ||
+ | op3 <- par(mfg=c(2, | ||
+ | image(x, | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | par(op3) | ||
+ | |||
+ | # | ||
+ | maximum <- max(DistanceMatrixTogether, | ||
+ | |||
+ | Legend <- seq(from=0, to=maximum, length=100) | ||
+ | Legend <- as.matrix(Legend) | ||
+ | |||
+ | op4 <- par(mfg=c(1, | ||
+ | image(y=Legend, | ||
+ | | ||
+ | box() | ||
+ | |||
+ | title(" | ||
+ | par(op4) | ||
+ | |||
+ | #----reset par parameter---- | ||
+ | par(op) | ||
+ | dev.off() | ||
+ | </ | ||
haplotype_distance_between_within_populations_and_groups.1200998090.txt.gz · Last modified: 2008/07/22 13:30 (external edit)