diff --git a/nextflow/modules/jabs_classifiers.nf b/nextflow/modules/jabs_classifiers.nf index af393c9..9f98995 100644 --- a/nextflow/modules/jabs_classifiers.nf +++ b/nextflow/modules/jabs_classifiers.nf @@ -101,4 +101,57 @@ process BEHAVIOR_TABLE_TO_FEATURES { """ Rscript ${params.support_code_dir}behavior_summaries.R -f ${in_summary_table} -b ${bin_size} -o "${in_summary_table.baseName}_features_${bin_size}.csv" """ +} + +/** +* Aggregate bout tables by behavior across all videos. +* +* This process uses jabs-postprocess to merge tables by behavior, +* creating separate merged files for each behavior detected. +* +* @param bout_tables List of paths to bout tables to be merged. +* +* @return merged_bout_tables List of paths to merged bout tables, one per behavior. +* @return merge_log Path to the log file detailing the merge process. +*/ +process AGGREGATE_BOUT_TABLES { + label "jabs_table_convert" + label "r_jabs_table_convert" + + publishDir "${params.outdir}/merged_behavior_tables", mode: 'copy' + + input: + path bout_tables + + output: + path("merged_*_bouts_merged.csv"), emit: merged_bout_tables + path("merge_log.txt"), emit: merge_log + + script: + """ + # Create a temporary directory for organizing tables + mkdir -p table_staging + + # Copy all bout tables to staging directory + for table in ${bout_tables}; do + cp "\${table}" table_staging/ + done + + # Use jabs-postprocess to merge tables by behavior + # This will automatically detect behaviors and create separate merged files for each + echo "Starting behavior table merging..." > merge_log.txt + echo "Input tables found:" >> merge_log.txt + ls table_staging/*.csv >> merge_log.txt + + uv run python -m jabs_postprocess.cli.main merge-multiple-tables \\ + --table-folder table_staging \\ + --table-pattern "*_bouts.csv" \\ + --output-prefix merged \\ + --overwrite \\ + 2>&1 | tee -a merge_log.txt + + # Log completion + echo "Merge completed. Output files:" >> merge_log.txt + ls merged_*_bouts_merged.csv >> merge_log.txt 2>/dev/null || echo "No merged files generated" >> merge_log.txt + """ } \ No newline at end of file diff --git a/nextflow/workflows/feature_generation.nf b/nextflow/workflows/feature_generation.nf index 1a4d055..d815230 100644 --- a/nextflow/workflows/feature_generation.nf +++ b/nextflow/workflows/feature_generation.nf @@ -29,7 +29,8 @@ include { GENERATE_FEATURE_CACHE; PREDICT_CLASSIFIERS; GENERATE_BEHAVIOR_TABLES; PREDICT_HEURISTICS; - BEHAVIOR_TABLE_TO_FEATURES } from "${projectDir}/nextflow/modules/jabs_classifiers" + BEHAVIOR_TABLE_TO_FEATURES; + AGGREGATE_BOUT_TABLES } from "${projectDir}/nextflow/modules/jabs_classifiers" include { EXTRACT_FECAL_BOLI_BINS } from "${projectDir}/nextflow/modules/fecal_boli" workflow SINGLE_MOUSE_V2_FEATURES { @@ -90,6 +91,14 @@ workflow SINGLE_MOUSE_V6_FEATURES { classifier_predictions = PREDICT_CLASSIFIERS(cached_features, params.single_mouse_classifiers) classifier_tables = GENERATE_BEHAVIOR_TABLES(classifier_predictions, params.single_mouse_classifiers) + // Aggregate bout tables by behavior for downstream analysis + all_bout_tables = heuristic_tables + .concat(classifier_tables) + .map { bout_table, summary_table -> bout_table } + .flatten() + .collect() + merged_bout_tables = AGGREGATE_BOUT_TABLES(all_bout_tables) + // Combine table data into feature file all_summary_tables = heuristic_tables .concat(classifier_tables) @@ -121,4 +130,5 @@ workflow SINGLE_MOUSE_V6_FEATURES { emit: wide_jabs_features fecal_boli_table + merged_bout_tables } \ No newline at end of file