File: rsnp.py

package info (click to toggle)
ugene 51.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 125,712 kB
  • sloc: cpp: 637,632; xml: 212,751; ansic: 81,474; javascript: 5,416; sh: 1,204; python: 771; makefile: 36
file content (115 lines) | stat: -rw-r--r-- 4,616 bytes parent folder | download
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
#!/usr/bin/env python3
"""
#v_1.00
"""
import sys, os, time, re
import urllib, urllib2, cookielib
from optparse import OptionParser

# print current time and information on screen
def Info(infoStr):
    print "[%s] %s" %(time.strftime('%H:%M:%S'), infoStr)

def prepare_optparser():
    """Prepare optparser object. New options will be added in this
    function first.
    """
    usage = "usage: %prog <-f STRING -s STRING> [options]"
    description = "Transcription factors binds to two DNAs: SNPs and\or site-directed mutations."

    optparser = OptionParser(version="%prog v1.02", 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("-f", "--snpf1",dest="firstSeq",type="string",
                         help="Select a nucleotide sequence of the SNP variant #1.")

    optparser.add_option("-s", "--snpf2",dest="secondSeq",type="string",
                         help="Select a nucleotide sequence of the SNP variant #2.")
    
    optparser.add_option("--snpdm1",dest="snpdm1",type="int",
                         help="""Select How the unknown binds to DNA#1.\n
                         Enter integer from 0 to 5\n
                         default:1""", default=1)

    optparser.add_option("--snpdm2",dest="snpdm2",type="int",
                         help="""Select How the unknown binds to DNA#2.\n
                         Enter integer from 0 to 5\n
                         default:2""", default=2)
                                        
    optparser.add_option("--sign",dest="significance",type="int",
                         help="Select significance. Enter integer from 0 to 15", default=10)
    return optparser

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

    Return: Validated options object.
    """
    (options,args) = optparser.parse_args()
 
    if options.firstSeq and '"' in options.firstSeq:
        options.firstSeq = options.firstSeq.replace('"','')
    if options.secondSeq and '"' in options.secondSeq:
        options.secondSeq = options.secondSeq.replace('"','')
        
    if not options.firstSeq and not options.secondSeq:
        optparser.print_help()
        sys.exit(1)

    if not 0 <= options.snpdm1 <= 5 and 0 <= options.snpdm2 <= 5:
        Info('Wanring: snpdm values must be in range from 0 to 5.')
        sys.exit(1)
    if not 0 <= options.significance <= 16:
        Info('Wanring: Significance value must be in range from 0 to 16.')
        sys.exit(1)
 
    print
    return options

class RSNP:
    def __init__(self, options):
        self.firstSeq = options.firstSeq
        self.secondSeq = options.secondSeq
        self.snpdm1 = options.snpdm1
        self.snpdm2 = options.snpdm2 
        self.significance = options.significance
        self.opts_string = "# Argument List:\n" +\
                           "# first sequence = %s\n" %options.firstSeq +\
                           "# second sequence = %s\n" %options.secondSeq +\
                           "# Unknown binds to DNA#1 = %d\n" %options.snpdm1 +\
                           "# Unknown binds to DNA#2 = %d\n" %options.snpdm2 +\
                           "# Significance = %d\n" %options.significance
        self.firstSeqFromFile = ""
        self.secondSeqFromFile = ""
        self.absentSides = []
        self.presentSides = []
        self.noIdeaSides = []

    def sendQuery(self):
        cookie_jar = cookielib.CookieJar()
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
        urllib2.install_opener(opener)

        # do POST
        url = 'http://samurai.bionet.nsc.ru/cgi-bin/03/programs/rsnp_lin/rsnpd.pl'
        req = urllib2.Request(url, "b1=Calculate&snpf1={0}&snpf2={1}&SNPDM_0={2}&SNPDM_1={3}&DSL={4}".format(self.firstSeq, self.secondSeq, self.snpdm1, self.snpdm2, self.significance))
        rsp = urllib2.urlopen(req)
        content = rsp.read()
        res = content.find('PRESENT:</b>&nbsp;')
        if res == -1:
            print "Result is empty"
        else:
            content = content[res+18:]
            res = content.find('<P align')
            content = content[:res]
            out = content.split()
            for item in out:
                if item:
                    print("PRESENT:"+item)
def main():
    opts=opt_validate(prepare_optparser())
    g = RSNP(opts)
    g.sendQuery()

if __name__ == "__main__":
    main()