Commits

Adrien Saladin  committed 1851878 Merge

Merge branch 'dev_atomsel' into dev_cython

  • Participants
  • Parent commits 928468f, e7188ec

Comments (0)

Files changed (6)

File Tests/unittest1.py

         self.assertEqual(atom.atomCharge, -1.23456)
         
 
-
+class TestAtomSelection(unittest.TestCase):
+    def setUp(self):
+        self.rig = Rigidbody("1F88.pdb")
+        
+    def testSelectAll(self):
+        allAtoms = self.rig.SelectAllAtoms()
+        self.assertEqual(len(allAtoms), 5067)
+    
+    def testSelectCA(self):
+        CAatoms = self.rig.CA()
+        self.assertEqual(len(CAatoms),643)
+        
+    def testSelectAtomType(self):
+        CAatoms = self.rig.SelectAtomType("CA")
+        self.assertEqual(len(CAatoms), 643)
+        
+    def testSelectBackbone(self):
+        bbAtoms = self.rig.Backbone()
+        self.assertEqual(len(bbAtoms), 2572)
+        
+    def testSelectResRange(self):
+        res_1_35 = self.rig.SelectResRange(1,35)
+        self.assertEqual(len(res_1_35), 566)  # two chains
+    
+    def testAnd(self):
+        res_1_35 = self.rig.SelectResRange(1,35) 
+        CAatoms = self.rig.SelectAtomType("CA")
+        
+        ca_1_35 = res_1_35 & CAatoms
+        self.assertEqual(len(ca_1_35), 70)  #2*35: two chains, A and B
+        
+    def testSelectResidType(self):
+        met1 = self.rig.SelectResidType("MET") & self.rig.SelectResRange(1,5) 
+        self.assertEqual(len(met1), 16)
+        met1A = self.rig.SelectResidType("MET") & self.rig.SelectResRange(1,5) & self.rig.SelectChainId("A")
+        self.assertEqual(len(met1A), 8)
+        
+    def testSelectChainId(self):
+        chainA = self.rig.SelectChainId("A")
+        self.assertEqual(len(chainA), 2638)
+        
 
 class TestRigidbody(unittest.TestCase):
     def setUp(self):
 include "matrix.pyx"
 include "atom.pyx"
 include "rigidbody.pyx"
+include "pdbio.pyx"
 include "attractrigidbody.pyx"
+include "atomselection.pyx"
 include "attractforcefield.pyx"
 include "lbfgs.pyx"
 include "rmsd.pyx"

File atomselection.h

     friend AtomSelection operator& (const AtomSelection& atsel1,const  AtomSelection& atsel2);
     friend AtomSelection operator| (const AtomSelection& atsel1,const AtomSelection& atsel2);
     friend AtomSelection operator! (const AtomSelection& seltoinverse);
+    friend AtomSelection op_not (const AtomSelection& seltoinverse);
 };
 
 
 AtomSelection operator| (const AtomSelection& atsel1,const AtomSelection& atsel2);
 AtomSelection operator! (const AtomSelection& seltoinverse);
 
+inline AtomSelection op_not (const AtomSelection& seltoinverse) {return !seltoinverse;};
+
 }
 
 #endif // ATOMSELECTION_H

File atomselection.pyx

+from cython.operator cimport dereference as deref
+from libcpp.string cimport string
+
+
+cdef extern from "atomselection.h" namespace "PTools":
+    cdef cppclass CppAtomSelection "PTools::AtomSelection":
+        CppAtomSelection()
+        CppAtomSelection(CppAtomSelection&)
+        CppAtomSelection(CppRigidbody)
+
+        unsigned int Size()
+        void SetRigid(CppRigidbody&)
+        CppAtom operator[]
+        CppAtom CopyAtom(unsigned int)
+        void AddAtomIndex(unsigned int)
+        CppRigidbody CreateRigid()
+
+        CppAtomSelection non(CppAtomSelection &)
+
+    cdef CppAtomSelection operator& (CppAtomSelection&, CppAtomSelection&)
+    cdef CppAtomSelection operator| (CppAtomSelection&, CppAtomSelection&)
+#    cdef CppAtomSelection operator! (CppAtomSelection&) 
+    cdef CppAtomSelection op_not (CppAtomSelection&)
+
+cdef class AtomSelection:
+
+    cdef CppAtomSelection * thisptr
+
+    def __cinit__(self, arg=None):
+        cdef AtomSelection atsel
+        cdef CppAtomSelection* atselptr
+        cdef Rigidbody rig
+        cdef CppRigidbody* rigptr
+
+        self.thisptr = <CppAtomSelection*> 0
+        if arg is None:
+            self.thisptr = new CppAtomSelection()
+            return
+
+        if isinstance(AtomSelection, arg):
+            atsel  = <AtomSelection> arg
+            atselptr  = <CppAtomSelection*> atsel.thisptr
+            self.thisptr = new CppAtomSelection(deref(atselptr))
+            return
+
+        if isinstance(Rigidbody, arg):
+            rig = <Rigidbody> arg
+            rigptr = rig.thisptr
+            self.thisptr = new CppAtomSelection(deref(rigptr))
+
+        raise RuntimeError("cannot reach here")
+
+    def __dealloc__(self):
+        if self.thisptr:
+            del self.thisptr
+
+    def __len__(self):
+        return self.thisptr.Size()
+    
+    def __and__(AtomSelection self, AtomSelection second):
+        ret = AtomSelection()
+        del ret.thisptr
+        cdef CppAtomSelection new_sel =   deref(self.thisptr) & deref(second.thisptr)    
+        ret.thisptr  = new CppAtomSelection(new_sel)
+        return ret
+
+    def __or__(AtomSelection self, AtomSelection second):
+        ret = AtomSelection()
+        del ret.thisptr
+        cdef CppAtomSelection new_sel =   deref(self.thisptr) | deref(second.thisptr)    
+        ret.thisptr  = new CppAtomSelection(new_sel)
+        return ret
+
+
+    def __not__(AtomSelection self):
+        ret = AtomSelection()
+        del ret.thisptr
+        cdef CppAtomSelection new_sel =   op_not(deref(self.thisptr))
+        ret.thisptr  = new CppAtomSelection(new_sel)
+        return ret
+from cython.operator cimport dereference as deref
+from libcpp.string cimport string
+
+
+cdef extern from "pdbio.h" namespace "PTools":
+    cdef void CppWritePDB "PTools::WritePDB" (CppRigidbody&, string)
+
+
+def WritePDB(Rigidbody rig, bytes filename):
+    cdef char* c_filename = <char *> filename
+    cdef string* cpp_filename = new string(c_filename)
+    cdef CppRigidbody* rigptr = rig.thisptr
+    CppWritePDB(deref(rigptr),  deref(cpp_filename) )
+    del cpp_filename

