Answers.Body: <p>You are printing the attribute before setting it, that's why it's complaining. So instead of this:</p>
  
  <pre><code>    print(i.attributes['Note']=["Gene description"])
  </code></pre>
  
  <p>Set it first and <em>then</em> print:</p>
  
  <pre><code>i.attributes['Note']=["Gene description"]
  print(i)
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-28T12:21:03.660Z
Answers.LastActivityDate: 2019-08-28T12:21:03.660Z
Answers.UserDisplayName: terdon
Body: <p>I failed to add <code>Note=Gene description</code> to mRNA attribute with the below code:</p>
  
  <pre><code>#!/usr/bin/python3
  import click
  import gffutils
  import gffutils.gffwriter as gffwriter
  
  @click.command()
  @click.option('--gff3', help="Provide GFF3 file", required=True)
  @click.option('--keep', help="Keep GFF3 file", required=True)
  @click.option('--reject', help="Reject GFF3 file", required=True)
  def run(gff3, keep, reject):
      #db = gffutils.create_db(gff3, dbfn='data/test.db', force=True, keep_order=True, merge_strategy='merge',
      #                        sort_attribute_values=True)
  
      db = gffutils.FeatureDB('data/test.db', keep_order=True)
      for gene in db.features_of_type('gene', order_by='start'):
          print(gene)
          for i in db.children(gene, featuretype='mRNA', order_by='start'):
              print(i)
              print(i.id)
              print(i.attributes['Note']=["Gene description"])
              db.update([i])
  </code></pre>
  
  <p>I got the following error:</p>
  
  <pre><code>File "/projects/bioinf-scripts/mod_braker_gff3.py", line 20
      print(i.attributes['Note']=["Gene description"])
           ^
  SyntaxError: keyword can't be an expression
  </code></pre>
  
  <p>What did I miss?</p>
  
  <p>Thank you in advance</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-08-28T11:55:44.043Z
Comments.Text: Please [edit] your question and add a few lines of your input gff file that we can use for testing.
Comments.UserDisplayName: terdon
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-08-28T11:57:09.473Z
Comments.Text: But why are you printing it? Shouldn't you first add the attribute (`i.attributes['Note']=["Gene description"]`) and _then_ print it `print(i.attributes['Note'])`? I don't know gffutils, but it seems strange to print something before setting it.
Comments.UserDisplayName: terdon
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-08-28T12:09:51.667Z
Comments.Text: Thank you it works now.
Comments.UserDisplayName: user977828
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-08-28T12:14:41.910Z
Comments.Text: @user977828 Please post the corrected code as an answer.
Comments.UserDisplayName: Devon Ryan
Tags: <python><sequence-annotation><gff3><gffutils>
Title: Adding an attribute to GFF3 file
ThreadText: # Adding an attribute to GFF3 file
  
  (Asked by: username_0 on 2019-08-28)
  
  I failed to add `Note=Gene description` to mRNA attribute with the below code:
  
  ```
  #!/usr/bin/python3
  import click
  import gffutils
  import gffutils.gffwriter as gffwriter
  
  @click.command()
  @click.option('--gff3', help="Provide GFF3 file", required=True)
  @click.option('--keep', help="Keep GFF3 file", required=True)
  @click.option('--reject', help="Reject GFF3 file", required=True)
  def run(gff3, keep, reject):
      #db = gffutils.create_db(gff3, dbfn='data/test.db', force=True, keep_order=True, merge_strategy='merge',
      #                        sort_attribute_values=True)
  
      db = gffutils.FeatureDB('data/test.db', keep_order=True)
      for gene in db.features_of_type('gene', order_by='start'):
          print(gene)
          for i in db.children(gene, featuretype='mRNA', order_by='start'):
              print(i)
              print(i.id)
              print(i.attributes['Note']=["Gene description"])
              db.update([i])
  
  ```
  
  I got the following error:
  
  ```
  File "/projects/bioinf-scripts/mod_braker_gff3.py", line 20
      print(i.attributes['Note']=["Gene description"])
           ^
  SyntaxError: keyword can't be an expression
  
  ```
  
  What did I miss?
  
  Thank you in advance
  
  **Tags:** python, sequence-annotation, gff3, gffutils
  
  > **Comments:**
  > **username_1**: Please [edit] your question and add a few lines of your input gff file that we can use for testing.  
  > **username_1**: But why are you printing it? Shouldn't you first add the attribute (`i.attributes['Note']=["Gene description"]`) and _then_ print it `print(i.attributes['Note'])`? I don't know gffutils, but it seems strange to print something before setting it.  
  > **username_0**: Thank you it works now.  
  > **username_2**: @username_0 Please post the corrected code as an answer.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-08-28. Score: 2 | Accepted answer
  
  You are printing the attribute before setting it, that's why it's complaining. So instead of this:
  
  ```
      print(i.attributes['Note']=["Gene description"])
  
  ```
  
  Set it first and *then* print:
  
  ```
  i.attributes['Note']=["Gene description"]
  print(i)
  
  ```
  
  
  ---
  
  