forked from marcopasi/DNAflexsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DNAFlexRBP.py
202 lines (175 loc) · 8.55 KB
/
DNAFlexRBP.py
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File: DNAFlexRBP.py
#
# Copyright (C) 2016 Marco Pasi <marco.pasi@nottingham.ac.uk>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
#"DNAFlexRBP.py v0.1 (C) 2016 Marco Pasi"
#
#
from mug import datatypes as mug_datatypes
from DNAFlexibility import DNAFlexibility
from pycompss.api.task import task
from pycompss.api.constraint import constraint
from pycompss.api.parameter import IN, OUT
import urllib2, getopt
import numpy as np
from scipy.linalg import block_diag
#------------------------------------------------------------------------------
__doc__ = """\
Predict the flexibility of a canonical B-form DNA oligomer of
arbitrary sequence by using the NAFlex webserver. NAFlex uses
MD-derived, tetranucleotide-dependent stiffness matrices (inverted
covariance matrices) as a function of the Curves+ inter-base-pair
helical parameters (i.e. Shift, Slide, Rise, Tilt, Roll, Twist). For
more information see the following references. If you find this
software useful, please cite the following references.
Usage:
DNAFlexRBP.py [options] <SEQUENCE>
Options:
-h (--help) Display this help.
References:
[1] Lavery,R., Moakher,M., Maddocks,J.H., Petkeviciute,D. and
Zakrzewska,K. (2009) Conformational analysis of nucleic acids
revisited: Curves+. Nucleic Acids Res., 37, 5917-5929.
[2] Hospital,A., Faustino,I., Collepardo-Guevara,R., Gonzalez,C.,
Gelpi,J.L. and Orozco,M. (2013) NAFlex: a web server for the study of
nucleic acid flexibility. Nucleic Acids Res., 41, W47-W55.
"""
#------------------------------------------------------------------------------
bases=list("ATCG")
_other = {
"A":"T",
"C":"G",
"T":"A",
"G":"C",
"R":"Y",
"Y":"R"
}
def other(seq):
return [_other[x] for x in seq]
def wcc(seq):
return "".join(reversed(other(seq)))
#------------------------------------------------------------------------------
class DNAFlexRBP(DNAFlexibility):
output_data_type = mug_datatypes.Matrix
configuration = {}
ABC_info = map(lambda x:x.split(","), """\
AAAA,AAAA,8;AAAC,AAAC,9;AAAG,GAAA,10;AAAT,AAAT,9;AACA,AAAC,10;AACC,TGGT,27;\
AACG,GAAC,10;AACT,TAAC,10;AAGA,GAAA,7;AAGC,CAAG,10;AAGG,AGGA,8;AAGT,TAAG,10;\
AATA,AAAT,10;AATC,CAAT,10;AATG,GAAT,10;AATT,TAAT,10;ACAA,AAAC,7;ACAC,TGTG,29;\
ACAG,TGTC,29;ACAT,TGTA,29;ACCA,TGGT,28;ACCC,GGGT,28;ACCG,CGGT,28;ACCT,AGGT,28;\
ACGA,GAAC,7;ACGC,CGTG,29;ACGG,CGGA,8;ACGT,CGTA,8;ACTA,TAAC,7;ACTC,AGTG,29;\
ACTG,AGTC,29;ACTT,TAAG,27;AGAA,GAAA,8;AGAC,TGTC,27;AGAG,AGAG,9;AGAT,GATA,8;\
AGCA,CAAG,7;AGCC,TGGC,27;AGCG,AGCG,9;AGCT,AGCT,9;AGGA,AGGA,9;AGGC,AGGC,9;\
AGGG,GGGA,8;AGGT,AGGT,9;AGTA,TAAG,7;AGTC,AGTC,9;AGTG,AGTG,9;AGTT,TAAC,27;\
ATAA,AAAT,7;ATAC,TGTA,27;ATAG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TCGT,GAAC,30;TCTA,GATA,30;TCTC,AGAG,29;\
TCTG,TGTC,7;TCTT,GAAA,30;TGAA,GAAT,8;TGAC,AGTC,27;TGAG,AGTG,7;TGAT,CAAT,30;\
TGCA,ATGC,10;TGCC,AGGC,27;TGCG,CGTG,7;TGCT,CAAG,30;TGGA,TGGA,9;TGGC,TGGC,9;\
TGGG,GGGT,8;TGGT,TGGT,9;TGTA,TGTA,9;TGTC,TGTC,9;TGTG,TGTG,9;TGTT,AAAC,27;\
TTAA,TAAT,8;TTAC,TAAG,29;TTAG,TAAC,29;TTAT,AAAT,30;TTCA,GAAT,29;TTCC,AGGA,27;\
TTCG,GAAC,29;TTCT,GAAA,29;TTGA,CAAT,29;TTGC,CAAG,29;TTGG,TGGT,8;TTGT,AAAC,30;\
TTTA,AAAT,29;TTTC,GAAA,28;TTTG,AAAC,29;TTTT,AAAA,29""".split(";"))
ABC_tetrads = dict(zip(zip(*ABC_info)[0],
[(x[1], int(x[2])) for x in ABC_info]))
def _ABC_tetrad_position(self, tetrad):
"""
Return the 4-letter name of the ABC oligomer where the specified
tetrad is found, and its position within the oligomer's sequence.
"""
return self.ABC_tetrads[tetrad]
def _get_flexibility_http(self, tetrad):
"""
Download the stiffness matrix relative to the specified tetrad from
the NAFlex website, and return it as a numpy array.
"""
oligomer, position = self._ABC_tetrad_position(tetrad)
if position > 18:
position = 36 - position
fullseq = "GC"+oligomer[2:]+oligomer+oligomer+oligomer+"GC"
dinucleotide = fullseq[position-1:position+1]
dinucleotide = dinucleotide + wcc(dinucleotide)
url = "http://mmb.irbbarcelona.org/NAFlex2/getFile.php?"+\
"fileloc=../NAFlex2/NAFlex-Data/NAFlex_parmBSC1/"+\
"NAFlex_mu{oligomer}/STIFFNESS/FORCE_CTES/{dinucleotide}.{position}.cte&type=curves".format(
oligomer=oligomer,
dinucleotide=dinucleotide.lower(),
position=position)
response = urllib2.urlopen(url)
if response.getcode() is not 200 or \
response.info().get("content-length") == '0':
exc_str = "<{url}>"
if response.geturl() != url:
exc_str += " [redirected to <{url}>]".format(
url = response.geturl())
exc_str += " replied {httpcode}\nHEADERS:\n{headers}"
headers = "\n".join("{}:{}".format(*item)
for item in response.info().dict.iteritems())
raise Exception(exc_str.format(
url = url,
httpcode = response.getcode(),
headers = headers))
return np.loadtxt(response)
def _get_flexibility_rest(self, tetrad):
pass
def _get_flexibility(self, tetrad):
"""Return the flexibility of the specified tetrad."""
return self._get_flexibility_http(tetrad)
@task(sequence = IN, stiffness_matrix = OUT)
@constraint(AppSoftware="numpy,scipy")
def run(self, sequence):
"""
Build the full-oligomer stiffness matrix by positioning the 6x6
stiffness matrices for each tetranucleotide (obtained by calling
DNAFlexibility's _get_flexibilities()) on the diagonal.
"""
stiffness_matrix = block_diag(*list(self._get_flexibilities(sequence)))
return stiffness_matrix
#------------------------------------------------------------------------------
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "h", ["help"])
except getopt.error, msg:
sys.stderr.write(msg+"\nSee --help\n")
sys.exit(1)
for o, a in opts:
if o in ("-h", "--help"):
print __doc__
sys.exit(0)
if len(args) < 1:
print __doc__
sys.exit(1)
print DNAFlexRBP().run(args[0])
if __name__ == "__main__":
main()