File: DNAVector.h

package info (click to toggle)
trinityrnaseq 2.15.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 468,004 kB
  • sloc: perl: 49,905; cpp: 17,993; java: 12,489; python: 3,282; sh: 1,989; ansic: 985; makefile: 717; xml: 62
file content (707 lines) | stat: -rw-r--r-- 16,163 bytes parent folder | download | duplicates (5)
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#ifndef _DNAVECTOR_H_
#define _DNAVECTOR_H_



#include "base/SVector.h"
#include "base/FileParser.h"
#include "analysis/AACodons.h"
#include <iostream>
#include <map>
#include <fstream>
#include <string.h>
#include <algorithm>
#include <sstream>

typedef svec<char> NumVector;
typedef svec<NumVector> vecNumVector;

double DNA_A(char l);
double DNA_C(char l);
double DNA_G(char l);
double DNA_T(char l);

inline double DNA_N(char l, char nuke) {
  switch(nuke) {
  case 'A':
    return DNA_A(l);
  case 'C':
    return DNA_C(l);
  case 'G':
    return DNA_G(l);
  case 'T':
    return DNA_T(l);

  default:
    cout << "ERROR";
    return 0.;
  }

}

// Returns the ambiguous code given all these bases
char GetAmbiguous(const string & bases);


//RC's a base
char GetRC(const char c);

char ResolveAmbiguous(const string & bases);



// Returns 3 ambiguos bases given the amino acid.
// Note: the character buffer needs to be at least 4 bytes long!
void AminoAcidToBases(char * out, char aa);
char BasesToAminoAcid(char * b);

double DNA_Equal(char letter1, char letter2);
double DNA_EqualAmb(char letter1, char letter2);

double DNA_EqualEmph(char letter1, char letter2);

double DNA_Diff(char letter1, char letter2);


const char plain_table[] = 
{
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0-9
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //10-19
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //20-29
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //30-39
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //40-49
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //50-59
  0, 0, 0, 0, 0, 1, 0, 2, 0, 0, //60-69
  0, 3, 0, 0, 0, 0, 0, 0, 5, 0, //70-79
  0, 0, 0, 0, 4, 0, 0, 0, 0, 0, //80-89
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //90-99
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //100-119
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //110-129
  
  

};

const char plain_table_aa[] = 
{
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0-9
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //10-19
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //20-29
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //30-39
  0, 0, 22, 0, 0, 0, 0, 0, 0, 0, //40-49
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //50-59
  0, 0, 0, 0, 0, 1, 0, 2, 3, 4, //60-69
  5, 6, 7, 8, 0, 9, 10, 11, 12, 0, //70-79
  13, 14, 15, 16, 17, 0, 18, 19, 0, 20, //80-89
  21, 0, 0, 0, 0, 0, 0, 0, 0, 0, //90-99
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //100-119
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //110-129
    

};

const char plain_table_aa_inverse[] =
{
  'A',  'C',  'D',  'E',
  'F',  'G',  'H',  'I',
  'K',  'L',  'M',  'N',
  'P',  'Q',  'R',  'S',
  'T',  'V',  'W',  'Y',
  '*'
};


inline int NucIndex(char letter) 
{
  return (plain_table[(int)letter] - 1);
}

inline int AminoAcidIndex(char letter) 
{
  return (plain_table_aa[(int)letter] - 1);
}

inline int AminoAcidLetter(char aa) 
{
  return (plain_table_aa_inverse[(int)aa]);
}



inline char NucLetter(int index) 
{
  switch(index) {
  case 0:
    return 'A';
  case 1:
    return 'C';
  case 2:
    return 'G';
  case 3:
    return 'T';
  default:
    return 'N';
  }
}

inline double DNA_EqualFast(char letter1, char letter2)
{
  if (plain_table[(int)letter1] > 0 && plain_table[(int)letter2] > 0) {
    //cout << letter1 << letter2 << endl;
    if (letter1 != 'N' && letter2 != 'N') {
      if (letter1 == letter2) {
	return 1.;
      } else {
	return 0.;
      }
    } else {
      return 0.;
    }
  }
  return DNA_Equal(letter1, letter2);
}

class DNACodec
{
 public:
  DNACodec();
  double A_Letter(int l) const {
    return m_A[l];
  }
  double C_Letter(int l) const {
    return m_C[l];
  }
  double G_Letter(int l) const {
    return m_G[l];
  }
  double T_Letter(int l) const {
    return m_T[l];
  }

