Logo Search packages:      
Sourcecode: massxpert version File versions

int massXpert::Formula::splitParts ( const QList< Atom * > &  refList,
int  times = 1,
bool  store = false,
bool  reset = false 
) [protected, inherited]

Splits the formula according to its plus-/minus- actions.

Analyzes the formula and separates all the minus components of that formula from all the plus components. The different components are set to their corresponding formula(minus formula and plus formula).

At the end of the split work, each sub-formula(plus- and/or minus-) is actually parsed for validity, using the reference atom list.

Parameters:
refList List of reference atoms.
times Number of times the formula has to be accounted for. Defaults to 1.
store Indicates if AtomCount objects created during the parsing of the sub-formulas generated by the split of the formula have to be stored, or not. Defaults to false.
reset Indicates if the list of AtomCount objects has to be reset before the splitParts work. This parameter may be useful in case the caller needs to "accumulate" the accounting of the formula. Defaults to false.
Returns:
MXT_FORMULA_SPLIT_FAIL if the splitting failed, MXT_FORMULA_SPLIT_PLUS if the components of the formula are all of type plus, MXT_FORMULA_SPLIT_MINUS if all the components of the formula are of type minus. The result value can be an OR'ing of MXT_FORMULA_SPLIT_PLUS and MXT_FORMULA_SPLIT_MINUS.

Definition at line 398 of file formula.cpp.

References massXpert::Formula::actions(), massXpert::Formula::m_atomCountList, massXpert::Formula::m_formula, massXpert::Formula::m_minusFormula, massXpert::Formula::m_plusFormula, and massXpert::Formula::parse().

Referenced by massXpert::Formula::accountAtoms(), massXpert::Formula::accountMasses(), and massXpert::Formula::validate().

  {
    QChar curChar;
    QString tempFormula;

    int result = 0;

    bool wasParsingFormula = false;
    bool shouldBeFormula = false;
    bool wasMinusSign = false;
      
    Q_ASSERT(refList.size());
  
    // We are asked to put all the '+' components of the formula
    // into corresponding formula and the same for the '-' components.
      
    m_plusFormula.clear();
    m_minusFormula.clear();

    // If the formula does not contain any '-' character, then we
    // can approximate that all the formula is a '+' formula, that is a
    // plusFormula:
      
    if (actions() == '+')
      {
      m_plusFormula.append(m_formula);
            
      // At this point we want to make sure that we have a correct
      // formula. Remove all the occurrences of the '+' sign.
      m_plusFormula.replace(QString("+"), QString(""));

      if(m_plusFormula.length() > 0)
        {
          if (!parse(refList, m_plusFormula, times, store, reset))
            return MXT_FORMULA_SPLIT_FAIL;
          else
            return MXT_FORMULA_SPLIT_PLUS;
        }
      }
  
    // At this point, we truly have to iterate in the formula...
  
    for (int iter = 0 ; iter < m_formula.length() ; ++iter)
      {
      curChar = m_formula.at(iter);
      //       qDebug() << "curChar:" << curChar;
      
      if(curChar == '+' || curChar == '-')
        {
          if (shouldBeFormula)
            return MXT_FORMULA_SPLIT_FAIL;

          if (wasParsingFormula)
            {
            // We were parsing a formula, wich means that we are
            // ending that formula now, by starting another one. For
            // example, if we had "-CH3+COOH" we would typically be
            // at the '+' after having parsed -CH3. So we now have
            // to account for that latter formula.

            if(wasMinusSign)
              m_minusFormula.append(tempFormula);
            else
              m_plusFormula.append(tempFormula);

            // Reinit the tempFormula for next round.
            tempFormula.clear();

            // Now set proper bool values for next round.
            shouldBeFormula = true;
            wasMinusSign =(curChar == '-' ? true : false);

            continue;
            }
          else
            {
            wasMinusSign =(curChar == '-' ? true : false);
            shouldBeFormula = true;

            continue;
            }
        }
      else
        {
          // We are parsing either a digit or an alphabetical
          // character : we just append it to the tempFormula:
          tempFormula.append(curChar);

          wasParsingFormula = true;

          // We do not necessarily have to expect another formula
          // component at next round, admitting we were on the
          // nitrogen atom of CH3CN:
          shouldBeFormula = false;

          continue;
        }
      } // End for (int iter = 0 ; iter < m_formula.length() ; ++iter)

    // At this point the loop was finished so we might have something
    // interesting cooking:

    if (wasParsingFormula && tempFormula.length() > 0)
      {
      if(wasMinusSign)
        m_minusFormula.append(tempFormula);
      else
        m_plusFormula.append(tempFormula);
      }
            
    // At this point we want to make sure that we have a correct
    // formula. First reset the atomcount stuff if required.

    if (reset)
      {
      while(!m_atomCountList.isEmpty())
        delete m_atomCountList.takeFirst();
      }
      
    // Now that we have reset if required the atomCountList, we need not
    // and we must not reset during the parsing below, otherwise if we
    // have -H+H3PO4, then we'll compute +H3PO4 first, then we compute
    // -H with reset to true : the +H3PO4 component is destroyed!

    if (m_plusFormula.length() > 0)
      {
      if(!parse(refList, m_plusFormula, times, store, false))
        return MXT_FORMULA_SPLIT_FAIL;
      else
        result = MXT_FORMULA_SPLIT_PLUS;
      }
  
    if (m_minusFormula.length() > 0)
      {
      if(!parse(refList, m_minusFormula, -times, store, false))
        return MXT_FORMULA_SPLIT_FAIL;
      else
        result |= MXT_FORMULA_SPLIT_MINUS;
      }
  
    //   qDebug() << __FILE__ << __LINE__
    //          << m_formula.toAscii() << "-->" 
    //          << "(+)" << m_plusFormula.toAscii() 
    //          << "(-)" << m_minusFormula.toAscii();
  
    return result;
  }


Generated by  Doxygen 1.6.0   Back to index