Esempio n. 1
0
bool Sequence::HighlightPattern(const string& prositePattern, const MoleculeHighlightMap& restrictTo) const
{
    bool retval = true;
    try {

        // update CRegexp if not the same pattern as before
        static auto_ptr < CRegexp > regexp;
        static string previousPrositePattern;
        static int nGroups;
        if (!regexp.get() || prositePattern != previousPrositePattern) {

            // convert from ProSite syntax
            string regexPattern;
            if (!Prosite2Regex(prositePattern, &regexPattern, &nGroups))
                throw "error converting ProSite to regex syntax";

            // create pattern buffer
            TRACEMSG("creating CRegexp with pattern '" << regexPattern << "'");
            regexp.reset(new CRegexp(regexPattern, CRegexp::fCompile_ungreedy));

            previousPrositePattern = prositePattern;
        }

        // do the search, finding all non-overlapping matches
        int i, start = 0;
        while (start < (int)Length()) {

            // do the search
            string ignore = regexp->GetMatch(sequenceString, start, 0, CRegexp::fMatch_default, true);
            if (regexp->NumFound() <= 0)
                break;

//            TRACEMSG("got match, num (sub)patterns: " << regexp->NumFound());
//            for (i=0; i<regexp->NumFound(); ++i)
//                TRACEMSG("    " << i << ": " << (regexp->GetResults(i)[0] + 1) << '-' << regexp->GetResults(i)[1]);

            // check to see if this entire match is within the restricted set
            bool addMatch = true;
            if (restrictTo.size() > 0) {
                MoleculeHighlightMap::const_iterator r = restrictTo.find(identifier);
                if (r != restrictTo.end()) {
                    for (i=1; i<regexp->NumFound(); ++i) {
                        for (int j=regexp->GetResults(i)[0]; j<=regexp->GetResults(i)[1]-1; ++j) {
                            if (!r->second[j]) {
                                addMatch = false;
                                break;
                            }
                        }
                        if (!addMatch)
                            break;
                    }
                } else
                    addMatch = false;
            }

            // parse the match subpatterns, highlight each subpattern range
            if (addMatch)
                for (i=1; i<regexp->NumFound(); ++i)
                    GlobalMessenger()->AddHighlights(this, regexp->GetResults(i)[0], regexp->GetResults(i)[1] - 1);

            // start next search after the end of this one
            start = regexp->GetResults(regexp->NumFound() - 1)[1];
        }

    } catch (const char *err) {
        ERRORMSG("Sequence::HighlightPattern() - " << err);
        retval = false;
    } catch (exception& e) {
        ERRORMSG("Sequence::HighlightPattern() - caught exception: " << e.what());
        retval = false;
    }

    return retval;
}