File: monomerDictionary.cpp

package info (click to toggle)
massxpert 2.3.6-1squeeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 20,736 kB
  • ctags: 3,541
  • sloc: cpp: 44,108; xml: 7,381; sh: 604; makefile: 108; ansic: 7
file content (461 lines) | stat: -rw-r--r-- 11,788 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* massXpert - the true massist's program.
   --------------------------------------
   Copyright(C) 2006,2007 Filippo Rusconi

   http://www.massxpert.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.
*/


/////////////////////// Qt includes
#include <QObject>


/////////////////////// Local includes
#include "monomerDictionary.hpp"
#include "sequence.hpp"


namespace massXpert
{

  MonomerDictionary::MonomerDictionary(QString filePath,
				       const QStringList &inputChainStringList,
				       int inputCodeLength,
				       int outputCodeLength)
    : m_filePath(filePath), m_inputChainStringList(inputChainStringList), 
      m_inputCodeLength(inputCodeLength), m_outputCodeLength(outputCodeLength)
  {
  }

  
  //! Destroys the cleavage rule.
  MonomerDictionary::~MonomerDictionary()
  {
  }
  
  void 
  MonomerDictionary::setFilePath(QString &filePath)
  {
    m_filePath = filePath;
  }
  
  void 
  MonomerDictionary::setInputChainStringList(const QStringList &inputChainStringList)
  {
    m_inputChainStringList = inputChainStringList;
  }
  
  void 
  MonomerDictionary::setInputCodeLength(int codeLength)
  {
    m_inputCodeLength = codeLength;
  }
  
  void 
  MonomerDictionary::setOutputCodeLength(int codeLength)
  {
    m_outputCodeLength = codeLength;
  }
  
  bool 
  MonomerDictionary::isLineProperSectionDivider(const QString &line)
  {
    // Section dividers in the monomer dictionary file format are
    // lines containing the following syntax: X>Y, that is for example
    // 3>1. This means that the following conversion rules (like
    // ILE>I) should convert 3-letter codes into 1-letter codes.

    // However, this line should only be considered proper if X is
    // actually the value of m_inputCodeLength and Y the value of
    // m_outputCodeLength.

//     qDebug() << __FILE__ << __LINE__
// 	     << "Checking if line is proper section divider :" << line;
    
    if (line.contains(QRegExp ("[0-9]+>[0-9]+")))
      {
	// We are opening a new section, get the input/output code
	// lengths and if they math what we expect, then set the
	// current stream position and call the section parser.

	int greaterThanIndex = line.indexOf('>');
	    
	QString codeLengthString = line.left(greaterThanIndex);

// 	qDebug() << __FILE__ << __LINE__
// 		 << "Left codeLengthString:" << codeLengthString
// 		 << "m_inputCodeLength:" << m_inputCodeLength;

	bool ok = false;
	int codeLength = codeLengthString.toInt(&ok, 10);
	    
	if (!codeLength && !ok)
	  {
	    qDebug() << __FILE__ << __LINE__
		     << "Monomer dictionary"
		     << "Failed to parse file " << m_filePath
		     << "at line " << line;
		
	    return false;
	  }
	    
	if (codeLength != m_inputCodeLength)
	  {
	    return false;
	  }
	    
	codeLengthString = line.mid(greaterThanIndex + 1, -1);

// 	qDebug() << __FILE__ << __LINE__
// 		 << "Right codeLengthString:" << codeLengthString
// 		 << "m_outputCodeLength:" << m_outputCodeLength;
	
	ok = false;
	codeLength = codeLengthString.toInt(&ok, 10);
	    
	if (!codeLength && !ok)
	  {
	    qDebug() << __FILE__ << __LINE__
		     << "Monomer dictionary"
		     << "Failed to parse file " << m_filePath
		     << "at line " << line;
		
	    return false;
	  }

	if (codeLength != m_outputCodeLength)
	  {
	    return false;
	  }
	    
	// At this point, it seems we are in the proper
	// section. 

	return true;
      }

    // If we are here, that means that the section is not for us.

//     qDebug() << __FILE__ << __LINE__
// 	     << "Line is no proper section divider.";
    
    return false;
  }
  

  void 
  MonomerDictionary::skipSection(QTextStream *stream)
  {
    // We have entered a section, all we have to do is go through it
    // and return when we have found either the end of the stream or
    // the {END} marker.

    qint64 lineLength = 1024;
    QString line;
    
    while (!stream->atEnd())
      {
	line = stream->readLine(lineLength);
	
	if (!line.contains("{END}"))
	  {
	    line = stream->readLine(lineLength);
	  }
	else
	  return;
      }
  }
  

