Answers.Body: <p>You might consider changing the code to make the whitespace a little more consistent (as it was, it failed when i tried to run it).</p>
  <p>Also, why are you using regular expressions? You have already extracted the <code>gene_id</code> using Biopython. The following works for me:</p>
  <p>(updated to handle the underscore delimiting)</p>
  <pre><code>from Bio import SeqIO
  import sys
  import re 
  
  fasta_file = (sys.argv[1])
  for myfile in SeqIO.parse(fasta_file, &quot;fasta&quot;):
      if len(myfile.seq) &gt; 250:  # note .seq here!
          gene_id = myfile.id
          # unclear why using regex
          #match_list = re.match('H149xcV\_\w+\_\w+\_\w+',gene_id)
          #print (&quot;&gt;&quot;+match_list.group(0)) 
          print(&quot;_&quot;.join(gene_id.split(&quot;_&quot;)[0:-1]))
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-23T20:43:13.083Z
Answers.Text: Hi @MaximilianPress , how would I change the regex so that it only provides me with `>H149xcV_Fge342_r3_h2` and not`>H149xcV_Fge342_r3_h2_d1`. I would not like the very end part that consists of `d1`. So basically, how can I format it so it it only provides this `>H149xcV_Fge342_r3_h2`. If I am using regex would I not need to use gene.id?
Answers.UserDisplayName: andnowmywatchbegins
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-23T21:13:17.480Z
Answers.Text: @andnowmywatchbegins wants to remove the stuff after the space in the fasta header. In phylogeny this is either removed or spaces substituted because the space in phylip format denotes the start of the sequence
Answers.UserDisplayName: M__
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-23T21:20:06.783Z
Answers.Text: @Michael I've tried reformatting the entire regular expression and I am still getting the full `>H149xcV_Fge342_r3_h2_d1`, is there a way to only receive `>H149xcV_Fge342_r3_h2` as in output? Thanks!
Answers.UserDisplayName: andnowmywatchbegins
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-23T21:23:37.600Z
Answers.Text: Can you put this as a separate question please? .. Yes you will but thats a different part of regex
Answers.UserDisplayName: M__
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-23T22:45:27.060Z
Answers.Text: @Michael stuff after the whitespace is already stripped out by biopython when you use the `.id`. this is one of the more annoying behaviors of biopython IMO, but in this instance it is useful. I've confirmed that my example code does this. It admittedly does not handle the underscore delimitation, which I missed. But that can be done with split much more simply.
Answers.UserDisplayName: Maximilian Press
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-23T22:48:33.910Z
Answers.Text: @andnowmywatchbegins why not just split on `_` and only keep as many `_`-delimited fields as you want? Regex will work for this in theory but it's annoying to debug in cases like this.
Answers.UserDisplayName: Maximilian Press
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-23T22:51:13.940Z
Answers.Text: @MaximilianPress I've put a response here .. https://bioinformatics.stackexchange.com/questions/14913/how-to-obtain-desired-output
Answers.UserDisplayName: M__
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-24T19:10:43.783Z
Answers.Text: @MaximilianPress "if len(myfile.seq) > 250:  # note .seq here!" -- `.seq` is actually not needed there
Answers.UserDisplayName: Chris_Rands
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-24T19:12:41.417Z
Answers.Text: @Chris_Rands ah, good to know... another confusing feature of biopython!
Answers.UserDisplayName: Maximilian Press
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-23T20:28:07.413Z
Answers.LastActivityDate: 2020-11-23T22:50:26.163Z
Answers.LastEditDate: 2020-11-23T22:50:26.163Z
Answers.UserDisplayName: Maximilian Press
Answers.Body: <p>You wrote,</p>
  <pre><code>mylist = re.match(H149xcV\_\w+\_\w+\_\w+)
  </code></pre>
  <p>try...</p>
  <pre><code>myregex = re.compile(r'H149xcV\_\w+\_\w+\_\w+')
  mylist = myregex.match(myfile.id)
  </code></pre>
  <p>or</p>
  <pre><code>mylist = re.match(r'H149xcV\_\w+\_\w+\_\w+',myfile.id)
  </code></pre>
  <p>Watch your indentation it should be 4 spaces. Note you are losing the last '_\w+', but don't think it matters.</p>
  <p>I think it is better to use,</p>
  <pre><code>mylist, _, _ = re.split(r' ', myfile.id)
  </code></pre>
  <p>The problem with your code is <code>list</code> will just return <code>true</code> ... and that is not what you want. If you are using <code>split</code> carefully watch the number of ' ' in your code you can also use <code>r'\s'</code></p>
  <p>You can also set maxsplit as a parameter, which might be useful.</p>
  <hr />
  <p><code>myfile</code> not defined.</p>
  <p>Please try,</p>
  <pre><code>fasta_file = (sys.argv[0])
  </code></pre>
  <p>Generally your argv command isn't cool and not just [0] versus [1]. I forget the argv module everyone uses .. there's two of them, they are a bit fiddle but are very cool of options. You way in is okay, okayish, sort of okay ... Anyway you appear to be requesting the second input into the commandline, wherea you are probably placing the fasta file in the first position.</p>
  <p>Also</p>
  <pre><code>print (fasta_file)
  </code></pre>
  <p>Check something happens that you picked up the file.</p>
  <p>The other way in is,</p>
  <pre><code>mypath = '/User/username/location/fasta.fa'
  for myfile in SeqIO.parse(mypath, &quot;fasta&quot;):
  </code></pre>
  <p>where <code>mypath</code> is a OS X style, you're probably using Linux so its... /home/username/pathtofile/fastafile.fa (something like that) or</p>
  <pre><code>for myfile in SeqIO.parse('/User/username/location/fasta.fa', &quot;fasta&quot;):
  </code></pre>
  <p>I forgot .... don't ever use <code>list</code> as a variable, by <code>mylist</code> is fine. Again I've not debugged my code.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-23T19:24:43.910Z
