text: user3762: I am looking to create a table (on RStudio) that will show the overlap between 8 different gene lists wherever possible. I have saved the 8 lists as .txt files all in one folder and set that particular folder as my working directory. Any idea of code that will work for this?
  
  If anyone knows what I can alter to make the current code work or suggest an alternative way I would be very happy to hear any suggestions!
  data.file1 <- "GSE108363_BCGdown_V.txt"
  data.file2 <- "GSE108363_BCGdown_D.txt"
  data.file3 <- "GSE108363_BCGup_V.txt"
  data.file4 <- "GSE108363_BCGup_D.txt"
  data.file5 <- "GSE108363_MTBdown_V.txt"
  data.file6 <- "GSE108363_MTBdown_D.txt"
  data.file7 <- "GSE108363_MTBup_V.txt"
  data.file8 <- "GSE108363_MTBup_D.txt"
  
  genevect1 <- scan(data.file1, what=character(), sep="\n")
  genevect2 <- scan(data.file2, what=character(), sep="\n")
  genevect3 <- scan(data.file3, what=character(), sep="\n")
  genevect4 <- scan(data.file4, what=character(), sep="\n")
  genevect5 <- scan(data.file5, what=character(), sep="\n")
  genevect6 <- scan(data.file6, what=character(), sep="\n")
  genevect7 <- scan(data.file7, what=character(), sep="\n")
  genevect8 <- scan(data.file8, what=character(), sep="\n")
  
  
  filelist <- list(data.file1, data.file2, data.file3, data.file4, data.file5, data.file6, data.file7, data.file8)
  #-------------------------------------------------------------------------------------------------------
  # "Lapply" is basically making a "for loop".
  #-------------------------------------------------------------------------------------------------------
  all_gene_vectors <- lapply(filelist, scan, what=character(), sep="\n")
  
  #-------------------------------------------------------------------------------------------------------
  # Then make the intersection of *all* genesets
  #-------------------------------------------------------------------------------------------------------
  final_inter <- all_gene_vectors[[1]]
  for (next_genevect in all_gene_vectors[2:length(all_gene_vectors)]) {
    final_inter <- intersect(final_inter, next_genevect)
  }
  show(final_inter)
  
  #-------------------------------------------------------------------------------------------------------
  # Or compute all pairwise intersections (untested)
  #-------------------------------------------------------------------------------------------------------
  pairwise_inters <- list()
  #-------------------------------------------------------------------------------------------------------
  # Iterate over all possible pairs i,j (i ≠ j)
  #-------------------------------------------------------------------------------------------------------
  N <- length(all_gene_vectors)
  for (i in 1:(N-1)) {
    ith_inters <- list()
    for (j in (i+1):N) {
      ith_inters[[ names(all_gene_vectors)[j ]]] <- intersect(all_gene_vectors[[i]], all_gene_vectors[[j]])
    }
    pairwise_inters[[ names(all_gene_vectors)[i] ]] <- ith_inters
  }
  
  #-------------------------------------------------------------------------------------------------------
  #Generate table in wide format:
  #-------------------------------------------------------------------------------------------------------  
  #Concatenate gene names of all tables to a separate file
  #Extract unique values in this file
  #Perform outer join between the separate file and each table
  
  all_names <- c(data.list)
  for (tp in timepoints){
    all_names <- append(all_names, tables[[tp]]$genes)
  }
  all_names <- unique(all_names)
  
  wide_table <- matrix(nrow=length(all_names), ncol=(1+length(timepoints)))
  wide_table <- as.data.frame(wide_table)
  colnames(wide_table) <- append("genes", timepoints)
  
  for (tp in timepoints){
    tmp <- tables[[tp]]
    tmp$tp <- 1
    tmp <- merge(wide_table, tmp, by="genes", all=T)
    wide_table$tp <- tmp$tp
  }
  
  
  set.seed(11)
  BCG_validation_Up <- sample(letters[1:429], 20)
  BCG_discovery_Up <- sample(letters[1:250], 20)
  MTB_validation_Up <- sample(letters[1:286], 20)
  MTB_discovery_Up <- sample(letters[1:128], 20)
  BCG_validation_Down <- sample(letters[1:267], 20)
  BCG_discovery_Down  <- sample(letters[1:350], 20)
  MTB_validation_Down <- sample(letters[1:244], 20)
  MTB_discovery_Down <- sample(letters[1:86], 20)
  
  cross_table <- matrix(, nrow = 4, ncol = 4)
  rownames(cross_table) <- c("BCG_validation_Up", "BCG_discovery_Up", "MTB_validation_Up", "MTB_discovery_Up")
  colnames(cross_table) <- c("BCG_validation_Down", "BCG_discovery_Down", "MTB_validation_Up", "MTB_discovery_Up")
  
  for (i in 1:4){
    for(j in 1:4){
      cross_table[i,j] <- length(intersect(get(paste0("",i,"_Up")),(get(paste0("",j,"_Down")))))
    }
  } 
  
  cross_table
  
  
  user939: To make a confusion matrix of 4 lists against 4 other lists, you need to get the length of all intersecting genes for each pair. Here an example with letters instead of gene names.
  set.seed(11)
  list1_up <- sample(letters[1:26], 20)
  list1_down <- sample(letters[1:26], 20)
  list2_up <- sample(letters[1:26], 20)
  list2_down <- sample(letters[1:26], 20)
  list3_up <- sample(letters[1:26], 20)
  list3_down <- sample(letters[1:26], 20)
  list4_up <- sample(letters[1:26], 20)
  list4_down <- sample(letters[1:26], 20)
  
  cross_table <- matrix(, nrow = 4, ncol = 4)
  rownames(cross_table) <- c("list1_up", "list2_up", "list3_up", "list4_up")
  colnames(cross_table) <- c("list1_down", "list2_down", "list3_down", "list4_down")
  
  for (i in 1:4){
    for(j in 1:4){
      cross_table[i,j] <- length(intersect(get(paste0("list",i,"_up")),(get(paste0("list",j,"_down")))))
      }
  } 
  
  cross_table
           list1_down list2_down list3_down list4_down
  list1_up         15         14         15         15
  list2_up         18         15         15         15
  list3_up         17         15         14         16
  list4_up         17         15         15         16
  
  
