Matrix Science header

config_quantitation.cpp

Example program for reading and manipulating the quantitation.xml file.

/*
##############################################################################
# file: config_quantitation.cpp                                                  #
# 'msparser' toolkit example code                                            #
##############################################################################
# COPYRIGHT NOTICE                                                           #
# Copyright 1998-2005 Matrix Science Limited  All Rights Reserved.           #
#                                                                            #
##############################################################################
#    $Source: parser/examples/test_cxx/config_quantitation.cpp $ #
#     $Author: villek@matrixscience.com $                                                      #
#       $Date: 2018-07-30 16:23:53 +0100 $                                         #
#   $Revision: 1b450440f9c97e1e41d0fc6016a27d68951d4532 | MSPARSER_REL_2_8_1-0-gea32989045 $                                                         #
# $NoKeywords::                                                            $ #
##############################################################################
*/


#include "msparser.hpp"

#include <iostream>


// All the classes are part of the matrix_science namespace
using namespace matrix_science;

int main(int argc, char * argv[])
{
    if ( argc < 3 )
    {
        std::cout << "The location of quantitation.xml file has to be specified as a parameter" << std::endl;
        std::cout << "The location should either be the full path to the file" << std::endl;
        std::cout << "or a URL to a Mascot server - e.g. http://mascot-server/mascot/cgi" << std::endl;
        std::cout << "The second parameter should specify schema file path, for instance C:\\quantitation_1.xsd" << std::endl;
        std::cout << "It should be either relative to the local quantitation.xml path or to the current folder" << std::endl;
        return 1;
    }

    // A sessionID can optionally be passed as the third parameter
    // This will only be required if the 'file' is a URL
    ms_connection_settings cs;
    if (argc > 3) 
    {
        cs.setSessionID(argv[3]);
    }
    ms_quant_configfile file(argv[1], argv[2], &cs);

    if (!file.isValid())
    {
        std::cout << "There are errors. Cannot continue. The last error description:" << std::endl;
        std::cout << file.getLastErrorString() << std::endl;
        return 1;
    }

    int n = file.getNumberOfMethods();
    std::cout << "There are " << n << " methods available:" << std::endl;

    // now get'em all!
    int i = 0;
    for(i=0; i < n; i++)
    {
        std::cout << file.getMethodByNumber(i)->getName() << std::endl;
    }

    // now try to retrieve typeinfo from the schema file
    ms_xml_schema schemaFile;
    schemaFile.setFileName(argv[2]);
    schemaFile.read_file();
    if (!schemaFile.isValid())
    {
        std::cout << "Errors while reading schema-file. The last error description:" << std::endl;
        std::cout << schemaFile.getLastErrorString() << std::endl;
        return 1;
    }

    // validate the original document
    std::string strValidationErrors = file.validateDocument();
    if ( !strValidationErrors.empty() )
    {
        printf("Initial document validation errors: %s\n", strValidationErrors.c_str());
    }

    // the document can be validated programmatically
    // but it is not schema-validation!!!
    for(i = 0; i < n; i++)
    {
        // Shallow validation: we check only presence of required attributes and elements
        const ms_quant_method *pMethod = file.getMethodByNumber(i);
        std::string strErrors = pMethod->validateShallow(&schemaFile); // pass the schema object as a parameter
        if ( !strErrors.empty() )
        {
            printf("Method %s\n", pMethod->getName().c_str());
            printf("Shallow validation errors: %s\n\n", strErrors.c_str());
        }

        // Deep validation: we check attributes and elements recursively applying basic constraints
        strErrors = pMethod->validateDeep(&schemaFile);
        if ( !strErrors.empty() )
        {
            printf("Method %s\n", pMethod->getName().c_str());
            printf("Deep validation errors: %s\n\n", strErrors.c_str());
        }
    }

    // create a new invalid document
    ms_quant_configfile configFileInvalid;

    // copy the first method from the original file (method "null")
    ms_quant_method myMethodInvalid(*file.getMethodByNumber(0));
    myMethodInvalid.setName(""); // invalid name with less than allowed string length

    // one way to validate an object
    // 1. shallow validation - only presence of attributes and elements is checked
    std::string strErr1 = myMethodInvalid.validateShallow(&schemaFile);
    printf("shallow validation of a method: %s\n", strErr1.c_str());

    // 2. deep validation - data types of attributes and elements are checked and a base type too
    std::string strErr2 = myMethodInvalid.validateDeep(&schemaFile);
    printf("deep validation of a method: %s\n", strErr2.c_str());

    // another way to validate an object - by using schema-object (not schema-document!!!)
    // the result is absolutely equivalent to the previous call
    std::string strErr3 = schemaFile.validateComplexObject(&myMethodInvalid, true); // "true" for deep validation
    printf("deep validation using schema-object: %s\n", strErr3.c_str());

    // one can validate an attribute separately
    // also methods validateSimpleInteger(), validateSimpleDouble() and validateSimpleBool() can be used
    std::string strErr4 = schemaFile.validateSimpleString(myMethodInvalid.getName().c_str(), // attribute value
                                                          myMethodInvalid.getNameSchemaType().c_str()); // attribute data type
    printf("attribute validation: %s\n", strErr4.c_str());

    // and finally, the whole document validation
    configFileInvalid.appendMethod(&myMethodInvalid);
    std::string errMethod = configFileInvalid.validateDocument();
    printf("document validation errors: %s\n", errMethod.c_str());

    // however, the config-file object itself stays valid
    if (!configFileInvalid.isValid())
    {
        std::cout << "Errors in config-file object. The last error description:" << std::endl;
        std::cout << configFileInvalid.getLastErrorString() << std::endl;
    }

    // file is going to be saved even if its invalid
    // but the errors will be stored in the config-file object and the object will become invalid
    configFileInvalid.setFileName("quantitation_temp.xml"); // give it another name
    configFileInvalid.save_file();
    if (!configFileInvalid.isValid())
    {
        std::cout << "Errors in config-file object. The last error description:" << std::endl;
        std::cout << configFileInvalid.getLastErrorString() << std::endl;
    }

    return 0;
}


/*

will give the output, for instance: 


# config_quantitation ../config/quantitation.xml quantitation_1.xsd

There are 10 methods available:
None
iTRAQ
ICAT C+9
ICPL C+6 pre-digest
ICPL C+6 post-digest
SILAC K+6 R+10
18O corrected, single scan
18O simple, single scan
SILAC K+6 R+4 multiplex
15N Metabolic
shallow validation of a method: 
deep validation of a method: Attribute 'name' -> String is shorter than minLength-limit
deep validation using schema-object: Attribute 'name' -> String is shorter than minLength-limit
attribute validation: String is shorter than minLength-limit
document validation errors: XML library failure with error: quantitation.xml: line 4, col 240 - Datatype error: Type:InvalidDatatypeValueException, Message:Value '' with length '0' is less than minimum length facet of '1' .
Errors in config-file object. The last error description:
Failed to save quantitation configuration file

*/

Copyright © 2022 Matrix Science Ltd.  All Rights Reserved. Generated on Thu Mar 31 2022 01:12:29