The reason the tag-value pair was not correctly printed in the SD file  
seems to be the lack of a GetValue function in OBPairTemplate.  And it  
is exactly GetValue() that is used in WriteMolecule() to print the tag- 
value block.

Therefore, I have added an GetValue() function that returns a string  
and adjusted the existing code such that when SetValue() is called, it  
immediately creates a string representation of the value and stores  
this in _str_value. I paste code below.

I am not sure this is the most robust fix, since this will probably  
only work for types where the << operator has been defined.


   //! \class OBPairTemplate generic.h <openbabel/generic.h>
   //! \brief Used to store arbitrary attribute/value relationsips of  
any type.
   // More detailed description in generic.cpp
   template <class ValueT>
     class OBAPI OBPairTemplate : public OBGenericData
   {
   protected:
     ValueT _value; //!< The data for this key/value pair
                std::string _str_value;
   public:
   OBPairTemplate():
     OBGenericData("PairData", OBGenericDataType::PairData) {};
                void SetValue(const ValueT t)             {
                        _value = t;
                        // create a string representation of t
                        std::ostringstream ss;
                        ss.str("");
                        ss << _value;
                        _str_value = ss.str();
                };
                virtual const ValueT &GetGenericValue() const    { 
return(_value); };
                virtual const std::string &GetValue() const       
{ return(_str_value); };
   };




On Dec 16, 2010, at 11:04 AM, Gert Thijs wrote:

> Is there any particular reason why OBPairTemplate<T> implements only
> GetGenericValue() and not GetValue()?
>
> Gert
>
>
>
> On Dec 16, 2010, at 10:12 AM, Gert Thijs wrote:
>
>> Tim,
>>
>> Thanks for the webpage reference. I somehow missed that one on my
>> search through the openbabel site. I adjusted the code to include the
>> dynamic_cast when retrieving the stored value with GetData("Energy").
>> Doing this dynamic cast prints the right value to the std::cerr and
>> confirms that the right value is stored with the molecule.
>>
>> However, my main problem is in writing the molecule into the SD file.
>> Below are the last lines of the output of this small test program and
>> as you can see the value printed with <Energy> is "Energy" and not  
>> the
>> expected double value.
>>
>> macbook1:conformers gert$ tail ~/test2.sdf
>> 18 31  1  0  0  0  0
>> M  CHG  1  18   1
>> M  END
>>> <CD_ID>
>> 100
>>
>>> <Energy>
>> Energy
>>
>> $$$$
>>
>>
>>
>>
>> On Dec 15, 2010, at 4:24 PM, Tim Vandermeersch wrote:
>>
>>> Hi,
>>>
>>> On Wed, Dec 15, 2010 at 3:58 AM, Gert Thijs <gert.th...@silicos.com>
>>> wrote:
>>>> Hello,
>>>>
>>>> I am encountering some unexpected results with OBPairTemplate. I am
>>>> trying to add a double value to a molecule and write in an SD file.
>>>> To
>>>> do this, I am using the OBPairTemplate class instead of the
>>>> OBGenericData, such that I do not have to go through the process of
>>>> converting a double into a string.
>>>>
>>>> Here is my code snippet :
>>>> ===========================
>>>> double e = pFF->Energy();
>>>>
>>>> OpenBabel::OBPairFloatingPoint *label = new
>>>> OpenBabel::OBPairFloatingPoint;
>>>> label->SetAttribute("Energy");
>>>> label->SetValue(e);
>>>> label->SetOrigin(OpenBabel::userInput); // set by user, not by Open
>>>> Babel
>>>> mol.SetData(label);
>>>> conv.Write(&mol);
>>>>
>>>> OpenBabel::OBGenericData * gd = mol.GetData("Energy");
>>>> if ( gd ){
>>>> std::cerr << "Energy : " << gd->GetValue() << std::endl;
>>>> }
>>>> ===========================
>>>>
>>>>
>>>> If I run this code, I find in the SD file the following tag:
>>>>> <Energy>
>>>> Energy
>>>>
>>>> and the std::cerr reads :
>>>> Energy : Energy
>>>>
>>>> So instead of printing the expected double value, actually the
>>>> attribute is  printed.
>>>>
>>>> Any suggestions of what I am missing here.
>>>
>>> Use the GetGenericData function, this will return the templated  
>>> type.
>>> The GetValue will convert the generic value to a string. See
>>> http://openbabel.org/api/2.3/generic_data.shtml (last example) for a
>>> full example.
>>>
>>> Tim
>>>
>>>> thanks,
>>>> Gert
>>>>
>>>>
>>>>
>>>>
>>>> ------------------------------------------------------------------------------
>>>> Lotusphere 2011
>>>> Register now for Lotusphere 2011 and learn how
>>>> to connect the dots, take your collaborative environment
>>>> to the next level, and enter the era of Social Business.
>>>> http://p.sf.net/sfu/lotusphere-d2d
>>>> _______________________________________________
>>>> OpenBabel-discuss mailing list
>>>> openbabel-disc...@lists.sourceforge.net
>>>> https://lists.sourceforge.net/lists/listinfo/openbabel-discuss
>>>>
>>
>> Gert Thijs
>>
>> Director Chemoinformatics
>> Silicos NV.
>> Wetenschapspark 7
>> B-3590 Diepenbeek
>> Belgium
>>
>> Tel:   +32 11 350703
>> Fax:  +32 11 220525
>>
>> http://www.silicos.com/
>>
>>
>>
>>
>> ------------------------------------------------------------------------------
>> Lotusphere 2011
>> Register now for Lotusphere 2011 and learn how
>> to connect the dots, take your collaborative environment
>> to the next level, and enter the era of Social Business.
>> http://p.sf.net/sfu/lotusphere-d2d
>> _______________________________________________
>> OpenBabel-discuss mailing list
>> openbabel-disc...@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/openbabel-discuss
>
> Gert Thijs
>
> Director Chemoinformatics
> Silicos NV.
> Wetenschapspark 7
> B-3590 Diepenbeek
> Belgium
>
> Tel:   +32 11 350703
> Fax:  +32 11 220525
>
> http://www.silicos.com/
>
>
>
>
> ------------------------------------------------------------------------------
> Lotusphere 2011
> Register now for Lotusphere 2011 and learn how
> to connect the dots, take your collaborative environment
> to the next level, and enter the era of Social Business.
> http://p.sf.net/sfu/lotusphere-d2d
> _______________________________________________
> OpenBabel-discuss mailing list
> openbabel-disc...@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/openbabel-discuss

Gert Thijs

Director Chemoinformatics
Silicos NV.
Wetenschapspark 7
B-3590 Diepenbeek
Belgium

Tel:   +32 11 350703
Fax:  +32 11 220525

http://www.silicos.com/




------------------------------------------------------------------------------
Lotusphere 2011
Register now for Lotusphere 2011 and learn how
to connect the dots, take your collaborative environment
to the next level, and enter the era of Social Business.
http://p.sf.net/sfu/lotusphere-d2d
_______________________________________________
OpenBabel-Devel mailing list
OpenBabel-Devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbabel-devel

Reply via email to