-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq2pipe_step10_metrics.sh
executable file
·333 lines (286 loc) · 13 KB
/
q2pipe_step10_metrics.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/bin/bash
################################
# #
# Qiime 2 Pipeline #
# By: Patrick Gagne (NRCan) #
# Step 10 - Metrics Generation #
# Complete Version #
# september 8, 2023 #
# #
################################
exit_on_error(){
echo "Qiime2 command error detected"
echo "Exiting program"
exit 1
}
optionfile=$1
if [ ! $optionfile ] || [ ! -e $optionfile ] || [ ! -r $optionfile ]
then
echo "ERROR: you must specify a valid, accessible qiime2 optionfile"
exit 1
fi
source $optionfile
if [ $TEMPORARY_DIRECTORY ]
then
echo "Overriding default temporary directory to $TEMPORARY_DIRECTORY"
if [ ! -d $TEMPORARY_DIRECTORY ] || [ ! -w $TEMPORARY_DIRECTORY ]
then
echo "ERROR: $TEMPORARY_DIRECTORY does not exist or is read only"
exit 2
fi
export TMPDIR=$TEMPORARY_DIRECTORY
fi
if [ ! $TMPDIR ]
then
export TMPDIR=/tmp
fi
if [ "$APPTAINER_COMMAND" != "" ]
then
echo "DEBUG: Checking temporary folder"
temp_check=$( $APPTAINER_COMMAND mktemp -t Q2PIPE_TEMPFOLDER_CHECK.XXXXXX.temp )
bname=$( basename $temp_check )
if [ ! -e $TMPDIR/$bname ]
then
echo "ERROR: Disparity between Apptainer temporary folder and system temporary folder"
echo "Please make sure both are pointing to the same folder"
exit 5
else
echo "DEBUG: Temporary file check status: OK"
rm $TMPDIR/Q2PIPE_TEMPFOLDER_CHECK.??????.temp
fi
fi
detected_error=0
# Preparing list to be for-loop compatible
alpha_metrics=$( echo $alpha_metrics | sed 's/,/ /g' )
beta_metrics=$( echo $beta_metrics | sed 's/,/ /g' )
if [ "$SKIP_RAREFACTION" == "both" ]
then
SKIP_RAREFACTION="false true"
if [ -d $ANALYSIS_NAME.metrics_norarefaction_dn$p_perc_identity ] || [ -d $ANALYSIS_NAME.metrics_rarefied_"$p_sampling_depth"_dn"$p_perc_identity" ]
then
echo "ERROR: Previous metrics folder detected from previous run"
echo "Please delete the following folders if they exist:"
echo "$ANALYSIS_NAME.metrics_norarefaction_dn$p_perc_identity"
echo "$ANALYSIS_NAME.metrics_rarefied_"$p_sampling_depth"_dn"$p_perc_identity""
exit 3
fi
fi
for raref_param in $SKIP_RAREFACTION
do
if [ "$raref_param" == "true" ]
then
echo "Skip Rarefaction parameter detected"
echo "$ANALYSIS_NAME.filtered_table_dn"$p_perc_identity".qza will be used"
input_table="$ANALYSIS_NAME.filtered_table_dn"$p_perc_identity".qza"
output_f="$ANALYSIS_NAME.metrics_norarefaction_dn$p_perc_identity"
if [ -d $output_f ]
then
echo "ERROR: Folder $output_f already exist"
echo "Please delete this folder before proceeding"
exit 3
fi
mkdir $output_f
else
echo "Rarefaction parameter detected"
echo "$ANALYSIS_NAME.rarefied_"$p_sampling_depth"_filtered_table_dn"$p_perc_identity".qza will be used"
input_table="$ANALYSIS_NAME.rarefied_"$p_sampling_depth"_filtered_table_dn"$p_perc_identity".qza"
output_f="$ANALYSIS_NAME.metrics_rarefied_"$p_sampling_depth"_dn"$p_perc_identity""
if [ -d $output_f ]
then
echo "ERROR: Folder $output_f already exist"
echo "Please delete this folder before proceeding"
exit 3
fi
mkdir $output_f
mkdir $output_f/alpha_qza_export
mkdir $output_f/beta_qza_export
fi
#echo "Input table : $input_table"
for i in $alpha_metrics
do
(
echo "Calculating alpha diversity metric $i"
$APPTAINER_COMMAND qiime diversity alpha \
--i-table $input_table \
--p-metric $i \
--no-recycle \
--o-alpha-diversity $output_f/alpha_$i.qza || exit_on_error
$APPTAINER_COMMAND qiime diversity alpha-group-significance \
--i-alpha-diversity $output_f/alpha_$i.qza \
--m-metadata-file $METADATA_FILE_PATH \
--o-visualization $output_f/alpha_$i.qzv || detected_error=1
# NEW
$APPTAINER_COMMAND qiime tools export \
--input-path $output_f/alpha_$i.qza \
--output-path $output_f/alpha_qza_export/alpha_$i
) &
while [[ $(jobs -r -p | wc -l) -ge $NB_THREADS ]]; do
sleep 1
done
# not sure yet
#mv $output_f/alpha_qza_export/alpha_$i/alpha-diversity.tsv $output_f/alpha_qza_export/alpha_$i.tsv
#rm -rf $output_f/alpha_qza_export/alpha_$i
done
#wait
for i in $beta_metrics
do
(
echo "Calculating beta diversity metric $i"
$APPTAINER_COMMAND qiime diversity beta \
--i-table $input_table \
--p-metric $i \
--no-recycle \
--o-distance-matrix $output_f/beta_"$i"_distance_matrix.qza || exit_on_error
#--p-n-jobs $NB_THREADS \
$APPTAINER_COMMAND qiime diversity pcoa \
--i-distance-matrix $output_f/beta_"$i"_distance_matrix.qza \
--o-pcoa $output_f/beta_"$i"_pcoa.qza || exit_on_error
$APPTAINER_COMMAND qiime emperor plot \
--i-pcoa $output_f/beta_"$i"_pcoa.qza \
--m-metadata-file $METADATA_FILE_PATH \
--o-visualization $output_f/beta_"$i"_emperor.qzv || exit_on_error
# New
$APPTAINER_COMMAND qiime tools export \
--input-path $output_f/beta_"$i"_distance_matrix.qza \
--output-path $output_f/beta_qza_export/beta_"$i"_distance_matrix
$APPTAINER_COMMAND qiime tools export \
--input-path $output_f/beta_"$i"_pcoa.qza \
--output-path $output_f/beta_qza_export/beta_"$i"_pcoa
) &
while [[ $(jobs -r -p | wc -l) -ge $NB_THREADS ]]; do
sleep 1
done
# not sure yet
#mv $output_f/beta_qza_export/beta_"$i"_distance_matrix/distance-matrix.tsv $output_f/beta_qza_export/beta_"$i"_distance_matrix.tsv
#mv $output_f/beta_qza_export/beta_"$i"_pcoa/ordination.txt $output_f/beta_qza_export/beta_"$i"_pcoa_ordination.txt
#rm -rf $output_f/beta_qza_export/beta_"$i"_distance_matrix
#rm -rf $output_f/beta_qza_export/beta_"$i"_pcoa
done
#wait
if [ "$GENERATE_PHYLOGENY" == "true" ]
then
alpha_metrics_phylo=$( echo $alpha_metrics_phylo | sed 's/,/ /g' )
beta_metrics_phylo=$( echo $beta_metrics_phylo | sed 's/,/ /g' )
for i in $alpha_metrics_phylo
do
(
echo "Calculating phylogenetic alpha diversity metric $i"
$APPTAINER_COMMAND qiime diversity alpha-phylogenetic \
--i-table $input_table \
--i-phylogeny $ANALYSIS_NAME.rooted_tree.qza \
--p-metric $i \
--no-recycle \
--o-alpha-diversity $output_f/alpha_$i.phylo.qza || exit_on_error
$APPTAINER_COMMAND qiime diversity alpha-group-significance \
--i-alpha-diversity $output_f/alpha_$i.phylo.qza \
--m-metadata-file $METADATA_FILE_PATH \
--o-visualization $output_f/alpha_$i.phylo.qzv || detected_error=1
$APPTAINER_COMMAND qiime tools export \
--input-path $output_f/alpha_$i.phylo.qza \
--output-path $output_f/alpha_qza_export/alpha_"$i"_phylo
) &
while [[ $(jobs -r -p | wc -l) -ge $NB_THREADS ]]; do
sleep 1
done
done
#wait
for i in $beta_metrics_phylo
do
(
echo "Calculating phylogenetic beta diversity metric $i"
$APPTAINER_COMMAND qiime diversity beta-phylogenetic \
--i-table $input_table \
--i-phylogeny $ANALYSIS_NAME.rooted_tree.qza \
--p-metric $i \
--no-recycle \
--o-distance-matrix $output_f/beta_"$i"_distance_matrix.phylo.qza || exit_on_error
#--p-threads $NB_THREADS \
$APPTAINER_COMMAND qiime diversity pcoa \
--i-distance-matrix $output_f/beta_"$i"_distance_matrix.phylo.qza \
--o-pcoa $output_f/beta_"$i"_pcoa.phylo.qza || exit_on_error
$APPTAINER_COMMAND qiime emperor plot \
--i-pcoa $output_f/beta_"$i"_pcoa.phylo.qza \
--m-metadata-file $METADATA_FILE_PATH \
--o-visualization $output_f/beta_"$i"_emperor.phylo.qzv || exit_on_error
$APPTAINER_COMMAND qiime tools export \
--input-path $output_f/beta_"$i"_distance_matrix.phylo.qza \
--output-path $output_f/beta_qza_export/beta_"$i"_distance_matrix_phylo
$APPTAINER_COMMAND qiime tools export \
--input-path $output_f/beta_"$i"_pcoa.phylo.qza \
--output-path $output_f/beta_qza_export/beta_"$i"_pcoa_phylo
) &
while [[ $(jobs -r -p | wc -l) -ge $NB_THREADS ]]; do
sleep 1
done
done
#wait
fi
#wait
done
wait
if [ $detected_error -eq 1 ]
then
echo "WARNING: an alpha-group-significance step for one or more metrics returned an error"
fi
# Alpha Diversity $APPTAINER_COMMAND qiime diversity alpha \ --i-table
#$ANALYSIS_NAME.filtered_table_dn"$p_perc_identity".qza \ --p-metric shannon \ --o-alpha-diversity
#$output_f/alpha_shannon.qza
#qiime diversity alpha-group-significance \
#--i-alpha-diversity $output_f/alpha_shannon.qza \
#--m-metadata-file $METADATA_FILE_PATH \
#--o-visualization $output_f/alpha_shannon.qzv
#$APPTAINER_COMMAND qiime tools export \
#--input-path $output_f/alpha_shannon.qza \
#--output-path $output_f/alpha_shannon.txt
#$APPTAINER_COMMAND qiime diversity alpha \
#--i-table $ANALYSIS_NAME.filtered_table_dn"$p_perc_identity".qza \
#--p-metric pielou_e \
#--o-alpha-diversity $output_f/alpha_pielou.qza
#$APPTAINER_COMMAND qiime tools export \
#--input-path $output_f/alpha_pielou.qza \
#--output-path $output_f/alpha_pielou.txt
#$APPTAINER_COMMAND qiime diversity alpha \
#--i-table $ANALYSIS_NAME.filtered_table_dn"$p_perc_identity".qza \
#--p-metric observed_features \
#--o-alpha-diversity $output_f/alpha_observedFeatures.qza
#$APPTAINER_COMMAND qiime tools export \
#--input-path $output_f/alpha_observedFeatures.qza \
#--output-path $output_f/alpha_observedFeatures.txt
# Beta Diversity
#$APPTAINER_COMMAND qiime diversity beta \
#--i-table $ANALYSIS_NAME.filtered_table_dn"$p_perc_identity".qza \
#--p-metric jaccard \
#--p-n-jobs $NB_THREADS \
#--o-distance-matrix $output_f/distance-matrix_jaccard.qza
#$APPTAINER_COMMAND qiime diversity pcoa \
#--i-distance-matrix $output_f/distance-matrix_jaccard.qza \
#--o-pcoa $output_f/pcoa_jaccard.qza
#$APPTAINER_COMMAND qiime emperor plot \
#--i-pcoa $output_f/pcoa_jaccard.qza \
#--m-metadata-file $METADATA_FILE_PATH \
#--o-visualization $output_f/pcoa_jaccard.qzv
#$APPTAINER_COMMAND qiime diversity core-metrics \
#--i-table $ANALYSIS_NAME.filtered_table_dn"$p_perc_identity".qza \
#--p-sampling-depth $p_sampling_depth \
#--m-metadata-file $METADATA_FILE_PATH \
#--output-dir $ANALYSIS_NAME.core-metrics-results-rarefied_"$p_sampling_depth"_dn"$p_perc_identity" --verbose || exit_on_error
#$APPTAINER_COMMAND qiime diversity alpha-group-significance \
#--i-alpha-diversity $ANALYSIS_NAME.core-metrics-results-rarefied_"$p_sampling_depth"_dn"$p_perc_identity"/evenness_vector.qza \
#--m-metadata-file $METADATA_FILE_PATH \
#--o-visualization $ANALYSIS_NAME.core-metrics-results-rarefied_"$p_sampling_depth"_dn"$p_perc_identity"/evenness-group-significance_rarefied_"$p_sampling_depth"_dn"$p_perc_identity".qzv --verbose || exit_on_error
#$APPTAINER_COMMAND qiime diversity alpha-group-significance \
#--i-alpha-diversity $ANALYSIS_NAME.core-metrics-results-rarefied_"$p_sampling_depth"_dn"$p_perc_identity"/shannon_vector.qza \
#--m-metadata-file $METADATA_FILE_PATH \
#--o-visualization $ANALYSIS_NAME.core-metrics-results-rarefied_"$p_sampling_depth"_dn"$p_perc_identity"/shannon-group-significance_rarefied_"$p_sampling_depth"_dn"$p_perc_identity".qzv --verbose || exit_on_error
#$APPTAINER_COMMAND qiime diversity alpha-group-significance \
#--i-alpha-diversity $ANALYSIS_NAME.core-metrics-results-rarefied_"$p_sampling_depth"_dn"$p_perc_identity"/observed_features_vector.qza \
#--m-metadata-file $METADATA_FILE_PATH \
#--o-visualization $ANALYSIS_NAME.core-metrics-results-rarefied_"$p_sampling_depth"_dn"$p_perc_identity"/observed_features-group-significance_rarefied_"$p_sampling_depth"_dn"$p_perc_identity".qzv --verbose || exit_on_error
#$APPTAINER_COMMAND qiime diversity alpha \
#--i-table $ANALYSIS_NAME.rarefied_"$p_sampling_depth"_filtered_table_dn"$p_perc_identity".qza \
#--p-metric simpson \
#--o-alpha-diversity $ANALYSIS_NAME.core-metrics-results-rarefied_"$p_sampling_depth"_dn"$p_perc_identity"/simpson_index_rarefied_"$p_sampling_depth"_dn"$p_perc_identity".qza --verbose || exit_on_error
#$APPTAINER_COMMAND qiime diversity alpha-group-significance \
#--i-alpha-diversity $ANALYSIS_NAME.core-metrics-results-rarefied_"$p_sampling_depth"_dn"$p_perc_identity"/simpson_index_rarefied_"$p_sampling_depth"_dn"$p_perc_identity".qza \
#--m-metadata-file $METADATA_FILE_PATH \
#--o-visualization $ANALYSIS_NAME.core-metrics-results-rarefied_"$p_sampling_depth"_dn"$p_perc_identity"/simpson_index-group-significance_rarefied_"$p_sampling_depth"_dn"$p_perc_identity".qzv --verbose || exit_on_error