text: user16067: 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"
      """
   }
  
  
  user10775: 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"
  """
  
  
original_text: 16067: <p>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:</p>
  <p><code>sed -i &quot;s/^&gt;/&gt;${assembly_id}_${gene_id}_/g&quot; &quot;${assembly_id}/${gene_id}/${assembly_id}_${gene_id}.fasta&quot;</code></p>
  <p>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.</p>
  <p>I tried using this command:</p>
  <p><code>sed -i &quot;s/^&gt;/&gt;${assembly_id%%_*}_${gene_id}_/g&quot; &quot;${assembly_id}/${gene_id}/${assembly_id}_${gene_id}.fasta&quot;  </code></p>
  <p>but I get this error message:</p>
  <pre><code>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
  </code></pre>
  <p>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.</p>
  <p>EDIT: this is the process block</p>
  <pre><code>process publish_gene_sequences {
  publishDir params.publish_dir
  input:
      tuple val(assembly_id), val(gene_id), path(&quot;gene.fasta&quot;)
  output:
      path(&quot;<span class="math-container">${gene_id}/$</span>{assembly_id}_<span class="math-container">${gene_id}.fasta")
  script:
      """
      mkdir -p $</span>{gene_id}
      cp gene.fasta &quot;<span class="math-container">${gene_id}/$</span>{assembly_id}_<span class="math-container">${gene_id}.fasta"
      sed -i "s/^&gt;/&gt;$</span>{assembly_id}_<span class="math-container">${gene_id}_/g" "$</span>{gene_id}/<span class="math-container">${assembly_id}_$</span>{gene_id}.fasta&quot;
      &quot;&quot;&quot;
   }
  </code></pre>
   10775: <p>Inside your script's command string, <em>assembly_id</em> is actually a <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html" rel="nofollow noreferrer"><code>java.lang.String</code></a> so trying to manipulate it with a Bash operation (i.e. using <code>${assembly_id%%_*}</code>) 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:</p>
  <pre><code>process publish_gene_sequences {
  
      publishDir params.publish_dir
  
      input:
      tuple val(assembly_id), val(gene_id), path(&quot;gene.fasta&quot;)
  
      output:
      path(&quot;<span class="math-container">${gene_id}/$</span>{assembly_id}_${gene_id}.fasta&quot;)
  
      script:
      def assembly = assembly_id.substring(0, assembly_id.indexOf('_'))
  
      &quot;&quot;&quot;
      mkdir &quot;<span class="math-container">${gene_id}"
      cp gene.fasta "$</span>{gene_id}/<span class="math-container">${assembly_id}_$</span>{gene_id}.fasta&quot;
      sed -i 's/^&gt;/&gt;<span class="math-container">${assembly}_$</span>{gene_id}_/g' &quot;<span class="math-container">${gene_id}/$</span>{assembly_id}_${gene_id}.fasta&quot;
      &quot;&quot;&quot;
  }
  </code></pre>
  <p>Note also that in the above example, copying the file can be avoided by applying the expression directly and redirecting stdout:</p>
  <pre><code>script:
  def assembly = assembly_id.substring(0, assembly_id.indexOf('_'))
  
  &quot;&quot;&quot;
  mkdir &quot;<span class="math-container">${gene_id}"
  sed \\
      -e 's/^&gt;/&gt;$</span>{assembly}_<span class="math-container">${gene_id}_/g' \\
      gene.fasta \\
      &gt; "$</span>{gene_id}/<span class="math-container">${assembly_id}_$</span>{gene_id}.fasta&quot;
  &quot;&quot;&quot;
  </code></pre>
  