Search code examples
rstringlistassignnames

creating iterative named lists, with variable names including with operators


SOLVED

pheno_vector = c("PAX5+PDL1-", "CD3+CD8+PD1-", "CD3+CD8-PD1+")
n = length(pheno_vector)
for (i in 1:(n-1)){
  for (j in (i+1):n){
    pheno1 = pheno_vector[i]
    pheno2 = pheno_vector[j]
    pairs = list(c(pheno1, pheno2))

    colors = c("cyan", "magenta")
    names(colors) = c(pheno1,pheno2)
    print(colors)

produces

               PAX5+PDL1- CD3+CD8+PD1- 
                "cyan"    "magenta" 
               PAX5+PDL1- CD3+CD8-PD1+ 
                "cyan"    "magenta" 
             CD3+CD8+PD1- CD3+CD8-PD1+ 
                "cyan"    "magenta"

I have a problem with creating a named list, in which the names should be able to iteratively change, but should be pasted for visual plotting.

I am working with data from Vectra Imaging, involving phenotypes of cells.

As there are multiple phenotypes, i want to iteratively make pairs of phenotypes to visually make plots as shown in the example in https://rdrr.io/github/PerkinElmer/phenoptr/man/spatial_distribution_report.html

Currently my code adapted to my data looks like this

pheno1 = "PAX5+PDL1-"
pheno2 = "CD3+CD8+PD1-"
pairs = list(c("PAX5+PDL1-", "CD3+CD8+PD1-"))
colors = c('PAX5+PDL1-'="cyan", 'CD3+CD8+PD1-'="magenta")
colors

This returns

>  PAX5+PDL1- CD3+CD8+PD1- 
>     "cyan"    "magenta" 

The pairs and colors is used to make the plots. Now I want to iteratively make plots for more pairs of phenotypes that I have in the data. In the end I'd like to for-loop over pheno1 and for-loop over pheno2 obtained from a general phenotypes-vector pheno_vector which contains .

What I tried is

pheno1 = "PAX5+PDL1-"
pheno2 = "CD3+CD8+PD1-"
pairs = list(c(pheno1, pheno2))
colors = c(pheno1="cyan", pheno2="magenta")
colors

Which returns

>     pheno1     pheno2 
>     "cyan"    "magenta" 

I understand why I see pheno1 and pheno2, and not "PAX5+PDL1-" and "CD3+CD8+PD1-". I tried some paste and eval methods as in Create a variable name with "paste" in R? but that did not work out.

Assign("PAX5+PDL1-", "cyan")

does not work out as I don't know how to make it into a named list then.

In the end I'd like to have this as a result

pheno_vector = c("PAX5+PDL1-", "CD3+CD8+PD1-", "CD3+CD8-PD1+")
for (pheno1 in pheno_vector){
 for (pheno2 in pheno_vector){
  if (uniqueness_statement and permutation_statement){
     pairs = list(c(pheno1, pheno2))
     colors = c(pheno1="cyan", pheno2="magenta")
     colors

Which should return

>     PAX5+PDL1-     CD3+CD8+PD1- 
>     "cyan"             "magenta" 
>     PAX5+PDL1-     CD3+CD8-PD1+ 
>     "cyan"              "magenta" 
>     CD3+CD8+PD1-   CD3+CD8-PD1+
>     "cyan"           "magenta" 

So these are specifically named lists, which they need to be for later plotting.

Doe anyone have a solution for this?


Solution

  • You could name the colors-vector once it is created:

    library(gtools)
    
    pheno_perms <- permutations(length(pheno_vector), 2, pheno_vector)
    combs <- apply(apply(pheno_perms, 1, sort), 2, paste0, collapse=' ')
    pheno_perms <- pheno_perms[match(unique(combs), combs), ]
    
    for (i in 1:nrow(pheno_perms)){
      pheno1 <- pheno_perms[i, 1]
      pheno2 <- pheno_perms[i, 2]
      pairs = list(c(pheno1, pheno2))
      colors = c("cyan", "magenta")
      names(colors) <- c(pheno1, pheno2)
      print(colors)
    }
    
    

    This produces:

    CD3+CD8-PD1+ CD3+CD8+PD1- 
          "cyan"    "magenta" 
    CD3+CD8-PD1+   PAX5+PDL1- 
          "cyan"    "magenta" 
    CD3+CD8+PD1-   PAX5+PDL1- 
          "cyan"    "magenta" 
    

    P.S.: I hope I interpret correctly what you mean by uniqueness_statement and permutation_statement!