Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Martin Larralde
peptides.py
Commits
d478aea3
Commit
d478aea3
authored
Oct 26, 2021
by
Martin Larralde
Browse files
Update `Peptide.charge` to use vectorized code
parent
d755f53e
Changes
2
Hide whitespace changes
Inline
Side-by-side
peptides/__init__.py
View file @
d478aea3
...
...
@@ -575,20 +575,27 @@ class Peptide(typing.Sequence[str]):
ISBN:978-83-01-12044-3.
"""
sign_scale
=
tables
.
CHARGE
[
"sign"
]
# get chosen the pKa scale
scale
=
tables
.
PK
.
get
(
pKscale
)
if
scale
is
None
:
raise
ValueError
(
f
"Invalid pK scale:
{
scale
!
r
}
"
)
# nterm
charge
=
1.0
/
(
1.0
+
10
**
(
1.0
*
(
pH
-
scale
[
"nTer"
])))
# aa
for
aa
in
self
.
sequence
:
sign
=
sign_scale
.
get
(
aa
,
0
)
charge
+=
sign
/
(
1
+
10
**
(
sign
*
(
pH
-
scale
.
get
(
aa
,
0
))))
# cterm
charge
+=
-
1.0
/
(
1.0
+
10
**
(
-
1.0
*
(
pH
-
scale
[
"cTer"
])))
# build a look-up table for the pKa scale and the charge sign
lut
=
array
([
scale
.
get
(
aa
,
0.0
)
for
aa
in
self
.
_CODE1
])
sign_lut
=
array
([
tables
.
CHARGE
[
"sign"
].
get
(
aa
,
0.0
)
for
aa
in
self
.
_CODE1
])
# compute charge of each amino-acid and sum
pK
=
take
(
lut
,
self
.
encoded
)
sign
=
take
(
sign_lut
,
self
.
encoded
)
charge
=
sum
(
sign
/
(
1
+
10
**
(
sign
*
(
pH
-
pK
))))
# add charge for C-terminal and N-terminal ends of the peptide
if
"nTer"
in
scale
:
charge
+=
1.0
/
(
1.0
+
10
**
(
1.0
*
(
pH
-
scale
[
"nTer"
])))
if
"cTer"
in
scale
:
charge
+=
-
1.0
/
(
1.0
+
10
**
(
-
1.0
*
(
pH
-
scale
[
"cTer"
])))
# return the net protein charge
return
charge
def
hydrophobic_moment
(
self
,
window
:
int
=
11
,
angle
:
int
=
100
)
->
float
:
...
...
peptides/_npcompat.py
View file @
d478aea3
...
...
@@ -11,13 +11,123 @@ class array(array.array):
def
__new__
(
cls
,
values
,
dtype
=
'f'
):
return
super
().
__new__
(
cls
,
dtype
,
values
)
def
__add__
(
self
,
other
):
if
isinstance
(
other
,
(
int
,
float
)):
return
array
((
x
+
other
for
x
in
self
),
dtype
=
self
.
typecode
)
elif
not
isinstance
(
other
,
array
):
return
NotImplemented
if
len
(
self
)
!=
len
(
other
):
raise
ValueError
(
"Cannot pairwise multiply arrays of different lengths"
)
return
array
(
(
x1
+
x2
for
x1
,
x2
in
zip
(
self
,
other
)),
dtype
=
self
.
typecode
)
def
__radd__
(
self
,
other
):
if
isinstance
(
other
,
(
int
,
float
)):
return
array
((
other
+
x
for
x
in
self
),
dtype
=
self
.
typecode
)
elif
not
isinstance
(
other
,
array
):
return
NotImplemented
if
len
(
self
)
!=
len
(
other
):
raise
ValueError
(
"Cannot pairwise multiply arrays of different lengths"
)
return
array
(
(
x2
+
x1
for
x1
,
x2
in
zip
(
self
,
other
)),
dtype
=
self
.
typecode
)
def
__sub__
(
self
,
other
):
if
isinstance
(
other
,
(
int
,
float
)):
return
array
((
x
-
other
for
x
in
self
),
dtype
=
self
.
typecode
)
elif
not
isinstance
(
other
,
array
):
return
NotImplemented
if
len
(
self
)
!=
len
(
other
):
raise
ValueError
(
"Cannot pairwise multiply arrays of different lengths"
)
return
array
(
(
x1
-
x2
for
x1
,
x2
in
zip
(
self
,
other
)),
dtype
=
self
.
typecode
)
def
__rsub__
(
self
,
other
):
if
isinstance
(
other
,
(
int
,
float
)):
return
array
((
other
-
x
for
x
in
self
),
dtype
=
self
.
typecode
)
elif
not
isinstance
(
other
,
array
):
return
NotImplemented
if
len
(
self
)
!=
len
(
other
):
raise
ValueError
(
"Cannot pairwise multiply arrays of different lengths"
)
return
array
(
(
x2
-
x1
for
x1
,
x2
in
zip
(
self
,
other
)),
dtype
=
self
.
typecode
)
def
__mul__
(
self
,
other
):
if
isinstance
(
other
,
(
int
,
float
)):
return
array
((
x
*
other
for
x
in
self
),
dtype
=
self
.
typecode
)
elif
not
isinstance
(
other
,
array
):
return
NotImplemented
if
len
(
self
)
!=
len
(
other
):
raise
ValueError
(
"Cannot pairwise multiply arrays of different lengths"
)
return
array
(
(
x1
*
x2
for
x1
,
x2
in
zip
(
self
,
other
)),
dtype
=
self
.
typecode
)
def
__rmul__
(
self
,
other
):
if
isinstance
(
other
,
(
int
,
float
)):
return
array
((
other
*
x
for
x
in
self
),
dtype
=
self
.
typecode
)
elif
not
isinstance
(
other
,
array
):
return
NotImplemented
if
len
(
self
)
!=
len
(
other
):
raise
ValueError
(
"Cannot pairwise multiply arrays of different lengths"
)
return
array
(
(
x2
*
x1
for
x1
,
x2
in
zip
(
self
,
other
)),
dtype
=
self
.
typecode
)
def
__truediv__
(
self
,
other
):
if
isinstance
(
other
,
(
int
,
float
)):
return
array
((
x
/
other
for
x
in
self
),
dtype
=
self
.
typecode
)
elif
not
isinstance
(
other
,
array
):
return
NotImplemented
if
len
(
self
)
!=
len
(
other
):
raise
ValueError
(
"Cannot pairwise multiply arrays of different lengths"
)
return
array
(
(
x1
/
x2
for
x1
,
x2
in
zip
(
self
,
other
)),
dtype
=
self
.
typecode
)
def
__rtruediv__
(
self
,
other
):
if
isinstance
(
other
,
(
int
,
float
)):
return
array
((
other
/
x
for
x
in
self
),
dtype
=
self
.
typecode
)
elif
not
isinstance
(
other
,
array
):
return
NotImplemented
if
len
(
self
)
!=
len
(
other
):
raise
ValueError
(
"Cannot pairwise multiply arrays of different lengths"
)
return
array
(
(
x2
/
x1
for
x1
,
x2
in
zip
(
self
,
other
)),
dtype
=
self
.
typecode
)
def
__pow__
(
self
,
other
):
if
isinstance
(
other
,
(
int
,
float
)):
return
array
((
x
**
other
for
x
in
self
),
dtype
=
self
.
typecode
)
if
not
isinstance
(
other
,
array
):
return
NotImplemented
if
len
(
self
)
!=
len
(
other
):
raise
ValueError
(
"Cannot pairwise multiply arrays of different lengths"
)
return
array
(
(
x1
**
x2
for
x1
,
x2
in
zip
(
self
,
other
)),
dtype
=
self
.
typecode
)
def
__rpow__
(
self
,
other
):
if
isinstance
(
other
,
(
int
,
float
)):
return
array
((
other
**
x
for
x
in
self
),
dtype
=
self
.
typecode
)
if
not
isinstance
(
other
,
array
):
return
NotImplemented
if
len
(
self
)
!=
len
(
other
):
raise
ValueError
(
"Cannot pairwise multiply arrays of different lengths"
)
return
array
(
[
x1
*
x
2
for
x1
,
x2
in
zip
(
self
,
other
)
]
,
(
x2
*
*
x
1
for
x1
,
x2
in
zip
(
self
,
other
)
)
,
dtype
=
self
.
typecode
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment