Reputation: 11
process extract_key_species {
input:
tuple val(sample_ID), path(fastq1), path(fastq2), path(kaiju_out), val(tax_ids)
output:
tuple val(sample_ID), file("*_R1.fastq.gz"), file("*_R2.fastq.gz")
script:
"""
echo "${tax_ids}" | tr ',' '\\n' > "${sample_ID}_tax_ids.list"
# Debugging to confirm the list was created
echo "Created tax ID list:"
cat "${sample_ID}_tax_ids.list"
while IFS= read -r tax_id; do
echo "Processing tax_id: ${tax_id}"
grep -Ew "${tax_id}" "${kaiju_out}" | cut -f2 > "${sample_ID}_${tax_id}_ids.tsv"
seqtk subseq "${fastq1}" "${sample_ID}_${tax_id}_ids.tsv" | gzip -c > "${sample_ID}_${tax_id}_R1.fastq.gz"
seqtk subseq "${fastq2}" "${sample_ID}_${tax_id}_ids.tsv" | gzip -c > "${sample_ID}_${tax_id}_R2.fastq.gz"
echo "Generated FASTQ files for tax_id: ${tax_id}"
done < "${sample_ID}_tax_ids.list"
"""
}
workflow Assemble {
assemble_input = Channel.fromPath(params.tsv)
.splitCsv(sep: "\t", header: true, strip: true)
.map { it -> [it['sample_id'], it['fastq_r1'], it['fastq_r2'], it['kaiju_out'], it['tax_ids']] }
extract_key_species_in_out = extract_key_species(assemble_input)
extract_key_species_in_out.view()
}
Here I am not able to perform while loop as it is giving the below error:
ERROR ~ Error executing process > 'Assemble:extract_key_species (sample1)'
Caused by:
No such variable: tax_id -- Check script 'mainpython1.nf' at line: 15
Upvotes: 1
Views: 14
Reputation: 54562
Caused by: No such variable: tax_id -- Check script 'mainpython1.nf' at line: 15
Nextflow uses the same Bash syntax for variable substitutions. Here, the tax_id variable is being evaluated as a Nextflow variable. For it to correctly be evaluated as a Bash variable, it must be escaped with \$
. Please try the following instead:
while IFS= read -r tax_id; do
echo "Processing tax_id: \${tax_id}"
grep -Ew "\${tax_id}" "${kaiju_out}" | cut -f2 > "${sample_ID}_\${tax_id}_ids.tsv"
seqtk subseq "${fastq1}" "${sample_ID}_\${tax_id}_ids.tsv" |
gzip -c > "${sample_ID}_\${tax_id}_R1.fastq.gz"
seqtk subseq "${fastq2}" "${sample_ID}_\${tax_id}_ids.tsv" |
gzip -c > "${sample_ID}_\${tax_id}_R2.fastq.gz"
echo "Generated FASTQ files for tax_id: \${tax_id}"
done < "${sample_ID}_tax_ids.list"
Upvotes: 0