Answers.Body: <p>The problem in your code is that the function atp_binding is not returning any value, but printing it to the stdout.</p>
  
  <p>You should add a return statement with the result you want to save in your output file.</p>
  
  <p>Also, you should change how the resulting value is being saved. As the return value of your function is None, the file handler will write nothing.</p>
  
  <p>I would use something similar to this:</p>
  
  <pre><code>def atp_binding(records,patern):
      s = ""
      for item in records:
          for m in patern.findall(str(item)):
              istart=m.start()
              iend=m.end()
              iend=iend+50
              s+="{0}\t{1}\n".format(istart,m.group())
      return s
  
  
  
  file = open("output.txt","w")
  
  for seq_record in SeqIO.parse(records, "fasta"): 
     file.write(atp_binding(seq_record.records,patern)+"\n")
     file.write('-------------------\n')
  file.close()
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-24T17:13:07.303Z
Answers.Text: It is a good practice to handle file with `with` command. like : with open("output.txt", "w") as file : .....
Answers.UserDisplayName: Dexter
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-20T20:28:28.813Z
Answers.LastActivityDate: 2019-06-20T20:28:28.813Z
Answers.UserDisplayName: Rafael Rios
Body: <p>I'm currently doing a python project where I need to write code that extracts the ATP binding site from a FASTA file using a regular expression.</p>
  
  <p>I've tried the following code but it does not work. It opens a new file but the file is empty. I don't know what's wrong.</p>
  
  <pre class="lang-py prettyprint-override"><code>from Bio import SeqIO
  import re
  import os
  path="C:/Users/nessr/Desktop/info projet"
  os.chdir(path)
  records=list(SeqIO.parse("plasmo.fasta","fasta"))
  ATPM="[LIV]G[^P]G[^P][FYWMGSTNH][SGA][^PW][LIVCAT][^PD].[GSTACLIVMFY].{5,18}[LIVMFYWCSTAR][AIVP][LIVMFAGCKR]K"
  patern=re.compile(r"(ATPM)")
  
  def atp_binding(records,patern):
  
      for item in records:
          for m in patern.findall(str(item)):
              istart=m.start()
              iend=m.end()
  
              iend=iend+50
  
              print(istart,m.group())
  
  
  file = open("output.txt","w")
  
  for seq_record in SeqIO.parse(records, "fasta"): 
      file.write(atp_binding(seq_record.records,patern)+"\n")
      file.write('-------------------\n')
  file.close()
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-06-18T19:23:41.543Z
Comments.Text: Welcome to Bioinformatics.SE! We're happy to help with homework and research problems, but you're much more likely to get helpful feedback if you describe precisely what your problem is. Asking "can you please tell me how to do this?" will rarely get you a useful answer. Dumping poorly formatted code is another way to turn potential helpers away. Please take a moment to clean up your Python code (use the [Markdown Editing Help guide](https://bioinformatics.stackexchange.com/editing-help#code) if needed), and then please be more specific about what is and is not working.
Comments.UserDisplayName: Daniel Standage
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-06-18T19:27:30.400Z
Comments.Text: I put backticks around your code and cleaned up the question a little bit. I hope this helps. Please continue to edit and clarify your question.
Comments.UserDisplayName: Daniel Standage
Tags: <fasta><phylogenetics><homework>
Title: Function to find ATP binding sites
ThreadText: # Function to find ATP binding sites
  
  (Asked by: username_0 on 2019-06-18)
  
  I'm currently doing a python project where I need to write code that extracts the ATP binding site from a FASTA file using a regular expression.
  
  I've tried the following code but it does not work. It opens a new file but the file is empty. I don't know what's wrong.
  
  ```py
  from Bio import SeqIO
  import re
  import os
  path="C:/Users/nessr/Desktop/info projet"
  os.chdir(path)
  records=list(SeqIO.parse("plasmo.fasta","fasta"))
  ATPM="[LIV]G[^P]G[^P][FYWMGSTNH][SGA][^PW][LIVCAT][^PD].[GSTACLIVMFY].{5,18}[LIVMFYWCSTAR][AIVP][LIVMFAGCKR]K"
  patern=re.compile(r"(ATPM)")
  
  def atp_binding(records,patern):
  
      for item in records:
          for m in patern.findall(str(item)):
              istart=m.start()
              iend=m.end()
  
              iend=iend+50
  
              print(istart,m.group())
  
  file = open("output.txt","w")
  
  for seq_record in SeqIO.parse(records, "fasta"): 
      file.write(atp_binding(seq_record.records,patern)+"\n")
      file.write('-------------------\n')
  file.close()
  
  ```
  
  **Tags:** fasta, phylogenetics, homework
  
  > **Comments:**
  > **username_1**: Welcome to Bioinformatics.SE! We're happy to help with homework and research problems, but you're much more likely to get helpful feedback if you describe precisely what your problem is. Asking "can you please tell me how to do this?" will rarely get you a useful answer. Dumping poorly formatted code is another way to turn potential helpers away. Please take a moment to clean up your Python code (use the [Markdown Editing Help guide](https://bioinformatics.stackexchange.com/editing-help#code) if needed), and then please be more specific about what is and is not working.  
  > **username_1**: I put backticks around your code and cleaned up the question a little bit. I hope this helps. Please continue to edit and clarify your question.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2019-06-20. Score: 1
  
  The problem in your code is that the function atp\_binding is not returning any value, but printing it to the stdout.
  
  You should add a return statement with the result you want to save in your output file.
  
  Also, you should change how the resulting value is being saved. As the return value of your function is None, the file handler will write nothing.
  
  I would use something similar to this:
  
  ```
  def atp_binding(records,patern):
      s = ""
      for item in records:
          for m in patern.findall(str(item)):
              istart=m.start()
              iend=m.end()
              iend=iend+50
              s+="{0}\t{1}\n".format(istart,m.group())
      return s
  
  file = open("output.txt","w")
  
  for seq_record in SeqIO.parse(records, "fasta"): 
     file.write(atp_binding(seq_record.records,patern)+"\n")
     file.write('-------------------\n')
  file.close()
  
  ```
  
  > **Comments:**
  > **username_3**: It is a good practice to handle file with `with` command. like : with open("output.txt", "w") as file : .....  
  
  ---
  
  