Skip to content

Commit

Permalink
Merge 47daf03 into 6f8b1ae
Browse files Browse the repository at this point in the history
  • Loading branch information
dwhswenson committed Oct 6, 2017
2 parents 6f8b1ae + 47daf03 commit f9dac5b
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 43 deletions.
2 changes: 2 additions & 0 deletions .bandit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[bandit]
exclude: */tests
17 changes: 17 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
engines:
radon:
enabled: true
exclude_paths:
- "*/tests/"

fixme:
enabled: true
exclude_paths:
- "**pdb"

duplication:
enabled: true

ratings:
paths:
- "**py"
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
language: python

branches:
only:
- master
- stable

python:
- "2.7"
- "3.4"
Expand Down Expand Up @@ -29,4 +34,3 @@ script:

after_success:
- coveralls

2 changes: 2 additions & 0 deletions ci/pip-install/install.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

pip install --upgrade pip
pip install cython # may be required for numpy override?
pip install --upgrade --force-reinstall numpy # override Travis numpy
Expand Down
1 change: 1 addition & 0 deletions ci/pip-install/testing_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pytest
pytest-cov
python-coveralls
codacy-coverage
32 changes: 13 additions & 19 deletions contact_map/contact_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ def residue_neighborhood(residue, n=1):
# good, and it only gets run once per residue
return [idx for idx in neighborhood if idx in chain]

def _residue_and_index(residue, topology):
res = residue
try:
res_idx = res.index
except AttributeError:
res_idx = residue
res = topology.residue(res_idx)
return (res, res_idx)


class ContactCount(object):
def __init__(self, counter, object_f, n_x, n_y):
Expand Down Expand Up @@ -181,12 +190,7 @@ def residue_ignore_atom_idxs(self):
return result

def most_common_atoms_for_residue(self, residue):
try:
residue_idx = residue.index
except AttributeError:
residue_idx = residue
residue = self.topology.residue(residue_idx)

(residue, residue_idx) = _residue_and_index(residue, self.topology)
residue_atoms = set(atom.index for atom in residue.atoms)
results = []
for contact in self.atom_contacts.most_common_idx():
Expand All @@ -202,16 +206,8 @@ def most_common_atoms_for_contact(self, contact_pair):
contact_pair = frozenset(contact_pair)
res_A = list(contact_pair)[0]
res_B = list(contact_pair)[1]
try:
res_A_idx = res_A.index
except AttributeError:
res_A_idx = res_A
res_A = self.topology.residue(res_A_idx)
try:
res_B_idx = res_B.index
except AttributeError:
res_B_idx = res_B
res_B = self.topology.residue(res_B_idx)
(res_A, res_A_idx) = _residue_and_index(res_A, self.topology)
(res_B, res_B_idx) = _residue_and_index(res_B, self.topology)
atom_idxs_A = set(atom.index for atom in res_A.atoms)
atom_idxs_B = set(atom.index for atom in res_B.atoms)
all_atom_pairs = [
Expand Down Expand Up @@ -359,9 +355,7 @@ def _build_contact_map(self):
# TODO: this whole thing should be cleaned up and should replace
# MDTraj's really slow old computer_contacts by using MDTraj's new
# neighborlists (unless the MDTraj people do that first).
topology = self.topology
trajectory = self.trajectory
cutoff = self.cutoff
self._atom_contacts_count = collections.Counter([])
self._residue_contacts_count = collections.Counter([])

Expand Down Expand Up @@ -429,7 +423,7 @@ def __init__(self, positive, negative):
def __sub__(self, other):
raise NotImplementedError

def contact_map(self, *args, **kwargs):
def contact_map(self, *args, **kwargs): #pylint: disable=W0221
raise NotImplementedError

@property
Expand Down
36 changes: 14 additions & 22 deletions contact_map/tests/test_contact_map.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import os
import collections
import itertools
import pytest
import numpy as np
from numpy.testing import assert_array_equal
import mdtraj as md

# pylint: disable=W0401, R0201
# wildcard imports, method could be function

from .utils import *

# stuff to be testing in this file
from contact_map.contact_map import *


traj = md.load(test_file("trajectory.pdb"))

traj_atom_contact_count = {
Expand Down Expand Up @@ -39,6 +43,10 @@
def counter_of_inner_list(ll):
return collections.Counter([frozenset(i) for i in ll])

def check_most_common_order(most_common):
for i in range(len(most_common) - 1):
assert most_common[i][1] >= most_common[i+1][1]

def test_residue_neighborhood():
top = traj.topology
residues = list(top.residues)
Expand Down Expand Up @@ -248,9 +256,7 @@ def test_most_common_atoms_for_residue(self, select_by):
raise RuntimeError("This should not happen")
# call both by residue and residue number
most_common_2 = self.map.most_common_atoms_for_residue(res_2)
# check order is correct
for i in range(len(most_common_2) - 1):
assert most_common_2[i][1] >= most_common_2[i+1][1]
check_most_common_order(most_common_2)

most_common_numbers_2 = {frozenset([k[0].index, k[1].index]): v
for (k, v) in most_common_2}
Expand Down Expand Up @@ -278,11 +284,7 @@ def test_most_common_atoms_for_contact(self, select_by):
raise RuntimeError("This should not happen")

most_common_2_3 = self.map.most_common_atoms_for_contact(pair)

# check order is correct
for i in range(len(most_common_2_3) - 1):
assert most_common_2_3[i][1] >= most_common_2_3[i+1][1]

check_most_common_order(most_common_2_3)
most_common_2_3_frozenset = [(frozenset(ll[0]), ll[1])
for ll in most_common_2_3]

Expand Down Expand Up @@ -371,42 +373,32 @@ def test_most_common(self, obj_type):
most_common = contacts.most_common()
cleaned = [(frozenset(ll[0]), ll[1]) for ll in most_common]

# check order
for i in range(len(most_common) - 1):
assert most_common[i][1] >= most_common[i+1][1]

# check contents
check_most_common_order(most_common)
assert set(cleaned) == set(expected)

@pytest.mark.parametrize("obj_type", ['atom', 'res'])
def test_most_common_with_object(self, obj_type):
top = self.topology
if obj_type == 'atom':
source_expected = traj_atom_contact_count
contacts = self.map.atom_contacts
obj = top.atom(4)
expected = [(frozenset([obj, top.atom(6)]), 1.0),
(frozenset([obj, top.atom(1)]), 0.8),
(frozenset([obj, top.atom(7)]), 0.4),
(frozenset([obj, top.atom(8)]), 0.2)]
elif obj_type == 'res':
source_expected = traj_residue_contact_count
contacts = self.map.residue_contacts
obj = self.topology.residue(2)
expected = [(frozenset([obj, top.residue(0)]), 1.0),
(frozenset([obj, top.residue(3)]), 1.0),
(frozenset([obj, top.residue(4)]), 0.2)]
else:
raise RuntimeError("This shouldn't happen")

most_common = contacts.most_common(obj)
cleaned = [(frozenset(ll[0]), ll[1]) for ll in most_common]

# check order
for i in range(len(most_common) - 1):
assert most_common[i][1] >= most_common[i+1][1]

# check contents
check_most_common_order(most_common)
assert set(cleaned) == set(expected)

@pytest.mark.parametrize("obj_type", ['atom', 'res'])
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
#from distutils.sysconfig import get_config_var
# from distutils.core import setup, Extension
from setuptools import setup, Extension
from setuptools import setup
# import numpy
# import glob
import os
Expand Down

0 comments on commit f9dac5b

Please sign in to comment.