Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

ConsensusXMLHandler.h (Maintainer: Eva Lange)

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // --------------------------------------------------------------------------
00005 //                   OpenMS Mass Spectrometry Framework
00006 // --------------------------------------------------------------------------
00007 //  Copyright (C) 2003-2008 -- Oliver Kohlbacher, Knut Reinert
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Lesser General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //  This library is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //  Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with this library; if not, write to the Free Software
00021 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // --------------------------------------------------------------------------
00024 // $Maintainer: Eva Lange$
00025 // --------------------------------------------------------------------------
00026 
00027 
00028 #ifndef OPENMS_FORMAT_HANDLERS_CONSENSUSXMLHANDLER_H
00029 #define OPENMS_FORMAT_HANDLERS_CONSENSUSXMLHANDLER_H
00030 
00031 #include <OpenMS/CONCEPT/Exception.h>
00032 #include <OpenMS/ANALYSIS/MAPMATCHING/StarAlignment.h>
00033 #include <OpenMS/FORMAT/FeatureXMLFile.h>
00034 #include <OpenMS/FORMAT/HANDLERS/XMLHandler.h>
00035 #include <OpenMS/DATASTRUCTURES/Param.h>
00036 #include <OpenMS/KERNEL/ConsensusMap.h>
00037 #include <OpenMS/KERNEL/ConsensusPeak.h>
00038 
00039 #include <fstream>
00040 
00041 // STL includes
00042 #include <iostream>
00043 
00044 
00045 namespace OpenMS
00046 {
00047   namespace Internal
00048   {
00053     template < typename AlignmentT >
00054     class ConsensusXMLHandler
00055       : public XMLHandler
00056     {
00057       public:
00058         typedef typename AlignmentT::ElementContainerType ElementContainerType;
00059         typedef typename AlignmentT::ElementType ElementType;
00060         typedef typename AlignmentT::ConsensusElementType ConsensusElementType;
00061 
00062       
00063         typedef DPosition<2> PositionType;
00064 
00066         ConsensusXMLHandler(ConsensusMap<ConsensusElementType>& consensus_map , const String& filename, const String& version, bool load_elements_maps = true)
00067             : XMLHandler(filename, version),
00068             consensus_map_(&consensus_map),
00069             act_cons_element_(),
00070             calignment_(0),
00071             consensus_element_range_(false),
00072             feature_map_flag_(true),
00073             load_elements_maps_(load_elements_maps)
00074         {
00075         }
00076 
00078         ConsensusXMLHandler(const StarAlignment< ConsensusElementType >& alignment, const String& filename, const String& version)
00079             : XMLHandler(filename, version),
00080             consensus_map_(0),
00081             act_cons_element_(),
00082             calignment_(&alignment),
00083             consensus_element_range_(false),
00084             feature_map_flag_(true)
00085         {
00086         }
00087 
00089         virtual ~ConsensusXMLHandler()
00090         {
00091         }
00092 
00093         // Docu in base class
00094         virtual void endElement(const XMLCh* const /*uri*/, const XMLCh* const /*local_name*/, const XMLCh* const qname);
00095 
00096         // Docu in base class
00097         virtual void startElement(const XMLCh* const /*uri*/, const XMLCh* const /*local_name*/, const XMLCh* const qname, const xercesc::Attributes& attributes);
00098 
00099         // Docu in base class
00100         virtual void characters(const XMLCh* const chars, unsigned int length);
00101 
00103         void writeTo(std::ostream& os);
00104 
00105 
00106 
00107       protected:
00108         ConsensusMap<ConsensusElementType>* consensus_map_;
00109         ConsensusElementType act_cons_element_;
00110         // StartAlignment pointer for writing
00111         const StarAlignment< ConsensusElementType >* calignment_;
00112         bool consensus_element_range_;
00113         bool feature_map_flag_;
00114         bool consensus_map_flag_;
00115         bool load_elements_maps_;
00116         typename ConsensusElementType::PositionType pos_;
00117         typename ConsensusElementType::IntensityType it_;
00118         typename ConsensusElementType::PositionBoundingBoxType pos_range_;
00119         typename ConsensusElementType::IntensityBoundingBoxType it_range_;
00120 
00122         template <typename ConsensusElementT >
00123         void loadFile_(const String& /* file_name */, UInt /* id */, const ConsensusElementT& /* c */) throw (Exception::FileNotFound, Exception::ParseError)
00124         {
00125           std::cout << "Read no file " << std::endl;
00126         }
00127 
00128         void writeCellList_(std::ostream& os, const Grid& grid)
00129         {
00130           // write features with their attributes
00131           for (UInt s=0; s < grid.size(); s++)
00132           {
00133             const GridCell& cell = grid[s];
00134 
00135             os << "\t\t<cell nr=\"" << s << "\">" << std::endl;
00136             os << "\t\t\t\t<range rtMin=\"" << cell.min()[0] << "\" rtMax=\"" << cell.max()[0]
00137             << "\" mzMin=\"" << cell.min()[1] << "\" mzMax=\"" << cell.max()[1] << "\"/>\n";
00138 
00139             os << "\t\t\t\t<mappinglist>\n";
00140             
00141             os << "\t\t\t\t\t<rtMapping name=\"LinearMapping\">\n";
00142             os << "\t\t\t\t\t\t<param name=\"slope\" value=\"" << cell.getMappings()[0].getSlope() << "\"/>\n";
00143             os << "\t\t\t\t\t\t<param name=\"intercept\" value=\"" << cell.getMappings()[0].getIntercept() << "\"/>\n";
00144             os << "\t\t\t\t\t</rtMapping>\n";
00145 
00146             os << "\t\t\t\t\t<mzMapping name=\"LinearMapping\">\n";
00147             os << "\t\t\t\t\t\t<param name=\"slope\" value=\"" << cell.getMappings()[1].getSlope() << "\"/>\n";
00148             os << "\t\t\t\t\t\t<param name=\"intercept\" value=\"" << cell.getMappings()[1].getIntercept() << "\"/>\n";
00149             os << "\t\t\t\t\t</mzMapping>\n";
00150 
00151             os << "\t\t\t\t</mappinglist>\n";
00152             os << "\t\t\t</cell>\n";
00153 
00154           } // end for ( features )
00155         }
00156     };
00157 
00158     //--------------------------------------------------------------------------------
00159 
00160     template < typename  AlignmentT >
00161     void ConsensusXMLHandler<AlignmentT>::endElement(const XMLCh* const /*uri*/, const XMLCh* const /*local_name*/, const XMLCh* const qname)
00162     {
00163       //std::cout << "END: " << sm_.convert(qname) << std::endl;
00164       static XMLCh* s_consensuselement = xercesc::XMLString::transcode("consensusElement");
00165       
00166       if (equal_(qname,s_consensuselement))
00167       {
00168         consensus_map_->push_back(act_cons_element_);
00169       }
00170     }
00171 
00172     template < typename AlignmentT >
00173     void ConsensusXMLHandler<AlignmentT>::characters(const XMLCh* const /*chars*/, unsigned int /*length*/)
00174     {
00175     }
00176 
00177     template < typename  AlignmentT >
00178     void ConsensusXMLHandler<AlignmentT>::startElement(const XMLCh* const /*uri*/, const XMLCh* const /*local_name*/, const XMLCh* const qname, const xercesc::Attributes& attributes)
00179     {
00180       //std::cout << "BEGIN: " << sm_.convert(qname) << std::endl;
00181       static XMLCh* s_consensuselement = xercesc::XMLString::transcode("consensusElement");
00182       static XMLCh* s_count = xercesc::XMLString::transcode("count");
00183       static XMLCh* s_name = xercesc::XMLString::transcode("name");
00184       static XMLCh* s_maplist = xercesc::XMLString::transcode("mapList");
00185       static XMLCh* s_maptype = xercesc::XMLString::transcode("mapType");
00186       static XMLCh* s_map = xercesc::XMLString::transcode("map");
00187       static XMLCh* s_element = xercesc::XMLString::transcode("element");
00188       static XMLCh* s_range = xercesc::XMLString::transcode("range");
00189       static XMLCh* s_centroid = xercesc::XMLString::transcode("centroid");
00190       static XMLCh* s_rt = xercesc::XMLString::transcode("rt");
00191       static XMLCh* s_mz = xercesc::XMLString::transcode("mz");
00192       static XMLCh* s_it = xercesc::XMLString::transcode("it");
00193       static XMLCh* s_id = xercesc::XMLString::transcode("id");
00194       static XMLCh* s_rtmin = xercesc::XMLString::transcode("rtMin");
00195       static XMLCh* s_rtmax = xercesc::XMLString::transcode("rtMax");
00196       static XMLCh* s_mzmin = xercesc::XMLString::transcode("mzMin");
00197       static XMLCh* s_mzmax = xercesc::XMLString::transcode("mzMax");
00198       static XMLCh* s_itmin = xercesc::XMLString::transcode("itMin");
00199       static XMLCh* s_itmax = xercesc::XMLString::transcode("itMax");
00200       static XMLCh* s_consensusxml = xercesc::XMLString::transcode("consensusXML");
00201         
00202       String tmp_str;
00203       if (equal_(qname,s_maplist))
00204       {
00205         tmp_str = attributeAsString_(attributes,s_count);
00206         if (tmp_str != "")
00207         {
00208           UInt count = asUInt_(tmp_str);
00209           consensus_map_->getMapVector().resize(count);
00210           consensus_map_->getFilenames().resize(count);
00211         }       
00212       }
00213       else if (equal_(qname,s_maptype))
00214       {
00215         tmp_str = attributeAsString_(attributes,s_name);
00216         if (tmp_str != "")
00217         {
00218           if (tmp_str == "feature_map")
00219           {
00220             feature_map_flag_ = true;
00221             consensus_map_flag_ = false;
00222           }
00223           else if (tmp_str == "consensus_map")
00224           {
00225             consensus_map_flag_ = true;
00226             feature_map_flag_ = false;
00227           }   
00228         }
00229       }
00230       else if (equal_(qname,s_map))
00231       {
00232         tmp_str = attributeAsString_(attributes,s_id);
00233         if (tmp_str != "")
00234         {
00235           UInt id = asUInt_(tmp_str);
00236           tmp_str = attributeAsString_(attributes,s_name);
00237 
00238           // load FeatureMapXML
00239           if (feature_map_flag_ && load_elements_maps_)
00240           {
00241             loadFile_(tmp_str,id,act_cons_element_);
00242           }
00243           // load MzData
00244           else
00245           {
00246             if (consensus_map_flag_ && load_elements_maps_)
00247             {
00248               loadFile_(tmp_str,id,act_cons_element_);
00249             }
00250             else if (load_elements_maps_)
00251             {
00252               loadFile_(tmp_str,id,act_cons_element_);
00253             }
00254           }
00255           consensus_map_->getFilenames()[id]=tmp_str;
00256         }
00257       }
00258       else if (equal_(qname,s_consensuselement))
00259       {
00260         act_cons_element_ = ConsensusElementType();
00261         consensus_element_range_ = true;
00262       }
00263       else if (equal_(qname,s_centroid))
00264       {
00265           tmp_str = attributeAsString_(attributes,s_rt);
00266           if (tmp_str != "")
00267           {
00268             pos_[Peak2D::RT] = asDouble_(tmp_str);
00269           }
00270 
00271           tmp_str = attributeAsString_(attributes,s_mz);
00272           if (tmp_str != "")
00273           {
00274             pos_[Peak2D::MZ] = asDouble_(tmp_str);
00275           }
00276 
00277           tmp_str = attributeAsString_(attributes,s_it);
00278           if (tmp_str != "")
00279           {
00280             it_ = asDouble_(tmp_str);
00281           }
00282 
00283       }
00284       else if (equal_(qname,s_range))
00285       {
00286         if (consensus_element_range_)
00287         {
00288           tmp_str = attributeAsString_(attributes, s_rtmin);
00289           if (tmp_str != "")
00290           {
00291             pos_range_.setMinX(asDouble_(tmp_str));
00292 
00293             tmp_str = attributeAsString_(attributes, s_rtmax);
00294             if (tmp_str != "")
00295             {
00296               pos_range_.setMaxX(asDouble_(tmp_str));
00297 
00298               tmp_str = attributeAsString_(attributes, s_mzmin);
00299               if (tmp_str != "")
00300               {
00301                 pos_range_.setMinY(asDouble_(tmp_str));
00302 
00303                 tmp_str = attributeAsString_(attributes, s_mzmax);
00304                 if (tmp_str != "")
00305                 {
00306                   pos_range_.setMaxY(asDouble_(tmp_str));
00307 
00308                   tmp_str = attributeAsString_(attributes, s_itmin);
00309                   if (tmp_str != "")
00310                   {
00311                     it_range_.setMin(asDouble_(tmp_str));
00312 
00313                     tmp_str = attributeAsString_(attributes, s_itmax);
00314                     if (tmp_str != "")
00315                     {
00316                       it_range_.setMax(asDouble_(tmp_str));
00317 
00318                       consensus_element_range_ = false;
00319                     }
00320                   }
00321                 }
00322               }
00323             }
00324           }
00325         }
00326       }
00327       else if (equal_(qname,s_element))
00328       {
00329         if (load_elements_maps_)
00330         {
00331           IndexTuple< ElementContainerType > act_index_tuple;
00332           tmp_str = attributeAsString_(attributes, s_map);
00333           if (tmp_str != "")
00334           {
00335             UInt map_index = asUInt_(tmp_str);
00336             tmp_str = attributeAsString_(attributes, s_id);
00337 
00338             if (tmp_str != "")
00339             {
00340               UInt element_index = asUInt_(tmp_str);
00341 
00342               act_index_tuple.setMapIndex(map_index);
00343               act_index_tuple.setElementIndex(element_index);
00344 
00345               tmp_str = attributeAsString_(attributes, s_rt);
00346               PositionType pos;
00347               pos[0] = asDouble_(tmp_str);
00348               tmp_str = attributeAsString_(attributes, s_mz);
00349               pos[1] = asDouble_(tmp_str);
00350 
00351               act_index_tuple.setTransformedPosition(pos);
00352               act_index_tuple.setElement((*((consensus_map_->getMapVector())[map_index]))[element_index]);
00353               act_cons_element_.insert(act_index_tuple);
00354             }
00355           }
00356         }
00357         act_cons_element_.getPosition() = pos_;
00358         act_cons_element_.getPositionRange() = pos_range_;
00359         act_cons_element_.setIntensity(it_);
00360         act_cons_element_.getIntensityRange() = it_range_;
00361       }
00362       else if (equal_(qname,s_consensusxml))
00363       {
00364         //check file version against schema version
00365         String file_version="1.0";
00366         optionalAttributeAsString_(file_version,attributes,"version");
00367         if (file_version.toDouble()>version_.toDouble())
00368         {
00369           warning("The XML file (" + file_version +") is newer than the parser (" + version_ + "). This might lead to undefinded program behaviour.");
00370         }
00371       }
00372     }
00373 
00374 
00375 
00376     template < typename  AlignmentT  >
00377     void ConsensusXMLHandler<AlignmentT>::writeTo(std::ostream& os)
00378     {
00379       os << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
00380       << "<consensusXML version=\"" << version_ << "\" xsi:noNamespaceSchemaLocation=\"http://open-ms.sourceforge.net/schemas/ConsensusXML_1_1.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n";
00381 
00382       const std::vector< ElementContainerType* >& map_vector = calignment_->getElementMapVector();
00383       const std::vector< String >& name_vector = calignment_->getFileNames();
00384       os << "\t<mapList count=\"" << map_vector.size() << "\">\n";
00385       os << "\t<mapType name=\"" << calignment_->getMapType() << "\"/>\n";
00386 
00387       // write aligned maps (mapList)
00388       UInt n = map_vector.size();
00389       for (UInt s=0; s < n; s++)
00390       {
00391         os << "\t\t<map id=\"" << s << "\" name =\"" << name_vector[s] << "\" count=\"" << map_vector[s]->size() << " \"/>\n";
00392       } // end for ( mapList)
00393       os << "\t</mapList>\n";
00394 
00395       os << "\t<alignmentMethod name=\"StarAlignmemt\">\n";
00396       os << "\t\t<matchingAlgorithm name=\"" << calignment_->getParameters().getValue("matching_algorithm:type") << "\"/>\n";
00397       os << "\t\t<consensusAlgorithm name=\"delaunay\"/>\n";
00398       os << "\t</alignmentMethod>\n";
00399 
00400       UInt ref_index = calignment_->getReferenceMapIndex();
00401       os << "\t<alignmentNewickTree> " << calignment_->getAlignmentTree() << "</alignmentNewickTree>\n";
00402 
00403       os << "\t<transformationList>\n";
00404       os << "\t\t<transformation id=\"0\" name =\"IdentityTransformation\"/>\n";
00405       const std::vector< Grid >& transformation_vector = calignment_->getTransformationVector();
00406       n = transformation_vector.size();
00407       UInt j=0;
00408       for (UInt s=0; s < n; ++s)
00409       {
00410         if (s != ref_index)
00411         {
00412           os << "\t\t<transformation id=\"" << (j+1) << "\" name=\"AffineTransformation\">\n";
00413           os << "\t";
00414           writeCellList_(os,transformation_vector[s]);
00415           os << "\t\t</transformation>\n";
00416           ++j;
00417         }
00418       }
00419       os << "\t</transformationList>\n";
00420 
00421       const std::vector < ConsensusElementType >& final_consensus_map = calignment_->getFinalConsensusMap();
00422       n=final_consensus_map.size();
00423       os << "\t<consensusElementList>\n";
00424       for (UInt i = 0; i < n; ++i)
00425       {
00426         os << "\t\t<consensusElement id=\""<< i << "\">\n";
00427         os << "\t\t\t<centroid rt=\"" << final_consensus_map[i].getRT()
00428         << "\" mz=\"" << final_consensus_map[i].getMZ()
00429         << "\" it=\"" << final_consensus_map[i].getIntensity() <<"\"/>\n";
00430         os << "\t\t\t<range rtMin=\"" << final_consensus_map[i].getPositionRange().min()[0]
00431         << "\" rtMax=\"" << final_consensus_map[i].getPositionRange().max()[0]
00432         << "\" mzMin=\"" << final_consensus_map[i].getPositionRange().min()[1]
00433         << "\" mzMax=\"" << final_consensus_map[i].getPositionRange().max()[1]
00434         << "\" itMin=\"" << final_consensus_map[i].getIntensityRange().min()
00435         << "\" itMax=\"" << final_consensus_map[i].getIntensityRange().max() <<"\"/>\n";
00436 
00437         os << "\t\t\t<groupedElementList>\n";
00438         for (typename ConsensusElementType::Group::const_iterator it = final_consensus_map[i].begin(); it != final_consensus_map[i].end(); ++it)
00439         {
00440           os  << "\t\t\t\t<element id=\"" << it->getElementIndex()
00441           << "\" map=\"" << it->getMapIndex()
00442           << "\" rt=\"" << it->getTransformedPosition()[0]
00443           << "\" mz=\"" << it->getTransformedPosition()[1]
00444           << "\" it=\"" << it->getElement().getIntensity() << "\"/>\n";
00445         }
00446         os << "\t\t\t</groupedElementList>\n";
00447         os << "\t\t</consensusElement>\n";
00448       }
00449       os << "\t</consensusElementList>\n";
00450       os << "</consensusXML>"<< std::endl;
00451     }
00452 
00454     template <>
00455     template <>
00456     void ConsensusXMLHandler< StarAlignment< ConsensusFeature< FeatureMap< > > > >::loadFile_< ConsensusFeature< FeatureMap< > > >(const String& file_name, UInt id, const ConsensusFeature< FeatureMap< > >& /* c */ ) throw (Exception::FileNotFound, Exception::ParseError);
00457 
00458     // load MzData
00459     template <>
00460     template <>
00461     void ConsensusXMLHandler< StarAlignment< ConsensusPeak< DPeakArray<Peak2D> > > >::loadFile_< ConsensusPeak< DPeakArray<Peak2D> > >( const String& file_name, UInt id, const ConsensusPeak< DPeakArray<Peak2D> >& /* c */) throw (Exception::FileNotFound, Exception::ParseError);
00462 
00463     // load consensusXML
00464     template <>
00465     template <>
00466     void ConsensusXMLHandler< StarAlignment< ConsensusFeature< ConsensusMap< ConsensusFeature< FeatureMap< > > > > > >::loadFile_<ConsensusFeature< ConsensusMap< ConsensusFeature< FeatureMap< > > > > >(const String& file_name, UInt id, const ConsensusFeature< ConsensusMap< ConsensusFeature< FeatureMap< > > > >& /* c */) throw (Exception::FileNotFound, Exception::ParseError);
00467 
00468   } // namespace Internal
00469 } // namespace OpenMS
00470 
00471 #endif // OPENMS_FORMAT_HANDLERS_CONSENSUSXMLHANDLER_H

Generated Tue Apr 1 15:36:33 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1