  int 
  MonomerDictionary::parseSection(QTextStream *stream)
  {
    Q_ASSERT(stream);
    
    qint64 lineLength = 1024;
    QString line;

    // Iterate in the file using the stream and for each line create
    // an item to insert into the dictionary hash.

    while (!stream->atEnd())
      {
	line = stream->readLine(lineLength);
            
	// We might encounter the end of the section, that is a line
	// having {END} as its sole content.
	
	if (line.contains("{END}"))
	  break;
	
	QStringList stringList = line.split('>');

	QString inputCode = stringList.first ();
	QString outputCode = stringList.last ();
	
	// Check that the monomer codes have the proper length.
	
	if (inputCode.length() != m_inputCodeLength ||
	    outputCode.length() != m_outputCodeLength)
	  {
	    qDebug() << __FILE__ << __LINE__
		     << QObject::tr("Monomer dictionary:")
		     << QObject::tr("Failed to load dictionary.")
		     << QObject::tr("Monomer code lengths do not match:")
		     << QObject::tr("inputCode:") << inputCode
		     << QObject::tr("outputCode:") << outputCode;
	    
	
	    // We have to empty the hash
	    m_dictionaryHash.clear();
	    
	    break;
	  }
	
	m_dictionaryHash.insert (inputCode, outputCode);

	// 	qDebug() << __FILE__ << __LINE__
	// 		 << stringList.first () << stringList.last ();	
      }
    
    // At this point the parsing is finished, either because we
    // encountered the {END} section-ending delimiter, or because we
    // reached the en of file.

    int hashSize = m_dictionaryHash.size();
    
    if (hashSize)
      m_dictionaryLoaded = true;
    else
      {
	qDebug() << __FILE__ << __LINE__
		 << QObject::tr ("Monomer dictionary:")
		 << QObject::tr ("Failed to load dictionary.");
	
	m_dictionaryLoaded = false;
      }
    
    return hashSize;
  }
  
  
  bool
  MonomerDictionary::loadDictionary ()
  {
    // 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.
    bool success = true;
    qint64 lineLength = 1024;
    QString line;
  
    QFile file(m_filePath);

    if (!file.open(QIODevice::ReadOnly)) 
      {
	
	m_dictionaryLoaded = false;
	
	qDebug() << __FILE__ << __LINE__
		 << "Monomer dictionary:"
		 << "Failed to open file" << m_filePath
		 << "for writing.";
	
	return false;
      }

    if (m_inputCodeLength < 1 || m_outputCodeLength < 1)
      {
	qDebug() << __FILE__ << __LINE__
		 << "Monomer dictionary:"
		 << "Failed to parse file " << m_filePath
		 << "Please, set the m_inputCodeLength and "
	  "m_ouputCodeLength variables first.";
	
	return false;
      }
    
    QTextStream *stream = new QTextStream(&file);
    stream->setCodec("UTF-8");

    while (!stream->atEnd())
      {
	line = stream->readLine(lineLength);

	// 	  qDebug() << __FILE__ << __LINE__
	// 		   << "line: " << line;
 
	// Remove spaces from start and end of line.
	line = line.simplified();
	
	if (line.startsWith ('#') || line.isEmpty())
	  {
	    line = stream->readLine(lineLength);
	    continue;
	  }

	// There might be any number of sections in the file, all
	// delimited with a X>Y directive, indicating how many
	// characters are allowed for the input code and for the
	// output code.

	if (!isLineProperSectionDivider(line))
	  {
// 	    qDebug() << __FILE__ << __LINE__
// 		     << "skipping line:" << line;

	    line = stream->readLine(lineLength);
	    continue;
	  }
	else
	  {
// 	    qDebug() << __FILE__ << __LINE__
// 		     << "parsing section: " << line;

	    if (parseSection(stream) < 1)
	      {
		qDebug() << __FILE__ << __LINE__
			 << "Monomer dictionary:"
			 << "Failed to parse file " << m_filePath;
		  
		success = false;
		break;
	      }
	    else
	      {
		// We successfully parsed the section. Our work is done.
		
		success = true;
		break;
	      }
	      
	  }
      }
      
    delete stream;
      
    return success;
  }


  QStringList *
  MonomerDictionary::translate(const QStringList &chainStringList)
  {
    // The string in sequence is a space-separated list of monomer
    // codes in the original monomer code format. We have to translate
    // that to the proper monomer code format using the hash in this
    // dictionary.

    QStringList *outputChainStringList = new QStringList();
    
    if (!chainStringList.isEmpty())
      m_inputChainStringList = chainStringList;
    
    // If there is nothing to do return an empty string list so that
    // caller knows nothing is actually wrong, only there is no
    // sequence to translate.
    if (m_inputChainStringList.isEmpty())
      return outputChainStringList;
    
    // Iterate in each chain string of the list and perform the
    // translation.

    for (int iter = 0; iter < m_inputChainStringList.size(); ++iter)
      {
	QString iterString = chainStringList.at(iter);
	
// 	qDebug() << __FILE__ << __LINE__
// 		 << "translating sequence:" << iterString;
		
	QStringList codeList =
	  iterString.split(QRegExp("\\s+"), QString::SkipEmptyParts);
	
// 	qDebug() << __FILE__ << __LINE__ 
// 		 << "codeList:" << codeList;

// 	qDebug() << __FILE__ << __LINE__ 
// 		 << "hash:"
// 		 << m_dictionaryHash;
	
	for (int jter = 0; jter < codeList.size(); ++jter)
	  {
	    QString code = codeList.at(jter);

	    QHash<QString, QString>::const_iterator hashIter = 
	      m_dictionaryHash.find(code);
	
	    if (hashIter != m_dictionaryHash.end())
	      codeList.replace(jter, hashIter.value());
	    else
	      {
		// Delete the string list, set the pointer to 0 and
		// return that pointer so that caller knows something
		// has gone wrong.
		
		qDebug() << __FILE__ << __LINE__
			 << "Monomer dictionary:"
			 << "Failed to convert monomer code "
			 << code;
	    
		outputChainStringList->clear();

		delete outputChainStringList;
		outputChainStringList = 0;

		return outputChainStringList;
	      }
	  }
 
	// At this point the sequence codes have been translated. Join all
	// the item of the codeList into one single string.

	outputChainStringList->append(codeList.join(QString("")));
      }
    
    // End of
    // for (int iter = 0; iter < chainStringList.size(); ++iter)
    
    // If no translation could be performed, return a n
    
    if (!outputChainStringList->size())
      {
	outputChainStringList->clear();

	delete outputChainStringList;
	outputChainStringList = 0;
      }
    
    return outputChainStringList;
  }
  
  
} // namespace massXpert