Logo Search packages:      
Sourcecode: massxpert version File versions

seqToolsPluginDlg.cpp

/* massXpert - the true massist's program.
   --------------------------------------
   Copyright (C) 2006,2007 Filippo Rusconi

   http://www.filomace.org/massXpert

   This file is part of the massXpert project.

   The massxpert project is the successor to the "GNU polyxmass"
   project that is an official GNU project package (see
   www.gnu.org). The massXpert project is not endorsed by the GNU
   project, although it is released ---in its entirety--- under the
   GNU General Public License. A huge part of the code in massXpert
   is actually a C++ rewrite of code in GNU polyxmass. As such
   massXpert was started at the Centre National de la Recherche
   Scientifique (FRANCE), that granted me the formal authorization to
   publish it under this Free Software License.

   This software is free software; you can redistribute it and/or
   modify it under the terms of the GNU  General Public
   License version 3, as published by the Free Software Foundation.
   

   This software is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this software; if not, write to the

   Free Software Foundation, Inc.,

   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include <QtGui>

#include "seqToolsPluginDlg.hpp"

SeqToolsPluginDlg::SeqToolsPluginDlg (QWidget *parent)
  : QDialog (parent, Qt::Dialog)
{
  mp_parent = static_cast<QMainWindow *> (parent);
  
  QVBoxLayout *mainLayout = new QVBoxLayout;
  
  QLabel *label = new QLabel (tr ("Sequence Manipulation Tools Plugin"));
    
  mainLayout->addWidget (label, 0, Qt::AlignHCenter);
    
  createEditorGroupBox ();
  mainLayout->addWidget (mp_editorGroupBox);

  m_inputDocument.setParent (this);
  m_outputDocument.setParent (this);

  mp_inputEditor->setDocument (&m_inputDocument);
  mp_outputEditor->setDocument (&m_outputDocument);
  
  createActionGroupBox ();
  mainLayout->addWidget (mp_actionGroupBox);
  m_dictionaryLoaded = false;
  
  setLayout (mainLayout);

  setAttribute (Qt::WA_DeleteOnClose);
  
  setWindowTitle (tr ("Sequence Manipulation Tools Plugin"));

  connect (mp_parent, 
         SIGNAL (aboutToClose ()),
         this,
         SLOT (parentClosing ()));
}


void
SeqToolsPluginDlg::parentClosing ()
{
  QDialog::reject ();
}


void 
SeqToolsPluginDlg::createEditorGroupBox ()
{
    mp_editorGroupBox = new QGroupBox (tr ("Manipulated Sequences"));

    QHBoxLayout *layout = new QHBoxLayout;

    mp_inputEditor = new QTextEdit;
    layout->addWidget (mp_inputEditor);

    mp_outputEditor = new QTextEdit;
    layout->addWidget (mp_outputEditor);
    
    mp_editorGroupBox->setLayout(layout);
}


void 
SeqToolsPluginDlg::createActionGroupBox ()
{
  mp_actionGroupBox = new QGroupBox (tr ("Actions"));

  QHBoxLayout *layout = new QHBoxLayout;

  mp_actionComboBox = new QComboBox ();

  QStringList menuItems = QStringList () 
    << tr ("To Upper Case")
    << tr ("To Lower Case")
    << tr ("Dictionary-based Conversion");
  
  mp_actionComboBox->addItems (menuItems);

  layout->addWidget (mp_actionComboBox);

  mp_executePushButton = new QPushButton (tr ("&Execute"));
  connect(mp_executePushButton, 
        SIGNAL (clicked ()), 
        this, 
        SLOT (execute ()));

  layout->addWidget (mp_executePushButton);
  
  mp_loadDicPushButton = new QPushButton (tr ("&Load Dictionary"));

  connect(mp_loadDicPushButton, 
        SIGNAL (clicked ()), 
        this, 
        SLOT (loadDictionary ()));

  layout->addWidget (mp_loadDicPushButton);

  mp_actionGroupBox->setLayout (layout);
}


void 
SeqToolsPluginDlg::loadDictionary ()
{
  // Pop up a file chooser dialog, and load the file.
  // Syntax of the file is per line:
  
  // Arg>R
  
  // Pro>P
  
  QString filePath =
    QFileDialog::getOpenFileName (this, tr ("Open Dictionary File"),
                          QDir::homePath (),
                          tr ("Dictionary files (*.dic)"));
  
  if (!QFile::exists (filePath))
    return;
  
  // Load the file and for each line deconstruct the item into two
  // QString objects that are used to make a QHash entry in
  // QHash<QString, QString> m_dictionaryHash.

  qint64 lineLength;
  QString line;
  char buffer [1024];
  
  QFile file (filePath);

  if (!file.open (QFile::ReadOnly)) 
    {
      m_dictionaryLoaded = false;
      return;
    }
  
  lineLength = file.readLine (buffer, sizeof (buffer));
  
  while (lineLength != -1)
    {
      // The line is now in buffer, and we want to convert
      // it to Unicode by setting it in a QString.
      line = buffer;
      
      if (line.startsWith ('#'))
      {
        lineLength = file.readLine (buffer, sizeof (buffer));
        continue;
      }
      
      QStringList stringList = line.split (">", QString::SkipEmptyParts);
      
      if (!stringList.size () == 2)
      {
        QMessageBox::warning (this,
                        tr ("Sequence Manipulation Tools Plugin"),
                        tr ("Failed to Load Dictionary."),
                        QMessageBox::Ok);
        
        m_dictionaryLoaded = false;
        file.close ();
        return;
      }
      
      if (stringList.last ().endsWith ("\r\n"))
      stringList.last ().chop(2);
      else if (stringList.last ().endsWith ('\n'))
      stringList.last ().chop(1);

      if (stringList.first ().contains (QRegExp ("[0-9]")))
      {
        bool ok = false;
        m_inputCodeLength = stringList.first ().toInt (&ok, 10);
        
        if (!m_inputCodeLength && !ok)
          {
            QMessageBox::warning (this,
                            tr ("Sequence Manipulation Tools Plugin"),
                            tr ("Failed to Load Dictionary."),
                            QMessageBox::Ok);
            
            m_dictionaryLoaded = false;
            file.close ();
            return;
          }
        
        ok = false;

        m_outputCodeLength = stringList.last ().toInt (&ok, 10);
        
        if (!m_outputCodeLength && !ok)
          {
            QMessageBox::warning (this,
                            tr ("Sequence Manipulation Tools Plugin"),
                            tr ("Failed to Load Dictionary."),
                            QMessageBox::Ok);
            
            m_dictionaryLoaded = false;
            file.close ();
            return;
          }

        lineLength = file.readLine (buffer, sizeof (buffer));

        continue;
      }
      
      m_dictionaryHash.insert (stringList.first (), stringList.last ());

      lineLength = file.readLine (buffer, sizeof (buffer));
    }
  
  file.close ();
  
//   qDebug () << __FILE__ << __LINE__ 
//        << "input/output code lengths" 
//        << m_inputCodeLength << "/" << m_outputCodeLength;
  
  if (m_dictionaryHash.size ())
    m_dictionaryLoaded = true;
  else
    {
      QMessageBox::warning (this,
                      tr ("Sequence Manipulation Tools Plugin"),
                      tr ("Failed to Load Dictionary."),
                      QMessageBox::Ok);
      
      m_dictionaryLoaded = false;
      return;
    }
  
//   qDebug () << "done loadDictionary";
}


void 
SeqToolsPluginDlg::execute ()
{
  // What's the task to be performed?

  QString comboText = mp_actionComboBox->currentText ();
  
  if (comboText == tr ("To Upper Case"))
    {
      QString docText = mp_inputEditor->toPlainText ();
      
      docText = docText.toUpper ();
      
      mp_outputEditor->clear ();

      mp_outputEditor->setPlainText (docText);
    }
  else if (comboText == tr ("To Lower Case"))
    {
      QString docText = mp_inputEditor->toPlainText ();
      
      docText = docText.toLower ();
      
      mp_outputEditor->clear ();

      mp_outputEditor->setPlainText (docText);
    }
  else if (comboText == tr ("Dictionary-based Conversion"))
    {
      if (!m_dictionaryLoaded)
      {
        QMessageBox::warning (this,
                        tr ("Sequence Manipulation Tools Plugin"),
                        tr ("Please Load A Dictionary First."),
                        QMessageBox::Ok);
        return;
      }
      
      QString docText = mp_inputEditor->toPlainText ();
      
      QStringList codeList;
      
      if (!makeCodeList (docText, codeList))
      {
        QMessageBox::warning (this,
                        tr ("Sequence Manipulation Tools Plugin"),
                        tr ("Failed To Parse The Input Sequence."),
                        QMessageBox::Ok);
        return;
      }
      
      for (int iter = 0; iter < codeList.size (); iter++)
      {
        QString curString = codeList.at (iter);
        
        QHash<QString, QString>::const_iterator hashIter = 
          m_dictionaryHash.find (curString);

        if (hashIter != m_dictionaryHash.end ())
          codeList.replace (iter, hashIter.value ());
        //curString = hashIter.value ();
        else
          {
            QMessageBox::warning (this,
                            tr ("Sequence Manipulation Tools Plugin"),
                            tr ("Failed To Parse The Input Sequence."),
                            QMessageBox::Ok);
            return;
          }
      }
      
      mp_outputEditor->clear ();

      docText.clear ();
      
      for (int iter = 0; iter < codeList.size (); iter++)
      docText += codeList.at (iter);
      
      mp_outputEditor->setPlainText (docText);
    }
}


int
SeqToolsPluginDlg::makeCodeList (QString &text, QStringList &stringList)
{
  int index = 0;
  int ret = -1;
  QString code;
  
  ret = nextCode (text, &code, &index);
  
  while (1)
    {
      if (ret < 0)
      {
        // There was an error in the parsed code. Store the index.
        break;
      }
      
      if (ret == 0)
      break;
      
      stringList.append (code);
      
      index++;
      ret = nextCode (text, &code, &index);
    }
  // End of 
  // while (1)
  
  if (ret == -1)
    return -1;
  
//   qDebug () << stringList;
  
  return stringList.size ();
}


int
SeqToolsPluginDlg::nextCode (QString &text, QString *code, int *index)
{
  QString newCode;
  int iter = 0;
  
  // We get a sequence of monomer codes (like "LysArgGlu" for example)
  // and we have to return the next code starting from *index. Note
  // that the sequence must not contain invalid characters. The
  // invalid characters might be placed in err for further scrutiny by
  // the caller.

  // Returns the count of actually parsed characters in the string
  // newCode (copied to 'code' param). If an error occurs -1 is
  // returned and the faulty character is copied in 'err'. 'index' is
  // updated with the index of the last valid character parsed for
  // current code.

  Q_ASSERT (code);
  Q_ASSERT (index);

  code->clear ();
  
  int length = text.length ();
  
  while (1)
    {
      if (iter >= m_inputCodeLength)
      {
        iter--;
        break;
      }
      
      if (iter + *index >= length)
      break;
      
      QChar curChar = text.at (iter + *index);
      
      if (!curChar.isLetter ())
      {
//      qDebug () << __FILE__ << __LINE__
//                  << "The character is not a letter:"
//              << curChar;
        
        iter--;
        
        return -1;
      }
      
      bool isLower = (curChar.category () == QChar::Letter_Lowercase);
      
      if (iter == 0)
      {
        if (isLower)
          {
//          qDebug () << "First character of monomer code might not be"
//                << "lower case; sequence is"
//                << m_monomerText.toAscii ();
            
            return -1;
          }
        else
          {
            // Good, first char is uppercase.
            newCode += curChar;
          }
      }
      else // (iter != 0)
      {
        // We are not in our first iteration. So either the current
        // character is lowercase and we are just continuing to
        // iterate into a multi-char monomer code, or the current
        // character is uppercase, in which case we are starting to
        // iterate in a new monomer code.

        if (isLower)
          newCode += curChar;
        else
          {
            // Decrement iter, because this round was for nothing:
            // we had "invaded" the next monomer code in sequence,
            // which we must not do.

            iter --;
            break;
          }
      }

      iter++;
    }
  
  // We finished parsing at most codeLength characters out of
  // 'm_monomerText', so we have a valid code in the 'code' variable. We
  // can also compute a new index position in the sequence and return
  // the number of characters that we effectively parsed. Note that
  // the caller will be responsible for incrementing the 'index' value
  // by one character unit so as not to reparse the last characters of
  // the sent 'code' object.

  *index = *index + iter;
  *code = newCode;
  
  return code->length ();
}

Generated by  Doxygen 1.6.0   Back to index