Skip to content

Commit 1c8bd13

Browse files
authored
Merge pull request #73 from KumarLabJax/KLAUS-118-add-bout-table-outputs-to-jabs-features
Add nextflow process to merge bout tables by behavior
2 parents 39a2792 + 1fb5541 commit 1c8bd13

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

nextflow/modules/jabs_classifiers.nf

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,57 @@ process BEHAVIOR_TABLE_TO_FEATURES {
101101
"""
102102
Rscript ${params.support_code_dir}behavior_summaries.R -f ${in_summary_table} -b ${bin_size} -o "${in_summary_table.baseName}_features_${bin_size}.csv"
103103
"""
104+
}
105+
106+
/**
107+
* Aggregate bout tables by behavior across all videos.
108+
*
109+
* This process uses jabs-postprocess to merge tables by behavior,
110+
* creating separate merged files for each behavior detected.
111+
*
112+
* @param bout_tables List of paths to bout tables to be merged.
113+
*
114+
* @return merged_bout_tables List of paths to merged bout tables, one per behavior.
115+
* @return merge_log Path to the log file detailing the merge process.
116+
*/
117+
process AGGREGATE_BOUT_TABLES {
118+
label "jabs_table_convert"
119+
label "r_jabs_table_convert"
120+
121+
publishDir "${params.outdir}/merged_behavior_tables", mode: 'copy'
122+
123+
input:
124+
path bout_tables
125+
126+
output:
127+
path("merged_*_bouts_merged.csv"), emit: merged_bout_tables
128+
path("merge_log.txt"), emit: merge_log
129+
130+
script:
131+
"""
132+
# Create a temporary directory for organizing tables
133+
mkdir -p table_staging
134+
135+
# Copy all bout tables to staging directory
136+
for table in ${bout_tables}; do
137+
cp "\${table}" table_staging/
138+
done
139+
140+
# Use jabs-postprocess to merge tables by behavior
141+
# This will automatically detect behaviors and create separate merged files for each
142+
echo "Starting behavior table merging..." > merge_log.txt
143+
echo "Input tables found:" >> merge_log.txt
144+
ls table_staging/*.csv >> merge_log.txt
145+
146+
uv run python -m jabs_postprocess.cli.main merge-multiple-tables \\
147+
--table-folder table_staging \\
148+
--table-pattern "*_bouts.csv" \\
149+
--output-prefix merged \\
150+
--overwrite \\
151+
2>&1 | tee -a merge_log.txt
152+
153+
# Log completion
154+
echo "Merge completed. Output files:" >> merge_log.txt
155+
ls merged_*_bouts_merged.csv >> merge_log.txt 2>/dev/null || echo "No merged files generated" >> merge_log.txt
156+
"""
104157
}

nextflow/workflows/feature_generation.nf

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ include { GENERATE_FEATURE_CACHE;
2929
PREDICT_CLASSIFIERS;
3030
GENERATE_BEHAVIOR_TABLES;
3131
PREDICT_HEURISTICS;
32-
BEHAVIOR_TABLE_TO_FEATURES } from "${projectDir}/nextflow/modules/jabs_classifiers"
32+
BEHAVIOR_TABLE_TO_FEATURES;
33+
AGGREGATE_BOUT_TABLES } from "${projectDir}/nextflow/modules/jabs_classifiers"
3334
include { EXTRACT_FECAL_BOLI_BINS } from "${projectDir}/nextflow/modules/fecal_boli"
3435

3536
workflow SINGLE_MOUSE_V2_FEATURES {
@@ -90,6 +91,14 @@ workflow SINGLE_MOUSE_V6_FEATURES {
9091
classifier_predictions = PREDICT_CLASSIFIERS(cached_features, params.single_mouse_classifiers)
9192
classifier_tables = GENERATE_BEHAVIOR_TABLES(classifier_predictions, params.single_mouse_classifiers)
9293

94+
// Aggregate bout tables by behavior for downstream analysis
95+
all_bout_tables = heuristic_tables
96+
.concat(classifier_tables)
97+
.map { bout_table, summary_table -> bout_table }
98+
.flatten()
99+
.collect()
100+
merged_bout_tables = AGGREGATE_BOUT_TABLES(all_bout_tables)
101+
93102
// Combine table data into feature file
94103
all_summary_tables = heuristic_tables
95104
.concat(classifier_tables)
@@ -121,4 +130,5 @@ workflow SINGLE_MOUSE_V6_FEATURES {
121130
emit:
122131
wide_jabs_features
123132
fecal_boli_table
133+
merged_bout_tables
124134
}

0 commit comments

Comments
 (0)