-
Notifications
You must be signed in to change notification settings - Fork 3
Triangular distance #50
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
Open
robertbjornson
wants to merge
7
commits into
master
Choose a base branch
from
triangular_distance
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
761010e
changed to triangular distance matrix to save memory
robertbjornson a70861e
added fastDistAA.R for amino acids. Also added test programs
robertbjornson 4fca530
Merge branch 'master' into triangular_distance
ggabernet be92cad
Updated fastDist_rcpp tests to use fastDist wrapper
ssnn-airr 8305a1f
Commented out redundant bump_pairs call
ssnn-airr 1a34e5b
change to devel number
ssnn-airr 9eaec44
Merge branch 'master' into triangular_distance
ssnn-airr 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| #include <Rcpp.h> | ||
| using namespace Rcpp; | ||
|
|
||
| // A=0 C=1 D=2 E=3 F=4 G=5 H=6 I=7 K=8 L=9 | ||
| // M=10 N=11 P=12 Q=13 R=14 S=15 T=16 V=17 W=18 Y=19 | ||
| // X=20 (wildcard) ?=21 | ||
| inline uint8_t code_aa(char c){ | ||
| switch(c){ | ||
| case 'A': return 0; case 'C': return 1; case 'D': return 2; | ||
| case 'E': return 3; case 'F': return 4; case 'G': return 5; | ||
| case 'H': return 6; case 'I': return 7; case 'K': return 8; | ||
| case 'L': return 9; case 'M': return 10; case 'N': return 11; | ||
| case 'P': return 12; case 'Q': return 13; case 'R': return 14; | ||
| case 'S': return 15; case 'T': return 16; case 'V': return 17; | ||
| case 'W': return 18; case 'Y': return 19; | ||
| case 'X': return 20; case '?': return 21; | ||
| default: return 255; | ||
| } | ||
| } | ||
|
|
||
| // [[Rcpp::export]] | ||
| int countAASeqsWithInvalidChars_rcpp(CharacterVector seqs) { | ||
| int N = seqs.size(); | ||
| int bad = 0; | ||
|
|
||
| for (int i = 0; i < N; ++i) { | ||
| if (seqs[i] == NA_STRING) { | ||
| bad++; | ||
| continue; | ||
| } | ||
|
|
||
| std::string s = as<std::string>(seqs[i]); | ||
| bool invalid = false; | ||
| for (char ch : s) { | ||
| char up = (char)std::toupper((unsigned char)ch); | ||
| if (code_aa(up) == 255) { | ||
| invalid = true; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (invalid) bad++; | ||
| } | ||
|
|
||
| return bad; | ||
| } | ||
|
|
||
| // [[Rcpp::export]] | ||
| IntegerVector fastDistAA_rcpp(CharacterVector seqs) { | ||
| int N = seqs.size(); | ||
|
|
||
| if (N == 0) stop("empty input"); | ||
|
|
||
| std::string s0 = as<std::string>(seqs[0]); | ||
| int L = (int)s0.size(); | ||
| for (int i = 0; i < N; ++i) { | ||
| if ((int)std::string(as<std::string>(seqs[i])).size() != L) | ||
| stop("All sequences must have the same length"); | ||
| } | ||
|
|
||
| // encode to uint8: N x L, row-major in a flat vector | ||
| std::vector<uint8_t> enc((size_t)N * L); | ||
| for (int i = 0; i < N; ++i) { | ||
| std::string s = as<std::string>(seqs[i]); | ||
| for (int p = 0; p < L; ++p) { | ||
| char up = (char)std::toupper((unsigned char)s[p]); | ||
| uint8_t c = code_aa(up); | ||
| if (c == 255) stop("Only the 20 standard AAs plus X and ? are allowed"); | ||
| enc[(size_t)i * L + p] = c; | ||
| } | ||
| } | ||
|
|
||
| // lower-triangle vector (column-major, matching R's dist storage): | ||
| // element at row i, col j (i > j, 0-indexed) maps to: | ||
| // j*N - j*(j+1)/2 + (i-j) - 1 | ||
| size_t tri_size = (size_t)N * (N - 1) / 2; | ||
| IntegerVector tri(tri_size); // zero-initialized | ||
|
|
||
| auto tri_idx = [&](int i, int j) -> size_t { | ||
| if (i < j) std::swap(i, j); | ||
| return (size_t)j * N - (size_t)j * (j + 1) / 2 + (i - j) - 1; | ||
| }; | ||
|
|
||
| for (int p = 0; p < L; ++p) { | ||
| // one bucket per code (0-19 known AAs, 20 = X, 21 = ?) | ||
| std::vector<int> buckets[22]; | ||
| for (int b = 0; b < 22; ++b) buckets[b].reserve(8); | ||
|
|
||
| for (int i = 0; i < N; ++i) | ||
| buckets[enc[(size_t)i * L + p]].push_back(i); | ||
|
|
||
| // increment lower-triangle match counts for pairs within the same bucket | ||
| auto bump_within = [&](const std::vector<int>& v) { | ||
| int m = (int)v.size(); | ||
| for (int ii = 1; ii < m; ++ii) | ||
| for (int jj = 0; jj < ii; ++jj) | ||
| tri[tri_idx(v[ii], v[jj])] += 1; | ||
| }; | ||
|
|
||
| // increment lower-triangle match counts for all cross-bucket pairs | ||
| auto bump_pairs = [&](const std::vector<int>& X, const std::vector<int>& Y) { | ||
| for (int xi : X) | ||
| for (int yj : Y) | ||
| if (xi != yj) | ||
| tri[tri_idx(xi, yj)] += 1; | ||
| }; | ||
|
|
||
| // same known AA | ||
| for (int b = 0; b < 20; ++b) | ||
| bump_within(buckets[b]); | ||
|
|
||
| // X (wildcard) with any known AA — but not X-X | ||
| std::vector<int> K; | ||
| K.reserve(N); | ||
| for (int b = 0; b < 20; ++b) | ||
| K.insert(K.end(), buckets[b].begin(), buckets[b].end()); | ||
| bump_pairs(buckets[20], K); | ||
|
|
||
| // ? with ? | ||
| bump_within(buckets[21]); | ||
| } | ||
|
|
||
| // convert matches -> distances | ||
| for (size_t k = 0; k < tri_size; ++k) | ||
| tri[k] = L - tri[k]; | ||
|
|
||
| return tri; | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@robertbjornson I made this change (commented out bump_pairs) to your code. Please review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I"m unclear where this bump_pairs(K, Ns) came from. It wasn't in my checked in code. Seems fine to remove it.