Answers.Text: Hi @Michael I tried doing that, but received a NameError indicating that the name 'myfile' is not defined. I appreciate your help!
Answers.UserDisplayName: andnowmywatchbegins
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-23T20:11:52.297Z
Answers.Text: @andnowmywatchbegins can you update your answer with this information?
Answers.UserDisplayName: Maximilian Press
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-23T20:16:51.913Z
Answers.Text: `list` is used in Python for creating lists. I would recommend naming this variable something else.
Answers.UserDisplayName: Alex Reynolds
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-23T20:34:52.373Z
Answers.Text: `list` .... Yes!! Use `mylist` as a variable. instead
Answers.UserDisplayName: M__
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-11-23T19:08:16.690Z
Answers.LastActivityDate: 2020-11-23T20:46:42.260Z
Answers.LastEditDate: 2020-11-23T20:46:42.260Z
Answers.UserDisplayName: M__
Body: <p>I am working on a project and am having issues with the following code that I have written in <code>nano</code>:</p>
  <pre><code>from Bio import SeqIO
  import sys
  import re 
  
  
      fasta_file = (sys.argv[1])
      for myfile in SeqIO.parse(fasta_file, &quot;fasta&quot;):
        if len(myfile) &gt; 250:
         gene_id = myfile.id
         mylist = re.match('H149xcV\_\w+\_\w+\_\w+',gene_id)
         print (&quot;&gt;&quot;+list.group(1)) 
  </code></pre>
  <p>This is the error I receive when I execute my command on <code>command-line</code>:</p>
  <pre><code>File &quot;mpo.py&quot;, line 7, in &lt;module&gt;
      gene_id = myfile.id
  NameError: name 'myfile' is not defined
  </code></pre>
  <p>I have a fasta file with the format</p>
  <pre class="lang-none prettyprint-override"><code>&gt;H149xcV_Fge342_r3_h2_d1 len=210 path=[0:0-206]
  ACTATACATGAGGAGAACATAGAACAAAAATGGGACCATAGATATATAACAATAGAAGATATAGAGAACACAATAGACAACTTATTAGGAAAGAGGTGTGTCGTCATGGAGCTGATGTTCGAGGATACTTTGCATGGTCATTCTTGGATAATTTTGAGTGGGCTATGGGATACACCAAGAGGTTTGGCATTGTTTATGTTGATTATAAGAACGGGC
  
  &gt;H149xcV_ytR1oP_r3_h2_d1 len=306 path=[0:0-207]
  ATTAGAGTCTGAGAGAGTCTTGATTTGTCGTCGTCGAGAAATATAGGAGATCTGATTAGAGGAGAGAGCGGCCTAGGCGATGCGCGATATAGCGCTATATAGGCCTAGAGGAGAGTCTCTCTCTTTTAGAAGAGATAATATATATATATATATGGCTCTCCGGCGGGGCCGCGCGAGAGCTCGATCGATCGATATTAGCTGTACGATGCTAGCTAGCTTATATTCGATCGATTATAGCTTAGATCTCTCTCTAAAGGTCGATATCGCTTATGCGCGCGTATATCG
  </code></pre>
  <p>I wish to reformat my file so that it only provides me with the unique gene id's for genes with a length greater than 250 bp.</p>
  <p>My desired output is:</p>
  <pre><code>&gt;H149xcV_Fge342_r3_h2
  &gt;H149xcV_ytR1oP_r3_h2
  &gt;H149xcV_DPN78333_r3_h2
  &gt;H149xcV_AgV472_r3_h2
  &gt;H149xcV_DNP733_r3_h2
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-11-23T20:13:03.213Z
Comments.Text: note that after your edit, your code now does not match your error. You should update the error to the new error, otherwise we can't help you very well.
Comments.UserDisplayName: Maximilian Press
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-11-23T20:18:01.603Z
Comments.Text: Hi @MaximilianPress I have updated the error!
Comments.UserDisplayName: andnowmywatchbegins
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-11-23T20:49:40.757Z
Comments.Text: I updated the post to attempt to trap the bugs.
Comments.UserDisplayName: M__
Tags: <python><phylogenetics><fasta><phylogeny><seqio>
Title: How to fix NameError?
ThreadText: # How to fix NameError?
  
  (Asked by: username_0 on 2020-11-23)
  
  I am working on a project and am having issues with the following code that I have written in `nano`:
  
  ```
  from Bio import SeqIO
  import sys
  import re 
  
      fasta_file = (sys.argv[1])
      for myfile in SeqIO.parse(fasta_file, "fasta"):
        if len(myfile) > 250:
         gene_id = myfile.id
         mylist = re.match('H149xcV\_\w+\_\w+\_\w+',gene_id)
         print (">"+list.group(1)) 
  
  ```
  
  This is the error I receive when I execute my command on `command-line`:
  
  ```
  File "mpo.py", line 7, in <module>
      gene_id = myfile.id
  NameError: name 'myfile' is not defined
  
  ```
  
  I have a fasta file with the format
  
  ```none
  >H149xcV_Fge342_r3_h2_d1 len=210 path=[0:0-206]
  ACTATACATGAGGAGAACATAGAACAAAAATGGGACCATAGATATATAACAATAGAAGATATAGAGAACACAATAGACAACTTATTAGGAAAGAGGTGTGTCGTCATGGAGCTGATGTTCGAGGATACTTTGCATGGTCATTCTTGGATAATTTTGAGTGGGCTATGGGATACACCAAGAGGTTTGGCATTGTTTATGTTGATTATAAGAACGGGC
  
  >H149xcV_ytR1oP_r3_h2_d1 len=306 path=[0:0-207]
  ATTAGAGTCTGAGAGAGTCTTGATTTGTCGTCGTCGAGAAATATAGGAGATCTGATTAGAGGAGAGAGCGGCCTAGGCGATGCGCGATATAGCGCTATATAGGCCTAGAGGAGAGTCTCTCTCTTTTAGAAGAGATAATATATATATATATATGGCTCTCCGGCGGGGCCGCGCGAGAGCTCGATCGATCGATATTAGCTGTACGATGCTAGCTAGCTTATATTCGATCGATTATAGCTTAGATCTCTCTCTAAAGGTCGATATCGCTTATGCGCGCGTATATCG
  
  ```
  
  I wish to reformat my file so that it only provides me with the unique gene id's for genes with a length greater than 250 bp.
  
  My desired output is:
  
  ```
  >H149xcV_Fge342_r3_h2
  >H149xcV_ytR1oP_r3_h2
  >H149xcV_DPN78333_r3_h2
  >H149xcV_AgV472_r3_h2
  >H149xcV_DNP733_r3_h2
  
  ```
  
  **Tags:** python, phylogenetics, fasta, phylogeny, seqio
  
  > **Comments:**
  > **username_1**: note that after your edit, your code now does not match your error. You should update the error to the new error, otherwise we can't help you very well.  
  > **username_0**: Hi @username_1 I have updated the error!  
  > **username_2**: I updated the post to attempt to trap the bugs.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2020-11-23. Score: 1 | Accepted answer
  
  You might consider changing the code to make the whitespace a little more consistent (as it was, it failed when i tried to run it).
  
  Also, why are you using regular expressions? You have already extracted the `gene_id` using Biopython. The following works for me:
  
  (updated to handle the underscore delimiting)
  
  ```
  from Bio import SeqIO
  import sys
  import re 
  
  fasta_file = (sys.argv[1])
  for myfile in SeqIO.parse(fasta_file, "fasta"):
      if len(myfile.seq) > 250:  # note .seq here!
          gene_id = myfile.id
          # unclear why using regex
          #match_list = re.match('H149xcV\_\w+\_\w+\_\w+',gene_id)
          #print (">"+match_list.group(0)) 
          print("_".join(gene_id.split("_")[0:-1]))
  
  ```
  
  > **Comments:**
  > **username_0**: Hi @username_1 , how would I change the regex so that it only provides me with `>H149xcV_Fge342_r3_h2` and not`>H149xcV_Fge342_r3_h2_d1`. I would not like the very end part that consists of `d1`. So basically, how can I format it so it it only provides this `>H149xcV_Fge342_r3_h2`. If I am using regex would I not need to use gene.id?  
  > **username_2**: @username_0 wants to remove the stuff after the space in the fasta header. In phylogeny this is either removed or spaces substituted because the space in phylip format denotes the start of the sequence  
  > **username_0**: @Michael I've tried reformatting the entire regular expression and I am still getting the full `>H149xcV_Fge342_r3_h2_d1`, is there a way to only receive `>H149xcV_Fge342_r3_h2` as in output? Thanks!  
  > **username_2**: Can you put this as a separate question please? .. Yes you will but thats a different part of regex  
  > **username_1**: @Michael stuff after the whitespace is already stripped out by biopython when you use the `.id`. this is one of the more annoying behaviors of biopython IMO, but in this instance it is useful. I've confirmed that my example code does this. It admittedly does not handle the underscore delimitation, which I missed. But that can be done with split much more simply.  
  > **username_1**: @username_0 why not just split on `_` and only keep as many `_`-delimited fields as you want? Regex will work for this in theory but it's annoying to debug in cases like this.  
  > **username_2**: @username_1 I've put a response here .. https://bioinformatics.stackexchange.com/questions/14913/how-to-obtain-desired-output  
  > **username_3**: @username_1 "if len(myfile.seq) > 250:  # note .seq here!" -- `.seq` is actually not needed there  
  > **username_1**: @username_3 ah, good to know... another confusing feature of biopython!  
  
  ---
  
  ### Answer by username_2 on 2020-11-23. Score: 3
  
  You wrote,
  
  ```
  mylist = re.match(H149xcV\_\w+\_\w+\_\w+)
  
  ```
  
  try...
  
  ```
  myregex = re.compile(r'H149xcV\_\w+\_\w+\_\w+')
  mylist = myregex.match(myfile.id)
  
  ```
  
  or
  
  ```
  mylist = re.match(r'H149xcV\_\w+\_\w+\_\w+',myfile.id)
  
  ```
  
  Watch your indentation it should be 4 spaces. Note you are losing the last '\_\w+', but don't think it matters.
  
  I think it is better to use,
  
  ```
  mylist, _, _ = re.split(r' ', myfile.id)
  
  ```
  
  The problem with your code is `list` will just return `true` ... and that is not what you want. If you are using `split` carefully watch the number of ' ' in your code you can also use `r'\s'`
  
  You can also set maxsplit as a parameter, which might be useful.
  
  ---
  
  `myfile` not defined.
  
  Please try,
  
  ```
  fasta_file = (sys.argv[0])
  
  ```
  
  Generally your argv command isn't cool and not just [0] versus [1]. I forget the argv module everyone uses .. there's two of them, they are a bit fiddle but are very cool of options. You way in is okay, okayish, sort of okay ... Anyway you appear to be requesting the second input into the commandline, wherea you are probably placing the fasta file in the first position.
  
  Also
  
  ```
  print (fasta_file)
  
  ```
  
  Check something happens that you picked up the file.
  
  The other way in is,
  
  ```
  mypath = '/User/username/location/fasta.fa'
  for myfile in SeqIO.parse(mypath, "fasta"):
  
  ```
  
  where `mypath` is a OS X style, you're probably using Linux so its... /home/username/pathtofile/fastafile.fa (something like that) or
  
  ```
  for myfile in SeqIO.parse('/User/username/location/fasta.fa', "fasta"):
  
  ```
  
  I forgot .... don't ever use `list` as a variable, by `mylist` is fine. Again I've not debugged my code.
  
  > **Comments:**
  > **username_0**: Hi @Michael I tried doing that, but received a NameError indicating that the name 'myfile' is not defined. I appreciate your help!  
  > **username_1**: @username_0 can you update your answer with this information?  
  > **username_4**: `list` is used in Python for creating lists. I would recommend naming this variable something else.  
  > **username_2**: `list` .... Yes!! Use `mylist` as a variable. instead  
  
  ---
  
  