Ymir  .9
Fast\C++toolforcomputationofassemblingprobabilities,statisticalinferenceofassemblingstatisticalmodelandgenerationofartificialsequencesofT-cellreceptorsdata.
writer.h
1 //
2 // Created by Vadim N. on 07/08/2015.
3 //
4 
5 #ifndef YMIR_WRITER_H
6 #define YMIR_WRITER_H
7 
8 
9 #include <ostream>
10 
11 #include "repertoire.h"
12 #include "genesegment.h"
13 
14 
15 namespace ymir {
16 
17  #define CELL_FILL(i, limit, ofs, if_letter, else_letter) { if (i < limit - 1) { ofs << if_letter; } else { ofs << else_letter; } }
18 
20  public:
21 
22 
23  RepertoireWriter() { }
24 
25 
26  virtual ~RepertoireWriter() { }
27 
28 
29  bool write(const std::string &filepath,
30  const ClonesetView &cloneset,
31  const VDJRecombinationGenes& gene_segments,
32  bool append = false) const {
33  std::ofstream ofs;
34 
35  ofs.open(filepath, std::ofstream::out | std::ofstream::app);
36 
37  if (ofs.is_open()) {
38 
39  if (!append) {
40  // write the header
41  ofs << "Nucleotide sequence" << '\t' <<
42  "Amino acid sequence" << '\t' <<
43  "Variable" << '\t' <<
44  "Diversity" << '\t' <<
45  "Joining" << '\t' <<
46  "V alignment" << '\t' <<
47  "D alignment" << '\t' <<
48  "J alignment" << std::endl;
49  }
50 
51  // write clonotypes
52  for (auto i = 0; i < cloneset.size(); ++i) {
53  if (cloneset[i].sequence_type() == NUCLEOTIDE) {
54  ofs << cloneset[i].sequence() << '\t';
55  ofs << translate(cloneset[i].sequence()) << '\t';
56  } else {
57  ofs << "" << '\t';
58  ofs << cloneset[i].sequence() << '\t';
59  }
60 
61  for (auto seg_i = 0; seg_i < cloneset[i].nVar(); ++seg_i) {
62  ofs << gene_segments.V()[cloneset[i].getVar(seg_i)].allele;
63  CELL_FILL(seg_i, cloneset[i].nVar(), ofs, ',', '\t')
64  }
65 
66  if (gene_segments.is_vdj()) {
67  for (auto seg_i = 0; seg_i < cloneset[i].nDiv(); ++seg_i) {
68  ofs << gene_segments.D()[cloneset[i].getDiv(seg_i)].allele;
69  CELL_FILL(seg_i, cloneset[i].nDiv(), ofs, ',', '\t')
70  }
71  } else {
72  ofs << "\t";
73  }
74 
75  for (auto seg_i = 0; seg_i < cloneset[i].nJoi(); ++seg_i) {
76  ofs << gene_segments.J()[cloneset[i].getJoi(seg_i)].allele;
77  CELL_FILL(seg_i, cloneset[i].nJoi(), ofs, ',', '\t')
78  }
79 
80  for (auto seg_i = 0; seg_i < cloneset[i].nVar(); ++seg_i) {
81  ofs << cloneset[i].getVarGeneStart(seg_i) << "|";
82  ofs << cloneset[i].getVarSeqStart(seg_i) << "|";
83  ofs << cloneset[i].getVarLen(seg_i);
84  CELL_FILL(seg_i, cloneset[i].nVar(), ofs, ',', '\t')
85  }
86 
87  if (gene_segments.is_vdj()) {
88  //
89  // TODO: make so every D alignment will show up in the output file
90  //
91  for (auto seg_i = 0; seg_i < cloneset[i].nDiv(); ++seg_i) {
92  ofs << cloneset[i].getDivGeneStart(seg_i, 0) << "|";
93  ofs << cloneset[i].getDivSeqStart(seg_i, 0) << "|";
94  ofs << cloneset[i].getDivLen(seg_i, 0);
95  CELL_FILL(seg_i, cloneset[i].nDiv(), ofs, ';', '\t')
96  }
97  } else {
98  ofs << "\t";
99  }
100 
101  for (auto seg_i = 0; seg_i < cloneset[i].nJoi(); ++seg_i) {
102  ofs << cloneset[i].getJoiGeneStart(seg_i) << "|";
103  ofs << cloneset[i].getJoiSeqStart(seg_i) << "|";
104  ofs << cloneset[i].getJoiLen(seg_i);
105  CELL_FILL(seg_i, cloneset[i].nJoi(), ofs, ',', std::endl)
106  }
107  }
108 
109  ofs.close();
110  return true;
111  }
112 
113  return false;
114  }
115 
116  protected:
117 
118 
119  };
120 }
121 
122 
123 #endif //YMIR_WRITER_H
Definition: aligner.h:37
Definition: writer.h:19
Definition: genesegment.h:265
Definition: repertoire.h:51