From 6a70d5bc6b59b901c3812bca2ba682a09a2b59a4 Mon Sep 17 00:00:00 2001 From: Stefan Bienert <stefan.bienert@unibas.ch> Date: Mon, 8 Aug 2011 17:51:21 +0200 Subject: [PATCH] Added functions ChainTypeFromString, ChainTypeFromString, StringFromChainType for handling ChainType, with tests and documentation. --- modules/mol/base/doc/editors.rst | 2 +- modules/mol/base/doc/entity.rst | 67 +++++++++++++++--- modules/mol/base/doc/mol.rst | 2 +- modules/mol/base/pymod/export_chain.cc | 7 +- modules/mol/base/src/CMakeLists.txt | 1 + modules/mol/base/src/chain_type.cc | 98 ++++++++++++++++++++++++++ modules/mol/base/src/chain_type.hh | 30 ++++++++ modules/mol/base/tests/test_chain.cc | 45 ++++++++++++ 8 files changed, 239 insertions(+), 13 deletions(-) create mode 100644 modules/mol/base/src/chain_type.cc diff --git a/modules/mol/base/doc/editors.rst b/modules/mol/base/doc/editors.rst index d73972f55..ad4d7fb07 100644 --- a/modules/mol/base/doc/editors.rst +++ b/modules/mol/base/doc/editors.rst @@ -94,7 +94,7 @@ The basic functionality of editors is implemented in the EditorBase class. :param chain: Must be a valid chain :param type: Must be a value of enum ChainType - (see :attr:`ChainHandle.chain_type`) + (see :attr:`ChainHandle.type`) .. method:: SetChainDescription(chain, description) diff --git a/modules/mol/base/doc/entity.rst b/modules/mol/base/doc/entity.rst index a9f6db99e..85b6abdc9 100644 --- a/modules/mol/base/doc/entity.rst +++ b/modules/mol/base/doc/entity.rst @@ -287,16 +287,7 @@ The Handle Classes .. attribute:: type - Describes the type of the chain. Is one of enum ChainType: - - ``CHAINTYPE_POLY``, ``CHAINTYPE_NON_POLY``, ``CHAINTYPE_WATER``, - ``CHAINTYPE_POLY_PEPTIDE_D``, ``CHAINTYPE_POLY_PEPTIDE_L``, - ``CHAINTYPE_POLY_DN``, ``CHAINTYPE_POLY_RN``, ``CHAINTYPE_POLY_SAC_D``, - ``CHAINTYPE_POLY_SAC_L``, ``CHAINTYPE_POLY_DN_RN``, - ``CHAINTYPE_UNKNOWN``, ``CHAINTYPE_N_CHAINTYPES`` - - Where ``CHAINTYPE_N_CHAINTYPES`` holds the number of different types - available. + Describes the type of the chain. See :ref:`chaintype`. .. attribute:: description @@ -1546,3 +1537,59 @@ Other Entity-Related Functions encountered :returns: :class:`EntityView` + +.. _chaintype: + +ChainType +-------------------------------------------------------------------------------- + +A ChainType fills the :attr:`ChainHandle.type` attribute. Different types are +described in the :ref:`ChainType enum <chaintype_enum>`. Functions for setting +a type belong to the :class:`EditorBase` class, getting is provided by the +:class:`ChainHandle` class, further convenience functions are described here. + +.. _chaintype_enum: + +The ChainType enum +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The ChainType enum enumerates all types defined by the PDB for the MMCif file +format. Following values are supported: + + ``CHAINTYPE_POLY``, ``CHAINTYPE_NON_POLY``, ``CHAINTYPE_WATER``, + ``CHAINTYPE_POLY_PEPTIDE_D``, ``CHAINTYPE_POLY_PEPTIDE_L``, + ``CHAINTYPE_POLY_DN``, ``CHAINTYPE_POLY_RN``, ``CHAINTYPE_POLY_SAC_D``, + ``CHAINTYPE_POLY_SAC_L``, ``CHAINTYPE_POLY_DN_RN``, + ``CHAINTYPE_UNKNOWN``, ``CHAINTYPE_N_CHAINTYPES`` + +Where ``CHAINTYPE_N_CHAINTYPES`` holds the number of different types available. + +Setter & Getter functions +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +:func:`EditorBase.SetChainType`, :func:`ChainHandle.GetType` + + +ChainType functions +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. function:: ChainTypeFromString(identifier) + + Create a ChainType item for a given string. + + :param identifier: String to request a type for. + :type identifier: :class:`str` + :raises: :class:`runtime_error` if **identifier** is unrecognised. + + :returns: :class:`ChainType` + +.. function:: StringFromChainType(type) + + Return the String identifier for a given **type**. + + :param type: To be translated + :type type: :class:`ChainType` + :raises: :class:`runtime_error` if **type** is unrecognised. + + :returns: :class:`str` + diff --git a/modules/mol/base/doc/mol.rst b/modules/mol/base/doc/mol.rst index 976c2c8aa..a41691149 100644 --- a/modules/mol/base/doc/mol.rst +++ b/modules/mol/base/doc/mol.rst @@ -13,4 +13,4 @@ The mol module implements data structures to work with molecular datasets. At it editors query surface - traj \ No newline at end of file + traj diff --git a/modules/mol/base/pymod/export_chain.cc b/modules/mol/base/pymod/export_chain.cc index 5f8dcaa1c..56bdf94ad 100644 --- a/modules/mol/base/pymod/export_chain.cc +++ b/modules/mol/base/pymod/export_chain.cc @@ -39,7 +39,9 @@ namespace { typedef EntityView (ChainHandle::*QueryMethod)(const Query&, uint) const; typedef EntityView (ChainHandle::*StringMethod)(const String&, uint) const; QueryMethod select_query=&ChainHandle::Select; - StringMethod select_string=&ChainHandle::Select; + StringMethod select_string=&ChainHandle::Select; + ChainType (*ChainTypeFromStringPtr)(const String& identifier) = + &ChainTypeFromString; } void export_Chain() @@ -126,4 +128,7 @@ void export_Chain() .export_values() ; } + + def("ChainTypeFromString", ChainTypeFromStringPtr); + def("StringFromChainType", &StringFromChainType); } diff --git a/modules/mol/base/src/CMakeLists.txt b/modules/mol/base/src/CMakeLists.txt index 22bda6d35..ac669907f 100644 --- a/modules/mol/base/src/CMakeLists.txt +++ b/modules/mol/base/src/CMakeLists.txt @@ -8,6 +8,7 @@ bond_handle.cc chain_base.cc chain_handle.cc chain_view.cc +chain_type.cc coord_frame.cc coord_group.cc editor_base.cc diff --git a/modules/mol/base/src/chain_type.cc b/modules/mol/base/src/chain_type.cc new file mode 100644 index 000000000..6b3841d89 --- /dev/null +++ b/modules/mol/base/src/chain_type.cc @@ -0,0 +1,98 @@ +//------------------------------------------------------------------------------ +// This file is part of the OpenStructure project <www.openstructure.org> +// +// Copyright (C) 2008-2011 by the OpenStructure authors +// +// This library is free software; you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License as published by the Free +// Software Foundation; either version 3.0 of the License, or (at your option) +// any later version. +// This library 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 Lesser General Public License for more +// details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this library; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +//------------------------------------------------------------------------------ + +#include <stdexcept> +#include "chain_type.hh" + +namespace ost { namespace mol { + +ChainType ChainTypeFromString(StringRef identifier) +{ + + // chain types as found in the entity category of a mmcif file + if(StringRef("polymer", 7) == identifier) { + return CHAINTYPE_POLY; + }else if(StringRef("non-polymer", 11) == identifier) { + return CHAINTYPE_NON_POLY; + }else if(StringRef("water", 5) == identifier) { + return CHAINTYPE_WATER; + // chain types as found in the entity_poly category of a mmcif file + } else if(StringRef("polypeptide(D)", 14) == identifier) { + return CHAINTYPE_POLY_PEPTIDE_D; + } else if(StringRef("polypeptide(L)", 14) == identifier) { + return CHAINTYPE_POLY_PEPTIDE_L; + } else if(StringRef("polydeoxyribonucleotide", 23) == identifier) { + return CHAINTYPE_POLY_DN; + } else if(StringRef("polyribonucleotide", 18) == identifier) { + return CHAINTYPE_POLY_RN; + } else if(StringRef("polysaccharide(D)", 17) == identifier) { + return CHAINTYPE_POLY_SAC_D; + } else if(StringRef("polysaccharide(L)", 17) == identifier) { + return CHAINTYPE_POLY_SAC_L; + } else if(StringRef("polydeoxyribonucleotide/polyribonucleotide hybrid", + 49) == identifier) { + return CHAINTYPE_POLY_DN_RN; + } else if(StringRef("other", 5) == identifier) { + return CHAINTYPE_UNKNOWN; + } + + throw std::runtime_error("Unrecognised chain type descriptor found: '" + + identifier.str() +"'!"); +} + +ChainType ChainTypeFromString(const String& identifier) +{ + return ChainTypeFromString(StringRef(identifier.c_str(), + identifier.length())); +} + +String StringFromChainType(ChainType type) +{ + // chain types as found in the entity category of a mmcif file + if (CHAINTYPE_POLY == type) { + return "polymer"; + } else if (CHAINTYPE_NON_POLY == type) { + return "non-polymer"; + } else if (CHAINTYPE_WATER == type) { + return "water"; + // chain types as found in the entity_poly category of a mmcif file + } else if (CHAINTYPE_POLY_PEPTIDE_D == type) { + return "polypeptide(D)"; + } else if (CHAINTYPE_POLY_PEPTIDE_L == type) { + return "polypeptide(L)"; + } else if (CHAINTYPE_POLY_DN == type) { + return "polydeoxyribonucleotide"; + } else if (CHAINTYPE_POLY_RN == type) { + return "polyribonucleotide"; + } else if (CHAINTYPE_POLY_SAC_D == type) { + return "polysaccharide(D)"; + } else if (CHAINTYPE_POLY_SAC_L == type) { + return "polysaccharide(L)"; + } else if (CHAINTYPE_POLY_DN_RN == type) { + return "polydeoxyribonucleotide/polyribonucleotide hybrid"; + } else if (CHAINTYPE_UNKNOWN == type) { + return "other"; + } + + std::stringstream ss("Unknonw ChainType item found: '"); + ss << type << "'!"; + throw std::runtime_error(ss.str()); +} + +}} //ns diff --git a/modules/mol/base/src/chain_type.hh b/modules/mol/base/src/chain_type.hh index 0311ebffb..18a00fb45 100644 --- a/modules/mol/base/src/chain_type.hh +++ b/modules/mol/base/src/chain_type.hh @@ -19,6 +19,11 @@ #ifndef OST_CHAIN_TYPE_HH #define OST_CHAIN_TYPE_HH +#include <ost/base.hh> +#include <ost/string_ref.hh> + +namespace ost { namespace mol { + /// \enum different kinds of chains typedef enum { CHAINTYPE_POLY, ///< polymer @@ -35,5 +40,30 @@ typedef enum { CHAINTYPE_N_CHAINTYPES ///< no. of chain types } ChainType; +/// \brief Create a ChainType item for a given string +/// +/// \param identifier StringRef to be translated +/// +/// \return The ChainType corresponding to the input, throws a +/// std::runtime_error on unknown type +ChainType ChainTypeFromString(const StringRef identifier); + +/// \brief Create a ChainType item for a given string +/// +/// \param identifier String to be translated +/// +/// \return The ChainType corresponding to the input, throws a +/// std::runtime_error on unknown type +ChainType ChainTypeFromString(const String& identifier); + +/// \brief Return the String identifier for a given type +/// +/// \param type ChainType to be translated +/// +/// \return String corresponding to the input, throws a std::runtime_error on +/// unknown type +String StringFromChainType(ChainType type); + +}} //ns #endif diff --git a/modules/mol/base/tests/test_chain.cc b/modules/mol/base/tests/test_chain.cc index abca07aed..b0ffc3087 100644 --- a/modules/mol/base/tests/test_chain.cc +++ b/modules/mol/base/tests/test_chain.cc @@ -208,6 +208,7 @@ BOOST_AUTO_TEST_CASE(chain_type) XCSEditor e = eh.EditXCS(); ChainHandle ch1 = e.InsertChain("A"); + // setting/ getting BOOST_CHECK(ch1.GetType() == CHAINTYPE_UNKNOWN); e.SetChainType(ch1, CHAINTYPE_POLY); BOOST_CHECK(ch1.GetType() == CHAINTYPE_POLY); @@ -233,6 +234,50 @@ BOOST_AUTO_TEST_CASE(chain_type) BOOST_CHECK(ch1.GetType() == CHAINTYPE_N_CHAINTYPES); e.SetChainType(ch1, CHAINTYPE_UNKNOWN); BOOST_CHECK(ch1.GetType() == CHAINTYPE_UNKNOWN); + + // string -> chain type + BOOST_CHECK(ChainTypeFromString("polymer") == CHAINTYPE_POLY); + BOOST_CHECK(ChainTypeFromString("non-polymer") == CHAINTYPE_NON_POLY); + BOOST_CHECK(ChainTypeFromString("water") == CHAINTYPE_WATER); + BOOST_CHECK(ChainTypeFromString("polypeptide(D)") == + CHAINTYPE_POLY_PEPTIDE_D); + BOOST_CHECK(ChainTypeFromString("polypeptide(L)") == + CHAINTYPE_POLY_PEPTIDE_L); + BOOST_CHECK(ChainTypeFromString("polydeoxyribonucleotide") == + CHAINTYPE_POLY_DN); + BOOST_CHECK(ChainTypeFromString("polyribonucleotide") == + CHAINTYPE_POLY_RN); + BOOST_CHECK(ChainTypeFromString("polysaccharide(D)") == + CHAINTYPE_POLY_SAC_D); + BOOST_CHECK(ChainTypeFromString("polysaccharide(L)") == + CHAINTYPE_POLY_SAC_L); + BOOST_CHECK(ChainTypeFromString( + "polydeoxyribonucleotide/polyribonucleotide hybrid") == + CHAINTYPE_POLY_DN_RN); + BOOST_CHECK(ChainTypeFromString("other") == CHAINTYPE_UNKNOWN); + BOOST_CHECK_THROW(ChainTypeFromString("supposed to fail"), + std::runtime_error); + + // chain type -> string + BOOST_CHECK(StringFromChainType(CHAINTYPE_POLY) == "polymer"); + BOOST_CHECK(StringFromChainType(CHAINTYPE_NON_POLY) == "non-polymer"); + BOOST_CHECK(StringFromChainType(CHAINTYPE_WATER) == "water"); + BOOST_CHECK(StringFromChainType(CHAINTYPE_POLY_PEPTIDE_D) == + "polypeptide(D)"); + BOOST_CHECK(StringFromChainType(CHAINTYPE_POLY_PEPTIDE_L) == + "polypeptide(L)"); + BOOST_CHECK(StringFromChainType(CHAINTYPE_POLY_DN) == + "polydeoxyribonucleotide"); + BOOST_CHECK(StringFromChainType(CHAINTYPE_POLY_RN) == "polyribonucleotide"); + BOOST_CHECK(StringFromChainType(CHAINTYPE_POLY_SAC_D) == + "polysaccharide(D)"); + BOOST_CHECK(StringFromChainType(CHAINTYPE_POLY_SAC_L) == + "polysaccharide(L)"); + BOOST_CHECK(StringFromChainType(CHAINTYPE_POLY_DN_RN) == + "polydeoxyribonucleotide/polyribonucleotide hybrid"); + BOOST_CHECK(StringFromChainType(CHAINTYPE_UNKNOWN) == "other"); + BOOST_CHECK_THROW(StringFromChainType(CHAINTYPE_N_CHAINTYPES), + std::runtime_error); } BOOST_AUTO_TEST_CASE(chain_description) -- GitLab