-
Notifications
You must be signed in to change notification settings - Fork 34
Adding difference and division #740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
db4ed3b
Update transformCounts.R
eotamm 51cdbe6
Update transformCounts.R
eotamm d8afe00
Update transformCounts.R
eotamm f4ee34a
Update test-5transformCounts.R
eotamm 6da343f
Update transformAssay.Rd
eotamm 1d0e17e
Update transformCounts.R
eotamm 251c95e
Update transformCounts.R
eotamm fe47012
Update test-5transformCounts.R
eotamm 379ce3a
Update transformCounts.R
eotamm 2644765
Update test-5transformCounts.R
eotamm b462575
Merge branch 'devel' into diff_and_div
antagomir 15b039b
Update transformCounts.R
eotamm 929a106
Adding C++ file for difference and division
eotamm 4b9e32e
Update RcppExports.R
eotamm 14c123b
Update RcppExports.cpp
eotamm 5a1a6b3
Merge branch 'microbiome:devel' into diff_and_div
eotamm 2412a9f
Fixed feature naming
eotamm 75bdc07
Updated generated rownames
eotamm 961b3e7
Update transformCounts.cpp
eotamm a17da60
Update transformCounts.R
eotamm c7e5386
Update test-5transformCounts.R
eotamm 3b141b1
Update DESCRIPTION
eotamm 2cd1986
Merge branch 'devel' into diff_and_div
TuomasBorman ba4d0e2
Simplify
TuomasBorman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,2 @@ | ||
| api.o | ||
| api_s.o | ||
| biom.o | ||
| biom_s.o | ||
| propstack.o | ||
| tree.o | ||
| tree_s.o | ||
| tse.o | ||
| unifrac.o | ||
| unifrac_internal.o | ||
| unifrac_internal_s.o | ||
| unifrac_s.o | ||
| RcppExports.o | ||
| assay.o | ||
| faith_R.o | ||
| propmap.o | ||
| mia.dll | ||
| mia.so | ||
| *.o | ||
| *.so |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| #include <Rcpp.h> | ||
| using namespace Rcpp; | ||
|
|
||
| // Helper for difference | ||
| void calculate_difference( | ||
| const NumericMatrix& mat, int i, int j, int k, int pair_idx, | ||
| std::vector<int>& i_vec, std::vector<int>& j_vec, | ||
| std::vector<double>& x_vec) { | ||
| double diff = mat(k, i) - mat(k, j); | ||
| if( diff != 0.0 ){ | ||
| i_vec.push_back(k); | ||
| j_vec.push_back(pair_idx); | ||
| x_vec.push_back(diff); | ||
| } | ||
| } | ||
|
|
||
| // Helper for division | ||
| void calculate_division( | ||
| const NumericMatrix& mat, int i, int j, int k, int pair_idx, | ||
| std::vector<int>& i_vec, std::vector<int>& j_vec, | ||
| std::vector<double>& x_vec) { | ||
| double denom = mat(k, j); | ||
| if( denom != 0.0 && std::isfinite(denom) ){ | ||
| double ratio = mat(k, i) / denom; | ||
| if( ratio != 0.0 && std::isfinite(ratio) ){ | ||
| i_vec.push_back(k); | ||
| j_vec.push_back(pair_idx); | ||
| x_vec.push_back(ratio); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // [[Rcpp::export(name = ".apply_transformation_difference_or_division")]] | ||
| S4 apply_transformation_difference_or_division( | ||
| NumericMatrix mat, std::string method = "difference") { | ||
| // samples = rows, features = cols | ||
| int n_samples = mat.nrow(); | ||
| int n_features = mat.ncol(); | ||
| int n_pairs = n_features * (n_features - 1) / 2; | ||
| CharacterVector original_feature_names = colnames(mat); | ||
|
|
||
| // Prepare output vectors | ||
| std::vector<int> i_vec, j_vec; | ||
| std::vector<double> x_vec; | ||
| CharacterVector colnames(n_pairs); | ||
|
|
||
| int pair_idx = 0; | ||
| for( int i = 0; i < n_features - 1; ++i ){ | ||
| for( int j = i + 1; j < n_features; ++j ){ | ||
| for( int k = 0; k < n_samples; ++k ){ | ||
| if( method == "difference" ){ | ||
| calculate_difference( | ||
| mat, i, j, k, pair_idx, i_vec, j_vec, x_vec); | ||
| } else if( method == "division" ){ | ||
| calculate_division( | ||
| mat, i, j, k, pair_idx, i_vec, j_vec, x_vec); | ||
| } else{ | ||
| stop("Unknown method: use 'difference' or 'division'"); | ||
| } | ||
| } | ||
| // Generate colname for feature pair | ||
| std::string name_i = as<std::string>(original_feature_names[i]); | ||
| std::string name_j = as<std::string>(original_feature_names[j]); | ||
| colnames[pair_idx] = name_i + "-" + name_j; | ||
| ++pair_idx; | ||
| } | ||
| } | ||
|
|
||
| // Create dgTMatrix (triplet sparse matrix) | ||
| S4 tmat("dgTMatrix"); | ||
| // Samples as rows | ||
| tmat.slot("i") = IntegerVector(i_vec.begin(), i_vec.end()); | ||
| // Pairs as columns | ||
| tmat.slot("j") = IntegerVector(j_vec.begin(), j_vec.end()); | ||
| tmat.slot("x") = NumericVector(x_vec.begin(), x_vec.end()); | ||
| tmat.slot("Dim") = IntegerVector::create(n_samples, n_pairs); | ||
| tmat.slot("Dimnames") = List::create(rownames(mat), colnames); | ||
|
|
||
| // Convert dgTMatrix to dgCMatrix | ||
| Function as("as"); | ||
| S4 cmat = as(tmat, "CsparseMatrix"); | ||
|
|
||
| return cmat; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.