[adegenet-commits] r789 - in pkg: R src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Feb 4 13:35:05 CET 2011


Author: jombart
Date: 2011-02-04 13:35:04 +0100 (Fri, 04 Feb 2011)
New Revision: 789

Modified:
   pkg/R/SNPbin.R
   pkg/src/SNPbin.c
Log:
conversion from raw to integers (not only binary) in C


Modified: pkg/R/SNPbin.R
===================================================================
--- pkg/R/SNPbin.R	2011-02-01 16:08:18 UTC (rev 788)
+++ pkg/R/SNPbin.R	2011-02-04 12:35:04 UTC (rev 789)
@@ -679,11 +679,12 @@
 #############
 ## .SNPbin2int
 #############
-## convert SNPbin to integers (0/1)
+## convert SNPbin to integers (0/1/2...)
 .SNPbin2int <- function(x){
-    res <- lapply(x at snp, .raw2bin)
-    res <- lapply(res, function(e) e[1:x at n.loc])
-    res <- as.integer(Reduce("+", res))
+    ##res <- lapply(x at snp, .raw2bin)
+    res <- .C("bytesToInt", unlist(x at snp), length(x at snp[[1]]), length(x at snp), integer(x at n.loc*8), PACKAGE="adegenet")[[4]][1:x at n.loc]
+    ##res <- lapply(res, function(e) e[1:x at n.loc])
+    ##res <- as.integer(Reduce("+", res))
     if(length(x at NA.posi)>0){
         res[x at NA.posi] <- NA
     }

Modified: pkg/src/SNPbin.c
===================================================================
--- pkg/src/SNPbin.c	2011-02-01 16:08:18 UTC (rev 788)
+++ pkg/src/SNPbin.c	2011-02-04 12:35:04 UTC (rev 789)
@@ -133,6 +133,7 @@
 
 
 /* Maps an array of values from 0-255 to sequences of 8 binary values */
+/* Input are unsigned char (hexadecimal), outputs are integers */
 void bytesToBinInt(unsigned char *vecbytes, int *vecsize, int *vecres){
 	int i, j, idres=0, *temp; /* idres: index in vecres*/
 	
@@ -156,8 +157,38 @@
 
 
 
+/* Maps an array of values from 0-255 to integers representing counts of alleles */
+/* This is done by adding arrays of 0-1 for indiv with ploidy > 1*/
+/* Input are unsigned char (hexadecimal), outputs are integers */
+/* veclength is the length of one vector of bytes */
+/* nbvec is the nb of input vectors*/
+/* input 'vecbytes' is actually concatenated, ie of size veclength * nbvec */
+void bytesToInt(unsigned char *vecbytes, int *veclength, int *nbvec, int *vecres){
+	int i, j, k, idres=0, *temp; /* idres: index in vecres*/
 
+	temp = (int *) calloc(8, sizeof(int));
 
+
+	for(k=0;k<*nbvec;k++){ /* for all input vector */
+		idres = 0;
+		for(i=0;i<*veclength;i++){ /* for one input vector */
+			byteToBinInt(vecbytes[i+ k* *veclength], temp); /* byte -> 8 int (0/1)*/
+			for(j=0;j<=7;j++){ /* fill in the result*/
+				vecres[j+idres] = vecres[j+idres] + temp[j];
+			}
+			idres = idres + 8;
+		}
+	}
+	free(temp);
+} /* end bytesToInt */
+
+
+
+
+
+
+
+
 /* Simple test function */
 /* Test: increases for a raw (unsigned char) vector */
 void testRaw(unsigned char *a, int *n){
@@ -173,7 +204,23 @@
 
 
 
+/* Function to compute all dot products between individuals */
+/* No centring, no scaling */
+/* a: 2-dim array, dim n x p*/
+/* naposi: 2-dim array, dim n x ...*/
+/* nbna: array of nb of NAs for each individual*/
+void dotProd(unsigned char **a, int *n, int *p, int **naposi, int *nbna){
+	/* define variables, allocate memory */
 
+
+	/* free memory */
+	
+}
+
+
+
+
+
 /* TESTING in R */
 
 /*
@@ -185,5 +232,10 @@
 toto <- .bin2raw(x)$snp
 all(.C("bytesToBinInt", toto, length(toto), integer(length(toto)*8))[[3]]==x)
 
+## test raw vec -> binary integers
+.C("bytesToBinInt",as.raw(c(12,11)), 2L, integer(16), PACKAGE="adegenet")
+
+## test several raw vec -> int (allele counts, any ploidy)
+.C("bytesToInt",as.raw(c(12,11)), 1L, 2L, integer(8), PACKAGE="adegenet")
 */
 



More information about the adegenet-commits mailing list