Source code for BioSimSpace._SireWrappers._search_result
####################################################################### BioSimSpace: Making biomolecular simulation a breeze!## Copyright: 2017-2024## Authors: Lester Hedges <lester.hedges@gmail.com>## BioSimSpace 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 3 of the License, or# (at your option) any later version.## BioSimSpace is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with BioSimSpace. If not, see <http://www.gnu.org/licenses/>.#####################################################################"""A thin wrapper around Sire.Mol.SelectResult. This is an internal package and shouldnot be directly exposed to the user."""__author__="Lester Hedges"__email__="lester.hedges@gmail.com"__all__=["SearchResult"]importsire.legacyas_Sire
[docs]classSearchResult:"""A thin wrapper around Sire.Mol.SelectResult."""
[docs]def__init__(self,select_result):""" Constructor. Parameters ---------- select_result : Output of a Sire search The Sire result object. """# Note that the type of a sire search will now be# whatever makes most sense for the result, e.g.# SelectorMol if this is a list of molecules,# SelectorBond if this is a list of bonds etc.# Store the Sire select result.self._sire_object=select_result.__deepcopy__()# Store the number of results.self._num_results=len(self._sire_object)# Initialise the iterator count.self._iter_count=0
def__str__(self):"""Return a human readable string representation of the object."""return"<BioSimSpace.SearchResult: nResults=%d>"%self.nResults()def__repr__(self):"""Return a string showing how to instantiate the object."""return"<BioSimSpace.SearchResult: nResults=%d>"%self.nResults()def__len__(self):""" Return the number of results. Returns ------- num_results : int The number of search results. """returnself._num_resultsdef__eq__(self,other):"""Equals to operator."""# Compare to another object of the same type.iftype(other)istype(self):returnself._sire_object==other._sire_objectelse:returnFalsedef__ne__(self,other):"""Not equals to operator."""# Compare to another object of the same type.iftype(other)istype(self):returnself._sire_object!=other._sire_objectelse:returnFalsedef__hash__(self):"""Hash operator."""returnhash(self._sire_object)def__getitem__(self,key):"""Get a search result from the container."""# Slice.iftype(key)isslice:# Create a list to hold the results.results=[]# Iterate over the slice.forxinrange(*key.indices(self._num_results)):results.append(self[x])# Return the results.returnresults# Index.else:try:key=int(key)except:raiseTypeError("'key' must be of type 'int'")ifkey<-self._num_resultsorkey>self._num_results-1:raiseIndexError("SearchResult index is out of range.")ifkey<0:key=key+self._num_results# Extract the result from the Sire object.result=self._sire_object[key]# Atom.ifisinstance(result,_Sire.Mol._Mol.Atom):return_Atom(result)# Residue.elifisinstance(result,_Sire.Mol._Mol.Residue):return_Residue(result)# Molecule.elifisinstance(result,_Sire.Mol._Mol.Molecule):return_Molecule(result)# Bondelifisinstance(result,_Sire.MM._MM.Bond):return_Bond(result)# Unsupported.else:print(f"WARNING: Type {type(result)} is not supported.")returnNonedef__setitem__(self,key,value):"""Set a molecule in the container."""raiseTypeError("'SearchResult' object does not support assignment.")def__iter__(self):"""An iterator for the object."""# Reset the iterator counter and return the object.self._iter_count=0returnselfdef__next__(self):"""An iterator for the object."""# Stop if we've reached the end of the container.ifself._iter_count==self._num_results:raiseStopIteration# Extract the next result in the container.result=self[self._iter_count]# Update the iterator counter.self._iter_count+=1# Return the result.returnresult
[docs]defcopy(self):""" Create a copy of this object. Returns ------- search_result : :class:`SearchResult <BioSimSpace._SireWrappers.SearchResult>` A copy of the object. """returnSearchResult(self)
[docs]defatoms(self):""" Return all of the atoms that contain the results of this search. Returns ------- search_result : :class:`SearchResult <BioSimSpace._SireWrappers.SearchResult>` A copy of the object viewed via its atoms. """returnSearchResult(self._sire_object.atoms())
[docs]defresidues(self):""" Return all of the residues that contain the results of this search. Returns ------- search_result : :class:`SearchResult <BioSimSpace._SireWrappers.SearchResult>` A copy of the object viewed via its residues. """returnSearchResult(self._sire_object.residues())
[docs]defchains(self):""" Return all of the chains that contain the results of this search. Returns ------- search_result : :class:`SearchResult <BioSimSpace._SireWrappers.SearchResult>` A copy of the object viewed via its chains. """returnSearchResult(self._sire_object.chains())
[docs]defsegments(self):""" Return all of the segments that contain the results of this search. Returns ------- search_result : :class:`SearchResult <BioSimSpace._SireWrappers.SearchResult>` A copy of the object viewed via its segments. """returnSearchResult(self._sire_object.segments())
[docs]defmolecules(self):""" Return all of the molecules that contain the results of this search. Returns ------- search_result : :class:`SearchResult <BioSimSpace._SireWrappers.SearchResult>` A copy of the object viewed via its molecules. """try:returnSearchResult(self._sire_object.molecules())exceptException:# this is a single molecule viewfromsire.molimportSelectorMolreturnSearchResult(SelectorMol(self._sire_object.molecule()))
[docs]defbonds(self):""" Return all of the bonds in this result. Returns ------- search_result : :class:`SearchResult <BioSimSpace._SireWrappers.SearchResult>` A copy of the object viewed via its bonds. """try:fromsire.mmimportSelectorMBondreturnSearchResult(SelectorMBond(self._sire_object))exceptException:fromsire.mmimportSelectorBondreturnSearchResult(SelectorBond(self._sire_object))
[docs]defnResults(self):""" Return the number of results. Returns ------- num_results : int The number of search results. """returnself._num_results
[docs]defgetResult(self,index):""" Return the result at the given index. Parameters ---------- index : int The index of the result. Returns ------- molecule : :class:`Molecule <BioSimSpace._SireWrappers.Molecule>` The requested molecule. result : :class:`Atom <BioSimSpace._SireWrappers.Atom>`, \ :class:`Residue <BioSimSpace._SireWrappers.Residue>`, \ :class:`Molecule <BioSimSpace._SireWrappers.Molecule>`, \ :class:`Bond <BioSimSpace._SireWrappers.Bond>` """returnself[index]
# Import at bottom of module to avoid circular dependency.from._atomimportAtomas_Atomfrom._moleculeimportMoleculeas_Moleculefrom._residueimportResidueas_Residuefrom._bondimportBondas_Bond