OpenMS
Loading...
Searching...
No Matches
UniProtXMLHandler Class Reference

SAX handler for UniProtKB XML <entry> documents. More...

#include <UniPEFF/include/UniProtXMLHandler.h>

Inheritance diagram for UniProtXMLHandler:
XMLHandler

Public Types

enum class  CaptureTarget {
  None , Accession , EntryName , FullName ,
  PrimaryGene , TaxName , Sequence , FeatureOriginal ,
  FeatureVariation , IsoformId , IsoformName
}
 
using EntryCallback = std::function< void(UniProtEntry &&)>
 
- Public Types inherited from XMLHandler
enum  ActionMode { LOAD , STORE }
 Action to set the current mode (for error messages) More...
 
enum  LOADDETAIL { LD_ALLDATA , LD_RAWCOUNTS , LD_COUNTS_WITHOPTIONS }
 

Public Member Functions

 UniProtXMLHandler (const std::string &filename, EntryCallback callback)
 Build a handler that delivers each parsed UniProtEntry to callback.
 
 ~UniProtXMLHandler () override
 Destructor.
 
void onStartElement (const char16_t *qname, const XMLAttributes &attrs) override
 Parsing method for opening tags.
 
void onEndElement (const char16_t *qname) override
 Parsing method for closing tags.
 
void onCharacters (const char16_t *chars, Size length) override
 Parsing method for character data.
 
- Public Member Functions inherited from XMLHandler
 XMLHandler (const std::string &filename, const std::string &version)
 Default constructor.
 
virtual ~XMLHandler ()
 Destructor.
 
