55
66# # Original Author: Amy Willis, March 2020
77# # Generalized by: Iva Veseli, Sept 2020
8+ # # Last updated: Amy Willis, November 2021
89
910# # Usage:
1011# anvi-script-enrichment-stats --input=input.txt --output=output.txt
@@ -115,7 +116,7 @@ if (file.access(opts$input) == -1) {
115116df_in <- readr :: read_tsv(file = opts $ input , col_types = cols())
116117
117118if (nrow(df_in ) < 2 ) {
118- stop(sprintf(" Your input file contains less than two lines :/ Not OK." ))
119+ stop(sprintf(" Your input file contains less than two lines :/ Not OK." ))
119120}
120121
121122# Find number of groups
@@ -130,13 +131,13 @@ if (num_groups %% 1 != 0) {
130131
131132# make sure the input file contains expected columns
132133for (column_name in c(" accession" , " associated_groups" )) {
133- if (not (column_name %in% colnames(df_in )))
134- stop(sprintf(" The input file '%s' does not contain an essential column ('%s')." , opts $ input , column_name ))
134+ if (not (column_name %in% colnames(df_in )))
135+ stop(sprintf(" The input file '%s' does not contain an essential column ('%s')." , opts $ input , column_name ))
135136}
136137
137138# make sure associated_groups column includes group names.
138139if (nrow(drop_na(distinct(df_in , associated_groups ))) == 0 ) {
139- stop(sprintf(" You have no group associations in your data. Which means, nothing is differentially occurring across your groups :/" ))
140+ stop(sprintf(" You have no group associations in your data. Which means, nothing is differentially occurring across your groups :/" ))
140141}
141142
142143# Set up functions to run
@@ -159,32 +160,87 @@ get_unadjusted_p_value <- function(anova_output) {
159160 stop(" glm gave you a not-small test statistic but a missing p-value. This shouldn't happen! But it has, please submit an issue on our GitHub and tag @adw96 :)" )))
160161}
161162
163+ # multiple functions have the same data, so get only unique data rows
164+ df_unique <- df_in %> %
165+ select(names(df_in )[n_columns_before_data + (1 : (num_groups * 2 ))]) %> %
166+ distinct %> %
167+ add_column(" data_structure_index" = 1 : nrow(. ), .before = 1 )
168+
162169# fit the GLMs in a nifty way using nest()
163- w_models <- df_in %> %
164- gather( key = " type" , value = " value" , - c( 1 : n_columns_before_data ) ) %> %
170+ w_models_unique <- df_unique %> %
171+ pivot_longer( cols = - 1 , names_to = " type" , values_to = " value" ) %> %
165172 separate(type , into = c(" type" , " group" ), sep = " _" , extra = " merge" ) %> %
166- spread( type , value ) %> %
167- mutate(x = N * p ) %> %
173+ pivot_wider( names_from = " type" , values_from = " value" ) %> %
174+ mutate(x = round( N * p ) ) %> %
168175 select(- p ) %> %
169- group_by(accession ) %> %
176+ group_by(data_structure_index ) %> %
170177 nest %> %
171178 mutate(model = map(data , run_test_no_spread ))
172179
173180# get the p-values & enrichment_scores
174- pvalues_df <- w_models %> %
175- transmute(accession ,
181+ pvalues_df_unique <- w_models_unique %> %
182+ transmute(data_structure_index ,
176183 " unadjusted_p_value" = map_dbl(model , get_unadjusted_p_value ),
177184 " enrichment_score" = map_dbl(model , get_enrichment_score ))
178185
186+ # Rao test statistic should always be positive. However,
187+ # it happens that Rao test statistics are numerically negative
188+ # but close to zero. If they are large and negative (say, smaller than
189+ # 0.00001), we should throw an error:
190+ if (min(pvalues_df_unique $ enrichment_score ) < - 1e-5 ) {
191+ message(" A Rao test statistic is large and negative, oh my!" )
192+ message(" Here are the rows where the test statistic was negative:" )
193+
194+ pvalues_df_unique %> %
195+ filter(enrichment_score < - 1e-5 ) %> %
196+ arrange(desc(enrichment_score )) %> %
197+ print(n = Inf )
198+
199+ stop(" Something has gone terribly wrong, and it's Amy's fault. Please let her know ASAP so she can fix it!" )
200+ }
201+
202+ pvalues_df <- df_unique %> %
203+ inner_join(pvalues_df_unique , by = " data_structure_index" ) %> %
204+ select(- data_structure_index ) %> %
205+ inner_join(df_in , . ,
206+ by = names(df_in )[n_columns_before_data + (1 : (num_groups * 2 ))])
207+
179208# find the maximum lambda for qvalue
180209# reasons for this change documented at https://github.com/merenlab/anvio/issues/1383
181210max_lambda <- min(0.95 , max(pvalues_df $ unadjusted_p_value , na.rm = T ))
211+ lambdas <- seq(0.05 , max_lambda , 0.05 )
212+
213+ # obtain an estimate of the proportion of features that are truly null
214+ pi0_est <- try(qvalue :: pi0est(pvalues_df $ unadjusted_p_value ,
215+ lambda = lambdas , pi0.method = " smoother" )$ pi0 ,
216+ silent = T )
217+
218+
219+ if (" try-error" %in% class(pi0_est )) {
220+
221+ # Sometimes, especially when there are many of p-values near 0.5 and fewer near 1
222+ # (documented at https://github.com/merenlab/anvio/issues/1828),
223+ # we can't reliably do the extrapolation we want. Let's instead just say
224+ # that features with p-values above 0.2 are likely null, and use that to estimate
225+ # the proportion of features that are truly null.
226+ lambdas <- 0.2
227+ pi0_est <- try(qvalue :: pi0est(pvalues_df $ unadjusted_p_value ,
228+ lambda = lambdas , pi0.method = " smoother" )$ pi0 ,
229+ silent = T )
230+
231+ if (" try-error" %in% class(pi0_est )) {
232+
233+ stop(" Doh! We still can't estimate the proportion of features that are truly null. It's Amy's fault, so please let her know ASAP so she can fix it!" )
234+
235+ }
236+
237+ }
182238
183239# get the q-values, then format the data and save
184- enrichment_output <- pvalues_df %> %
185- mutate(" adjusted_q_value" = qvalue :: qvalue(unadjusted_p_value ,
186- lambda = seq( 0.05 , max_lambda , 0.05 )) $ qvalues ) % > %
187- left_join( df_in , by = " accession " ) %> %
240+ qvalues_df <- pvalues_df %> %
241+ mutate(" adjusted_q_value" = qvalue :: qvalue(p = unadjusted_p_value , lambda = lambdas , pi0.method = " smoother " ) $ qvalues )
242+
243+ enrichment_output <- qvalues_df %> %
188244 select(names(df_in )[1 ], # e.g. COG_FUNCTION
189245 enrichment_score ,
190246 unadjusted_p_value ,
@@ -194,23 +250,7 @@ enrichment_output <- pvalues_df %>%
194250 names(df_in )[3 ],
195251 names(df_in )[n_columns_before_data + (1 : (num_groups * 2 ))])
196252
197- # Rao test statistic should always be positive. However,
198- # it happens that Rao test statistics are numerically negative
199- # but close to zero. If they are large and negative (say, smaller than
200- # 0.00001), we should throw an error:
201- if (min(enrichment_output $ enrichment_score ) < - 1e-5 ) {
202- message(" A Rao test statistic is large and negative, oh my!" )
203- message(" Here are the rows where the test statistic was negative:" )
204-
205- enrichment_output %> %
206- filter(enrichment_score < 0 ) %> %
207- arrange(desc(enrichment_score )) %> %
208- print(n = Inf )
209-
210- stop(" Something has gone terribly wrong, and it's Amy's fault. Please let her know ASAP so she can fix it!" )
211- }
212-
213- # If they are small and negative, just set them to zero. Then,
253+ # If they are small and negative (checked earlier), just set them to zero. Then,
214254# order the columns from largest to smallest enrichment score:
215255enrichment_output %<> %
216256 mutate(" enrichment_score" = pmax(0 , enrichment_score )) %> %
0 commit comments