original_text: 3762: <p>I am looking to create a table (on RStudio) that will show the overlap between 8 different gene lists wherever possible. I have saved the 8 lists as .txt files all in one folder and set that particular folder as my working directory. Any idea of code that will work for this?</p>
  
  <p><a href="https://i.stack.imgur.com/Pn9A1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Pn9A1.png" alt="Output table with NA values"></a></p>
  
  <p>If anyone knows what I can alter to make the current code work or suggest an alternative way I would be very happy to hear any suggestions!</p>
  
  <pre><code>data.file1 &lt;- "GSE108363_BCGdown_V.txt"
  data.file2 &lt;- "GSE108363_BCGdown_D.txt"
  data.file3 &lt;- "GSE108363_BCGup_V.txt"
  data.file4 &lt;- "GSE108363_BCGup_D.txt"
  data.file5 &lt;- "GSE108363_MTBdown_V.txt"
  data.file6 &lt;- "GSE108363_MTBdown_D.txt"
  data.file7 &lt;- "GSE108363_MTBup_V.txt"
  data.file8 &lt;- "GSE108363_MTBup_D.txt"
  
  genevect1 &lt;- scan(data.file1, what=character(), sep="\n")
  genevect2 &lt;- scan(data.file2, what=character(), sep="\n")
  genevect3 &lt;- scan(data.file3, what=character(), sep="\n")
  genevect4 &lt;- scan(data.file4, what=character(), sep="\n")
  genevect5 &lt;- scan(data.file5, what=character(), sep="\n")
  genevect6 &lt;- scan(data.file6, what=character(), sep="\n")
  genevect7 &lt;- scan(data.file7, what=character(), sep="\n")
  genevect8 &lt;- scan(data.file8, what=character(), sep="\n")
  
  
  filelist &lt;- list(data.file1, data.file2, data.file3, data.file4, data.file5, data.file6, data.file7, data.file8)
  #-------------------------------------------------------------------------------------------------------
  # "Lapply" is basically making a "for loop".
  #-------------------------------------------------------------------------------------------------------
  all_gene_vectors &lt;- lapply(filelist, scan, what=character(), sep="\n")
  
  #-------------------------------------------------------------------------------------------------------
  # Then make the intersection of *all* genesets
  #-------------------------------------------------------------------------------------------------------
  final_inter &lt;- all_gene_vectors[[1]]
  for (next_genevect in all_gene_vectors[2:length(all_gene_vectors)]) {
    final_inter &lt;- intersect(final_inter, next_genevect)
  }
  show(final_inter)
  
  #-------------------------------------------------------------------------------------------------------
  # Or compute all pairwise intersections (untested)
  #-------------------------------------------------------------------------------------------------------
  pairwise_inters &lt;- list()
  #-------------------------------------------------------------------------------------------------------
  # Iterate over all possible pairs i,j (i ≠ j)
  #-------------------------------------------------------------------------------------------------------
  N &lt;- length(all_gene_vectors)
  for (i in 1:(N-1)) {
    ith_inters &lt;- list()
    for (j in (i+1):N) {
      ith_inters[[ names(all_gene_vectors)[j ]]] &lt;- intersect(all_gene_vectors[[i]], all_gene_vectors[[j]])
    }
    pairwise_inters[[ names(all_gene_vectors)[i] ]] &lt;- ith_inters
  }
  
  #-------------------------------------------------------------------------------------------------------
  #Generate table in wide format:
  #-------------------------------------------------------------------------------------------------------  
  #Concatenate gene names of all tables to a separate file
  #Extract unique values in this file
  #Perform outer join between the separate file and each table
  
  all_names &lt;- c(data.list)
  for (tp in timepoints){
    all_names &lt;- append(all_names, tables[[tp]]$genes)
  }
  all_names &lt;- unique(all_names)
  
  wide_table &lt;- matrix(nrow=length(all_names), ncol=(1+length(timepoints)))
  wide_table &lt;- as.data.frame(wide_table)
  colnames(wide_table) &lt;- append("genes", timepoints)
  
  for (tp in timepoints){
    tmp &lt;- tables[[tp]]
    tmp<span class="math-container">$tp &lt;- 1
    tmp &lt;- merge(wide_table, tmp, by="genes", all=T)
    wide_table$</span>tp &lt;- tmp$tp
  }
  
  
  set.seed(11)
  BCG_validation_Up &lt;- sample(letters[1:429], 20)
  BCG_discovery_Up &lt;- sample(letters[1:250], 20)
  MTB_validation_Up &lt;- sample(letters[1:286], 20)
  MTB_discovery_Up &lt;- sample(letters[1:128], 20)
  BCG_validation_Down &lt;- sample(letters[1:267], 20)
  BCG_discovery_Down  &lt;- sample(letters[1:350], 20)
  MTB_validation_Down &lt;- sample(letters[1:244], 20)
  MTB_discovery_Down &lt;- sample(letters[1:86], 20)
  
  cross_table &lt;- matrix(, nrow = 4, ncol = 4)
  rownames(cross_table) &lt;- c("BCG_validation_Up", "BCG_discovery_Up", "MTB_validation_Up", "MTB_discovery_Up")
  colnames(cross_table) &lt;- c("BCG_validation_Down", "BCG_discovery_Down", "MTB_validation_Up", "MTB_discovery_Up")
  
  for (i in 1:4){
    for(j in 1:4){
      cross_table[i,j] &lt;- length(intersect(get(paste0("",i,"_Up")),(get(paste0("",j,"_Down")))))
    }
  } 
  
  cross_table
  </code></pre>
   939: <p>To make a confusion matrix of 4 lists against 4 other lists, you need to get the length of all intersecting genes for each pair. Here an example with letters instead of gene names.</p>
  
  <pre><code>set.seed(11)
  list1_up &lt;- sample(letters[1:26], 20)
  list1_down &lt;- sample(letters[1:26], 20)
  list2_up &lt;- sample(letters[1:26], 20)
  list2_down &lt;- sample(letters[1:26], 20)
  list3_up &lt;- sample(letters[1:26], 20)
  list3_down &lt;- sample(letters[1:26], 20)
  list4_up &lt;- sample(letters[1:26], 20)
  list4_down &lt;- sample(letters[1:26], 20)
  
  cross_table &lt;- matrix(, nrow = 4, ncol = 4)
  rownames(cross_table) &lt;- c("list1_up", "list2_up", "list3_up", "list4_up")
  colnames(cross_table) &lt;- c("list1_down", "list2_down", "list3_down", "list4_down")
  
  for (i in 1:4){
    for(j in 1:4){
      cross_table[i,j] &lt;- length(intersect(get(paste0("list",i,"_up")),(get(paste0("list",j,"_down")))))
      }
  } 
  
  cross_table
           list1_down list2_down list3_down list4_down
  list1_up         15         14         15         15
  list2_up         18         15         15         15
  list3_up         17         15         14         16
  list4_up         17         15         15         16
  </code></pre>
  