User Tools

Site Tools


fst_matrix

Fst Matrix

  • file: matrix_FstMatrix.r
  • document page: 2


code:

read.table("D:/Heidi/Master/R_Daten/FstMatrix/DistanceMatrix_mic.txt",
            header=TRUE, skip=1 ,row.names=1, fill=TRUE ) -> Data

as.matrix.data.frame(Data) -> Matrix
  Matrix
a <- ncol(Matrix)
b <- nrow(Matrix)
x <- c(1:a)
y <- c(1:b)

library(fields)
  ColorRamp <- rgb( seq(1,0,length=256),  # Red
                    seq(1,0,length=256),  # Green
                    seq(1,1,length=256))  # Blue

  image.plot(x,y,Matrix, col=ColorRamp, main="Distance matrix: No. of
              different alleles (FST)", xlab="Populations",
              ylab="Populations", legend.lab="Number of different Allels (FST)")

rad data from a HTML file

  • file: matrix_PairwiseFst_HTML.r
  • document page: 16


code:

#----read HTML/XML file----
whole <- readLines("D:/Heidi/Master/R_Daten/XML/PairwiseFst_HTML_geändert.html")
  whole

x <- 1
while(x <= length(whole)){
  if(whole[x] == "<FstDistanceMatrix>"){
     Begin <<- x
  }
  if(whole[x] == "</FstDistanceMatrix>"){
     End <<- x
  }
  x <- x + 1
}

#----how much rows to read----
Data <- scan("D:/Heidi/Master/R_Daten/XML/PairwiseFst_HTML_geändert.html", skip=End-2, nlines=1)

Columns <- (length(Data))
Row <- Columns

#----read data row by row:----
x <- (Begin+2)
n <- 1

DistanceMatrix <- as.matrix(scan("D:/Heidi/Master/R_Daten/XML/PairwiseFst_HTML_geändert.html",
                                  what=double(0), skip=x, nlines=1, nmax=n))
DistanceMatrix <- cbind(DistanceMatrix, matrix(NA, ncol=(Columns), nrow=1))
DistanceMatrix <- DistanceMatrix[,2:Columns]

n <- n + 1
x <- x + 1

while(n<(Row)){
  nextrow <- as.matrix(scan("D:/Heidi/Master/R_Daten/XML/PairwiseFst_HTML_geändert.html",
                             what=double(0), skip=x, nlines=1, nmax=n))
  nextrow <- cbind(t(nextrow), matrix(NA, ncol=(Columns), nrow=1))
  nextrow <- nextrow[,2:Columns]

  DistanceMatrix <- rbind(DistanceMatrix, nextrow)

  n <- n + 1
  x <- x + 1
}

#----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))
}

DistanceMatrix <- rotate270.matrix(DistanceMatrix)

#----draw matrix plot----
library(fields)

  a <- ncol(DistanceMatrix)
  b <- nrow(DistanceMatrix) 
  x <- c(1:a)
  y <- c(1:b)

  ColorRamp <- rgb( seq(1,0,length=256),  # Red
                    seq(1,0,length=256),  # Green
                    seq(1,1,length=256))  # Blue

    image.plot(x,y,DistanceMatrix, col=ColorRamp, main="Distance matrix: No. of different allels (FST)",
                xlab="Population", ylab="Population", axes = FALSE, legend.mar=4.3, legend.width=0.8,
                legend.lab="Number of different allels (Fst)")
        contour(DistanceMatrix, add = TRUE)
        axis(1, at = c(1:a),cex.axis=0.7)
        axis(2, at = c(1:b), labels=c(b:1),cex.axis=0.7)
        box()

read data from a XML file

  • file: Node auslesen_matrix_PairwiseFst.r
  • document page: 20


code:

#----open XML package-------------------------------------------------
library(XML)

#----read data between an XML tag-------------------------------------
filename = "D:/Heidi/Master/R_Daten/XML/PairwiseFst_XML.xml"
tag = "//Fst"
doc = xmlTreeParse(filename, useInternal = TRUE)
ch = getNodeSet(doc, tag)