void reset ()
 Release internal memory used for parsing (call.
 
void fatalError (ActionMode mode, const std::string &msg, UInt line=0, UInt column=0) const
 Fatal error handler. Throws a ParseError exception.
 
void error (ActionMode mode, const std::string &msg, UInt line=0, UInt column=0) const
 Error handler for recoverable errors.
 
void warning (ActionMode mode, const std::string &msg, UInt line=0, UInt column=0) const
 Warning handler.
 
virtual void writeTo (std::ostream &)
 Writes the contents to a stream.
 
virtual LOADDETAIL getLoadDetail () const
 handler which support partial loading, implement this method
 
virtual void setLoadDetail (const LOADDETAIL d)
 handler which support partial loading, implement this method
 
DataValue cvParamToValue (const ControlledVocabulary &cv, const std::string &parent_tag, const std::string &accession, const std::string &name, const std::string &value, const std::string &unit_accession) const
 Convert the value of a <cvParam value=.> (as commonly found in PSI schemata) to the DataValue with the correct type (e.g. int) according to the type stored in the CV (usually PSI-MS CV), as well as set its unit.
 
DataValue cvParamToValue (const ControlledVocabulary &cv, const CVTerm &raw_term) const
 Convert the value of a <cvParam value=.> (as commonly found in PSI schemata) to the DataValue with the correct type (e.g. int) according to the type stored in the CV (usually PSI-MS CV), as well as set its unit.
 
void checkUniqueIdentifiers_ (const std::vector< ProteinIdentification > &prot_ids) const
 

Private Member Functions

void resetEntry_ ()
 Clear all per-entry state so the next <entry> starts fresh.
 
void resetFeature_ ()
 Clear all per-feature state so the next <feature> starts fresh.
 

Static Private Member Functions

static int parsePosition_ (const std::string &attr)
 

Private Attributes

EntryCallback callback_
 
UniProtEntry current_entry_
 
int depth_ {0}
 Depth of the most recent element start, used to scope subtree handling.
 
int entry_depth_ {0}
 <entry>
 
int recommended_name_depth_ {0}
 <protein>/<recommendedName>
 
int gene_depth_ {0}
 <gene>
 
int organism_depth_ {0}
 <organism>
 
int alt_products_depth_ {0}
 <comment type="alternative products"> (isoform definitions harvested, rest ignored)
 
int isoform_depth_ {0}
 <isoform> inside the alternative-products comment
 
int feature_depth_ {0}
 <feature>
 
int sequence_depth_ {0}
 <sequence length="..."> (only the canonical sequence; isoform <sequence> elements have no content)
 
bool full_name_captured_ {false}
 
std::string char_buf_
 Buffer for character data; appended-to in characters(), consumed in endElement().
 
CaptureTarget capture_ {CaptureTarget::None}
 Destination for the next buffered run of character data (None == not capturing).
 
UniProtFeature current_feature_
 Per-feature working state (used between startElement("feature") and endElement("feature")).
 
UniProtIsoform current_isoform_
 Per-isoform working state (used between startElement("isoform") and endElement("isoform")).
 

Additional Inherited Members

- Static Public Member Functions inherited from XMLHandler
static std::string writeXMLEscape (const std::string &to_escape)
 Escapes a string and returns the escaped string.
 
static DataValue fromXSDString (const std::string &type, const std::string &value)
 Convert an XSD type (e.g. 'xsd:double') to a DataValue.
 
- Protected Member Functions inherited from XMLHandler
void writeUserParam_ (const std::string &tag_name, std::ostream &os, const MetaInfoInterface &meta, UInt indent) const
 Writes the content of MetaInfoInterface to the file.
 
Int asInt_ (const std::string &in) const
 Conversion of a std::string to an integer value.
 
Int asInt_ (const char16_t *in) const
 Conversion of a Xerces string to an integer value.
 
UInt asUInt_ (const std::string &in) const
 Conversion of a std::string to an unsigned integer value.
 
double asDouble_ (const std::string &in) const
 Conversion of a std::string to a double value.
 
float asFloat_ (const std::string &in) const
 Conversion of a std::string to a float value.
 
bool asBool_ (const std::string &in) const
 Conversion of a string to a boolean value.
 
DateTime asDateTime_ (std::string date_string) const
 Conversion of a xs:datetime string to a DateTime value.
 
bool equal_ (const char16_t *a, const char16_t *b) const
 Returns if two Xerces strings are equal.
 
SignedSize cvStringToEnum_ (const Size section, const std::string &term, const char *message, const SignedSize result_on_error=0)
 
std::string attributeAsString_ (const XMLAttributes &a, const char *name) const
 Converts an attribute to a String.
 
Int attributeAsInt_ (const XMLAttributes &a, const char *name) const
 Converts an attribute to a Int.
 
double attributeAsDouble_ (const XMLAttributes &a, const char *name) const
 Converts an attribute to a double.
 
DoubleList attributeAsDoubleList_ (const XMLAttributes &a, const char *name) const
 Converts an attribute to a DoubleList.
 
IntList attributeAsIntList_ (const XMLAttributes &a, const char *name) const
 Converts an attribute to an IntList.
 
StringList attributeAsStringList_ (const XMLAttributes &a, const char *name) const
 Converts an attribute to an StringList.
 
bool optionalAttributeAsString_ (std::string &value, const XMLAttributes &a, const char *name) const
 Assigns the attribute content to the String value if the attribute is present.
 
bool optionalAttributeAsInt_ (Int &value, const XMLAttributes &a, const char *name) const
 Assigns the attribute content to the Int value if the attribute is present.
 
bool optionalAttributeAsUInt_ (UInt &value, const XMLAttributes &a, const char *name) const
 Assigns the attribute content to the UInt value if the attribute is present.
 
bool optionalAttributeAsDouble_ (double &value, const XMLAttributes &a, const char *name) const
 Assigns the attribute content to the double value if the attribute is present.
 
bool optionalAttributeAsDoubleList_ (DoubleList &value, const XMLAttributes &a, const char *name) const
 Assigns the attribute content to the DoubleList value if the attribute is present.
 
bool optionalAttributeAsStringList_ (StringList &value, const XMLAttributes &a, const char *name) const
 Assigns the attribute content to the StringList value if the attribute is present.
 
bool optionalAttributeAsIntList_ (IntList &value, const XMLAttributes &a, const char *name) const
 Assigns the attribute content to the IntList value if the attribute is present.
 
std::string attributeAsString_ (const XMLAttributes &a, const char16_t *name) const
 Converts an attribute to a String.
 
Int attributeAsInt_ (const XMLAttributes &a, const char16_t *name) const
 Converts an attribute to a Int.
 
double attributeAsDouble_ (const XMLAttributes &a, const char16_t *name) const
 Converts an attribute to a double.
 
DoubleList attributeAsDoubleList_ (const XMLAttributes &a, const char16_t *name) const
 Converts an attribute to a DoubleList.
 
IntList attributeAsIntList_ (const XMLAttributes &a, const char16_t *name) const
 Converts an attribute to a IntList.
 
StringList attributeAsStringList_ (const XMLAttributes &a, const char16_t *name) const
 Converts an attribute to a StringList.
 
bool optionalAttributeAsString_ (std::string &value, const XMLAttributes &a, const char16_t *name) const
 Assigns the attribute content to the String value if the attribute is present.
 
bool optionalAttributeAsInt_ (Int &value, const XMLAttributes &a, const char16_t *name) const
 Assigns the attribute content to the Int value if the attribute is present.
 
bool optionalAttributeAsUInt_ (UInt &value, const XMLAttributes &a, const char16_t *name) const
 Assigns the attribute content to the UInt value if the attribute is present.
 
bool optionalAttributeAsDouble_ (double &value, const XMLAttributes &a, const char16_t *name) const
 Assigns the attribute content to the double value if the attribute is present.
 
bool optionalAttributeAsDoubleList_ (DoubleList &value, const XMLAttributes &a, const char16_t *name) const
 Assigns the attribute content to the DoubleList value if the attribute is present.
 
bool optionalAttributeAsIntList_ (IntList &value, const XMLAttributes &a, const char16_t *name) const
 Assigns the attribute content to the IntList value if the attribute is present.
 
bool optionalAttributeAsStringList_ (StringList &value, const XMLAttributes &a, const char16_t *name) const
 Assigns the attribute content to the StringList value if the attribute is present.
 
- Protected Attributes inherited from XMLHandler
std::string file_
 File name.
 
std::string version_
 Schema version.
 
StringManager sm_
 Helper class for string conversion.
 
std::vector< std::string > open_tags_
 Stack of open XML tags.
 
LOADDETAIL load_detail_
 parse only until total number of scans and chroms have been determined from attributes
 
std::vector< std::vector< std::string > > cv_terms_
 Array of CV term lists (one sublist denotes one term and it's children)
 

Detailed Description

SAX handler for UniProtKB XML <entry> documents.

Streams individual <entry> elements into UniProtEntry POD instances and delivers each via a callback at </entry>. Implements the same scoping rules as the upstream C# UniPEFF tool: only the canonical <sequence> (carrying a "length" attribute) is captured, <gene> and <organism> are scanned via bounded depth tracking so their inner <name> elements never leak into the top-level "entry mnemonic" capture, and <feature> contents are buffered and classified at the closing tag rather than incrementally. The <comment type="alternative products"> subtree is parsed into UniProtIsoform records (id, name, sequence type and splice-variant refs); everything else inside it is ignored.

Member Typedef Documentation

◆ EntryCallback

using EntryCallback = std::function<void(UniProtEntry&&)>

Callback invoked once per </entry>: receives ownership of the populated UniProtEntry. The handler resets its working state immediately after, so the callback is the only place the entry is reachable.

Member Enumeration Documentation

◆ CaptureTarget

enum class CaptureTarget
strong

Where the next character-data run, if buffered, should be deposited at the closing tag of its enclosing element.

Enumerator
None 
Accession 
EntryName 
FullName 
PrimaryGene 
TaxName 
Sequence 
FeatureOriginal 
FeatureVariation 
IsoformId 
IsoformName 

Constructor & Destructor Documentation

◆ UniProtXMLHandler()

UniProtXMLHandler ( const std::string &  filename,
EntryCallback  callback 
)

Build a handler that delivers each parsed UniProtEntry to callback.

◆ ~UniProtXMLHandler()

~UniProtXMLHandler ( )
overridedefault

Destructor.

Member Function Documentation

◆ onCharacters()

void onCharacters ( const char16_t *  chars,
Size  length 
)
overridevirtual

Parsing method for character data.

Reimplemented from XMLHandler.

References UniProtXMLHandler::capture_, UniProtXMLHandler::char_buf_, and UniProtXMLHandler::None.

◆ onEndElement()

◆ onStartElement()

void onStartElement ( const char16_t *  qname,
const XMLAttributes &  attributes 
)
overridevirtual

Parsing method for opening tags.

Reimplemented from XMLHandler.

References UniProtXMLHandler::Accession, UniProtXMLHandler::alt_products_depth_, UniProtFeature::begin, UniProtXMLHandler::capture_, UniProtXMLHandler::char_buf_, UniProtXMLHandler::current_entry_, UniProtXMLHandler::current_feature_, UniProtXMLHandler::current_isoform_, UniProtEntry::dataset, UniProtXMLHandler::depth_, UniProtFeature::description, UniProtFeature::end, UniProtXMLHandler::entry_depth_, UniProtEntry::entry_version, UniProtXMLHandler::EntryName, UniProtXMLHandler::feature_depth_, UniProtXMLHandler::FeatureOriginal, UniProtXMLHandler::FeatureVariation, UniProtXMLHandler::full_name_captured_, UniProtXMLHandler::FullName, UniProtXMLHandler::gene_depth_, UniProtFeature::has_position, UniProtFeature::has_range, UniProtFeature::id, UniProtIsoform::id, UniProtXMLHandler::isoform_depth_, UniProtXMLHandler::IsoformId, UniProtXMLHandler::IsoformName, UniProtFeature::location_sequence, UniProtIsoform::name, UniProtEntry::name, UniProtEntry::ncbi_tax_id, UniProtXMLHandler::None, UniProtXMLHandler::organism_depth_, UniProtXMLHandler::parsePosition_(), UniProtFeature::position, UniProtEntry::primary_gene, UniProtXMLHandler::PrimaryGene, UniProtEntry::protein_existence, UniProtXMLHandler::recommended_name_depth_, UniProtXMLHandler::resetEntry_(), UniProtXMLHandler::resetFeature_(), UniProtXMLHandler::Sequence, UniProtXMLHandler::sequence_depth_, UniProtIsoform::sequence_ref, UniProtIsoform::sequence_type, UniProtEntry::sequence_version, UniProtEntry::tax_name, UniProtXMLHandler::TaxName, UniProtFeature::type, and UniProtFeature::variation.

◆ parsePosition_()

int parsePosition_ ( const std::string &  attr)
staticprivate

Parse a UniProt position attribute string; returns 0 when the attribute is absent or non-numeric (UniProt encodes "unknown" by omitting the attribute).

Referenced by UniProtXMLHandler::onStartElement().

◆ resetEntry_()

◆ resetFeature_()

void resetFeature_ ( )
private

Clear all per-feature state so the next <feature> starts fresh.

References UniProtXMLHandler::current_feature_.

Referenced by UniProtXMLHandler::onEndElement(), and UniProtXMLHandler::onStartElement().

Member Data Documentation

◆ alt_products_depth_

int alt_products_depth_ {0}
private

<comment type="alternative products"> (isoform definitions harvested, rest ignored)

Referenced by UniProtXMLHandler::onEndElement(), and UniProtXMLHandler::onStartElement().

◆ callback_

EntryCallback callback_
private

◆ capture_

CaptureTarget capture_ {CaptureTarget::None}
private

Destination for the next buffered run of character data (None == not capturing).

Referenced by UniProtXMLHandler::onCharacters(), UniProtXMLHandler::onEndElement(), and UniProtXMLHandler::onStartElement().

◆ char_buf_

std::string char_buf_
private

Buffer for character data; appended-to in characters(), consumed in endElement().

Referenced by UniProtXMLHandler::onCharacters(), UniProtXMLHandler::onEndElement(), and UniProtXMLHandler::onStartElement().

◆ current_entry_

◆ current_feature_

UniProtFeature current_feature_
private

Per-feature working state (used between startElement("feature") and endElement("feature")).

Referenced by UniProtXMLHandler::onEndElement(), UniProtXMLHandler::onStartElement(), and UniProtXMLHandler::resetFeature_().

◆ current_isoform_

UniProtIsoform current_isoform_
private

Per-isoform working state (used between startElement("isoform") and endElement("isoform")).

Referenced by UniProtXMLHandler::onEndElement(), UniProtXMLHandler::onStartElement(), and UniProtXMLHandler::resetEntry_().

◆ depth_

int depth_ {0}
private

Depth of the most recent element start, used to scope subtree handling.

Referenced by UniProtXMLHandler::onEndElement(), and UniProtXMLHandler::onStartElement().

◆ entry_depth_

int entry_depth_ {0}
private

<entry>

Subtree gates. Each holds the depth at which the corresponding element opened so we know when its closing tag fires (depth_ == gate value during endElement). 0 means "not currently inside this subtree".

Referenced by UniProtXMLHandler::onEndElement(), and UniProtXMLHandler::onStartElement().

◆ feature_depth_

◆ full_name_captured_

bool full_name_captured_ {false}
private

Captures the <fullName> the first time we encounter it inside <protein>/<recommendedName>; later <fullName> elements (e.g. alternative names) are skipped.

Referenced by UniProtXMLHandler::onEndElement(), UniProtXMLHandler::onStartElement(), and UniProtXMLHandler::resetEntry_().

◆ gene_depth_

◆ isoform_depth_

int isoform_depth_ {0}
private

<isoform> inside the alternative-products comment

Referenced by UniProtXMLHandler::onEndElement(), UniProtXMLHandler::onStartElement(), and UniProtXMLHandler::resetEntry_().

◆ organism_depth_

int organism_depth_ {0}
private

◆ recommended_name_depth_

int recommended_name_depth_ {0}
private

◆ sequence_depth_

int sequence_depth_ {0}
private

<sequence length="..."> (only the canonical sequence; isoform <sequence> elements have no content)

Referenced by UniProtXMLHandler::onEndElement(), UniProtXMLHandler::onStartElement(), and UniProtXMLHandler::resetEntry_().