Skip to content

Commit cef7ba1

Browse files
authored
Merge pull request #2 from pinin4fjords/limma_formula_jon
Simplify variable filtering
2 parents d83c78b + e50056f commit cef7ba1

File tree

2 files changed

+21
-62
lines changed

2 files changed

+21
-62
lines changed

assets/differentialabundance_report.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ if (is.null(differential_file_suffix)) {
367367
differential_file_suffix <- paste0(".", params$differential_method, ".results.tsv")
368368
}
369369
differential_files <- lapply(contrasts$id, function(d){
370-
file.path(params$input_dir, paste0(gsub(' |;', '_', d), '_', params$study_name, differential_file_suffix))
370+
file.path(params$input_dir, paste0(gsub(' |;', '_', d), differential_file_suffix))
371371
})
372372
differential_names <- paste0(contrasts$id, '_', params$study_name)
373373

workflows/differentialabundance.nf

Lines changed: 20 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ workflow DIFFERENTIALABUNDANCE {
410410
}
411411
contrast.formula = contrast.formula?.trim() ? contrast.formula.trim() : null
412412
contrast.make_contrasts_str = contrast.make_contrasts_str?.trim() ? contrast.make_contrasts_str.trim() : null
413+
413414
return [meta, contrast, contrast.variable, contrast.reference, contrast.target, contrast.formula, contrast.make_contrasts_str]
414415
}
415416
.groupTuple() // [meta, [contrast], [variable], [reference], [target], [formula], [comparison]]
@@ -684,39 +685,29 @@ workflow DIFFERENTIALABUNDANCE {
684685
// create temporary contrast files with the entries based on the order of the gathered
685686
// differential results
686687

687-
// As contrasts having 'formula' and 'comparison' won't have a variable,
688-
// and it is needed for "checkListIsSubset()" in `SHINYNGS` make_app_from_files.R
689-
// for backwards-compatibility, keep only the channels that have non-empty variable
690-
691-
// Filter by those channels containing 'variable', regroup
692-
ch_contrasts_filtered = ch_contrasts
693-
.transpose()
694-
.filter { meta, contrast, variable, reference, target, formula, comparison ->
695-
variable?.trim()
696-
}
697-
.groupTuple()
698-
699-
ch_differential_results_with_variable = ch_differential_results
700-
.filter { meta, contrast, results ->
701-
contrast.variable?.trim()
702-
}
703-
704688
// Create a channel with the differential results and the corresponding map with
705689
// the contrast entries
706-
ch_differential_with_contrast = ch_shinyngs
707-
.join( ch_differential_results_with_variable.groupTuple() ) // [meta, [meta with contrast], [differential results]]
708-
.join( ch_contrasts_filtered ) // [meta, [contrast], [variable], [reference], [target], [formula], [comparison]]
690+
differential_with_contrast = ch_shinyngs
691+
.join( ch_differential_results
692+
.filter { meta, contrast, results -> contrast.variable?.trim() }
693+
.groupTuple()
694+
) // [meta, [meta with contrast], [differential results]]
695+
.join( ch_contrasts ) // [meta, [contrast], [variable], [reference], [target], [formula], [comparison]]
709696
.map { meta, meta_with_contrast, results, contrast, variable, reference, target, formula, comparison ->
710697
// extract the contrast entries from the meta dynamically
711698
// in this way we don't need to harcode the contrast keys
712-
def contrast_keys = contrast[0].keySet()
713-
def contrast_maps = meta_with_contrast.collect { it.subMap(contrast_keys) }
714-
[meta, results, contrast_maps] // [meta, [differential results], [contrast maps]]
699+
def paramset_contrast_keys = contrast[0].keySet()
700+
def contrast_maps = meta_with_contrast.collect { it.subMap(paramset_contrast_keys) }
701+
[meta, meta_with_contrast, results, contrast_maps]
702+
}
703+
.multiMap { meta, meta_with_contrast, results, contrast_maps ->
704+
differential_results: [meta, meta_with_contrast, results]
705+
contrast_maps: [meta, contrast_maps]
715706
}
716707

717708
// Save temporary contrast csv files with the entries ordered by the differential results
718-
ch_contrasts_sorted = ch_differential_with_contrast
719-
.collectFile { meta, results, contrast_map ->
709+
ch_contrasts_sorted = differential_with_contrast.contrast_maps
710+
.collectFile { meta, contrast_map ->
720711
def header = contrast_map[0].keySet().join(',')
721712
def content = contrast_map.collect { it.values().join(',') }
722713
def lines = header + '\n' + content.join('\n')
@@ -730,10 +721,10 @@ workflow DIFFERENTIALABUNDANCE {
730721
}
731722

732723
// Parse input for shinyngs app
733-
ch_shinyngs_input = ch_differential_with_contrast
724+
ch_shinyngs_input = differential_with_contrast.differential_results
734725
.join(ch_contrasts_sorted)
735726
.join(ch_all_matrices)
736-
.multiMap { meta, differential_results, contrast_map, contrast_file, samplesheet, features, matrices ->
727+
.multiMap { meta, meta_with_contrast, differential_results, contrast_file, samplesheet, features, matrices ->
737728
matrices: [meta, samplesheet, features, matrices]
738729
contrasts_and_differential: [meta, contrast_file, differential_results]
739730
contrast_stats_assay: meta.params.exploratory_assay_names.split(',').findIndexOf { it == meta.params.exploratory_final_assay } + 1
@@ -780,7 +771,7 @@ workflow DIFFERENTIALABUNDANCE {
780771
// So all the files generated with the same paramset meta will go together to report.
781772
// Note that the files generated with different contrasts will also be grouped together for the same paramset meta.
782773

783-
ch_differential_grouped = ch_differential_results
774+
ch_differential_grouped = differential_with_contrast.differential_results.transpose()
784775
.join(ch_differential_model, by:[0,1])
785776
.groupTuple() // [ meta, [meta with contrast], [differential results], [differential model] ]
786777
.map { [it[0], it.tail().tail().flatten()] } // [ meta, [differential results and models] ]
@@ -789,45 +780,13 @@ workflow DIFFERENTIALABUNDANCE {
789780
.groupTuple() // [ meta, [meta with contrast], [functional results] ]
790781
.map { [it[0], it.tail().tail().flatten()] } // [ meta, [functional results] ]
791782

792-
// If users provide a `report_grouping_variable` then update the contrasts file 'variable' column with that information
793-
// Split the CSV once at the start
794-
ch_split = ch_validated_contrast
795-
.splitCsv(header: true, sep: '\t')
796-
797-
// If the user provided a grouping variable, fill missing/NA values
798-
if (params.report_grouping_variable) {
799-
ch_split = ch_split
800-
.map { meta, row ->
801-
def variable = row.variable?.trim()
802-
if (!variable || variable == 'NA') {
803-
row.variable = params.report_grouping_variable
804-
}
805-
[meta, row]
806-
}
807-
}
808-
809-
// Now remove any rows where variable is still empty
810-
ch_validated_contrast = ch_split
811-
.filter { meta, row ->
812-
def variable = row.variable?.trim()
813-
variable
814-
}
815-
.groupTuple()
816-
.map { meta, rows ->
817-
def header = rows[0].keySet().join('\t')
818-
def lines = rows.collect { it.values().join('\t') }
819-
def content = ([header] + lines).join('\n')
820-
def outFile = file("${workflow.workDir}/${meta.id ?: meta.paramset_name}_contrast_variable_filled.tsv")
821-
outFile.text = content
822-
[meta, outFile]
823-
}
824783
// Prepare input for report generation
825784
// Each paramset will generate one markdown report by gathering all the files created with the same paramset
826785

827786
ch_report_input = ch_report_files // [meta, [report_file, logo_file, css_file, citations_file]]
828787
.combine(ch_collated_versions) // [versions file]
829788
.join(ch_all_matrices) // [meta, samplesheet, features, [matrices]]
830-
.join(ch_validated_contrast) // [meta, contrast file]
789+
.join(ch_contrasts_sorted) // [meta, contrast file]
831790
.join(ch_differential_grouped) // [meta, [differential results and models]]
832791
.join(ch_functional_grouped, remainder: true) // [meta, [functional results]]
833792
.map { [it[0], it.tail().flatten().grep()] } // [meta, [files]] // note that grep() would remove null files from join with remainder true

0 commit comments

Comments
 (0)