subDoc = xmlDoc(ch[[1]])
tagData <- xpathApply(subDoc, tag, xmlValue)
free(subDoc)
#print(tagData, indent=FALSE)

#----convert string data to a numeric matrix---------------------------
#----split string----
tagData2 <- as.character(tagData)

tagData3 <- strsplit(tagData2, "\n")  
tagMatrix <- as.matrix(as.data.frame(tagData3))
tagMatrix <- tagMatrix[4:nrow(tagMatrix)]
Data <- strsplit(tagMatrix, "   ")

#----to string matrix----
Row <- length(Data)

Matrix <- as.matrix(as.data.frame(Data[1]))
Matrix <- rbind(Matrix, matrix(NA, ncol=1, nrow=(Row-1)))

for(n in 2:(Row)){
  nextrow <- as.matrix(as.data.frame(Data[n]))
  nextrow <- rbind(nextrow, matrix(NA, ncol=1, nrow=(Row-n)))
  Matrix <- cbind(Matrix, nextrow)
}
Matrix <- Matrix[5:nrow(Matrix),]

#----to numeric matrix----
numericList <- as.numeric(Matrix[,1])
numericMatrix <- t(as.matrix(numericList))

for(n in 2:(Row)){
  numericList <- as.numeric(Matrix[,n])
  numericMatrix <- rbind(numericMatrix, t(as.matrix(numericList)))
}
numericMatrix

# numericTable <- as.table(numericMatrix)     
# numericTable

DistanceMatrix <- numericMatrix

#----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))
}

DistanceMatrix <- rotate270.matrix(DistanceMatrix)

#----draw matrix plot----
library(fields)

  a <- ncol(DistanceMatrix)
  b <- nrow(DistanceMatrix) 
  x <- c(1:a)
  y <- c(1:b)

  ColorRamp <- rgb( seq(1,0,length=256),  # Red
                   seq(1,0,length=256),  # Green
                   seq(1,1,length=256))  # Blue
                   
    image.plot(x,y,DistanceMatrix, col=ColorRamp, main="Distance matrix: No. of different allels (FST)",
                xlab="Population", ylab="Population", axes = FALSE, legend.lab="Number of different allels (Fst)",
                legend.mar=4.3, legend.width=0.8)
        contour(DistanceMatrix, add = TRUE)
        axis(1, at = c(1:a),cex.axis=0.7)
        axis(2, at = c(1:b), labels=c(b:1),cex.axis=0.7)
        box()

move legend

  • file: matrix_PairwiseFst.r
  • document page: 22


code:

#----read data--------------------------------------------------------------
read.table("D:/Heidi/Master/R_Daten/FstMatrix/PairwiseFst_mic.txt",
            header=TRUE, skip=5 ,row.names=1, fill=TRUE )-> Data
as.matrix.data.frame(Data) -> Matrix

a <- ncol(Matrix)
b <- nrow(Matrix)

x <- c(1:a)
y <- c(1:b)

#----draw plot--------------------------------------------------------------
library(fields)
ColorRamp <- rgb( seq(1,0,length=256),  # Red
                  seq(1,0,length=256),  # Green
                  seq(1,1,length=256))  # Blue
 
#----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))
}

Matrix <- rotate270.matrix(Matrix)

#----draw matrix plot----
image.plot(x,y,Matrix, col=ColorRamp, main="Distance matrix: No. of different alleles (FST)",
            xlab="Population", ylab="Population", legend.args=list( text="Number of different Allels (FST)",
            cex=1.0, side=4, line=2), axes = FALSE)
        contour(Matrix, add = TRUE)
        axis(1, at = c(1:a))
        axis(2, at = c(1:b), labels=c(b:1))  
        box()
fst_matrix.txt · Last modified: 2008/07/22 13:31 by 127.0.0.1