forked from PhanstielLab/plotgardener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckRow.cpp
57 lines (41 loc) · 1.29 KB
/
checkRow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <Rcpp.h>
using namespace Rcpp;
int rowNum(NumericVector v, NumericMatrix x, int maxRows, int rowCol, double wiggle) {
// genomic coord start and stop, incorporating wiggle (in bp)
int start = std::min(v[0], v[1]) - wiggle;
int stop = std::max(v[0], v[1]) + wiggle;
// check every row value
for (int j = 1; j <= maxRows; j++){
// initialize a row counter
int rowCounter = 0;
// go through dataframe to check for approriate row values
int dfRows = x.nrow();
for (int row = 0; row < dfRows; row++){
NumericVector v = x(row,_);
if ((v[rowCol] == j) &
((start >= v[0] & start <= v[1]) |
(stop >= v[0] & stop <= v[1]) |
(start <= v[0] & stop >= v[1]))){
rowCounter++;
}
}
if (rowCounter == 0){
return j;
}
}
return 0;
}
// [[Rcpp::export]]
NumericMatrix checkRow(NumericMatrix x, int maxRows, int rowCol, double wiggle){
// go through each row of the dataframe
int dfRows = x.nrow();
for (int i = 0; i < dfRows; ++i){
// get the row
NumericVector row = x(i,_);
// determine its rownumber from rowNum function
int rowNumber = rowNum(row, x, maxRows, rowCol, wiggle);
// update whole dataframe with that value's rownumber
x(i,rowCol) = rowNumber;
}
return x;
}