Exemplo n.º 1
0
def get_opm_file(arg_pdbid):
    """
    Ensure the flatfile with annotations from OPM for the given PDB id is present
    and return the path to it.
    Search order: 1:Locally in fetch_path, 2:OPM Remote
    """

    path = setting.get('fetch_path')    #Path also used by the 'fetch' command. If not set in pymolrc, it's probably ~
    localfn = os.path.join(path, arg_pdbid.upper() + '.opm')
    if not os.access(localfn, os.R_OK):
        #Retrieve the file from OPM website
        try:
            #Retrieve all segments at once
            opm_segments_urlobj = urllib2.urlopen(OPM_SUBUNITS_WEBSITE)
            opm_segments_html = opm_segments_urlobj.read()
            #Get the relevant part from that site
            soup = BeautifulSoup.BeautifulSoup(opm_segments_html)
            opm_segments_div = str(soup.find("div", {"id": "body"}))

            if opm_segments_div is not None:
                opm_segments = opm_segments_div.split('<br />')
                pdb_id = ''
                arg_pdbid_lines = []
                for segment in opm_segments:
                    match = re.search('(\w{4}) <b>(\w)</b>', segment)
                    if match:
                        pdb_id = match.group(1)
                        if pdb_id == arg_pdbid.lower():
                            arg_pdbid_lines.append(segment)
                #Write all relevant lines to output file
                if len(arg_pdbid_lines) > 0:
                    output = open(localfn,'wb')
                    output.write('\n'.join(arg_pdbid_lines) + '\n')
                    output.close()
                else:
                    print 'Could not find any annotation for pdbid %s in OPM' % (arg_pdbid)
                    exit(1)
            else:
                print 'Error during accession or retrieval of OPM file. Cannot find segment annotations in page.'
                exit(1)
        except:
            print 'Error during accession or retrieval of OPM file'
            raise
    else:
        print 'Found local file'
    return localfn
Exemplo n.º 2
0
def get_pdbtm_xml(arg_pdbid):
    """
    Make sure the XML file for the given pdbid is present and return its path
    Search order: 1:Locally in fetch_path, 2:PDBTM Remote
    """

    path = setting.get('fetch_path')    #Path also used by the 'fetch' command. If not set in pymolrc, it's probably ~
    localfn = os.path.join(path, arg_pdbid.upper() + '.xml')
    if not os.access(localfn, os.R_OK):
        #Retrieve file from remote
        try:
            xmlfile = urllib2.urlopen('http://pdbtm.enzim.hu/data/database/' + arg_pdbid[1:3] + '/' + arg_pdbid.lower() + '.xml')
            output = open(localfn,'wb')
            output.write(xmlfile.read())
            output.close()
        except:
            print 'Error during accession or retrieval of XML file'
            raise
    return localfn