1
$\begingroup$

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?

Output table with NA values

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
$\endgroup$
13
  • $\begingroup$ Please show how the end table should look like, and give a small example to test with. $\endgroup$
    – benn
    Nov 29 '18 at 14:25
  • $\begingroup$ It looks like you already have code. Can you describe why what you have is not acceptable? $\endgroup$
    – winni2k
    Nov 29 '18 at 14:43
  • $\begingroup$ @winni2k Yes but I get the following error code when I attempt to execute it: Error in ith_inters[[names(all_gene_vectors)[j]]] <- intersect(all_gene_vectors[[i]], : attempt to select less than one element in OneIndex $\endgroup$
    – user3762
    Nov 29 '18 at 15:05
  • $\begingroup$ @b.nota I have inserted a screenshot of the table I would like to be filled in, please find it above $\endgroup$
    – user3762
    Nov 29 '18 at 15:08
  • 2
    $\begingroup$ Please don't post screenshots of code! That just makes the question heavier to load, useless for people using screen readers and, most importantly, makes the code you show useless to us. We need to be able to copy your code and run it locally to test any solutions we come up with. Please edit your question and include the code here directly. Use the formatting tools to format it as code. $\endgroup$
    – terdon
    Nov 29 '18 at 15:40
1
$\begingroup$

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
$\endgroup$
4
  • $\begingroup$ Please could you look at my attempt at altering this code to suit my data and see where I have gone wrong? I have added the code to my question above $\endgroup$
    – user3762
    Nov 30 '18 at 0:26
  • 1
    $\begingroup$ No, I will not look at your attempt. Here is my suggestion, do with it what you want. $\endgroup$
    – benn
    Nov 30 '18 at 7:51
  • $\begingroup$ @user3762 It would be better if you describe what has gone wrong and how the solution here fails you. Also show your attempts to fix or adapt this solution into one that works for you. Good luck! $\endgroup$
    – llrs
    Nov 30 '18 at 8:18
  • $\begingroup$ @benn nice manners $\endgroup$ Dec 5 '18 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.