#Checking for similar genes in both data sets

#Required libraries
library(MASS)
library(readr)
library(dplyr)

Check2Match <- function(){
    numrows <- "How many rows of clinical data are their in the each data set?: " %>%
	readline(prompt = .) %>%
	strsplit(.,split = ",") %>%
	.[[1]] %>%
	as.integer(.)
    i <- 1
    ANDIS <- select.list(choices = list.files(),multiple = TRUE, title = "Choose the file/files you want to analyze:")

    for(i in 1:length(numrows)){
	    if( i == 1){
		    edfile <- ANDIS[i]
		    ed <- edfile %>%
			    read_delim(.,delim = "\t")
		    ednocd <- ed[-(1:numrows[1]),]
		    #Second file brought in
		    eddfile <- ANDIS[i + 1]
		    edd <- eddfile %>%
			    read_delim(.,delim = "\t")	
		    eddnocd <- edd[-(1:numrows[2]),]
        
		    ##Fully matched both data sets
		    eddy <- inner_join(ednocd,eddnocd)
		    #Matches		
		    meds <- eddy[,1]
		
	    }	
	    if(i > 1 && i + 1 < length(numrows)){
		    eddfile <- ANDIS[i + 1]
		    edd <- eddfile %>%
		    	read_delim(.,delim = "\t")	
		    eddnocd <- edd[-(1:numrows[i + 1]),]
		    ##Fully matched both data sets
		    eddy <- inner_join(meds,eddnocd)
		    meds <- eddy[,1]
	
		
	    }
	    i <- i + 1
    }
    meds
    for(j in 1:length(numrows)){
	    edfile <- ANDIS[j]
	    ed <- edfile %>%
    		read_delim(.,delim = "\t")
    	ednocd <- ed[-(1:numrows[j]),]
    	#use meds to match
    	eddy <- inner_join(meds,ednocd)
    	Finedm <- rbind(ed[1:numrows[j],],eddy)
    	nam_fil_ed <- strsplit(edfile,"[\\|/]") %>%
    		.[[1]] %>%
    		.[length(.)] %>%
    		gsub("\\D","",.) %>%
    		c("GSE",.,"matched.txt") %>%
    		paste(collapse = "")
    	write.table(Finedm,file = nam_fil_ed,sep = "\t",row.names = FALSE)
    	j <- j + 1
    }
    meds	
}

Check2Match()