Mercurial > repos > workflow4metabolomics > correlation_analysis
comparison correlation_analysis.r @ 0:58997c28b268 draft default tip
"planemo upload for repository https://github.com/workflow4metabolomics/tools-metabolomics/blob/master/tools/correlation_analysis/ commit 35a01e4ef59a91f43d0b1de1d08db29dcc7aae1e"
| author | workflow4metabolomics |
|---|---|
| date | Tue, 19 Jan 2021 16:41:47 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:58997c28b268 |
|---|---|
| 1 #!/usr/local/public/bin/Rscript --vanilla --slave --no-site-file | |
| 2 #For questions: Antoine Gravot (Protocole conception) and Misharl Monsoor (for galaxy wrapper and R script). | |
| 3 | |
| 4 #Load the different libraries | |
| 5 library(batch) #necessary for parseCommandArgs function | |
| 6 library(reshape) #necessary for using melt function | |
| 7 library(MASS) # necessary for using the write.matrix() | |
| 8 #interpretation of arguments given in command line as an R list of objects | |
| 9 list_arguments <- parseCommandArgs(evaluate = FALSE) | |
| 10 | |
| 11 cat("\nJob starting time:\n", format(Sys.time(), "%a %d %b %Y %X"), | |
| 12 "\n\n--------------------------------------------------------------------", | |
| 13 "\nParameters used in 'Metabolites Correlation Analysis':\n\n") | |
| 14 print(list_arguments) | |
| 15 cat("--------------------------------------------------------------------\n\n") | |
| 16 | |
| 17 #The main function of this script that will execute all the other functions | |
| 18 | |
| 19 | |
| 20 main_function <- function(sorting, variable_metadata, data_matrix, sample_metadata, corrdel, param_correlation, param_cytoscape, matrix_corr, user_matrix_corr, corr_method) { | |
| 21 | |
| 22 | |
| 23 if (sorting == 1) { | |
| 24 ####Executing the sorting function#### | |
| 25 cat("\nExecuting the sorting function\n") | |
| 26 #Read the tsv annotateDiffreport file and don't modify the columns name (check.names = FALSE) | |
| 27 variable_metadata_input <- read.csv(variable_metadata, header = T, sep = "\t", dec = ".", check.names = FALSE) | |
| 28 data_matrix_input <- read.csv(data_matrix, header = T, sep = "\t", dec = ".", check.names = FALSE) | |
| 29 first_column_variable <- toString(names(variable_metadata_input)[1]) | |
| 30 first_column_datamatrix <- toString(names(data_matrix_input)[1]) | |
| 31 #@TODO merge | |
| 32 input_tsv <- cbind(variable_metadata_input, data_matrix_input[, !(colnames(data_matrix_input) %in% c(first_column_datamatrix))]) | |
| 33 #Load the sample.info from the xcmsSet | |
| 34 sample_metadata_info_tsv <- read.table(sample_metadata, header = T, sep = "\t", dec = ",", check.names = FALSE) | |
| 35 #Extract the samples name from the sample.info file generated from the xcmsSet step in ABIMS Workflow4Metabo. | |
| 36 samples_name <- as.vector(t(sample_metadata_info_tsv[1])) | |
| 37 output_tsv <- sorting(input_tsv, samples_name) | |
| 38 # output now if corrdel == 0 | |
| 39 if (corrdel == 0) { | |
| 40 output_tsv_vm <- output_tsv[, which(!(colnames(output_tsv) %in% samples_name))] | |
| 41 write.table(output_tsv_vm[, c(3:ncol(output_tsv_vm), 2, 1)], sep = "\t", quote = FALSE, col.names = TRUE, row.names = FALSE, file = "sorted_table.tsv") | |
| 42 } | |
| 43 | |
| 44 } | |
| 45 if (corrdel == 1) { | |
| 46 cat("\nExecuting the corr_matrix_del function\n") | |
| 47 corr_matrix_del(output_tsv, samples_name, param_correlation, param_cytoscape, corr_method) | |
| 48 } | |
| 49 | |
| 50 if (matrix_corr == 1) { | |
| 51 cat("\nExecuting the corr_matrix function\n") | |
| 52 corr_matrix_user(user_matrix_corr, param_cytoscape, corr_method) | |
| 53 | |
| 54 } | |
| 55 | |
| 56 } | |
| 57 | |
| 58 #The sorting function will sort the dataframe by rt column. | |
| 59 #Then it creates a "signal_moy" column which contains the mean values of the signal values of the sample by row. | |
| 60 #It finally creates a table tsv format "sorted_table.tsv". | |
| 61 | |
| 62 sorting <- function(input_tsv, samples_name) { | |
| 63 | |
| 64 #Sort by rt column | |
| 65 new_input <- input_tsv[order(input_tsv$rt), ] | |
| 66 #Compute the mean operation of all the signal values of the sample by row, and put the results in a new column "signal_moy" | |
| 67 new_input["signal_moy"] <- data.frame(Means = rowMeans(new_input[, colnames(new_input) %in% samples_name])) | |
| 68 #Rearrange the data frame in order to have the columns "signal_moy" and "pcgroup" at the beginning of the table | |
| 69 new_input <- cbind(new_input["signal_moy"], new_input["pcgroup"], new_input[, !(colnames(new_input) %in% c("signal_moy", "pcgroup"))]) | |
| 70 #Sort the "signal_moy" column data frame by pcgroup | |
| 71 new_input <- new_input[order(new_input$pcgroup, -new_input$signal_moy), ] | |
| 72 #Write the data frame to a file named "sorted_table.tsv" | |
| 73 #Erase the rownames of the table | |
| 74 rownames(new_input) <- NULL | |
| 75 #Suppress rows which contains Nas | |
| 76 new_input <- new_input[complete.cases(new_input$pcgroup), ] | |
| 77 return(new_input) | |
| 78 } | |
| 79 | |
| 80 corr_matrix_del <- function(output_tsv, samples_name, param_correlation, param_cytoscape, corr_method) { | |
| 81 | |
| 82 #statmatrix, a dataframe which contains only the columns "name" and the values of the intensity for all the samples | |
| 83 first_column <- toString(names(output_tsv)[3]) | |
| 84 statmatrix <- output_tsv[(names(output_tsv) %in% c(first_column, samples_name))] | |
| 85 statmatrix2 <- output_tsv[(names(output_tsv) %in% c(first_column, "signal_moy", samples_name))] | |
| 86 n <- statmatrix[, first_column] | |
| 87 #transpose all but the first column (name) | |
| 88 statmatrix <- as.data.frame(t(statmatrix[, -1])) | |
| 89 #Rename the columns of the dataframe "statmatrix" | |
| 90 colnames(statmatrix) <- n | |
| 91 #Transform dataframe to matrix before doing the cor function | |
| 92 corr_transpo <- data.matrix(statmatrix) | |
| 93 #Create a matrix which contains only the data of the samples without the other columns | |
| 94 statmatrix <- output_tsv[(names(output_tsv) %in% c(samples_name))] | |
| 95 #Add the rownames previously | |
| 96 rownames(statmatrix) <- make.unique(as.vector(output_tsv[, first_column])) | |
| 97 #Do the cor step, with the transposed statmatrix (metabolites as columns) | |
| 98 corr_transpo <- cor(t(as.matrix(statmatrix)), method = corr_method) | |
| 99 #Add the columns "signal_moy", "pcgroup" and "name" to the final correlation matrix output | |
| 100 new_dataframe <- cbind(output_tsv["signal_moy"], output_tsv["pcgroup"], output_tsv[names(output_tsv)[3]], corr_transpo) | |
| 101 #Add a new column "suppress" which indicates if the metabolite will be removed from the analysis (because it is correlated with the other metabolite) | |
| 102 new_dataframe["suppress"] <- "" | |
| 103 #Take the pcgroup names | |
| 104 pcgroups <- unique(new_dataframe$pcgroup) | |
| 105 #Remove NAs from the pcgroups vector | |
| 106 pcgroups <- pcgroups[which(pcgroups != "NA")] | |
| 107 #Creates a pcgroups list | |
| 108 list_data <- vector(mode = "list", length = length(pcgroups)) | |
| 109 #Will do the following steps by pcgroup | |
| 110 for (pcgroup in pcgroups) { | |
| 111 #Select a subset data frame for each pcgroup | |
| 112 subset_data <- new_dataframe[new_dataframe$pcgroup == pcgroup, ] | |
| 113 #Stores the metabolites names for each pcgroup into a matrix one dimension. | |
| 114 pc_group_elements <- t(as.matrix(subset_data[3])) | |
| 115 #uses the matrix "pc_group_elements" containing the metabolites to have the correlation data frame "subset_data2" by pcgroup | |
| 116 subset_data2 <- subset_data[names(subset_data) %in% c(names(output_tsv)[3], pc_group_elements)] | |
| 117 #We keep the first metabolites for each pcgroup that correspond to the metabolites with the highest amount of signal | |
| 118 first_metabolite <- pc_group_elements[1] | |
| 119 #Remove the first metabolite from the pc_group_elements matrix | |
| 120 pc_group_elements <- pc_group_elements[, c(seq_len(length(pc_group_elements)))] | |
| 121 #print (pc_group_elements) | |
| 122 for (metabolite in pc_group_elements) { | |
| 123 metabolite_dataframe <- subset_data2[subset_data2[, first_column] == metabolite, ] | |
| 124 #retrives metabolite index from the vector object "pc_group_elements" | |
| 125 index_metabolite <- as.numeric(which(pc_group_elements == metabolite, arr.ind = TRUE)) | |
| 126 for (f in 2:index_metabolite - 1) { | |
| 127 if (metabolite != first_metabolite) { | |
| 128 value_intensity <- t(data.matrix(metabolite_dataframe[, pc_group_elements[f]])) | |
| 129 value_intensity <- value_intensity[1:1] | |
| 130 #If one value is >0.75, it means that the metabolite est correlated with at least one previous metabolite in the table, so the boolean variable is set to true. | |
| 131 if (value_intensity > param_correlation) { | |
| 132 new_dataframe$suppress[new_dataframe[, first_column] == metabolite] <- "DEL" | |
| 133 } | |
| 134 } | |
| 135 } | |
| 136 } | |
| 137 } | |
| 138 #The dataframe with the column "suppress" at the begginning | |
| 139 new_dataframe_suppression <- cbind(new_dataframe[first_column], new_dataframe["suppress"], output_tsv["rt"], new_dataframe["signal_moy"], new_dataframe[, !(colnames(new_dataframe) %in% c(first_column, "suppress", "pcgroup", "signal_moy"))]) | |
| 140 | |
| 141 | |
| 142 #remove all the rows which have the "suppress" data and keep only the selected metabolites | |
| 143 selected_metabolite_dataframe <- new_dataframe_suppression[new_dataframe_suppression$suppress != "DEL", ] | |
| 144 #Keep the metabolites selected in a list | |
| 145 metabolites_selected_list <- as.vector(t(selected_metabolite_dataframe[1])) | |
| 146 #Add the other columns to keep | |
| 147 metabolites_selected_list <- c(metabolites_selected_list, first_column, "rt", "signal_moy") | |
| 148 #Keep the metabolites selected to keep only the columns | |
| 149 selected_metabolite_dataframe <- selected_metabolite_dataframe[, colnames(selected_metabolite_dataframe) %in% metabolites_selected_list] | |
| 150 | |
| 151 #Export to siff table format for visualization in cytoscape for the selected metabolites | |
| 152 siff_table <- melt(selected_metabolite_dataframe[, !colnames(selected_metabolite_dataframe) %in% c("pcgroup", "rt", "signal_moy")]) | |
| 153 #Remove the values equal to 1 (correlation between two metabolite identical) | |
| 154 siff_table <- siff_table[siff_table$value != 1, ] | |
| 155 #Keep only the values corresponding to the param_cytoscape | |
| 156 siff_table <- siff_table[siff_table$value >= param_cytoscape, ] | |
| 157 #Change the order of the columns | |
| 158 siff_table <- cbind(siff_table[first_column], siff_table["value"], siff_table["variable"]) | |
| 159 | |
| 160 | |
| 161 | |
| 162 #Join the two datasets to keep only the selected metabolite, with all the information about the intensity value for statistics analysis in the next step of the workflow. | |
| 163 joined_dataframe <- merge(statmatrix2, selected_metabolite_dataframe[first_column], by = first_column) | |
| 164 #Order by the signal intensity | |
| 165 joined_dataframe <- joined_dataframe[order(-joined_dataframe$signal_moy), ] | |
| 166 #Transposition of the dataframe | |
| 167 joined_dataframe <- joined_dataframe[, !(colnames(joined_dataframe) %in% c("signal_moy"))] | |
| 168 | |
| 169 | |
| 170 #Write the different tables into files | |
| 171 write.table(selected_metabolite_dataframe, sep = "\t", quote = FALSE, col.names = TRUE, row.names = FALSE, file = "correlation_matrix_selected.tsv") | |
| 172 write.table(siff_table, sep = "\t", quote = FALSE, col.names = FALSE, row.names = FALSE, file = "siff_table.tsv") | |
| 173 output_tsv_vm <- output_tsv[, which(!(colnames(output_tsv) %in% samples_name))] | |
| 174 output_tsv_vm <- data.frame(output_tsv_vm[, c(3:ncol(output_tsv_vm), 2, 1)], ori.or = seq_len(nrow(output_tsv_vm))) | |
| 175 output_tsv_vm <- merge(x = output_tsv_vm, y = new_dataframe[, c(3, which(colnames(new_dataframe) == "suppress"))], by.x = 1, by.y = 1, sort = FALSE) | |
| 176 output_tsv_vm <- output_tsv_vm[order(output_tsv_vm$ori.or), ][, -c(which(colnames(output_tsv_vm) == "ori.or"))] | |
| 177 write.table(output_tsv_vm, sep = "\t", quote = FALSE, col.names = TRUE, row.names = FALSE, file = "sorted_table.tsv") | |
| 178 | |
| 179 } | |
| 180 | |
| 181 | |
| 182 corr_matrix_user <- function(user_matrix_corr, param_cytoscape, corr_method) { | |
| 183 #read the input table | |
| 184 input_tsv <- read.csv(user_matrix_corr, header = T, sep = "\t", dec = ".", check.names = FALSE) | |
| 185 n <- input_tsv$HD | |
| 186 #transpose all but the first column (name) | |
| 187 statmatrix_corr <- as.data.frame(t(input_tsv[, -1])) | |
| 188 #Rename the columns of the dataframe "statmatrix" | |
| 189 colnames(statmatrix_corr) <- n | |
| 190 #Do the cor step, with the transposed statmatrix. | |
| 191 corr_transpo <- cor(t(as.matrix(statmatrix_corr)), method = corr_method) | |
| 192 #Write the matrix to a tsv file | |
| 193 write.table(corr_transpo, sep <- "\t", quote = FALSE, col.names = NA, file = "correlation_matrix.tsv") | |
| 194 #Export to siff table format for visualization in cytoscape for the selected conditions | |
| 195 siff_table <- melt(corr_transpo) | |
| 196 #Remove the values equal to 1 (correlation between two metabolite identical) | |
| 197 siff_table <- siff_table[siff_table$value != 1, ] | |
| 198 #Keep only the values corresponding to the param_cytoscape | |
| 199 siff_table <- siff_table[siff_table$value >= param_cytoscape, ] | |
| 200 #Change the order of the columns | |
| 201 siff_table <- cbind(Node1 = siff_table["X1"], interaction = siff_table["value"], Node2 = siff_table["X2"]) | |
| 202 #Write the siff table | |
| 203 write.table(siff_table, sep = "\t", quote = FALSE, col.names = FALSE, row.names = FALSE, file = "siff_table.tsv") | |
| 204 | |
| 205 } | |
| 206 do.call(main_function, list_arguments) | |
| 207 | |
| 208 | |
| 209 cat("\n--------------------------------------------------------------------", | |
| 210 "\nInformation about R (version, Operating System, attached or loaded packages):\n\n") | |
| 211 sessionInfo() | |
| 212 cat("--------------------------------------------------------------------\n", | |
| 213 "\nJob ending time:\n", format(Sys.time(), "%a %d %b %Y %X")) |
