Skip to content

Commit

Permalink
fixed parseTCR and slightly refactored combineTCR
Browse files Browse the repository at this point in the history
  • Loading branch information
Qile0317 committed Nov 9, 2023
1 parent daba008 commit dc1d7c7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 58 deletions.
37 changes: 0 additions & 37 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -277,43 +277,6 @@ is_seurat_or_se_object <- function(obj) {
return(x)
}

# to be removed
.parseTCR <- function(Con.df, unique_df, data2) {
tcr1_index <- 2
tcr2_index <- 5
data2 <- data2 %>% arrange(., chain, cdr3_nt)
for (y in seq_along(unique_df)){
barcode.i <- Con.df$barcode[y]
location.i <- which(barcode.i == data2$barcode)
for (z in seq_along(location.i)) {
where.chain <- data2[location.i[z],"chain"]

if (where.chain == "TRA" || where.chain == "TRG") {
if (is.na(Con.df[y, tcr1_index])) {
Con.df[y,tcr1_lines] <- data2[location.i[z], data1_lines]
} else {
Con.df[y,tcr1_lines] <- paste(
Con.df[y, tcr1_lines],
data2[location.i[z],data1_lines],
sep=";"
)
}
} else if (where.chain == "TRB" || where.chain == "TRD") {
if (is.na(Con.df[y, tcr2_index])) {
Con.df[y,tcr2_lines] <- data2[location.i[z], data2_lines]
} else {
Con.df[y,tcr2_lines] <- paste(
Con.df[y, tcr2_lines],
data2[location.i[z],data2_lines],
sep=";"
)
}
}
}
}
Con.df
}

# Assigning positions for TCR contig data, returning Con.df with the parsed TCR data
#' @author Gloria Kraus, Nick Bormann, Nicky de Vrij, Nick Borcherding, Qile Yang
#' @keywords internal
Expand Down
30 changes: 9 additions & 21 deletions src/constructConDfAndparseTCR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "scRepHelper.h"

#define BarcodeIndciesMap std::unordered_map<std::string, std::vector<int>>
#define StringMatrix std::vector<std::vector<std::string>>

std::vector<std::vector<int>> constructBarcodeIndex(
std::vector<std::string>& conDfBarcodes, std::vector<std::string> data2Barcodes
Expand All @@ -28,10 +27,9 @@ std::vector<std::vector<int>> constructBarcodeIndex(
class TcrParser {
public:
// variable for the eventual output conDf
StringMatrix conDf;
std::vector<std::vector<std::string>> conDf;

// variables for *references* to columns on data2
Rcpp::CharacterVector data2Barcodes;
Rcpp::CharacterVector data2ChainTypes;
Rcpp::CharacterVector data2Tcr1;
Rcpp::CharacterVector data2Tcr2;
Expand All @@ -41,7 +39,6 @@ class TcrParser {
// variable for helper barcode index
std::vector<std::vector<int>> barcodeIndex;

// constructor
TcrParser(
Rcpp::DataFrame& data2, std::vector<std::string>& uniqueData2Barcodes
) {
Expand All @@ -52,7 +49,6 @@ class TcrParser {
conDf[0] = uniqueData2Barcodes;

// set references to data2 columns
data2Barcodes = data2[0];
data2ChainTypes = data2[5];
data2Tcr1 = data2[19];
data2Tcr2 = data2[20];
Expand All @@ -61,23 +57,15 @@ class TcrParser {

// barcodeIndex
barcodeIndex = constructBarcodeIndex(
uniqueData2Barcodes, Rcpp::as<std::vector<std::string>>(data2Barcodes)
uniqueData2Barcodes, Rcpp::as<std::vector<std::string>>(data2[0])
);
}

// .parseTCR() equivalent
void parseTCR() {
for (int y = 0; y < (int) conDf[0].size(); y++) {

std::vector<int>& indicies = barcodeIndex[y];
if (indicies.size() == 0) {
continue;
}

for (int index : indicies) {
for (int index : barcodeIndex[y]) {
std::string chainType = std::string(data2ChainTypes[index]);
//Rcpp::Rcout << "data2ChainTypes[" << index << "] = " << chainType << "\n";
if (chainType == "TRA" || chainType == "TRB") {
if (chainType == "TRA" || chainType == "TRG") {
handleTcr1(y, index);
} else if (chainType == "TRB" || chainType == "TRD") {
handleTcr2(y, index);
Expand All @@ -96,17 +84,17 @@ class TcrParser {
}

void handleTcr(
int y, int data2index, Rcpp::CharacterVector data2tcr, int tcr, int cdr3aa, int cdr3nt
int y, int data2index, Rcpp::CharacterVector& data2tcr, int tcr, int cdr3aa, int cdr3nt
) {
if (conDf[tcr][y] == "NA") {
conDf[tcr][y] = data2tcr[data2index];
conDf[cdr3aa][y] = data2Cdr3[data2index];
conDf[cdr3nt][y] = data2Cdr3Nt[data2index];
return;
} else {
conDf[tcr][y] += ";" + data2tcr[data2index];
conDf[cdr3aa][y] += ";" + data2Cdr3[data2index];
conDf[cdr3nt][y] += ";" + data2Cdr3Nt[data2index];
}
conDf[tcr][y] += ";" + data2tcr[data2index];
conDf[cdr3aa][y] += ";" + data2Cdr3[data2index];
conDf[cdr3nt][y] += ";" + data2Cdr3Nt[data2index];
}

// return Con.df after TCR parsing
Expand Down

0 comments on commit dc1d7c7

Please sign in to comment.