예제 #1
0
  /**
   * Try to open .ssl file and read the header line.  Return false if
   * the file cannot be opened or if the header is incorrect.
   */
  bool SslReader::openSsl(){

    Verbosity::debug("Opening ssl File.");
    sslFile_.open(sslName_.c_str());
    if( !sslFile_.is_open() ){
      throw BlibException(true, "Could not open ssl file '%s'.", 
                          sslName_.c_str());
    }
    // confirm that header looks correct
    string line;
    getline(sslFile_, line);
    const char* header = "file\tscan\tcharge\tsequence\tmodifications";
    if( line.compare(0, strlen(header), header) ){
      throw BlibException(false,
                          "SSL header is not correct.  Should be '%s'.", 
                          header);
    }
    return true;
  }
예제 #2
0
/**
 * Converts a position string from a mqpar file into a MAXQUANT_MOD_POSITION.
 */
MaxQuantModification::MAXQUANT_MOD_POSITION MaxQuantModReader::stringToPosition(string positionString)
{
    const char* positionStringChars = positionString.c_str();

    if (isIElement(positionStringChars, "anywhere"))
    {
        return MaxQuantModification::ANYWHERE;
    }
    else if (isIElement(positionStringChars, "proteinNterm"))
    {
        return MaxQuantModification::PROTEIN_N_TERM;
    }
    else if (isIElement(positionStringChars, "proteinCterm"))
    {
        return MaxQuantModification::PROTEIN_C_TERM;
    }
    else if (isIElement(positionStringChars, "anyNterm"))
    {
        return MaxQuantModification::PROTEIN_C_TERM;
    }
    else if (isIElement(positionStringChars, "anyCterm"))
    {
        return MaxQuantModification::PROTEIN_C_TERM;
    }
    else if (isIElement(positionStringChars, "notNterm"))
    {
        return MaxQuantModification::NOT_N_TERM;
    }
    else if (isIElement(positionStringChars, "notCterm"))
    {
        return MaxQuantModification::NOT_C_TERM;
    }
    else
    {
        throw BlibException(false, "Invalid position value: %s",
                            positionStringChars);
    }
}
예제 #3
0
bool SAXHandler::parse()
{
    FILE* pfIn = fopen(m_strFileName_.data(), "r");
    if (pfIn == NULL) {
        throw BlibException(true, "Failed to open input file '%s'.",
                            m_strFileName_.c_str());
    }

    bool success = true;
    string message;

    try {
        // HACK!! I have no idea why this is, but without this string
        // declaration, the MSVC optimizer removes the catch block below.
        // Very mysterious.
        string temp;

        char buffer[8192];
        int readBytes = 0;

        while (success && (readBytes = (int) fread(buffer, 1, sizeof(buffer), pfIn)) != 0) {
            success = (XML_Parse(m_parser_, buffer, readBytes, false) != 0);
        }
        success = success && (XML_Parse(m_parser_, buffer, 0, true) != 0);
    }
    catch(string thrown_msg) { // from parsers
        message = thrown_msg;
        success = false;
    }
    catch(BlibException e) { // probably from BuildParser
        if( e.hasFilename() ) {
            throw e;
        } else {
            message = e.what();
            success = false;
        }
    }
    catch(std::exception e) {  // other runtime errors (e.g. memory)
        message = e.what();
        success = false;
    }

    fclose(pfIn);

    if (!success) {
        XML_Error error = XML_GetErrorCode(m_parser_);
        int lineNum = XML_GetCurrentLineNumber(m_parser_);
        ostringstream stringBuilder(ostringstream::out);

        stringBuilder << m_strFileName_
                      << "(line " << lineNum
                      << "): " << message << flush;

        if (message.length() == 0) {
            switch (error) {
            case XML_ERROR_SYNTAX:
                stringBuilder << "Syntax error parsing XML.";
                break;
            case XML_ERROR_INVALID_TOKEN:
                stringBuilder << "Invalid token error parsing XML.";
                break;
            case XML_ERROR_UNCLOSED_TOKEN:
                stringBuilder << "Unclosed token error parsing XML.";
                break;
            case XML_ERROR_NO_ELEMENTS:
                stringBuilder << "No elements error parsing XML.";
                break;
            case XML_ERROR_TAG_MISMATCH:
                stringBuilder << "Tag mismatch error parsing XML.";
                break;
            case XML_ERROR_DUPLICATE_ATTRIBUTE:
                stringBuilder << "Duplicate attribute error parsing XML.";
                break;
            case XML_ERROR_UNKNOWN_ENCODING:
            case XML_ERROR_INCORRECT_ENCODING:
                stringBuilder << "Unknown or incorrect encoding XML error.";
                break;
            case XML_ERROR_UNCLOSED_CDATA_SECTION:
                stringBuilder << "Unclosed data section error parsing XML.";
                break;

            default:
                stringBuilder << "XML parsing error.";
                break;
            }
        }

        string er_msg = stringBuilder.str();
        throw BlibException(true, er_msg.c_str());
    }

    return true;
}
예제 #4
0
/**
 * Called by saxhandler when the closing tag of an xml element is
 * reached.  Change the state based on the tag and the current state.
 */
