[R] Character SNP data to binary MAF data

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Thu Jan 29 09:53:05 CET 2009


2009/1/29 Patrick Aboyoun <paboyoun at fhcrc.org>:
> Hadassa,
> You may want to check out the snpMatrix package in Bioconductor
>
> http://bioconductor.org/packages/2.3/bioc/html/snpMatrix.html
> http://bioconductor.org/packages/2.4/bioc/html/snpMatrix.html
>
> It contains classes that manage this type of information and should minimize
> your coding effort.

It's not that much effort - this code turns all ties into 1s:

snp2maf=function(m){
m=toupper(m)
return(t(apply(m,1,makeBin)))
}

makeBin = function(chars){
tc = table(chars)
maxV = names(tc[tc==max(tc)])
matches = match(chars,maxV)
r=as.integer(!is.na(matches))
return(r)
}

 then:

> m
      [,1] [,2] [,3] [,4] [,5]
 [1,] "t"  "g"  "g"  "g"  "t"
 [2,] "a"  "G"  "a"  "C"  "c"
 [3,] "A"  "T"  "c"  "c"  "C"
 [4,] "g"  "T"  "c"  "A"  "C"
 [5,] "G"  "C"  "G"  "g"  "G"
 [6,] "G"  "t"  "T"  "a"  "C"
 [7,] "A"  "G"  "T"  "g"  "T"
 [8,] "T"  "a"  "C"  "a"  "T"
 [9,] "t"  "g"  "g"  "c"  "T"
[10,] "A"  "t"  "t"  "c"  "A"
> snp2maf(m)
      [,1] [,2] [,3] [,4] [,5]
 [1,]    0    1    1    1    0
 [2,]    1    0    1    1    1
 [3,]    0    0    1    1    1
 [4,]    0    0    1    0    1
 [5,]    1    0    1    1    1
 [6,]    0    1    1    0    0
 [7,]    0    1    1    1    1
 [8,]    1    1    0    1    1
 [9,]    1    1    1    0    1
[10,]    1    1    1    0    1
>

Barry




More information about the R-help mailing list