File rigidbody.pyx

         
         CppAtomproperty & GetAtomProperty(unsigned int)
         void SetAtomProperty(unsigned int, CppAtomproperty& )
+
+
+        #AtomSelection:
+        CppAtomSelection SelectAllAtoms()
+        CppAtomSelection SelectAtomType(string)
+        CppAtomSelection SelectResidType(string)
+        CppAtomSelection SelectChainId(string)
+        CppAtomSelection SelectResRange(unsigned int, unsigned int)
+        CppAtomSelection CA()
+        CppAtomSelection Backbone()
         
 
 cdef extern from "pdbio.h" namespace "PTools":
     
     
     #    void SetAtomProperty(unsigned int, Atomproperty& )
+
+
+    #AtomSelection:
+    def SelectAllAtoms(self):
+       ret = AtomSelection()
+       del ret.thisptr
+       cdef CppAtomSelection new_sel =  self.thisptr.SelectAllAtoms()
+       ret.thisptr  = new CppAtomSelection(new_sel)
+       return ret
+
+
+    def SelectAtomType(self, bytes b):
+       ret = AtomSelection()
+       del ret.thisptr
+       cdef char* c_typename = b
+       cdef string* cpp_atomtype = new string(c_typename)
+       cdef CppAtomSelection new_sel =  self.thisptr.SelectAtomType(deref(cpp_atomtype))
+       del cpp_atomtype
+       ret.thisptr  = new CppAtomSelection(new_sel)
+       return ret
+       
+
+
+    def SelectResidType(self, bytes b):
+       ret = AtomSelection()
+       del ret.thisptr
+       cdef char* c_typename = b
+       cdef string* cpp_residtype = new string(c_typename)
+       cdef CppAtomSelection new_sel =  self.thisptr.SelectResidType(deref(cpp_residtype))
+       del cpp_residtype
+       ret.thisptr  = new CppAtomSelection(new_sel)
+       return ret
+
+
+    def SelectChainId(self, bytes b):
+       ret = AtomSelection()
+       del ret.thisptr
+       cdef char* c_typename = b
+       cdef string* cpp_chainid = new string(c_typename)
+       cdef CppAtomSelection new_sel =  self.thisptr.SelectChainId(deref(cpp_chainid))
+       del cpp_chainid
+       ret.thisptr  = new CppAtomSelection(new_sel)
+       return ret
+
+
+    def SelectResRange(self, unsigned int i, unsigned int j):
+       ret = AtomSelection()
+       del ret.thisptr
+       cdef CppAtomSelection new_sel =  self.thisptr.SelectResRange(i,j)
+       ret.thisptr  = new CppAtomSelection(new_sel)
+       return ret
+
+
+    def CA(self):
+       ret = AtomSelection()
+       del ret.thisptr
+       cdef CppAtomSelection new_sel =  self.thisptr.CA()
+       ret.thisptr  = new CppAtomSelection(new_sel)
+       return ret
+
+
+    def Backbone(self):
+       ret = AtomSelection()
+       del ret.thisptr
+       cdef CppAtomSelection new_sel =  self.thisptr.Backbone()
+       ret.thisptr  = new CppAtomSelection(new_sel)
+       return ret
+
     
 
         
 
       
 
-def c_to_python_string():
+cdef c_to_python_string():
     cdef char * test = "hello world"
     cdef bytes b = test
     return b