Skip to content

Commit

Permalink
Added tibble::column_to_rownames
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristinaSchmidt1 committed Oct 31, 2024
1 parent b0d28df commit 5c8badc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
12 changes: 6 additions & 6 deletions R/MetaDataAnalysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ MetaAnalysis <- function(InputData,

# Extract loadings for each PC
PCA.res_Loadings <- as.data.frame(PCA.res$rotation)%>%
rownames_to_column("FeatureID")
tibble::rownames_to_column("FeatureID")

#--- 2. Merge with demographics
PCA.res_Info <- merge(x=SettingsFile_Sample%>%rownames_to_column("UniqueID") , y=PCA.res_Info%>%rownames_to_column("UniqueID"), by="UniqueID", all.y=TRUE)%>%
column_to_rownames("UniqueID")
tibble::column_to_rownames("UniqueID")

#--- 3. convert columns that are not numeric to factor:
## Demographics are often non-numerical, categorical explanatory variables, which is often stored as characters, sometimes integers
Expand Down Expand Up @@ -149,7 +149,7 @@ MetaAnalysis <- function(InputData,

#Add explained variance into the table:
prop_var_ex <- as.data.frame(((PCA.res[["sdev"]])^2/sum((PCA.res[["sdev"]])^2))*100)%>%#To compute the proportion of variance explained by each component in percent, we divide the variance by sum of total variance and multiply by 100(variance=standard deviation ^2)
rownames_to_column("PC")%>%
tibble::rownames_to_column("PC")%>%
mutate(PC = paste("PC", PC, sep=""))%>%
dplyr::rename("Explained_Variance"=2)

Expand Down Expand Up @@ -243,7 +243,7 @@ MetaAnalysis <- function(InputData,
select(term, PC, Explained_Variance)

Data_Heat <- reshape2::dcast( Data_Heat, term ~ PC, value.var = "Explained_Variance")%>%
column_to_rownames("term")%>%
tibble::column_to_rownames("term")%>%
mutate_all(~replace(., is.na(.), 0))

#Plot
Expand Down Expand Up @@ -325,11 +325,11 @@ MetaPK <- function(InputData,
if(is.null(SettingsInfo)==TRUE){
MetaData <- names(SettingsFile_Sample)
SettingsFile_Sample <- SettingsFile_Sample%>%
rownames_to_column("SampleID")
tibble::rownames_to_column("SampleID")
}else{
MetaData <- SettingsInfo
SettingsFile_Sample_subset <- SettingsFile_Sample[, MetaData, drop = FALSE]%>%
rownames_to_column("SampleID")
tibble::rownames_to_column("SampleID")
}

# Convert into a pathway DF
Expand Down
33 changes: 17 additions & 16 deletions vignettes/Metadata Analysis.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ library(magrittr)
library(dplyr)
library(rlang)
library(tidyr)
library(tibble)
#Please install the Biocmanager Dependencies:
#BiocManager::install("clusterProfiler")
Expand Down Expand Up @@ -263,16 +264,16 @@ Here we can also use the `MetaproViz::VizVolcano()` function to plot comparisons
```{r}
#Early versus Late Stage
MetaProViz::VizVolcano(PlotSettings="Compare",
InputData=ResList[["EarlyStage"]][["DMA"]][["TUMOR_vs_NORMAL"]]%>%column_to_rownames("Metabolite"),
InputData2= ResList[["LateStage"]][["DMA"]][["TUMOR_vs_NORMAL"]]%>%column_to_rownames("Metabolite"),
InputData=ResList[["EarlyStage"]][["DMA"]][["TUMOR_vs_NORMAL"]]%>%tibble::column_to_rownames("Metabolite"),
InputData2= ResList[["LateStage"]][["DMA"]][["TUMOR_vs_NORMAL"]]%>%tibble::column_to_rownames("Metabolite"),
ComparisonName= c(InputData="EarlyStage", InputData2= "LateStage"),
PlotName= "EarlyStage-TUMOR_vs_NORMAL compared to LateStage-TUMOR_vs_NORMAL",
Subtitle= "Results of DMA" )
# Young versus Old
MetaProViz::VizVolcano(PlotSettings="Compare",
InputData=ResList[["Young"]][["DMA"]][["TUMOR_vs_NORMAL"]]%>%column_to_rownames("Metabolite"),
InputData2= ResList[["Old"]][["DMA"]][["TUMOR_vs_NORMAL"]]%>%column_to_rownames("Metabolite"),
InputData=ResList[["Young"]][["DMA"]][["TUMOR_vs_NORMAL"]]%>%tibble::column_to_rownames("Metabolite"),
InputData2= ResList[["Old"]][["DMA"]][["TUMOR_vs_NORMAL"]]%>%tibble::column_to_rownames("Metabolite"),
ComparisonName= c(InputData="Young", InputData2= "Old"),
PlotName= "Young-TUMOR_vs_NORMAL compared to Old-TUMOR_vs_NORMAL",
Subtitle= "Results of DMA" )
Expand Down Expand Up @@ -301,14 +302,14 @@ ggVennDiagram::ggVennDiagram(list(Top = top_entries,
MetaData_Metab <- merge(x=Tissue_MetaData,
y= MetaRes[["res_summary"]][, c(1,5:6) ]%>%column_to_rownames("FeatureID"),
y= MetaRes[["res_summary"]][, c(1,5:6) ]%>%tibble::column_to_rownames("FeatureID"),
by=0,
all.y=TRUE)%>%
column_to_rownames("Row.names")
#Make a Volcano plot:
MetaProViz::VizVolcano(PlotSettings="Standard",
InputData=ResList[["TissueType"]][["DMA"]][["TUMOR_vs_NORMAL"]]%>%column_to_rownames("Metabolite"),
InputData=ResList[["TissueType"]][["DMA"]][["TUMOR_vs_NORMAL"]]%>%tibble::column_to_rownames("Metabolite"),
SettingsFile_Metab = MetaData_Metab,
SettingsInfo = c(color = "MainDriver_Term"),
PlotName= "TISSUE_TYPE-TUMOR_vs_NORMAL",
Expand Down Expand Up @@ -347,14 +348,14 @@ Now we can use this information to colour code our volcano plot:
```{r, eval=FALSE}
#Add metabolite information such as KEGG ID or pathway to results
MetaData_Metab <- merge(x=Tissue_MetaData,
y= MCAres[["MCA_2Cond_Results"]][, c(1, 14:15)]%>%column_to_rownames("Metabolite"),
y= MCAres[["MCA_2Cond_Results"]][, c(1, 14:15)]%>%tibble::column_to_rownames("Metabolite"),
by=0,
all.y=TRUE)%>%
column_to_rownames("Row.names")
tibble::column_to_rownames("Row.names")
MetaProViz::VizVolcano(PlotSettings="Compare",
InputData=ResList[["Young"]][["DMA"]][["TUMOR_vs_NORMAL"]]%>%column_to_rownames("Metabolite"),
InputData2= ResList[["Old"]][["DMA"]][["TUMOR_vs_NORMAL"]]%>%column_to_rownames("Metabolite"),
InputData=ResList[["Young"]][["DMA"]][["TUMOR_vs_NORMAL"]]%>%tibble::column_to_rownames("Metabolite"),
InputData2= ResList[["Old"]][["DMA"]][["TUMOR_vs_NORMAL"]]%>%tibble::column_to_rownames("Metabolite"),
ComparisonName= c(InputData="Young", InputData2= "Old"),
SettingsFile_Metab = MetaData_Metab,
PlotName= "Young-TUMOR_vs_NORMAL compared to Old-TUMOR_vs_NORMAL",
Expand All @@ -367,14 +368,14 @@ MetaProViz::VizVolcano(PlotSettings="Compare",
```{r, echo=FALSE, warning=FALSE, fig.align="left", fig.width=7, fig.height=5}
#Add metabolite information such as KEGG ID or pathway to results
MetaData_Metab <- merge(x=Tissue_MetaData,
y= MCAres[["MCA_2Cond_Results"]][, c(1, 14:15)]%>%column_to_rownames("Metabolite"),
y= MCAres[["MCA_2Cond_Results"]][, c(1, 14:15)]%>%tibble::column_to_rownames("Metabolite"),
by=0,
all.y=TRUE)%>%
column_to_rownames("Row.names")
tibble::column_to_rownames("Row.names")
MetaProViz::VizVolcano(PlotSettings="Compare",
InputData=ResList[["Young"]][["DMA"]][["TUMOR_vs_NORMAL"]]%>%column_to_rownames("Metabolite"),
InputData2= ResList[["Old"]][["DMA"]][["TUMOR_vs_NORMAL"]]%>%column_to_rownames("Metabolite"),
InputData=ResList[["Young"]][["DMA"]][["TUMOR_vs_NORMAL"]]%>%tibble::column_to_rownames("Metabolite"),
InputData2= ResList[["Old"]][["DMA"]][["TUMOR_vs_NORMAL"]]%>%tibble::column_to_rownames("Metabolite"),
ComparisonName= c(InputData="Young", InputData2= "Old"),
SettingsFile_Metab = MetaData_Metab,
PlotName= "Young-TUMOR_vs_NORMAL compared to Old-TUMOR_vs_NORMAL",
Expand All @@ -393,12 +394,12 @@ LoadKEGG()
comparisons <- names(ResList)
for(comparison in comparisons){
#Ensure that the Metabolite names match with KEGG IDs or KEGG trivial names.
DMA <- merge(Tissue_MetaData%>%rownames_to_column("Metabolite") ,ResList[[comparison]][["DMA"]][["TUMOR_vs_NORMAL"]], by="Metabolite", all.y=TRUE)
DMA <- merge(Tissue_MetaData%>%tibble::rownames_to_column("Metabolite") ,ResList[[comparison]][["DMA"]][["TUMOR_vs_NORMAL"]], by="Metabolite", all.y=TRUE)
DMA <- DMA[,c(1,10,12:17)]
DMA <- DMA[complete.cases(DMA),-1]#we remove metabolites that do not have a KEGG ID/KEGG pathway
#remove dublons
DMA_Select <- DMA%>%distinct(KEGG, .keep_all = TRUE)%>%remove_rownames()%>%column_to_rownames("KEGG")
DMA_Select <- DMA%>%distinct(KEGG, .keep_all = TRUE)%>%remove_rownames()%>%tibble::column_to_rownames("KEGG")
#Perform ORA
DM_ORA_res[[comparison]] <- MetaProViz::StandardORA(InputData= DMA_Select, #Input data requirements: column `t.val` and column `Metabolite`
Expand Down

0 comments on commit 5c8badc

Please sign in to comment.