3
$\begingroup$

I was wondering if someone can help me figure out this error message. I’m at the end of my Nextflow pipeline and I want to change the header in a FASTA file and it works when I use this command:

sed -i "s/^>/>${assembly_id}_${gene_id}_/g" "${assembly_id}/${gene_id}/${assembly_id}_${gene_id}.fasta"

but it prints out much more than I actually need (i.e., “goodID_extra_extra_extra”). That’s just part of the file name (it includes what assembler was used) but I only want it to add “goodID_” to the header.

I tried using this command:

sed -i "s/^>/>${assembly_id%%_*}_${gene_id}_/g" "${assembly_id}/${gene_id}/${assembly_id}_${gene_id}.fasta"

but I get this error message:

N E X T F L O W  ~  version 21.10.6
Launching `geneTargets.nf` [amazing_elion] - revision: b875027a52
Script compilation error
- file : /Users/rimo/Desktop/AWS/nextflow_work/PROJECTS/gene_targets/geneTargets.nf
- cause: Unexpected input: '{' @ line 94, column 32.
   process publish_gene_sequences {
                                  ^

1 error

Can someone explain why this is happening? If this is something I can’t do without breaking the script that’s okay the header will still do it’s purpose I just wanted it to be clearer/less noise.

EDIT: this is the process block

process publish_gene_sequences {
publishDir params.publish_dir
input:
    tuple val(assembly_id), val(gene_id), path("gene.fasta")
output:
    path("${gene_id}/${assembly_id}_${gene_id}.fasta")
script:
    """
    mkdir -p ${gene_id}
    cp gene.fasta "${gene_id}/${assembly_id}_${gene_id}.fasta"
    sed -i "s/^>/>${assembly_id}_${gene_id}_/g" "${gene_id}/${assembly_id}_${gene_id}.fasta"
    """
 }
$\endgroup$
3
  • $\begingroup$ Can you please share a minimal reproducible example? For now, I would guess this has something to do with escaping like this example here: github.com/nextflow-io/nextflow/issues/… $\endgroup$ Nov 1, 2022 at 18:51
  • $\begingroup$ I added the entire process block where the line of code I'm trying to use is @mribeirodantas $\endgroup$
    – rimo
    Nov 1, 2022 at 20:34
  • $\begingroup$ Ideally, a minimal reproducible example would include fold structure, some sample files and a workflow call to this process block. Trying to guess things on my side, adding -e to the sed line after -i gave me no errors but it's guess work unless I can try to reproduce what you're finding in your machine :-\ $\endgroup$ Nov 1, 2022 at 21:08

1 Answer 1

5
$\begingroup$

Inside your script's command string, assembly_id is actually a java.lang.String so trying to manipulate it with a Bash operation (i.e. using ${assembly_id%%_*}) won't produce the expected result. Here's one way to return a substring up to the first underscore; note that we can use single quotes around our sed expression:

process publish_gene_sequences {

    publishDir params.publish_dir

    input:
    tuple val(assembly_id), val(gene_id), path("gene.fasta")

    output:
    path("${gene_id}/${assembly_id}_${gene_id}.fasta")

    script:
    def assembly = assembly_id.substring(0, assembly_id.indexOf('_'))

    """
    mkdir "${gene_id}"
    cp gene.fasta "${gene_id}/${assembly_id}_${gene_id}.fasta"
    sed -i 's/^>/>${assembly}_${gene_id}_/g' "${gene_id}/${assembly_id}_${gene_id}.fasta"
    """
}

Note also that in the above example, copying the file can be avoided by applying the expression directly and redirecting stdout:

script:
def assembly = assembly_id.substring(0, assembly_id.indexOf('_'))

"""
mkdir "${gene_id}"
sed \\
    -e 's/^>/>${assembly}_${gene_id}_/g' \\
    gene.fasta \\
    > "${gene_id}/${assembly_id}_${gene_id}.fasta"
"""
$\endgroup$
1
  • 1
    $\begingroup$ That makes a lot more sense thank you for clarifying! That solution worked for me! $\endgroup$
    – rimo
    Nov 2, 2022 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.