diff --git a/bin/workflow_glue/de_plots.py b/bin/workflow_glue/de_plots.py index 9e967c4..65c7720 100755 --- a/bin/workflow_glue/de_plots.py +++ b/bin/workflow_glue/de_plots.py @@ -237,29 +237,29 @@ def dtu_section(dtu_file, section, gt_dic, ge_dic): section.table(dtu_results.loc[dtu_pvals.index]) -def add_gene_names_dge(dge_file, geid_gname): +def copy_dge_add_gene_names(dge_file, geid_gname, newfile_name): """Add gene name column to DGE TSV with gene ID to gene Name dict.""" dge_results = pd.read_csv(dge_file, sep='\t') column_to_move = dge_results.index.map( lambda x: geid_gname.get(x)) dge_results.insert(0, "gene_name", column_to_move) - dge_results.to_csv(dge_file, index=True, index_label="gene_id", sep="\t") + dge_results.to_csv(newfile_name, index=True, index_label="gene_id", sep="\t") -def add_gene_names_filtered(unfiltered_file, geid_gname): +def copy_filtered_add_gene_names(unfiltered_file, geid_gname, newfile_name): """Use gene id to gene name dict to add name column to counts TSV.""" unfiltered = pd.read_csv(unfiltered_file, sep='\t') unfiltered.insert(1, "gene_name", unfiltered.gene_id.map( lambda x: geid_gname.get(x))) - unfiltered.to_csv(unfiltered_file, index=False, sep='\t') + unfiltered.to_csv(newfile_name, index=False, sep='\t') -def add_tpm_names(tpm_file, txid_gname): - """Use transcript id to gene name dict to add gname column to TPM TSV.""" +def copy_tpm_add_gene_names(tpm_file, txid_gname, newfile_name): + """Use transcript id to gene name dict to add name column to TPM TSV.""" tpm = pd.read_csv(tpm_file, sep='\t') tpm.insert(1, "gene_name", tpm.Reference.map( lambda x: txid_gname.get(x))) - tpm.to_csv(tpm_file, index=False, sep='\t') + tpm.to_csv(newfile_name, index=False, sep='\t') def dge_section(dge_file, section, ids_dic): @@ -404,13 +404,14 @@ Find the full sequences of any transcripts in the section.table(alignment_summary_df, key='alignment-stats', index=True) salmon_table(tpm, section) gene_txid, gene_name, geid_gname = get_translations(stringtie) - # Use dictionaries to add gene names to the counts tsv files to help users - add_gene_names_dge(dge, geid_gname) - add_gene_names_dge(gene_counts, geid_gname) - add_gene_names_filtered(filtered, geid_gname) - add_gene_names_filtered(unfiltered, geid_gname) - add_tpm_names(tpm, gene_txid) + copy_dge_add_gene_names(dge, geid_gname, "results_dge.tsv") + copy_dge_add_gene_names(gene_counts, geid_gname, "all_gene_counts.tsv") + copy_filtered_add_gene_names( + filtered, geid_gname, "filtered_transcript_counts_with_genes.tsv") + copy_filtered_add_gene_names( + unfiltered, geid_gname, "unfiltered_transcript_counts_with_genes.tsv") + copy_tpm_add_gene_names(tpm, gene_txid, "unfiltered_tpm_transcript_counts.tsv") # Add tables to report dge_section(dge, section, gene_name) diff --git a/main.nf b/main.nf index 5e71972..e23745b 100644 --- a/main.nf +++ b/main.nf @@ -407,10 +407,13 @@ process makeReport { path "seqkit/*" path "isoforms_table/*" output: - path("wf-transcriptomes-*.html"), emit: report - // for DE analysis, a `gene_name` column will be added to - // `de_report/results_dge.tsv` - path "results_dge.tsv", emit: de_analysis, optional: true + path ("wf-transcriptomes-*.html"), emit: report + // If de analysis has been run output the counts files with gene name added. + path ("results_dge.tsv"), emit: results_dge, optional: true + path ("unfiltered_tpm_transcript_counts.tsv"), emit: tpm, optional: true + path ("unfiltered_transcript_counts_with_genes.tsv"), emit: unfiltered, optional: true + path ("filtered_transcript_counts_with_genes.tsv"), emit: filtered, optional: true + path ("all_gene_counts.tsv"), emit: gene_counts, optional: true shell: // Convert the sample_id arrayList. sids = new BlankSeparatedList(sample_ids) @@ -691,7 +694,7 @@ workflow pipeline { report = makeReport.out.report - results = results.concat(makeReport.out.report) + results = results.concat(report) if (use_ref_ann){ results = run_gffcompare.output.gffcmp_dir.concat( @@ -719,9 +722,13 @@ workflow pipeline { results = results.map{ [it, null] }.concat(fastq_ingress_results.map { [it, "fastq_ingress_results"] }) if (params.de_analysis){ - de_update = makeReport.out.de_analysis - de_results = report.concat(transcriptome, de_outputs.flatten(), counts.flatten(), de_update) - results = results.concat(de_results.map{ [it, "de_analysis"] }) + de_results = report.concat( + transcriptome, de_outputs.flatten(), counts.flatten(), + makeReport.out.results_dge, makeReport.out.tpm, + makeReport.out.filtered, makeReport.out.unfiltered, + makeReport.out.gene_counts) + // Output de_analysis results in the dedicated directory. + results = results.concat(de_results.map{ [it, "de_analysis"] }) } results.concat(workflow_params.map{ [it, null]}) diff --git a/subworkflows/differential_expression.nf b/subworkflows/differential_expression.nf index 97cd76f..471bc5f 100644 --- a/subworkflows/differential_expression.nf +++ b/subworkflows/differential_expression.nf @@ -76,6 +76,7 @@ process deAnalysis { path "de_analysis/results_dge.tsv", emit: dge path "de_analysis/results_dexseq.tsv", emit: dexseq path "de_analysis", emit: de_analysis + path "de_analysis/cpm_gene_counts.tsv", emit: cpm script: // Just try both annotation file type because a .gff extension may be gff2(gtf) or gff3 String annotation_type = "gtf" @@ -115,7 +116,15 @@ process plotResults { output: path "de_analysis/dtu_plots.pdf", emit: dtu_plots path "sample_sheet.tsv", emit: sample_sheet_csv + // Output all DE files for use in report process path "de_analysis/*", emit: stageR + // Output selected DE files to be output in out_dir + path "de_analysis/results_dge.pdf", emit: dge_pdf + path "de_analysis/results_dge.tsv", emit: dge_tsv + path "de_analysis/results_dtu_gene.tsv", emit: dtu_gene + path "de_analysis/results_dtu_transcript.tsv", emit: dtu_transcript + path "de_analysis/results_dtu_stageR.tsv", emit: dtu_stageR + path "de_analysis/results_dtu.pdf", emit: dtu_pdf """ plot_dtu_results.R """ @@ -178,14 +187,18 @@ workflow differential_expression { merged_TPM = mergeTPM(count_transcripts.out.counts.collect()) analysis = deAnalysis(sample_sheet, merged, ref_annotation) plotResults(analysis.flt_counts, analysis.stageR, sample_sheet, analysis.de_analysis) + // Concat files required for making the report de_report = analysis.flt_counts.concat(analysis.gene_counts, analysis.dge, analysis.dexseq, analysis.stageR, plotResults.out.sample_sheet_csv, merged, ref_annotation, merged_TPM, analysis.unflt_counts).collect() + // Concat files required to be output to user + de_outputs_concat = analysis.cpm.concat(plotResults.out.dtu_plots, plotResults.out.dge_pdf, plotResults.out.dge_tsv, + plotResults.out.dtu_gene, plotResults.out.dtu_transcript, plotResults.out.dtu_stageR, plotResults.out.dtu_pdf).collect() count_transcripts_file = count_transcripts.out.seqkit_stats.collect() all_counts = merged_TPM.concat(analysis.flt_counts, analysis.gene_counts) emit: all_de = de_report count_transcripts = count_transcripts_file dtu_plots = plotResults.out.dtu_plots - de_outputs = plotResults.out.stageR + de_outputs = de_outputs_concat counts = all_counts }