  char GetRC(int l) const {
    return m_rc[l];
  }

 private:

  void Set(int letter, double a, double c, double g, double t, int rc);

  double m_A[256];
  double m_C[256];
  double m_G[256];
  double m_T[256];

  char m_rc[256];

};

//=================================================
class AAAmb
{
public:
  AAAmb() {}


  const string & Base(int i) const {return m_bases[i];}

  void Add(const string & b) {
    const char * p = b.c_str();
    char tmp[16];
    tmp[1] = 0;
    int i;
    for (i=0; i<3; i++) {
      tmp[0] = p[i];
      m_bases[i] += tmp;
    }
  }

private:
  string m_bases[3];
};


class DNAVector;


//==========================================================

class DNAVector
{
 public:
  DNAVector() {}
  ~DNAVector() {}

  void resize(int n) {
    m_data.resize(n, 0);
    if (m_qual.isize() > 0)
      m_qual.resize(n, 0);
  }

  void resize(int n, char c) {
    m_data.resize(n, c);
    if (m_qual.isize() > 0)
      m_qual.resize(n, 0);
  }

  //void Compact();

  void SetToSubOf(const DNAVector & v, int start, int len);

  void ReverseComplement();
  //ML: ReverseComplement for sequences stored as std::string
  static void ReverseComplement(string & seq);

  const char & operator [] (int i) const {
    return m_data[i];
  }
  char & operator [] (int i) {
    return m_data[i];
  }

  int Qual(int i) const {
    if (m_qual.isize() == 0)
      return 0;
    return m_qual[i];
  }

  int QualSize() const {return m_qual.isize();}

  void Set(int i, char c) {
    m_data[i] = c;
  }

  void SetFromBases(const string & s);

  void SetFromProteins(const string & s);

  void Proteinize();

  int size() const {return m_data.isize();}
  int isize() const {return m_data.isize();}
  long long lsize() const {return m_data.lsize();}
  
  void SetQual(int i, int score) {
    if (m_qual.isize() == 0)
      m_qual.resize(m_data.isize(), 0);
    m_qual[i] = score;
  }


  void Write(FILE * p) const;
  void Write(ostream &s) const;
  void WriteOneLine(ostream &s) const;
  void WriteQual(FILE * p) const;
  void WriteQual(ostream &s) const;

  void ToUpper();

  void operator += (const DNAVector & d) {
    int n = isize();
    resize(n+d.isize());
    for (int i=0; i<d.isize(); i++)
      (*this)[i+n] = d[i];
  }


  bool Append(const DNAVector & d, int min, int max, double ident);

  void clear() {
    m_data.clear();
    m_qual.clear();
  }

  bool operator == (const DNAVector & d) const {
    int i;
    if (isize() != d.isize())
      return false;
    for (i=0; i<isize(); i++) {
      if (m_data[i] != d[i])
	return false;
    }
    return true;
  }

  bool operator != (const DNAVector & d) const {
    return !(*this == d);
  }

  bool operator < (const DNAVector & d) const {
    int i;
    int n = m_data.isize();
    if (d.isize() < n)
      n = d.isize();

    for (i=0; i<n; i++) {
      if (m_data[i] == d[i])
	continue;
      if (m_data[i] < d[i]) {	
	//cout << "1" << endl;
	return true;
      }
      if (m_data[i] > d[i]) {
	//cout << "2" << endl;
	return false;
      }
    } 

    if (m_data.isize() < d.isize()) {
      //cout << "3" << endl;
      return true;
    }
    //cout << "4" << endl;
    return false;
  }

  void Print(ostream &o) const {
    for(int i=0; i<m_data.isize(); i++) o << m_data[i];
  }

  void Print() const {
    for(int i=0; i<m_data.isize(); i++) cout << m_data[i];
  }


  string AsString() const {
    string s;
    for(unsigned int i=0; i<m_data.size(); i++) s.push_back(m_data[i]);
    return s;
  }
  
  void Substring(string & s, int start, int length) const {
    s.resize(length);
    for(int i=0; i<length; i++) 
      s[i] = m_data[start+i];
  }


  string Substring(int start, int length) const {
    string s;    
    for(int i=0; i<length; i++) s.push_back(m_data[start+i]);
    return s;
  }


  //char * Data() {return &m_data[0]};
  //unsigned char * DataQual() {return &m_qual[0]};

