0

I have the following code:

condition_1_top_genes = list("abc", "def", "efg")
condition_2_top_genes = list("a35", "2353", "rea3")
condition_3_top_genes = list("fae", "wai", "wtes")

top_genes = list(condition_1_top_genes, 
                 condition_2_top_genes,
                 condition_3_top_genes)

genotypes <- list("genotype1" = genotype1, 
                  "genotype2" = genotype2,
                  "genotype3"  genotype3)

for (i in length(genotypes)){
  for (j in ????){
    FeaturePlot(genotypes[[i]], features = ??)
  }
}

genotypes is a list of Seurat objects.

In the outer loop I'm looping over the Seurat objects, for the inner loop I want to loop over each of the lists in top_genes. So when i = 1 I want j to move along condition_1_top_genes, then when i=2, I want j to move along condition_2_top_genes, etc.

Any help on this syntax would be appreciated.

  • Hi, usually in R we try and avoid for-loops, as most common operations are vectorised. Can you tell us more about what you are trying to do, and make your example reproducible? (We don't have `gene1`, `gene2` etc so we can't replicate `top_genes`). https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Andrea M Jun 16 '22 at 14:58
  • What is `genotype1`, `genotype2`, etc – akrun Jun 16 '22 at 16:01
  • I said in my post, genotypes is a list of Seurat objects. But that part is working...I loop over that list multiple times successfully. I just need to know how to move along the list of list of top_genes. –  Jun 16 '22 at 16:03
  • @WantaghMomma Is it one feature at a time from top_genes – akrun Jun 16 '22 at 16:05
  • Without a reproducible example, it is difficult to test. Perhaps `for (i in seq_along(genotypes)){ FeaturePlot(genotypes[[i]], features = top_genes[[i]]) }` – akrun Jun 16 '22 at 16:06
  • I figured it out: ``` for (i in 1:length(e2f.conditions)){ for (j in 1:length(top_genes)){ print(FeaturePlot(e2f.conditions[[i]], features = top_genes[[i]][[j]])) } } ``` just needed the double brackets around the i and j. –  Jun 16 '22 at 16:11

2 Answers2

0

[, wouldn't extract the list element, we need [[ to go inside the list and extract as vector. Also, we can use a single for loop instead of nested as top_genes showed is not a nested list but a list of vectors

for(i in seq_along(top_genes)) {
     top_genes[[i]] <- seq_along(top_genes[[i]]) * 3
}
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Here's a bit more code: ``` gene1 <- list('a', 'b', 'c') gene2 <- list('d', 'e', 'f') top_genes <- list("gene1" = gene1, "gene2" = gene2) ``` What I want to do is loop over top_genes and print the contents of the gene1 list, then move onto gene2 and print its contents. –  Jun 16 '22 at 15:16
  • @WantaghMomma your new requirement is not clear. Perhaps `for(i in seq_along(top_genes)) print(top_genes[[i]])` – akrun Jun 16 '22 at 15:24
  • Thanks...sorry if I'm not being clear. The actual data can't be shared and would take me a long time to make fake code to share. Thanks anyway –  Jun 16 '22 at 15:38
  • 1
    @WantaghMomma Please consider to provide a small reproducible example I guess I already showed the code for your posted question – akrun Jun 16 '22 at 15:39
  • I'm going to try...I'm editing the original post. The new code isn't reproducible but all I need to know is the syntax to use in place of the ????. I really appreciate your help. –  Jun 16 '22 at 15:55
0
condition_1_top_genes = list("abc", "def", "efg")
condition_2_top_genes = list("a35", "2353", "rea3")
condition_3_top_genes = list("fae", "wai", "wtes")

top_genes = list(condition_1_top_genes, 
                condition_2_top_genes,
                condition_3_top_genes)

If I understand you correctly, you'd probably want to change the way you define the list top_genes.

# Define genes in character vector, allowing you to use concise syntax later on
condition_1_top_genes = c("abc", "def", "efg")
condition_2_top_genes = c("a35", "2353", "rea3")
condition_3_top_genes = c("fae", "wai", "wtes")

# Define list elements with names
top_genes = list(
  condition_1_top_genes = condition_1_top_genes, 
  condition_2_top_genes = condition_2_top_genes,
  condition_3_top_genes = condition_3_top_genes
)

Now you top_genes is a named list, you can access list elements in top_genes with [[""]] operator.

> top_genes[["condition_1_top_genes"]]
[1] "abc" "def" "efg"

# with paste0 function, you can access arbitrary list element
> i=1
> top_genes[[paste0("condition_",i,"_top_genes")]]
[1] "abc" "def" "efg"

# To access jth gene in condition_1_top_genes
> j=1
> top_genes[[paste0("condition_",i,"_top_genes")]][j]
[1] "abc"

So in your for loop, you can do:

for (i in length(genotypes)){
  for (j in length(top_genes[[paste0("condition_",i,"_top_genes")]])){
    FeaturePlot(genotypes[[i]], features = top_genes[[paste0("condition_",i,"_top_genes")]][j])
  }
}