You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -762,15 +762,15 @@ for (contrast in names(de_list)) {
762
762
# Retrieve the significantly differential expressed genes data frame for the contrast
763
763
res_sig <- de_list[[contrast]][["sig"]]
764
764
# Populate the dt_list list
765
-
dt_list <- c(
765
+
dt_list <- c(
766
766
dt_list, # With the contents of dt_list from the previous loop
767
767
list(h3(contrast)), # Odd numbered objects in the list will be the contrast name in a HTML renderable Heading 3 size
768
768
list(DT::datatable(res_sig)) # Even numbered object in the list will hold HTML renderable data table from the DT package containing the significant genes
769
769
)
770
770
}
771
771
772
772
# Render the dt_list into HTML
773
-
tagList(dt_list)
773
+
tagList(dt_list)
774
774
```
775
775
776
776
## Plot top 16 genes {.tabset}
@@ -787,44 +787,55 @@ for (contrast in names(de_list)) {
787
787
# Retrieve the significantly differential expressed genes data frame for the contrast
788
788
res_sig <- de_list[[contrast]][["sig"]]
789
789
# Extract the names of the top n (defined at the start of the code block) most significant genes
790
-
top_n <- res_sig %>% # Use the significantly differentially expressed genes
791
-
slice_min( # Retain the genes with the smallest values
790
+
top_n <- res_sig %>% # Use the significantly differentially expressed genes
791
+
slice_min( # Retain the genes with the smallest values
792
792
order_by = padj, # As ordered by their adjusted p-value
793
793
n = n, # The number of genes to retain
794
-
with_ties = FALSE) %>% # Do not retain ties if they return the same adjusted p-value
794
+
with_ties = FALSE
795
+
) %>% # Do not retain ties if they return the same adjusted p-value
795
796
dplyr::select(gene_name, gene_id) # Only retain the gene_name and gene_id columns
796
-
# Extract the variance stabilized transformed expression of the top n (defined at the start of the code block) most significant genes
797
+
# Extract the variance stabilized transformed expression of the top n (defined at the start of the code block) most significant genes
797
798
top_n_exp <- norm_matrix %>% # Use the variance stabilized transformed expression counts matrix
798
799
as.data.frame() %>% # Convert the matrix to a data frame
799
800
rownames_to_column("gene_id") %>% # Make the row names (the gene_ids) into a column named "gene_id"
800
801
# dplyr::select(-group, -group_name) %>%
801
802
pivot_longer( # Pivot the data across the rows and give each value its own new row
802
803
cols = !gene_id, # Pivot all columns except the gene_id column
803
804
names_to = "sample", # Name the samples column to be "sample"
804
-
values_to = "log2_expression") %>% # Name the variance stabilized transformed counts column to "log2_expression"
805
+
values_to = "log2_expression"
806
+
) %>% # Name the variance stabilized transformed counts column to "log2_expression"
805
807
right_join( # Subset the expression table and add a new column to the table
806
808
y = top_n, # Subset by the values matching by gene_id from top_n and add the gene_name
807
-
relationship = "many-to-many") %>% # Multiple rows in the norm_matrix can rows in top_n
808
-
left_join( # Add data
809
+
relationship = "many-to-many"
810
+
) %>% # Multiple rows in the norm_matrix can rows in top_n
811
+
left_join( # Add data
809
812
y = coldata, # Add the metadata
810
-
by = "sample") # Match it by the value in sample
813
+
by = "sample"
814
+
) # Match it by the value in sample
811
815
812
816
# Create a facet wrapped plot for the expression of the top n (defined at the start of the code block) most significant genes
813
-
p1 <- ggplot(top_n_exp, # Use the data from top_n_exp to create this figure
814
-
aes_string(x = column, # In each panel the x-axis will have the variable of interest
815
-
y = "log2_expression")) + # In each panel the y-axis will be the variance stabilized transformed counts
817
+
p1 <- ggplot(
818
+
top_n_exp, # Use the data from top_n_exp to create this figure
819
+
aes_string(
820
+
x = column, # In each panel the x-axis will have the variable of interest
821
+
y = "log2_expression"
822
+
)
823
+
) + # In each panel the y-axis will be the variance stabilized transformed counts
linewidth = 0.5, # Set the line width of the box in the boxplot
819
-
color = "grey") + # Color of the box outline in each panel as "grey"
827
+
color = "grey"
828
+
) + # Color of the box outline in each panel as "grey"
820
829
geom_point() + # Overlap the data points on the boxplot
821
830
facet_wrap(~gene_name) + # Make a different panel for each gene
822
831
# facet_wrap(~gene_name, scales = "free") + # If you want the y-axis to be able to be different in each plot use this facet-wrap instead of the above line of code
823
832
ggtitle(str_interp("Expression of Top ${n} DEGs")) + # Add a title
824
-
theme(axis.text.x = element_text(angle = 90, # Rotate the text on the x-axis 90 degrees
825
-
vjust = 0.5, # Center the text relative to the tick mark on the x-axis
826
-
hjust = 1)) # Right align the text against the x-axis
827
-
# Print the plot
833
+
theme(axis.text.x = element_text(
834
+
angle = 90, # Rotate the text on the x-axis 90 degrees
835
+
vjust = 0.5, # Center the text relative to the tick mark on the x-axis
# Go through each contrast in the de_list list object and
939
+
# Go through each contrast in the de_list list object and
926
940
fa_list <- lapply(de_list, function(contrast) {
927
941
# For a given contrast pull out the full differential expression results
928
942
res <- contrast[["all"]]
929
-
943
+
930
944
# Extract the universe of genes to the ORA on and assign it to the object universe
931
945
universe <- res %>% # Start with the full differential expression results (NOTE: these do exclude genes without any expression, p-value or adjusted p-value)
932
946
filter(!is.na(padj)) %>% # Remove any adjusted p-values that are NA, but there should be since they were filtered when making the de_list object
# NOTE: Change to the correct species if not working in human or mouse
945
959
input_entrezid <- rdata %>% # Start with the full annotations
946
960
filter(gene_id %in% ora_input, !is.na(entrez)) # Only retain the the annotations that are are in the ora_input object (ajusted p-value less than 0.01, lfc greater than the absolute value of 0.3) and have an ENTREZ ID
# Run the ORA using the run_fora_v2 function within the FA.R file in the 00_libs directory
951
965
all <- run_fora_v2(
952
966
input = input_entrezid, # Data frame of genes that are being evaluated for enrichment
953
967
uni = universe_mapping, # Universe of gene to look for enrichment within
954
-
all_in_life = all_in_life) # Annotation databases to query against
968
+
all_in_life = all_in_life
969
+
) # Annotation databases to query against
955
970
956
971
# Create the input vector of genes for the ORA with a positive lfc
957
972
ora_input <- res %>% # Start with the full differential expression results (NOTE: these do exclude genes without any expression, p-value or adjusted p-value)
# NOTE: Change to the correct species if not working in human or mouse
962
977
input_entrezid <- rdata %>% # Start with the full annotations
963
978
filter(gene_id %in% ora_input, !is.na(entrez)) # Only retain the the annotations that are are in the ora_input object (adjusted p-value less than 0.01 and lfc greater than 0.3) and have an ENTREZ ID
964
-
979
+
965
980
# Run the ORA using the run_fora_v2 function within the FA.R file in the 00_libs directory
966
981
up <- run_fora_v2(
967
982
input = input_entrezid, # Data frame of genes that are being evaluated for enrichment
968
983
uni = universe_mapping, # Universe of gene to look for enrichment within
969
-
all_in_life = all_in_life) # Annotation databases to query against
984
+
all_in_life = all_in_life
985
+
) # Annotation databases to query against
970
986
971
987
# Create the input vector of genes for the ORA with a negative lfc
972
988
ora_input <- res %>% # Start with the full differential expression results (NOTE: these do exclude genes without any expression, p-value or adjusted p-value)
# NOTE: Change to the correct species if not working in human or mouse
977
993
input_entrezid <- rdata %>% # Start with the full annotations
978
994
filter(gene_id %in% ora_input, !is.na(entrez)) # Only retain the the annotations that are are in the ora_input object (adjusted p-value less than 0.01 and lfc less than -0.3) and have an ENTREZ ID
979
-
995
+
980
996
# Run the ORA using the run_fora_v2 function within the FA.R file in the 00_libs directory
981
997
down <- run_fora_v2(
982
998
input = input_entrezid, # Data frame of genes that are being evaluated for enrichment
983
999
uni = universe_mapping, # Universe of gene to look for enrichment within
984
-
all_in_life = all_in_life) # Annotation databases to query against
1000
+
all_in_life = all_in_life
1001
+
) # Annotation databases to query against
985
1002
986
1003
# Create a list to hold the ORA results for a given contrast
987
1004
list(
988
1005
all = all, # Assign the positive and negative lfc combined ORA results to "all"
989
1006
up = up, # Assign the positive lfc combined ORA results to "up"
990
1007
down = down # Assign the negative lfc combined ORA results to "down"
# Create a file name for the differential expression analysis
1099
1116
name_deg_fn <- file.path( # Construct a file path
1100
-
basedir, # Base directory path to write output files to specificied in the params.R file within the 00_params directory
1117
+
basedir, # Base directory path to write output files to specificied in the params.R file within the 00_params directory
1101
1118
str_interp("${filename}_${contrast}_deg.csv") # Interpret the value of "filename" and "contrast", then insert that in front of "_deg.csv"
1102
1119
)
1103
-
1120
+
1104
1121
# Create a file name for the ORA analysis
1105
1122
name_pathways_fn <- file.path( # Construct a file path
1106
-
basedir, # Base directory path to write output files to specificied in the params.R file within the 00_params directory
1123
+
basedir, # Base directory path to write output files to specificied in the params.R file within the 00_params directory
1107
1124
str_interp("${filename}_${contrast}_pathways.csv") # Interpret the value of "filename" and "contrast", then insert that in front of "_pathways.csv"
1108
1125
)
1109
1126
1110
1127
# Assign the full differential expression analysis for a given contrast to the object called res_for_writing
1111
1128
res_for_writing <- de_list[[contrast]][["all"]] %>% # Start with the full differential expression analysis dataframe for the given contrast
1112
1129
mutate(comparison = contrast) # Add a column to the table with the given contrast
1113
-
1130
+
1114
1131
# Assign the full ORA for positive and negative lfc for a given contrast to the object called pathways_for_writing
1115
1132
# NOTE: Choose what ORA to save (all, down or up). To save everything, add more lines of this code
1116
1133
pathways_for_writing <- fa_list[[contrast]][["all"]] %>% # Start with the full ORA dataframe evaluating negative and positive lfc for the given contrast
0 commit comments