title: How can you extract gene names from overlapping gene lists
body: <p>I currently have a script looking at the gene overlap between different gene lists. So what the code returns is an eight by eight matrix with the different number of genes found overlapping (i.e. common to both lists). Is there a way I can look at these specific genes and find out the gene symbols for them?</p>
  
  <pre><code>#-------------------------------------------------------------------------------
  # Set the working directory and load the data files
  #-------------------------------------------------------------------------------
  setwd("~/Desktop/R_Project/Gene_overlap")
  getwd()
  files &lt;- list.files(pattern="*.txt", full.names = TRUE)
  files
  
  data.list &lt;- lapply(files, function(fil) {
    scan(file=fil, what=character())
  })
  
  names(data.list) &lt;- basename(files) %&gt;% stringr::str_remove("\\.txt$")
  
  str(data.list)
  # List of 8
  # $ GSE108363_BCGdown_D:chr [1:350] "IL1B" "IL6" "IL1A" "CCL20" ...
  # $ GSE108363_BCGdown_V: chr [1:267] "IL6" "CCL20" "IL1A" "CXCL5" ...
  # $ GSE108363_BCGup_D  : chr [1:250] "FABP4" "CMTM2" "FUCA1" "CD36" ...
  # $ GSE108363_BCGup_V  : chr [1:429] "FCN1" "FCGR3B" "MNDA" "CPVL" ...
  # $ GSE108363_MTBdown_D: chr [1:86] "CCL20" "IL1B" "IL1A" "IL6" ...
  # $ GSE108363_MTBdown_V: chr [1:244] "IL1B" "IL1A" "CCL20" "IL6" ...
  # $ GSE108363_MTBup_D  : chr [1:128] "FUCA1" "FGL2" "TGFBI" "CPVL" ...
  # $ GSE108363_MTBup_V  : chr [1:286] "FABP4" "RNASE1" "MNDA" "CPVL" ...
  
  intersect(data.list$GSE108363_BCGdown_D, data.list$GSE108363_BCGdown_V) %&gt;% length
  
  sapply(data.list, length)
  
  
  
  #-------------------------------------------------------------------------------
  # Using the intersect function to see the overlaps 
  #-------------------------------------------------------------------------------
  
  data.file1 &lt;- "GSE108363_BCGdown_D.txt"
  data.file2 &lt;- "GSE108363_BCGdown_V.txt"
  data.file3 &lt;- "GSE108363_BCGup_D.txt"
  data.file4 &lt;- "GSE108363_BCGup_V.txt"
  data.file5 &lt;- "GSE108363_MTBdown_D.txt"
  data.file6 &lt;- "GSE108363_MTBdown_V.txt"
  data.file7 &lt;- "GSE108363_MTBup_D.txt"
  data.file8 &lt;- "GSE108363_MTBup_V.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)
  all(sapply(filelist, file.exists))
  
  #-------------------------------------------------------------------------------
  # read files:
  #-------------------------------------------------------------------------------
  
  gene.lists &lt;- lapply(filelist, function(f) {
    scan(file=f, what=character())
  })
  
  
  #-------------------------------------------------------------------------------
  # set up empty matrix for overlaps
  #-------------------------------------------------------------------------------
  x &lt;- (length(gene.lists))^2
  x
  y &lt;- rep(NA, x)
  mx &lt;- matrix(y, ncol=length(gene.lists))
  mx
  row.names(mx) &lt;- sapply(filelist, basename) %&gt;% stringr::str_remove('.txt$')
  colnames(mx) &lt;- sapply(filelist, basename) %&gt;% stringr::str_remove('.txt$')
  mx
  
  mx.overlap.count &lt;- mx
  
  #-------------------------------------------------------------------------------
  # Overlaps
  #-------------------------------------------------------------------------------
  for (i in seq_along(gene.lists)) {
    g1 &lt;- gene.lists[[i]]
    for (j in seq_along(gene.lists)) {
      g2 &lt;- gene.lists[[j]]
      a &lt;- intersect(g1, g2)
      b &lt;- length(a)
      mx.overlap.count[j,i] &lt;- b
    }
  }
  
  mx.overlap.count
  round(as.numeric(mx.overlap.count),digits = 1)
  View(mx.overlap.count)
  </code></pre>
  
  <p>At the moment, this code returns numerical values. However, I would like to generate some sort of list for each two gene lists being compared (or something similar) so that I can look to see exactly which genes were common to both. </p>