Skip to content

Commit

Permalink
Changes in the way of how the package and his files are loaded (#21)
Browse files Browse the repository at this point in the history
* changes in the way of how the package and is files are loaded

* little change in tests

* undo previous commit
  • Loading branch information
PabloGGaray authored and aloctavodia committed Dec 21, 2017
1 parent ac4b9c7 commit 68d1d72
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 131 deletions.
24 changes: 4 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ After installation is done you can run the automatic tests (optional), you will
```
cd chesweet-master
```

**or**

```
Expand All @@ -67,7 +68,7 @@ import chesweet as chsw
```

### Load look-up tables (lut)
One of the main argument of `load` is `full`. Passing `False` (default) is useful when working with the glycosidic torsional angles phi (Φ), psi (Ψ), and omega (Ω), `True` allows you to work with the mentioned torsional angles and also included the chi (*Χ*) torsionals. In the image below you can see the mentioned torsional angles for α-D-Glcp-(1-4)-α-D-Glcp.
The main argument of `load` is `full`. Passing `False` (default) is useful when working with the glycosidic torsional angles phi (Φ), psi (Ψ), and omega (Ω), `True` allows you to work with the mentioned torsional angles and also included the chi (*Χ*) torsionals. In the image below you can see the mentioned torsional angles for α-D-Glcp-(1-4)-α-D-Glcp.

<p align="center">
<img src="img/maltose_all_torsional.png", width=500>
Expand All @@ -77,6 +78,7 @@ The names of the disaccharides used by *Che*Sweet follow the IUPAC syntax, but w

Examples:
Load **all calculated dissacharides** in *Che*Sweet:

```python
# Reduced version of the datasets (using only phi, psi and omega)
disaccharides_red = chsw.CheSweet()
Expand All @@ -85,25 +87,7 @@ disaccharides_red = chsw.CheSweet()
disaccharides_full = chsw.CheSweet(full=True)
```

Load **one dissacharide**:
```python
# Reduced version of the datasets
maltose_red = chsw.CheSweet(disaccharides = ['a-D-Glcp-1-4-a-D-Glcp'])

# Using full=True
maltose_full = chsw.CheSweet(disaccharides = ['a-D-Glcp-1-4-a-D-Glcp'], full=True)
```

Load **a number of dissacharides**:
```python
disaccharides_list = ['a-D-Glcp-1-4-a-D-Glcp', 'a-D-Glcp-1-1-a-D-Glcp', 'a-D-Galp-1-3-b-D-Galp', 'b-D-Galp-1-6-b-D-Galp']

# Reduced version of the datasets
disaccharides_red = chsw.CheSweet(disaccharides = disaccharides_list)

# Using full=True
disaccharides_full = CheSweet(disaccharides = disaccharides_list, full=True)
```
You have the option of changing the location of the folder `lut`, in that case, you have to indicate the new location on the argument `path`.

Once you have loaded the look-up tables it is possible to calculate the chemical shifts or the torsional that you want.
In the next examples we show how to use these functions for maltose \[α-D-Glcp-(1-4)-α-D-Glcp].
Expand Down
67 changes: 29 additions & 38 deletions chesweet/chesweet.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
import glob
from os.path import basename as bn
import pkg_resources as pkg
from os.path import join
import sys
import math
import numpy as np
from scipy.interpolate import griddata
from os.path import dirname
from os.path import join
import sys


class CheSweet():
"""
Class to compute chemical shift or torsional angles of glycosidics bonds.
"""

def __init__(self, path=None, disaccharides=None, full=False):
def __init__(self, path=None, full=False):
"""
Parameters
----------
disaccharides : list of strings
names of the disaccharides used as keys in the dictionary
path : string
folder of lookup table, if None (default) CheSweet's lookup table will be used
full : Boolean
whether to include chi's torsional angles (True) in the computation
of chemical shifts or not (False)
"""
if path == None:
path = dirname(__file__)
sys.path.append(path)
lut_path = join(path, 'lut')
self.path = lut_path
else:
self.path = path
self.full = full
if disaccharides is None:
files = '{}/*[!_red]'.format(self.path)
self.disaccharides = [bn(a) for a in glob.glob(files)]

if path is None:
files = pkg.resource_filename(__name__, '/'.join(['lut']))
else:
self.disaccharides = disaccharides
files = '/'.join([path, 'lut'])

if full:
self.disaccharides = glob.glob('{}/*[!_red]'.format(files))
else:
self.disaccharides = glob.glob('{}/*_red'.format(files))

self.lt = _load(self)


Expand Down Expand Up @@ -77,6 +75,8 @@ def compute_cs(self, disaccharide, phi, psi, chi1=None, chi2=None,
"""

lt = self.lt
if not self.full:
disaccharide = disaccharide + '_red'
# phi and psi angles in lt are compute using a 10 degree grid.
phi_range = _round_down_up(phi, 10)
psi_range = _round_down_up(psi, 10)
Expand Down Expand Up @@ -159,9 +159,14 @@ def compute_tors(self, disaccharide, cs0, cs1, ef_corr=183.4, eps=0.5):
# transform cs into shieldings
cs0 = ef_corr - cs0
cs1 = ef_corr - cs1

if not self.full:
disaccharide = disaccharide + '_red'

x = self.lt[disaccharide]
cond0 = (x[:,-2] < cs0 + eps) & (x[:,-2] > cs0 - eps)
cond1 = (x[:,-1] < cs1 + eps) & (x[:,-1] > cs1 - eps)

if self.full:
if int(disaccharide.split('-')[4]) == 1:# bonds 1-1
theoric_tors = x[:,:4][cond0 & cond1]
Expand Down Expand Up @@ -190,29 +195,15 @@ def _load(self):
lut = {}
if self.full:
for disaccharide in self.disaccharides:
try:
lut[disaccharide] = np.fromfile('{}/{}'.format(self.path,
disaccharide),
sep=' ').reshape(-1, 8)
except FileNotFoundError:
print("The dissacharide {} is not calculated, "
"or the path {} is wrong".format(disaccharide, self.path))
lut[bn(disaccharide)] = np.fromfile(disaccharide, sep=' ').reshape(-1, 8)
else:
for disaccharide in self.disaccharides:
try:
# Disaccharides with 1-6 glycosidic bond
if '-1-6-' in disaccharide:
lut[disaccharide] = np.fromfile('{}/{}_red'.format(self.path,
disaccharide),
sep=' ').reshape(-1, 5)
# Disaccharides with glycosidic bond different from 1-6
else:
lut[disaccharide] = np.fromfile('{}/{}_red'.format(self.path,
disaccharide),
sep=' ').reshape(-1, 4)
except FileNotFoundError:
print("The dissacharide {} is not calculated, "
"or the path {} is wrong".format(disaccharide, self.path))
# Disaccharides with 1-6 glycosidic bond
if '-1-6-' in disaccharide:
lut[bn(disaccharide)] = np.fromfile(disaccharide, sep=' ').reshape(-1, 5)
# Disaccharides with glycosidic bond different from 1-6
else:
lut[bn(disaccharide)] = np.fromfile(disaccharide, sep=' ').reshape(-1, 4)

if lut:
return lut
Expand Down
Loading

0 comments on commit 68d1d72

Please sign in to comment.