  svec<char> & Data() {return m_data;}
 


 private:
  svec<char> m_data;
  svec<int> m_qual;
};


class vecDNAVector
{
 public:
  vecDNAVector();
  vecDNAVector(const vecDNAVector &other);
  ~vecDNAVector();

  vecDNAVector &operator = (const vecDNAVector &other);

  const DNAVector & operator [] (int i) const;
  DNAVector & operator [] (int i);
  const DNAVector &operator () (const string &name) const;
  DNAVector &operator () (const string &name);

  void setMaxSeqsToRead(long max_seq_count);


  class DNAVectorRef;
  class const_DNAVectorRef;

  /* getDNAVectorRef
   * -------------------
   * Returns a DNAVectorRef object pointing to the DNAVector at index 'i'
   * / with the name 'name'.
   */
  DNAVectorRef getDNAVectorRef(int i);
  DNAVectorRef getDNAVectorRef(const string &name);
  const_DNAVectorRef getDNAVectorRef(int i) const;
  const_DNAVectorRef getDNAVectorRef(const string &name) const;

  void resize(int n);
  void clear();

  int NameIndex(const string & name) const;
  const string & Name(size_t i) const;
  const char * NameClean(size_t i) const;

  void SetName(size_t i, const string & s);

  void push_back(const DNAVector & v);
  void push_back(const DNAVector & v, const string & name);

  /* erase
   * -------------
   * Erases the DNAVector at the index 'index'.
   *
   * Note that this involves an ~O(n) operation internally.
   * If you do not care about the element order and n is large,
   * fast_erase would be a better choice.
   */
  void erase(size_t index);

  /* erase
   * -------------
   * Erases the DNAVector with the name 'name', if it exists, and returns true.
   * If there is no DNAVector with that name, returns false.
   *
   * Note that this involves an ~O(n) operation internally.
   * If you do not care about the element order and n is large,
   * fast_erase would be a better choice.
   */
  bool erase(const string &name);

  /* fast_erase
   * -------------
   * Erases the DNAVector at the index 'index'; as a side effect
   * the last element in the vecDNAVector is moved to the vacated index.
   */
  void fast_erase(size_t index);

  /* fast_erase
   * -------------
   * Erases the DNAVector with the name 'name'; as a side effect
   * the last element in the vecDNAVector is moved to the vacated index.
   */
  bool fast_erase(const string &name);


  size_t size() const;
  
  long long totalBases() const;

  void ReadV(const string & file);
  void WriteV(const string & file) const;


  void Write(const string & fileName, bool bSkipEmpty = false) const;
  void WriteQuals(const string & fileName) const;
  int Read(const string & fileName, bool bProteins = false, bool shortName = false, bool allUpper = true, int bigChunk = 200000000);
  void Read(const string & fileName, svec<string> & names);
  /* ReadPartial
   * -----------------
   * Reads a section of a fasta file into the vecDNAVector, starting at the sequence at firstToRead and reading
   * numToRead consecutive sequences.
   */
  void ReadPartial(const string & fileName, unsigned int firstToRead, unsigned int numToRead, bool bProteins = false, bool shortName = false, bool allUpper = true);

  void ReadQuals(const string & fileName);

  void ReverseComplement();

  // The sequence is actually proteins even though given in nucleotide space,
  // so make it ambiguous.
  void DoProteins(bool bKeepProt = false);

  void UniqueSort();

  friend class DNAVectorRef;
  friend class const_DNAVectorRef;

  /* ReferenceListener
   * --------------------
   * Abstract class representing a reference to an internal
   * DNAVector object
   */
  class ReferenceListener {
  public:
	  virtual string name() const = 0;

	  friend class vecDNAVector;
  private:
	  virtual void invalidate() = 0;
  };




  /* DNAVectorRef
   * -----------------
   * A reference to a DNAVector object inside a vecDNAVector.
   * It can be dereferenced just like a normal pointer. It will become invalid
   * if the DNAVector object is removed from the vecDNAVector.
   *
   * (Note that if you simply use a normal pointer, if the vecDNAVector
   * changes internally, for example because a new DNAVector is added,
   * your pointer may become invalid)
   */
  class DNAVectorRef : ReferenceListener {
  public:
	  DNAVectorRef(const DNAVectorRef &toCopy);
	  /* Default Constructor
	   * ------------------------
	   * Generates an invalid reference, pointing nowhere.
	   */
	  DNAVectorRef();
	  ~DNAVectorRef();

