... | ... |
@@ -1,4 +1,4 @@ |
1 |
-// [[Rcpp::depends(RcppEigen)]] |
|
1 |
+// [[Rcpp::depends(Rcpp)]] |
|
2 | 2 |
#include <Rcpp.h> |
3 | 3 |
using namespace Rcpp ; |
4 | 4 |
|
... | ... |
@@ -27,10 +27,6 @@ SEXP fastNormProp(NumericMatrix R_counts, double R_alpha) { |
27 | 27 |
} |
28 | 28 |
|
29 | 29 |
|
30 |
-// [[Rcpp::depends(RcppEigen)]] |
|
31 |
-#include <Rcpp.h> |
|
32 |
-using namespace Rcpp ; |
|
33 |
- |
|
34 | 30 |
//' Fast normalization for numeric matrix |
35 | 31 |
//' |
36 | 32 |
//' @param R_counts An integer matrix |
... | ... |
@@ -56,10 +52,6 @@ SEXP fastNormPropLog(NumericMatrix R_counts, double R_alpha) { |
56 | 52 |
} |
57 | 53 |
|
58 | 54 |
|
59 |
-// [[Rcpp::depends(RcppEigen)]] |
|
60 |
-#include <Rcpp.h> |
|
61 |
-using namespace Rcpp ; |
|
62 |
- |
|
63 | 55 |
//' Fast normalization for numeric matrix |
64 | 56 |
//' |
65 | 57 |
//' @param R_counts An integer matrix |
... | ... |
@@ -86,10 +78,6 @@ SEXP fastNormPropSqrt(NumericMatrix R_counts, double R_alpha) { |
86 | 78 |
|
87 | 79 |
|
88 | 80 |
|
89 |
-// [[Rcpp::depends(RcppEigen)]] |
|
90 |
-#include <Rcpp.h> |
|
91 |
-using namespace Rcpp ; |
|
92 |
- |
|
93 | 81 |
//' get row and column indices of none zero elements in the matrix |
94 | 82 |
//' |
95 | 83 |
//' @param R_counts A matrix |
... | ... |
@@ -84,3 +84,47 @@ SEXP fastNormPropSqrt(NumericMatrix R_counts, double R_alpha) { |
84 | 84 |
return Rcpp::wrap(res); |
85 | 85 |
} |
86 | 86 |
|
87 |
+ |
|
88 |
+ |
|
89 |
+// [[Rcpp::depends(RcppEigen)]] |
|
90 |
+#include <Rcpp.h> |
|
91 |
+using namespace Rcpp ; |
|
92 |
+ |
|
93 |
+//' get row and column indices of none zero elements in the matrix |
|
94 |
+//' |
|
95 |
+//' @param R_counts A matrix |
|
96 |
+//' @return An integer matrix where each row is a row, column indices pair |
|
97 |
+// [[Rcpp::export]] |
|
98 |
+SEXP nonzero(NumericMatrix R_counts) { |
|
99 |
+ |
|
100 |
+ IntegerVector row(1); |
|
101 |
+ IntegerVector col(1); |
|
102 |
+ NumericVector val(1); |
|
103 |
+ |
|
104 |
+ int nR = R_counts.nrow(); |
|
105 |
+ int nC = R_counts.ncol(); |
|
106 |
+ double x; |
|
107 |
+ |
|
108 |
+ for (int c = 0; c < nC; c++) { |
|
109 |
+ for (int r = 0; r < nR; r++) { |
|
110 |
+ x = R_counts[c * nR + r]; |
|
111 |
+ if (x != 0) { |
|
112 |
+ row.push_back(r + 1); |
|
113 |
+ col.push_back(c + 1); |
|
114 |
+ val.push_back(x); |
|
115 |
+ } |
|
116 |
+ } |
|
117 |
+ } |
|
118 |
+ |
|
119 |
+ row.erase(0); |
|
120 |
+ col.erase(0); |
|
121 |
+ val.erase(0); |
|
122 |
+ |
|
123 |
+ List res; |
|
124 |
+ res["row"] = row; |
|
125 |
+ res["col"] = col; |
|
126 |
+ res["val"] = val; |
|
127 |
+ |
|
128 |
+ return(res); |
|
129 |
+} |
|
130 |
+ |
... | ... |
@@ -46,6 +46,9 @@ SEXP fastNormPropLog(NumericMatrix R_counts, double R_alpha) { |
46 | 46 |
// Normalize cell counts to proportions after adding pseudocount |
47 | 47 |
double alpha_tot = R_counts.nrow() * R_alpha; |
48 | 48 |
for (int i = 0; i < R_counts.ncol(); ++i) { |
49 |
+ if (cs[i] + alpha_tot == 0) { |
|
50 |
+ stop("Division by 0. Make sure colSums of counts does not contain 0 after rounding counts to integers."); |
|
51 |
+ } |
|
49 | 52 |
res(_,i) = log((R_counts(_,i) + R_alpha) / (cs[i] + alpha_tot)); |
50 | 53 |
} |
51 | 54 |
|
... | ... |
@@ -72,6 +75,9 @@ SEXP fastNormPropSqrt(NumericMatrix R_counts, double R_alpha) { |
72 | 75 |
// Normalize cell counts to proportions after adding pseudocount |
73 | 76 |
double alpha_tot = R_counts.nrow() * R_alpha; |
74 | 77 |
for (int i = 0; i < R_counts.ncol(); ++i) { |
78 |
+ if (cs[i] + alpha_tot == 0) { |
|
79 |
+ stop("Division by 0. Make sure colSums of counts does not contain 0 after rounding counts to integers."); |
|
80 |
+ } |
|
75 | 81 |
res(_,i) = sqrt((R_counts(_,i) + R_alpha) / (cs[i] + alpha_tot)); |
76 | 82 |
} |
77 | 83 |
|
... | ... |
@@ -17,6 +17,9 @@ SEXP fastNormProp(NumericMatrix R_counts, double R_alpha) { |
17 | 17 |
// Normalize cell counts to proportions after adding pseudocount |
18 | 18 |
double alpha_tot = R_counts.nrow() * R_alpha; |
19 | 19 |
for (int i = 0; i < R_counts.ncol(); ++i) { |
20 |
+ if (cs[i] + alpha_tot == 0) { |
|
21 |
+ stop("Division by 0. Make sure colSums of counts does not contain 0 after rounding counts to integers."); |
|
22 |
+ } |
|
20 | 23 |
res(_,i) = (R_counts(_,i) + R_alpha) / (cs[i] + alpha_tot); |
21 | 24 |
} |
22 | 25 |
|
Former-commit-id: 483084aa24c74526bc5f6004e6059b3810b73ab2
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,77 @@ |
1 |
+// [[Rcpp::depends(RcppEigen)]] |
|
2 |
+#include <Rcpp.h> |
|
3 |
+using namespace Rcpp ; |
|
4 |
+ |
|
5 |
+//' Fast normalization for numeric matrix |
|
6 |
+//' |
|
7 |
+//' @param R_counts An integer matrix |
|
8 |
+//' @param R_alpha A double value to be added to the matrix as a pseudocount |
|
9 |
+//' @return A numeric matrix where the columns have been normalized to proportions |
|
10 |
+// [[Rcpp::export]] |
|
11 |
+SEXP fastNormProp(NumericMatrix R_counts, double R_alpha) { |
|
12 |
+ |
|
13 |
+ // Get colSums and instantiate new matrix |
|
14 |
+ NumericVector cs = colSums(R_counts); |
|
15 |
+ NumericMatrix res = NumericMatrix(R_counts.nrow(), R_counts.ncol()); |
|
16 |
+ |
|
17 |
+ // Normalize cell counts to proportions after adding pseudocount |
|
18 |
+ double alpha_tot = R_counts.nrow() * R_alpha; |
|
19 |
+ for (int i = 0; i < R_counts.ncol(); ++i) { |
|
20 |
+ res(_,i) = (R_counts(_,i) + R_alpha) / (cs[i] + alpha_tot); |
|
21 |
+ } |
|
22 |
+ |
|
23 |
+ return Rcpp::wrap(res); |
|
24 |
+} |
|
25 |
+ |
|
26 |
+ |
|
27 |
+// [[Rcpp::depends(RcppEigen)]] |
|
28 |
+#include <Rcpp.h> |
|
29 |
+using namespace Rcpp ; |
|
30 |
+ |
|
31 |
+//' Fast normalization for numeric matrix |
|
32 |
+//' |
|
33 |
+//' @param R_counts An integer matrix |
|
34 |
+//' @param R_alpha A double value to be added to the matrix as a pseudocount |
|
35 |
+//' @return A numeric matrix where the columns have been normalized to proportions |
|
36 |
+// [[Rcpp::export]] |
|
37 |
+SEXP fastNormPropLog(NumericMatrix R_counts, double R_alpha) { |
|
38 |
+ |
|
39 |
+ // Get colSums and instantiate new matrix |
|
40 |
+ NumericVector cs = colSums(R_counts); |
|
41 |
+ NumericMatrix res = NumericMatrix(R_counts.nrow(), R_counts.ncol()); |
|
42 |
+ |
|
43 |
+ // Normalize cell counts to proportions after adding pseudocount |
|
44 |
+ double alpha_tot = R_counts.nrow() * R_alpha; |
|
45 |
+ for (int i = 0; i < R_counts.ncol(); ++i) { |
|
46 |
+ res(_,i) = log((R_counts(_,i) + R_alpha) / (cs[i] + alpha_tot)); |
|
47 |
+ } |
|
48 |
+ |
|
49 |
+ return Rcpp::wrap(res); |
|
50 |
+} |
|
51 |
+ |
|
52 |
+ |
|
53 |
+// [[Rcpp::depends(RcppEigen)]] |
|
54 |
+#include <Rcpp.h> |
|
55 |
+using namespace Rcpp ; |
|
56 |
+ |
|
57 |
+//' Fast normalization for numeric matrix |
|
58 |
+//' |
|
59 |
+//' @param R_counts An integer matrix |
|
60 |
+//' @param R_alpha A double value to be added to the matrix as a pseudocount |
|
61 |
+//' @return A numeric matrix where the columns have been normalized to proportions |
|
62 |
+// [[Rcpp::export]] |
|
63 |
+SEXP fastNormPropSqrt(NumericMatrix R_counts, double R_alpha) { |
|
64 |
+ |
|
65 |
+ // Get colSums and instantiate new matrix |
|
66 |
+ NumericVector cs = colSums(R_counts); |
|
67 |
+ NumericMatrix res = NumericMatrix(R_counts.nrow(), R_counts.ncol()); |
|
68 |
+ |
|
69 |
+ // Normalize cell counts to proportions after adding pseudocount |
|
70 |
+ double alpha_tot = R_counts.nrow() * R_alpha; |
|
71 |
+ for (int i = 0; i < R_counts.ncol(); ++i) { |
|
72 |
+ res(_,i) = sqrt((R_counts(_,i) + R_alpha) / (cs[i] + alpha_tot)); |
|
73 |
+ } |
|
74 |
+ |
|
75 |
+ return Rcpp::wrap(res); |
|
76 |
+} |
|
77 |
+ |