I’m trying to develop a table from a series of lists generated using the Immunarch library to process TCR sequencing results. Each list is comprised of CDR3.aa (clone) information which are character strings and their count in a particular sample. The clones are short but vary between 7 and 20 (or more). Each list has a header that identifies the sample sequenced. I have a list of 66 samples. Each sample can have several thousand clone strings. Not every clone is contained in every sample so the number of clones listed in the samples varies. Here’s an example of the structure of a single sample list.
$PreAg_18_2
CDR3.aa Count
CASSYGTAYTGELFF 1623
CASSRGDSDNSPLHF 1440
CASSREKAFF 1161
CSGMGALAKNIQYF 949
CSAYTGLSYEQYF 813
CASSLSLAVNSPLHF 634
CAIRDTPGSPQHF 574
CATGQVNTEAFF 555
CASSLKGQGGSPLHF 499
CASSYSRSPQPQHF 478
I want to combine the results in a single table showing the clone counts with all the clone strings listed on the y-axis and the sample ID listed on the x-axis. For example:
10_pep_10_1 preAg_10_2 Dec_2_18_1 …...
CASSYGTAYTGELFF 1623 234 0
CASSRGDSDNSPLHF 1440 522 28
CASSREKAFF 1161 445 50
CSGMGALAKNIQYF 949 24 0
CASSYSRSPQPQHF 478 0 398
.
.
Currently, I'm working with R because that's what Immunarch is in, but I wonder if a) There's a better way to do this in Python, or b) If it's doable in r, then how do I generate the matrix matches. The main problem is that not all the samples have the same number of clones, so I first have to separate each sample data before I can convert it to a data frame, and then how can I match the actual count with the actual clone. I can't just merge the data frames. Any suggestion is greatly appreciated.
Here's what i have for code right now. Code: library(immunarch) library(stringr) library(plyr)
immdata = repLoad("/mnt/data/Development/Analysis_Scripts/input_files/")
all <- immdata$data
# Get list headers (names) and convert to df
sample.id <- names(all)
sample.id <- data.frame(sample.id)
# Get list of clones and filter for unique clones per list.
for (i in 1:length(all)){
all[[i]]$Sample.ID<-names(all)[i]
all[[i]]<-all[[i]][,c("CDR3.aa", "Clones")]
}
# all is the variable that contains the list of samples and clones.
all.split <- split(all, )
# make vector of all clones
all.clones <- unlist(all, use.names=FALSE)
# Removes clone repeats
all.clones.u <- unique(all.clones)
# convert list of clones to data frame
all.clones.u <- data.frame(all.clones.u)
At this point I have both the list of sample ids and the list of clones. My problem is now matching each count with respective sample and clone in the table. Any suggestions?
all
that we can use? But based on what I think you want to do, I'd probably use a combination ofleft_join
of thedplyr
package combined withReduce
. $\endgroup$