@@ -35,55 +35,67 @@ test_that("Counting the number of samples with reads", {
35
35
36
36
test_that(" Observation proportions" , {
37
37
# Calculate proportions for all numeric columns
38
- calc_obs_props(x , " tax_data" )
38
+ result <- calc_obs_props(x , " tax_data" )
39
+ expect_equal(colnames(x $ data $ tax_data )[- (1 : 3 )], colnames(result )[- 1 ])
40
+ expect_true(all(result $ `700016050` == x $ data $ tax_data $ `700016050` / sum(x $ data $ tax_data $ `700016050` )))
39
41
40
42
# Calculate proportions for a subset of columns
41
- calc_obs_props(x , " tax_data" , cols = c(" 700035949" , " 700097855" , " 700100489" ))
42
- calc_obs_props(x , " tax_data" , cols = 4 : 6 )
43
- calc_obs_props(x , " tax_data" , cols = startsWith(colnames(x $ data $ tax_data ), " 70001" ))
43
+ col_subset <- c(" 700035949" , " 700097855" , " 700100489" )
44
+ result <- calc_obs_props(x , " tax_data" , cols = col_subset )
45
+ expect_equal(col_subset , colnames(result )[- 1 ])
46
+
47
+ result <- calc_obs_props(x , " tax_data" , cols = 4 : 6 )
48
+ expect_equal(col_subset , colnames(result )[- 1 ])
49
+
50
+ result <- calc_obs_props(x , " tax_data" ,
51
+ cols = startsWith(colnames(x $ data $ tax_data ), " 70001" ))
52
+ expect_equal(colnames(x $ data $ tax_data )[startsWith(colnames(x $ data $ tax_data ), " 70001" )],
53
+ colnames(result )[- 1 ])
44
54
45
55
# Including all other columns in ouput
46
- calc_obs_props(x , " tax_data" , other_cols = TRUE )
56
+ expect_warning(result <- calc_obs_props(x , " tax_data" , other_cols = TRUE ))
57
+ expect_true(all(c(" otu_id" , " lineage" ) %in% colnames(result )))
47
58
48
59
# Inlcuding specific columns in output
49
- calc_obs_props(x , " tax_data" , cols = c( " 700035949 " , " 700097855 " , " 700100489 " ) ,
60
+ result <- calc_obs_props(x , " tax_data" , cols = col_subset ,
50
61
other_cols = 2 : 3 )
62
+ expect_true(all(c(" otu_id" , " lineage" ) %in% colnames(result )))
51
63
52
64
# Rename output columns
53
- calc_obs_props(x , " tax_data" , cols = c( " 700035949 " , " 700097855 " , " 700100489 " ) ,
65
+ result <- calc_obs_props(x , " tax_data" , cols = col_subset ,
54
66
out_names = c(" a" , " b" , " c" ))
67
+ expect_equal(colnames(result ), c(" taxon_id" , " a" , " b" , " c" ))
68
+
55
69
})
56
70
57
71
58
72
test_that(" Summing counts per taxon" , {
59
73
# Calculate the taxon abundance for each numeric column (i.e. sample)
60
- calc_taxon_abund(x , " tax_data" )
74
+ result <- calc_taxon_abund(x , " tax_data" )
75
+ expect_equal(sum(x $ data $ tax_data $ `700035949` ), result $ `700035949` [1 ])
61
76
62
77
# Calculate the taxon abundance for a subset of columns
63
- calc_taxon_abund(x , " tax_data" , cols = 4 : 5 )
64
- calc_taxon_abund(x , " tax_data" , cols = c(" 700035949" , " 700097855" ))
65
- calc_taxon_abund(x , " tax_data" , cols = startsWith(colnames(x $ data $ tax_data ), " 70001" ))
66
-
78
+ expect_equal(calc_taxon_abund(x , " tax_data" , cols = 4 : 5 ),
79
+ calc_taxon_abund(x , " tax_data" , cols = c(" 700035949" , " 700097855" )))
80
+
67
81
# Calculate the taxon abundance for groups of columns (e.g. treatments)
68
82
# Note that we do not need to use the "cols" option for this since all
69
83
# numeric columns are samples in this dataset. If there were numeric columns
70
84
# that were not samples present in hmp_samples, the "cols" would be needed.
71
- calc_taxon_abund(x , " tax_data" , groups = hmp_samples $ sex )
72
- calc_taxon_abund(x , " tax_data" , groups = hmp_samples $ body_site )
73
-
74
- # The above example using the "cols" option, even though not needed in this case
75
- calc_taxon_abund(x , " tax_data" , cols = hmp_samples $ sample_id ,
76
- groups = hmp_samples $ sex )
85
+ result <- calc_taxon_abund(x , " tax_data" , groups = hmp_samples $ sex )
86
+ expect_equal(colnames(result ), c(" taxon_id" , " female" , " male" ))
77
87
78
88
# Rename the output columns
79
- calc_taxon_abund(x , " tax_data" , cols = hmp_samples $ sample_id [1 : 10 ],
80
- out_names = letters [1 : 10 ])
81
- calc_taxon_abund(x , " tax_data" , groups = hmp_samples $ sex ,
82
- out_names = c(" Women" , " Men" ))
89
+ total_counts <- sum(x $ data $ tax_data [, hmp_samples $ sample_id ])
90
+ result <- calc_taxon_abund(x , " tax_data" , groups = hmp_samples $ sex ,
91
+ out_names = c(" Women" , " Men" ))
92
+ expect_equal(colnames(result ), c(" taxon_id" , " Women" , " Men" ))
93
+ expect_equal(total_counts , sum(result [1 , c(" Women" , " Men" )]))
83
94
84
95
# Geting a total for all columns
85
- calc_taxon_abund(x , " tax_data" , cols = hmp_samples $ sample_id ,
86
- groups = rep(" total" , nrow(hmp_samples )))
96
+ result <- calc_taxon_abund(x , " tax_data" , cols = hmp_samples $ sample_id ,
97
+ groups = rep(" total" , nrow(hmp_samples )))
98
+ expect_equal(total_counts , result $ total [1 ])
87
99
})
88
100
89
101
@@ -95,52 +107,24 @@ test_that("Comparing groups of samples", {
95
107
x $ data $ tax_table <- calc_taxon_abund(x , dataset = " otu_table" , cols = hmp_samples $ sample_id )
96
108
97
109
# Calculate difference between groups
98
- x $ data $ diff_table <- compare_treatments(x , dataset = " tax_table" ,
110
+ expect_warning( x $ data $ diff_table <- compare_treatments(x , dataset = " tax_table" ,
99
111
cols = hmp_samples $ sample_id ,
100
- groups = hmp_samples $ body_site )
112
+ groups = hmp_samples $ body_site ))
113
+ expect_equal(nrow(x $ data $ diff_table ),
114
+ ncol(combn(length(unique(hmp_samples $ body_site )), 2 )) * nrow(x $ data $ tax_table ))
101
115
102
116
})
103
117
104
118
105
119
test_that(" Rarefying observation counts" , {
106
120
# Rarefy all numeric columns
107
- rarefy_obs(x , " tax_data" )
108
-
109
- # Rarefy a subset of columns
110
- rarefy_obs(x , " tax_data" , cols = c(" 700035949" , " 700097855" , " 700100489" ))
111
- rarefy_obs(x , " tax_data" , cols = 4 : 6 )
112
- rarefy_obs(x , " tax_data" , cols = startsWith(colnames(x $ data $ tax_data ), " 70001" ))
113
-
114
- # Including all other columns in ouput
115
- rarefy_obs(x , " tax_data" , other_cols = TRUE )
116
-
117
- # Inlcuding specific columns in output
118
- rarefy_obs(x , " tax_data" , cols = c(" 700035949" , " 700097855" , " 700100489" ),
119
- other_cols = 2 : 3 )
120
-
121
- # Rename output columns
122
- rarefy_obs(x , " tax_data" , cols = c(" 700035949" , " 700097855" , " 700100489" ),
123
- out_names = c(" a" , " b" , " c" ))
121
+ result <- rarefy_obs(x , " tax_data" )
122
+ expect_equal(length(unique(colSums(result [, hmp_samples $ sample_id ]))), 1 )
124
123
})
125
124
126
125
127
126
test_that(" Converting low counts to zero" , {
128
127
# Calculate proportions for all numeric columns
129
- calc_obs_props(x , " tax_data" )
130
-
131
- # Calculate proportions for a subset of columns
132
- calc_obs_props(x , " tax_data" , cols = c(" 700035949" , " 700097855" , " 700100489" ))
133
- calc_obs_props(x , " tax_data" , cols = 4 : 6 )
134
- calc_obs_props(x , " tax_data" , cols = startsWith(colnames(x $ data $ tax_data ), " 70001" ))
135
-
136
- # Including all other columns in ouput
137
- calc_obs_props(x , " tax_data" , other_cols = TRUE )
138
-
139
- # Inlcuding specific columns in output
140
- calc_obs_props(x , " tax_data" , cols = c(" 700035949" , " 700097855" , " 700100489" ),
141
- other_cols = 2 : 3 )
142
-
143
- # Rename output columns
144
- calc_obs_props(x , " tax_data" , cols = c(" 700035949" , " 700097855" , " 700100489" ),
145
- out_names = c(" a" , " b" , " c" ))
128
+ result <- zero_low_counts(x , " tax_data" )
129
+ expect_equal(sum(result [, hmp_samples $ sample_id ] == 1 ), 0 )
146
130
})
0 commit comments