################### # Clear workspace # ################### rm(list = ls()) ################## # Load libraries # ################## library(tidyverse) library(melonnpan) library(data.table) ################# # Load datasets # ################# metab<-fread('Metabolite training.txt') metag<-fread('GENEFAMILY_TRAINING.txt') ################### # Assign rownames # ################### metab<-column_to_rownames(metab, 'SAMPLE') metag<-column_to_rownames(metag, 'SAMPLE') ########################## # Non-specific filtering # ########################## abd_threshold = 1e-08 prev_threshold = 0.05 metab<- metab[, colSums(metab > abd_threshold) > nrow(metab)* prev_threshold] # Did not remove anything ################### # Train MelonnPan # ################### DD<-melonnpan::melonnpan.train(metab = metab, metag = metag, output = getwd()) ############################ # Subset valid predictions # ############################ predicted_summary<-fread('./MelonnPan_Training_Summary.txt') predicted_summary<-predicted_summary[predicted_summary$Model.Size>1,] predicted_summary$Q.value<-p.adjust(predicted_summary$P.value) predicted_summary<-predicted_summary[order(predicted_summary$Q.value, decreasing = FALSE),] ################################################ # Subset weight matrix and predicted compounds # ################################################ predicted_compounds<-fread('./MelonnPan_Trained_Metabolites.txt') predicted_compounds<-column_to_rownames(predicted_compounds, 'ID') predicted_compounds<-predicted_compounds[, predicted_summary$ID] predicted_compounds<-rownames_to_column(predicted_compounds, 'ID') weight_matrix<-fread('./MelonnPan_Trained_Weights.txt') weight_matrix<-column_to_rownames(weight_matrix, 'ID') weight_matrix<-weight_matrix[, predicted_summary$ID] weight_matrix<-rownames_to_column(weight_matrix, 'ID') ########################## # Replace original files # ########################## write.table(predicted_summary, 'MelonnPan_Training_Summary_2.txt', row.names = F, col.names = T, quote = F, sep = "\t") write.table(predicted_compounds, 'MelonnPan_Trained_Metabolites_2.txt', row.names = F, col.names = T, quote = F, sep = "\t") write.table(weight_matrix, 'MelonnPan_Trained_Weights_2.txt', row.names = F, col.names = T, quote = F, sep = "\t")