Newer
Older
include {read_inputs} from './pipeline/inputs.nf'
include {validation} from './pipeline/validation.nf'
params.bcbio = null
mwham
committed
params.bcbio_template = null
params.output_dir = null
params.pipeline_project_id = null
params.pipeline_project_version = null
params.target_bed = null
label 'medium'
tuple(val(indv_id), val(family_id), val(father), val(mother), val(sex), val(affected), path(r1), path(r2))
output:
tuple(
val(indv_id),
path("${indv_id}_merged_r1.fastq.gz"),
path("${indv_id}_merged_r2.fastq.gz")
)
script:
// todo: pigz if gzip becomes a bottleneck
"""
zcat ${r1.join(' ')} | gzip -c > ${indv_id}_merged_r1.fastq.gz &
zcat ${r2.join(' ')} | gzip -c > ${indv_id}_merged_r2.fastq.gz
"""
}
process write_bcbio_csv {
label 'small'
input:
tuple(val(family_id), val(individual_info))
output:
tuple(val(family_id), path("${family_id}.csv"))
script:
"""
#!/usr/bin/env python
individual_info = '$individual_info'
lines = individual_info.lstrip('[').rstrip(']').split('], [')
with open('${family_id}.csv', 'w') as f:
f.write('samplename,description,batch,sex,phenotype,variant_regions\\n')
for l in lines:
f.write(l.replace(', ', ',') + '\\n')
mwham
committed
"""
}
process bcbio {
label 'large'
mwham
committed
input:
tuple(val(family_id), val(individuals), path(family_csv))
output:
tuple(val(family_id), val(individuals), path("${family_id}-merged"))
mwham
committed
script:
"""
${params.bcbio}/anaconda/bin/bcbio_prepare_samples.py --out . --csv $family_csv &&
${params.bcbio}/anaconda/bin/bcbio_nextgen.py -w template ${params.bcbio_template} ${family_csv.getBaseName()}-merged.csv ${individuals.collect({"${it}.fastq.gz"}).join(' ')} &&
cd ${family_id}-merged &&
${params.bcbio}/anaconda/bin/bcbio_nextgen.py config/${family_id}-merged.yaml -n 16 -t local
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
"""
}
process bcbio_family_outputs {
label 'small'
input:
tuple(val(family_id), val(individuals), path(bcbio_output_dir))
script:
"""
merged_bcbio_family_output="\$(ls -d $bcbio_output_dir/results/????-??-??_* | head -n 1)" &&
bcbio_family_output="\$(echo \$merged_bcbio_family_output | sed 's/-merged\\/*\$//g')" &&
run_date="\$(basename \$bcbio_family_output | sed -E 's/^([0-9]{4}-[0-9]{2}-[0-9]{2})_.+\$/\\1/g')" &&
broken_family_id="\$(echo ${family_id} | sed 's/_//g')" &&
if [ \$merged_bcbio_family_output != \$bcbio_family_output ] && [ -d \$merged_bcbio_family_output ]
then
mv \$merged_bcbio_family_output \$bcbio_family_output
fi
for suffix in gatk-haplotype-annotated.vcf.gz{,.tbi}
do
src=\$bcbio_family_output/\${broken_family_id}-\${suffix}
if [ -f \$src ]
then
mv \$src \$bcbio_family_output/${family_id}-\${suffix}
fi
done &&
for sample in ${individuals.join(' ')}
do
if [ -d "$bcbio_output_dir/results/\$sample" ]
then
mv "$bcbio_output_dir/results/\$sample" "\$bcbio_family_output"
fi
done &&
cd \$bcbio_family_output &&
samtools=${params.bcbio}/anaconda/bin/samtools &&
for bam in */*.bam
do
cram=\${bam%.bam}.cram &&
\$samtools view -@ ${task.cpus} -T ${params.reference_genome} -C -o \$cram \$bam &&
rm \$bam &&
\$samtools index \$cram &&
\$samtools flagstat \$bam > \$bam.flagstat.txt &&
\$samtools flagstat \$cram > \$cram.flagstat.txt &&
diff \$bam.flagstat.txt \$cram.flagstat.txt
if [ \$? -ne 0 ]
then
echo "Unexpected flagstat results in \$cram.flagstat.txt"
exit 1
fi
done &&
for f in \$(find . -type f | grep -v '\\.bam')
do
md5sum \$f >> md5sum.txt
done
}
process collate_pipeline_outputs {
label 'small'
publishDir "${params.output_dir}/${params.pipeline_project_id}/families/$family_id", mode: 'copy', pattern: "${bcbio_output_dir}"
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
input:
val(family_ids)
val(bcbio_family_output_dirs)
script:
"""
mkdir -p outputs/{config,families,params,prioritization,qc} &&
for d in ${bcbio_family_output_dirs.join(' ')}
do
ln -s \$d outputs/families/\$(basename \$d)
done &&
cd outputs/families &&
multiqc \
--title "Trio whole exome QC report: ${params.pipeline_project_id}_${params.pipeline_project_version}" \
--outdir ../qc \
--filename ${params.pipeline_project_id}_${params.pipeline_project_version}_qc_report.html \
. &&
peddy_output=../qc/${params.pipeline_project_id}_${params.pipeline_project_version}.ped_check.txt
# trio_whole_exome_parse_peddy_ped_csv.pl \
# --output \$peddy_output \
# --project ${params.pipeline_project_id} \
# --batch ${family_ids[0].split('_')[0]} \
# --version ${params.pipeline_project_version} \
# --ped ${params.ped_file} \
# --families . &&
# no && here - exit status checked below
# grep -v 'False\$' \$peddy_output
# if [ \$? -ne 0 ]
# then
# echo "Found Peddy mismatches in \$peddy_output"
# exit 1
# fi
"""
/*
- For each individual, merge all R1 and R2 fastqs
- For each family:
- Read the relevant information from the samplesheet, ped files and merged fastqs
- Write a BCBio config file
- Run BCBio on it
*/
take:
ch_merged_fastqs = merge_fastqs(ch_individuals)
ch_joined_indv_info = ch_individuals.join(ch_merged_fastqs)
mwham
committed
{ sample_id, family_id, father, mother, sex, phenotype, r1s, r2s, merged_r1, merged_r2 ->
[family_id, sample_id, father, mother, sex, phenotype, merged_r1]
})
.tap {ch_read1_meta} // I hate this syntax so much
mwham
committed
{ sample_id, family_id, father, mother, sex, phenotype, r1s, r2s, merged_r1, merged_r2 ->
[family_id, sample_id, father, mother, sex, phenotype, merged_r2]
})
.tap {ch_read2_meta}
ch_metadata = ch_read1_meta.mix(ch_read2_meta).map(
{ family_id, sample_id, father, mother, sex, phenotype, merged_fastq ->
[family_id, [merged_fastq, sample_id, family_id, sex, phenotype, params.target_bed]]
mwham
committed
}
).groupTuple()
ch_bcbio_csvs = write_bcbio_csv(ch_metadata)
ch_bcbio_inputs = ch_joined_indv_info.map(
{ sample_id, family_id, father, mother, sex, phenotype, r1s, r2s, merged_r1, merged_r2 ->
[family_id, sample_id]
}).groupTuple().join(ch_bcbio_csvs)
ch_bcbio_outputs = bcbio(ch_bcbio_inputs)
bcbio_family_outputs(ch_bcbio_outputs)
collate_pipeline_outputs(
ch_bcbio_outputs.map({it[0]}).collect(),
ch_bcbio_outputs.map({it[2]}).collect()
)
}
workflow {
read_inputs()
ch_individual_info = read_inputs.out[0]
validation(ch_individual_info)
run_bcbio(ch_individual_info)