File: protstability3d.py

package info (click to toggle)
ugene 52.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 133,376 kB
  • sloc: cpp: 747,593; xml: 212,891; ansic: 82,871; javascript: 5,416; yacc: 1,768; sh: 1,256; python: 771; perl: 226; makefile: 37
file content (146 lines) | stat: -rw-r--r-- 4,888 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#v 1.0
import sys, os, time, re
import urllib, urllib2 
from optparse import OptionParser
from HTMLParser import HTMLParser
from xml.dom.minidom import parseString

#id mapping
def getPDBIdandChain(uniprotID):
    #data from http://www.bioinf.org.uk/pdbsws/
    dirPath = os.path.dirname(sys.argv[0])
    fileName ='pdb_uniprot_chain_map.lst.2'
    if len(dirPath) != 0:
        fileName = dirPath + "/pdb_uniprot_chain_map.lst.2"
    fileName = fileName.replace('\\', '/')
    
    f = open (fileName, 'r')
    pdbId = ''
    chain = ''
    while(1):
        line = f.readline()
        if not line:
            break
        line = line.rstrip('\n')
        columns = line.split()
        if len(columns) != 3:
            continue
        if columns[2] == uniprotID:
            pdbId = columns[0]
            chain = columns[1]
    f.close()
    return pdbId, chain

# Define a function for the thread

class NoRedirectHandler(urllib2.HTTPRedirectHandler):
    def http_error_302(self, req, fp, code, msg, headers):
        infourl = urllib.addinfourl(fp, headers, req.get_full_url())
        infourl.status = code
        infourl.code = code
        return infourl
    http_error_300 = http_error_302
    http_error_301 = http_error_302
    http_error_303 = http_error_302
    http_error_307 = http_error_302

def prepare_optparser():
    """Prepare optparser object. New options will be added in this
    function first.
    """
    usage = "usage: %prog <-i STRING -c STRING -p INT -t STRING> [options]"
    description = "Script for protStability3D tool."

    optparser = OptionParser(version="%prog v1.0", description=description, usage=usage, add_help_option=False)
    optparser.add_option("-h","--help",action="help",help="Show this help message and exit.")
    
    optparser.add_option("-i","--protid",dest="protId",type="string",
                         help="Enter a protein (uniprot) ID.")

    optparser.add_option("-c", "--chain", dest="chain",type="string",
                         help="Enter chain.")
    
    optparser.add_option("-p", "--pos", dest="mutPos",type="int",
                         help="Enter mutation position.")

    optparser.add_option("-t", "--type", dest="aminoType",type="string",
                         help="Enter amino acid type.")
                                        
    return optparser

def opt_validate(optparser):
    """Validate options from a OptParser object.

    Return: Validated options object.
    """
    
    (options,args) = optparser.parse_args()
    if not (options.chain and options.protId and options.mutPos and options.aminoType):
        optparser.print_help()
        sys.exit(1)
 
    id, chain = getPDBIdandChain(options.protId)
    if not (id and chain):
        print "NO PDB ID"
        sys.exit(1)
    
    options.chain = chain
    options.protId = id
    
    return options

class Protstability3d:
    def __init__(self, options):
        self.protId = options.protId
        self.mutPos = options.mutPos
        self.chain = options.chain
        self.aminoType = options.aminoType
        self.url = 'http://www-bionet.sscc.ru/psd2/rest.php?tool=protstability3d'

    def sendQuery(self):
        opener = urllib2.build_opener(NoRedirectHandler())
        urllib2.install_opener(opener)

        # do POST
                                          
        params = urllib.urlencode({'pdb': self.protId, 'chain': self.chain,  'pos_mut': self.mutPos, 'mutation': self.aminoType})
        req = urllib2.Request(self.url,  params)
        
        rsp = urllib2.urlopen(req) 

        content = rsp.read()
        res = content.find('qid=')
        content = content[res+5:]
        res = content.find('"')
        content = content[:res]
        self.url = "http://www-bionet.sscc.ru/psd2/rest.php?tool=protstability3d&q=%s" %content
        self.getResult()      

    def getResult(self):
        opener = urllib2.build_opener(NoRedirectHandler())
        urllib2.install_opener(opener)
        content = "working"
        while content.find('working') >= 0:
            
            req = urllib2.Request(self.url)
            rsp = urllib2.urlopen(req) 
            content = rsp.read()
            time.sleep(0.1)
        self.parseOutput(content)
    def parseOutput(self, content):
        xmldoc = parseString(content)
        
        itemlist = xmldoc.getElementsByTagName('stability')
        if itemlist.length <= 0:
            print "Result is empty"
            sys.exit(1)
 
        res = 'STABILITY_3D' ':' +  str(itemlist[0].firstChild.nodeValue)
        print res                           
def main():
    opts=opt_validate(prepare_optparser())
    g = Protstability3d(opts)
    g.sendQuery()
if __name__ == "__main__":
    main()