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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jan 18 13:54:01 CET 2011


Author: jombart
Date: 2011-01-18 13:54:01 +0100 (Tue, 18 Jan 2011)
New Revision: 765

Added:
   pkg/src/SNPbin.h
Modified:
   pkg/R/SNPbin.R
   pkg/src/SNPbin.c
Log:
Can now handle byte data entirely within C.


Modified: pkg/R/SNPbin.R
===================================================================
--- pkg/R/SNPbin.R	2011-01-17 19:16:38 UTC (rev 764)
+++ pkg/R/SNPbin.R	2011-01-18 12:54:01 UTC (rev 765)
@@ -566,13 +566,13 @@
 
     ## map info to bytes (0:255)
     vecSnp <- as.integer(vecSnp)
-    vecRaw <- integer(nbBytes)
+    ##vecRaw <- integer(nbBytes) # no longer needed - sending raw type directly
+    vecRaw <- raw(nbBytes)
 
     vecRaw <- .C("binIntToBytes", vecSnp, length(vecSnp), vecRaw, nbBytes, PACKAGE="adegenet")[[3]]
     ## vecraw <- sapply(seq(1, by=8, length=nbBytes), function(i) which(apply(SNPCOMB,1, function(e) all(temp[i:(i+7)]==e))) ) # old R version
 
-    ## code information as raw and add missing data
-    vecRaw <- as.raw(vecRaw)
+    ## return result
     res <- list(snp=vecRaw, n.loc=as.integer(ori.length), NA.posi=as.integer(NAposi))
     return(res)
 } # end .bin2raw

Modified: pkg/src/SNPbin.c
===================================================================
--- pkg/src/SNPbin.c	2011-01-17 19:16:38 UTC (rev 764)
+++ pkg/src/SNPbin.c	2011-01-18 12:54:01 UTC (rev 765)
@@ -23,7 +23,47 @@
 #include "adesub.h"
 
 
-/* 
+
+
+/*
+   ==========================
+   === INTERNAL FUNCTIONS ===
+   ==========================
+*/
+
+
+/* Maps one value from 0-255 to sequences of 8 binary values */
+void byteToBinInt(unsigned char in, int *out){
+	short int rest, i, temp;
+
+	rest = (int)in;
+
+	/* initialize all values to 0*/
+	for(i=0;i<=7;i++)
+		out[i]=0;
+
+	for(i=7;i>=0;i--){
+		temp = pow(2, i);
+		if(rest >= temp) {
+			out[i] = 1;
+			rest = rest- temp;
+			if(rest == 0) break;
+		}
+	}
+}
+
+
+
+
+
+
+
+
+
+
+
+
+/*
    ===============================
    === MAIN EXTERNAL FUNCTIONS ===
    ===============================
@@ -37,7 +77,7 @@
    - res: vector of integers valued on 0:255
    - ressize: length of res
 */
-void binIntToBytes(int *vecsnp, int *vecsize, int *vecres, int *ressize){
+void binIntToBytes(int *vecsnp, int *vecsize, unsigned char *vecres, int *ressize){
 	/* declarations */
 	int i, j, idres, *binBasis; /* must use dynamic allocation */
 
@@ -51,7 +91,7 @@
 
 	/* set all values of vecres to 0 */
 	for(i=0;i < *ressize;i++){
-		vecres[i] = 0;
+		vecres[i] = 0x00;
 	}
 
 
@@ -70,16 +110,16 @@
 	idres = 0;
 	j = 1;
 	for(i=0;i< *vecsize;i++){
-		vecres[idres] = vecres[idres] + binBasis[j] * vecsnp[i];
+		vecres[idres] = vecres[idres] + (unsigned char)(binBasis[j] * vecsnp[i]);
 		if(j == 8){
-			idres = idres +1;
+			idres++;
 			j = 1;
 		} else {
-			j = j+1;
+			j++;
 		}
 	}
-	
-	
+
+
 	/* free memory */
 	freeintvec(binBasis);
 
@@ -92,10 +132,58 @@
 
 
 
+/* Maps an array of values from 0-255 to sequences of 8 binary values */
+void bytesToBinInt(unsigned char *vecbytes, int *vecsize, int *vecres){
+	int i, j, idres=0, *temp; /* idres: index in vecres*/
+	
+	temp = (int *) calloc(8, sizeof(int));
 
+	for(i=0;i<*vecsize;i++){
+		byteToBinInt(vecbytes[i], temp);
+		for(j=0;j<=7;j++){
+			vecres[j+idres] = temp[j];
+		}
+		idres = idres + 8;
+	}
 
-/* TESTING */
+	free(temp);
+} /* end binIntToBytes*/
+
+
+
+
+
+
+
+
+
+
+/* Simple test function */
+/* Test: increases for a raw (unsigned char) vector */
+void testRaw(unsigned char *a, int *n){
+	int i;
+	for(i=0; i<*n; i++){
+		a[i] = (unsigned char)(i);
+	}
+}
+
+
+
+
+
+
+
+
+/* TESTING in R */
+
 /*
+## test raw conversion
+.C("testRaw", raw(256), 256L, PACKAGE="adegenet")
 
+## test raw->int conversion
+x <- sample(0:1,80,replace=TRUE)
+toto <- .bin2raw(x)$snp
+all(.C("bytesToBinInt", toto, length(toto), integer(length(toto)*8))[[3]]==x)
 
 */
+

Added: pkg/src/SNPbin.h
===================================================================
--- pkg/src/SNPbin.h	                        (rev 0)
+++ pkg/src/SNPbin.h	2011-01-18 12:54:01 UTC (rev 765)
@@ -0,0 +1,15 @@
+#include <math.h>
+#include <time.h>
+#include <string.h>
+#include <stdlib.h>
+#include <R.h>
+#include <R_ext/Utils.h>
+#include "adesub.h"
+
+/* EXTERNAL */
+void binIntToBytes(int *vecsnp, int *vecsize, unsigned char *vecres, int *ressize);
+void bytesToBinInt(unsigned char *vecbytes, int *vecsize, int *vecres);
+
+/* INTERNAL */
+void byteToBinInt(unsigned char in, int out[8]);
+void testRaw(unsigned char *a, int *n);



More information about the adegenet-commits mailing list