	  /* operator ==
	   * -------------------
	   * Returns true if the two references point to the same DNAVector, and false otherwise.
	   */
	  bool operator == (const DNAVectorRef & other) const;
	  bool operator == (const const_DNAVectorRef & other) const;
	  bool operator != (const DNAVectorRef & other) const;
	  bool operator != (const const_DNAVectorRef & other) const;
	  DNAVectorRef &operator = (const DNAVectorRef &other);
	  DNAVector &operator *();
	  DNAVector *operator ->();
	  string name() const;

	  /* isInvalid
	   * ------------
	   * Returns true if the reference is no longer valid, i.e. if
	   * it has been removed from the vecDNAVector.
	   */
	  bool isInvalid() const;

	  friend class const_DNAVectorRef;
	  friend class vecDNAVector;

  private:
	  DNAVectorRef(vecDNAVector * owner, string DNAName);
	  /* invalidate
	   * --------------
	   * Causes the DNAVectorRef to become invalid.
	   */
	  void invalidate();

	  vecDNAVector *owner;
	  string DNAName;
  
  };

  /* const_DNAVectorRef
   * --------------------
   * A const version of DNAVectorRef
   */
  class const_DNAVectorRef : ReferenceListener {
  public:
	  const_DNAVectorRef(const const_DNAVectorRef &toCopy);
	  const_DNAVectorRef(const DNAVectorRef &toCopy);
	  const_DNAVectorRef();
	  ~const_DNAVectorRef();

	  bool operator == (const DNAVectorRef & other) const;
	  bool operator == (const const_DNAVectorRef & other) const;
	  bool operator != (const DNAVectorRef & other) const;
	  bool operator != (const const_DNAVectorRef & other) const;
	  const_DNAVectorRef &operator = (const const_DNAVectorRef &other);
	  const_DNAVectorRef &operator = (const DNAVectorRef &other);
	  const DNAVector &operator *() const;
	  const DNAVector *operator ->() const;
	  string name() const;

	  bool isInvalid() const;

	  friend class DNAVectorRef;
	  friend class vecDNAVector;

  private:
	  const_DNAVectorRef(const vecDNAVector * owner, string DNAName);

	  void invalidate();

	  const vecDNAVector *owner;
	  string DNAName;
  };

 private:
  /* addReference / removeReference
   * --------------------------------
   * Adds / removes the specified ReferenceListener from the list of
   * active references.
   */
  void addReference(ReferenceListener *newReference) const;
  void removeReference(ReferenceListener *toRemove) const;
  /* invalidateReferences
   * ----------------------
   * Invalidates all references to the given DNAVector, if any.
   */
  void invalidateReferences(string toInvalidate);

  void setupMap();

  svec<string> m_names;
  svec<DNAVector> m_data;

  int default_name_index;
  map<string,int> m_name2index;

  FlatFileParser * m_pParser;
  string m_lastName;

  long max_seqs_to_read;
  


  /* references
   * --------------
   * A list of the currently active references to internal DNAVectors.
   */
  mutable map<string, vector<ReferenceListener *> > references;
};



//================================================
// Streaming version of the vecDNAVector...
class vecDNAVectorStream
{
public:
    vecDNAVectorStream() {
        m_pParser = NULL;
        m_lastName = "";
    }
    ~vecDNAVectorStream();

    void ReadStream(const string & fileName);

    const string& getLastName();
    // Returns an empty vector at the end
    const DNAVector & Next();

private:
    FlatFileParser * m_pParser;
    DNAVector m_seq;
    string m_lastName;
};



int CountNs(const DNAVector & d);

// a single base constitutes a >= 'threshold' fraction of the sequence 
bool IsHomopolymer(const DNAVector &d, double threshold=1);


//================================================
//ML: fast reader for fasta files, reads sequences and name into std:string

class DNAStringStreamFast
{
public:
    // open the stream for given fasta file
    void ReadStream(const string & fileName);
    // get next sequence, ignore sequence name, returns an empty string at EOF
    void Next(string & seq);
    // push sequence and name to string vectors, returns false at EOF
    bool NextToVector(vector<string> & seqv, vector<string> & namev);
    static void formatReadNameString(const string & name, string & outname);
private:
    ifstream m_ifs;
    string m_buf;
};



#endif //_DNAVECTOR_H_