void MaxQuantModReader::endElement(const XML_Char* name)
{
    // modifications.xml mode
    if (modBank_ && !fixedMods_)
    {
        if (isIElement("modification", name))
        {
            if (curMod_.massDelta != 0.0)
            {
                modBank_->insert(curMod_);
            }
            state_ = ROOT_STATE;
        }
        else if (state_ == READING_POSITION)
        {
            curMod_.position = stringToPosition(charBuf_);
            state_ = MODIFICATION_TAG;
        }
    }
    // mqpar.xml mode
    else if (!modBank_ && fixedMods_)
    {
        if (isIElement("fixedModifications", name))
        {
            state_ = ROOT_STATE;
        }
        else if (state_ == READING_FIXED_MODIFICATION)
        {
            fixedMods_->insert(charBuf_);
            state_ = FIXED_MODIFICATIONS_TAG;
        }
        else if (isIElement("filePaths", name))
        {
            state_ = ROOT_STATE;
        }
        else if (state_ == READING_FILEPATH)
        {
            string rawBaseName(charBuf_);
            size_t lastSlash = rawBaseName.find_last_of("/\\");
            if (lastSlash != string::npos)
            {
                rawBaseName = rawBaseName.substr(lastSlash + 1);
            }
            size_t extensionBegin = rawBaseName.find_last_of(".");
            if (extensionBegin != string::npos)
            {
                rawBaseName.erase(extensionBegin);
            }
            MaxQuantLabels newLabelingStates(rawBaseName);
            labelBank_->push_back(newLabelingStates);
            state_ = FILEPATHS_TAG;
        }
        else if (isIElement("paramGroupIndices", name))
        {
            // check that each raw file had a corresponding param group index
            if (paramGroupIndices_.size() != labelBank_->size())
            {
                throw BlibException(false, "Number of raw files (%d) did not match "
                                    "number of paramGroupIndices (%d).",
                                    labelBank_->size(), paramGroupIndices_.size());
            }
            state_ = ROOT_STATE;
        }
        else if (state_ == READING_PARAMGROUPINDEX)
        {
            paramGroupIndices_.push_back(lexical_cast<int>(charBuf_));
            state_ = PARAMGROUPINDICES_TAG;
        }
        else if (isIElement("parameterGroups", name) && !paramGroupIndices_.empty())
        {
            // check that each raw file had a corresponding parameterGroup
            for (vector<int>::iterator i = paramGroupIndices_.begin(); i != paramGroupIndices_.end(); ++i)
            {
                if (*i > rawIndex_)
                {
                    throw BlibException(false, "Parameter group index %d was outside the range of "
                                        "of parameter groups (%d).", *i, rawIndex_ + 1);
                }
            }
        }
        else if (isIElement("GroupParams", name))
        {
            // check that each raw file had a corresponding groupParam
            if (--groupParams_ == 0 &&
                    (rawIndex_ + 1 != (int)labelBank_->size()))
            {
                throw BlibException(false, "Number of raw files (%d) did not match "
                                    "number of label sets (%d).", labelBank_->size(), rawIndex_ + 1);
            }
        }
        else if (state_ == LABELS_TAG &&
                 (isIElement("labels", name) || isIElement("labelMods", name)))
        {
            state_ = ROOT_STATE;
        }
        else if (state_ == READING_LABEL)
        {
            vector<string> newLabelSubset;
            // split multiple labels (e.g. "Arg6; Lys4")
            split(newLabelSubset, charBuf_, is_any_of(";"));
            // trim whitespace
            for_each(newLabelSubset.begin(), newLabelSubset.end(),
                     boost::bind(&boost::trim<string>, _1, std::locale()));

            if (!paramGroupIndices_.empty())
            {
                // Assign label to all raw files with this parameter index
                for (size_t i = 0; i < paramGroupIndices_.size(); ++i)
                {
                    if (paramGroupIndices_[i] == rawIndex_)
                    {
                        (*labelBank_)[i].addModsStrings(newLabelSubset);
                        Verbosity::debug("Adding to labelBank_[%d] : %s", i, charBuf_.c_str());
                    }
                }
            }
            else
            {
                (*labelBank_)[rawIndex_].addModsStrings(newLabelSubset);
            }

            state_ = LABELS_TAG;
        }
    }
}