Ymir  .9
Fast\C++toolforcomputationofassemblingprobabilities,statisticalinferenceofassemblingstatisticalmodelandgenerationofartificialsequencesofT-cellreceptorsdata.
repertoire.h
1 /*
2  * Ymir <imminfo.github.io/ymir>
3  *
4  * This file is part of Ymir, a fast C++ tool for computation of assembling
5  * probabilities, statistical inference of assembling statistical model
6  * and generation of artificial sequences of T-cell receptors data.
7  *
8  *
9  * Copyright 2015 Vadim Nazarov <vdn at mailbox dot com>
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 
24 #ifndef _REPERTOIRE_H_
25 #define _REPERTOIRE_H_
26 
27 
28 #include <memory>
29 
30 #include "aligner.h"
31 #include "clonotype.h"
32 #include "maag.h"
33 #include "tools.h"
34 
35 
36 namespace ymir {
37 
38  #define DEFAULT_REPERTOIRE_RESERVE_SIZE 300000
39 
40 
41  typedef std::vector<MAAG> MAAGRepertoire;
42 
43 
44  // typedef std::vector<ClonotypePtr> ClonotypeVector;
45  typedef std::vector<Clonotype> ClonotypeVector;
46 
47 
48  typedef std::shared_ptr<ClonotypeVector> SharedClonotypeVectorPtr;
49 
50 
51  class ClonesetView {
52 
53  public:
54 
55  ClonesetView()
56  : _source(new ClonotypeVector())
57  {
58  }
59 
60 
61  ClonesetView(SharedClonotypeVectorPtr pvec,
62  const std::vector<size_t>& shifts)
63  : _source(pvec), _shifts(shifts)
64  {
65  }
66 
67 
69  const Clonotype& operator[] (size_t index) const {
70  return _source->at(_shifts[index]);
71  }
72 
73  ClonesetView subvec(const std::vector<size_t> &indices) const {
74  std::vector<size_t> shifts;
75  shifts.reserve(indices.size());
76  for (size_t i = 0; i < indices.size(); ++i) {
77  shifts.push_back(_shifts[indices[i]]);
78  }
79  return ClonesetView(_source, shifts);
80  };
82 
83 
84  size_t size() const { return _shifts.size(); }
85 
86 
87  ClonesetView coding() const {
88  std::vector<size_t> inds;
89  inds.reserve(this->size() / 3);
90  for (size_t i = 0; i < this->size(); ++i) {
91  if ((*this)[i].isCoding()) {
92  inds.push_back(i);
93  }
94  }
95  return this->subvec(inds);
96  }
97 
98 
99  ClonesetView noncoding(bool out_of_frames_only = false) const {
100  std::vector<size_t> inds;
101  inds.reserve(this->size() / 3);
102  if (out_of_frames_only) {
103  for (size_t i = 0; i < this->size(); ++i) {
104  if ((*this)[i].isOutOfFrame()) {
105  inds.push_back(i);
106  }
107  }
108  } else {
109  for (size_t i = 0; i < this->size(); ++i) {
110  if ((*this)[i].isNoncoding()) {
111  inds.push_back(i);
112  }
113  }
114  }
115 
116  return this->subvec(inds);
117  }
118 
119 
120  ClonesetView head(size_t size) const {
121  std::vector<size_t> shifts;
122  shifts.reserve(size);
123  for (size_t i = 0; i < size; ++i) {
124  shifts.push_back(i);
125  }
126  return this->subvec(shifts);
127  };
128 
129 
130  ClonesetView slice(size_t start, size_t end) const {
131  if (end > start) {
132  std::vector<size_t> shifts;
133  shifts.reserve(end - start + 1);
134  for (size_t i = start; i < end; ++i) {
135  shifts.push_back(i);
136  }
137  return this->subvec(shifts);
138  } else {
139  cerr << "Error in ClonesetView: the end index is lower than the start index." << endl;
140  return ClonesetView();
141  }
142  };
143 
144 
145  ClonesetView shuffle() {
146  std::default_random_engine generator;
147  std::uniform_int_distribution<size_t> distribution(0, this->size());
148 
149  std::vector<size_t> indices(this->size());
150  for (size_t i = 0; i < this->size(); ++i) {
151  indices[i] = distribution(generator);
152  }
153 
154  return this->subvec(indices);
155  }
156 
157 
158  ClonesetView sample(size_t n) {
159  return this->shuffle().head(n);
160  }
161 
162 
163  protected:
164 
165  SharedClonotypeVectorPtr _source;
166  std::vector<size_t> _shifts;
167 
168  };
169 
170 
171  class Cloneset : public ClonesetView {
172 
173  public:
174 
175 
176  Cloneset() : ClonesetView()
177  {
178  }
179 
180 
181  // swap constructor
182  Cloneset(ClonotypeVector& vec) {
183  this->swap(vec);
184  }
185 
186 
187  void swap(ClonotypeVector& vec) {
188  this->_source->swap(vec);
189  this->_shifts.resize(this->_source->size());
190  for (size_t i = 0; i < this->_source->size(); ++i) {
191  this->_shifts[i] = i;
192  }
193  }
194 
195 
196  Clonotype& operator[] (size_t index) {
197  return _source->at(_shifts[index]);
198  }
199 
200 
201  protected:
202 
203  };
204 
205 }
206 
207 #endif
Definition: aligner.h:37
Definition: clonotype.h:46
Definition: repertoire.h:171
Definition: repertoire.h:51