Extracting Fasta Moonlight Protein Sequences with Python
<p>I want to extract the FASTA files that have the aminoacid sequence from the Moonlighting Protein Database ( www.moonlightingproteins.org/results.php?search_text= ) via Python, since it's an iterative process, which I'd rather learn how to program than manually do it, b/c come on, we're in 2016. The problem is I donÂ´t know how to write the code, because I'm a rookie programmer :( . The basic pseudocode would be: </p>

<pre><code> for protein_name in site: www.moonlightingproteins.org/results.php?search_text=:

       go to the uniprot option 

       download the fasta file 

       store it in a .txt file inside a given folder
</code></pre>

<p>Thanks in advance! </p>

<p>I would strongly suggest to ask the authors for the database. From the <a href="http://www.moonlightingproteins.org/faqs.php" rel="nofollow">FAQ</a>:</p>

<blockquote>
  <p>I would like to use the MoonProt database in a project to analyze the
  amino acid sequences or structures using bioinformatics. </p>
  
  <p>Please contact us at bioinformatics@moonlightingproteins.org if you are
  interested in using MoonProt database for analysis of sequences and/or
  structures of moonlighting proteins.</p>
</blockquote>

<p>Assuming you find something interesting, how are you going to cite it in your paper or your thesis? 
"The sequences were scraped from a public webpage without the consent of the authors". Much better to give credit to the original researchers.</p>

<p>That's a good introduction to <a href="http://docs.python-guide.org/en/latest/scenarios/scrape/" rel="nofollow">scraping</a></p>

<p>But back to your your original question.</p>

<pre><code>import requests
from lxml import html
#let's download one protein at a time, change 3 to any other number
page = requests.get('http://www.moonlightingproteins.org/detail.php?id=3')
#convert the html document to something we can parse in Python
tree = html.fromstring(page.content)
#get all table cells
cells = tree.xpath('//td')

for i, cell in enumerate(cells):
    if cell.text:
        #if we get something which looks like a FASTA sequence, print it
        if cell.text.startswith('&gt;'):
            print(cell.text)
    #if we find a table cell which has UniProt in it
    #let's print the link from the next cell
    if 'UniProt' in cell.text_content():
        if cells[i + 1].find('a') is not None and 'href' in cells[i + 1].find('a').attrib:
            print(cells[i + 1].find('a').attrib['href'])
</code></pre>

python database data-mining bioinformatics protein-database