sequencesMatrix.cpp
Go to the documentation of this file.
1 /* *****************************************************************************
2 
3  trimAl v2.0: a tool for automated alignment trimming in large-scale
4  phylogenetics analyses.
5 
6  readAl v2.0: a tool for automated alignment conversion among different
7  formats.
8 
9  2009-2019
10  Fernandez-Rodriguez V. (victor.fernandez@bsc.es)
11  Capella-Gutierrez S. (salvador.capella@bsc.es)
12  Gabaldon, T. (tgabaldon@crg.es)
13 
14  This file is part of trimAl/readAl.
15 
16  trimAl/readAl are free software: you can redistribute it and/or modify
17  it under the terms of the GNU General Public License as published by
18  the Free Software Foundation, the last available version.
19 
20  trimAl/readAl are distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  GNU General Public License for more details.
24 
25  You should have received a copy of the GNU General Public License
26  along with trimAl/readAl. If not, see <http://www.gnu.org/licenses/>.
27 
28 ***************************************************************************** */
29 
31 #include "Alignment/sequencesMatrix.h"
32 #include "Alignment/Alignment.h"
33 #include "utils.h"
34 
36  // Create a timer that will report times upon its destruction
37  // which means the end of the current scope.
38  StartTiming("Alignment::sequencesMatrix::sequencesMatrix(void) ");
39 
40  resNumber = 0;
41  seqsNumber = 0;
42 
43  seqsName = nullptr;
44  matrix = nullptr;
45 
46 }
47 
49  // Create a timer that will report times upon its destruction
50  // which means the end of the current scope.
51  StartTiming("Alignment::sequencesMatrix::sequencesMatrix(Alignment *parent) ");
52  alig = parent;
53  int i, j, k;
54 
57 
59 
60  matrix = new int *[seqsNumber];
61  for (i = 0; i < seqsNumber; i++) {
62  matrix[i] = new int[resNumber];
64  }
65 
66  // Determinate the sequence for each alignment specie
67  for (i = 0, k = 1; i < seqsNumber; i++, k = 1) {
68  for (j = 0; j < resNumber; j++) {
69  if (alig->sequences[i][j] != '-') {
70  matrix[i][j] = k;
71  k++;
72  }
73  }
74  }
75 
76 }
77 
78 
79 Alignment::sequencesMatrix::sequencesMatrix(string *alignmentMatrix, string *alignmentSeqsName, int sequences, int residues) {
80  // Create a timer that will report times upon its destruction
81  // which means the end of the current scope.
82  StartTiming("Alignment::sequencesMatrix::sequencesMatrix(string *alignmentMatrix, string *alignmentSeqsName, int sequences, int residues) ");
83  int i, j, k;
84 
85  seqsNumber = sequences;
86  resNumber = residues;
87 
88  seqsName = new string[seqsNumber];
89  for (i = 0; i < seqsNumber; i++)
90  seqsName[i] = alignmentSeqsName[i];
91 
92 
93  matrix = new int *[seqsNumber];
94  for (i = 0; i < seqsNumber; i++) {
95  matrix[i] = new int[resNumber];
97  }
98 
99  // Determinate the sequence for each alignment specie
100  for (i = 0, k = 1; i < seqsNumber; i++, k = 1) {
101  for (j = 0; j < resNumber; j++) {
102  if (alignmentMatrix[i][j] != '-') {
103  matrix[i][j] = k;
104  k++;
105  }
106  }
107  }
108 }
109 
111  int i, j;
112 
113  if (this != &old) {
114 
115  seqsNumber = old.seqsNumber;
116  resNumber = old.resNumber;
117 
118  seqsName = new string[seqsNumber];
119  for (i = 0; i < seqsNumber; i++)
120  seqsName[i] = old.seqsName[i];
121 
122  matrix = new int *[seqsNumber];
123  for (i = 0; i < seqsNumber; i++) {
124  matrix[i] = new int[resNumber];
125  for (j = 0; j < resNumber; j++)
126  matrix[i][j] = old.matrix[i][j];
127  }
128 
129  }
130  return *this;
131 }
132 
134  // Create a timer that will report times upon its destruction
135  // which means the end of the current scope.
136  StartTiming("Alignment::sequencesMatrix::~sequencesMatrix(void) ");
137  int i;
138 
139  if (matrix != nullptr) {
140  for (i = 0; i < seqsNumber; i++)
141  delete [] matrix[i];
142  delete[] matrix;
143  }
144 
145  seqsNumber = 0;
146  resNumber = 0;
147 
148  matrix = nullptr;
149  seqsName = nullptr;
150 }
151 
153  // Create a timer that will report times upon its destruction
154  // which means the end of the current scope.
155  StartTiming("void Alignment::sequencesMatrix::printMatrix(void) ");
156  int i, j, k;
157 
158  for (i = 0; i < resNumber; i += 20) {
159  for (j = 0; j < seqsNumber; j++) {
160  for (k = i; k < (20 + i) && k < resNumber; k++) {
161  cout << setw(4) << matrix[j][k] << " ";
162  }
163  cout << endl;
164  }
165  cout << endl;
166  }
167 }
168 
169 void Alignment::sequencesMatrix::getColumn(int column, int *columnSeqMatrix) {
170  // Create a timer that will report times upon its destruction
171  // which means the end of the current scope.
172  StartTiming("void Alignment::sequencesMatrix::getColumn(int column, int *columnSeqMatrix) ");
173  int i;
174 
175  if (column < resNumber)
176  for (i = 0; i < seqsNumber; i++)
177  columnSeqMatrix[i] = matrix[i][column];
178 
179  else
180  for (i = 0; i < seqsNumber; i++)
181  columnSeqMatrix[i] = 0;
182 
183 }
184 
185 void Alignment::sequencesMatrix::getColumn(int value, int row, int *columnSeqMatrix) {
186  // Create a timer that will report times upon its destruction
187  // which means the end of the current scope.
188  StartTiming("void Alignment::sequencesMatrix::getColumn(int value, int row, int *columnSeqMatrix) ");
189  int i, j;
190 
191  for (i = 0; i < resNumber; i++)
192  if (matrix[row][i] == value) break;
193 
194  if (i < resNumber)
195  for (j = 0; j < seqsNumber; j++)
196  columnSeqMatrix[j] = matrix[j][i];
197 
198  else
199  for (j = 0; j < seqsNumber; j++)
200  columnSeqMatrix[j] = -1;
201 }
202 
203 void Alignment::sequencesMatrix::setOrder(int *order) {
204  // Create a timer that will report times upon its destruction
205  // which means the end of the current scope.
206  StartTiming("void Alignment::sequencesMatrix::setOrder(int *order) ");
207  int i, j, **resg;
208 
209  resg = new int *[seqsNumber];
210  for (i = 0; i < seqsNumber; i++)
211  resg[i] = new int[resNumber];
212 
213  for (i = 0; i < seqsNumber; i++)
214  for (j = 0; j < resNumber; j++)
215  resg[i][j] = matrix[order[i]][j];
216 
217  for (i = 0; i < seqsNumber; i++) {
218  for (j = 0; j < resNumber; j++)
219  matrix[i][j] = resg[i][j];
220  delete[] resg[i];
221  }
222  delete[] resg;
223 }
224 
225 bool Alignment::sequencesMatrix::getSequence(string seqName, int *sequence) {
226  // Create a timer that will report times upon its destruction
227  // which means the end of the current scope.
228  StartTiming("bool Alignment::sequencesMatrix::getSequence(string seqName, int *sequence) ");
229  int i, pos;
230 
231  for (pos = 0; pos < seqsNumber; pos++)
232  if (seqsName[pos].compare(seqName) == 0)
233  break;
234 
235  if (pos == seqsNumber)
236  return false;
237 
238  for (i = 0; i < resNumber; i++)
239  sequence[i] = matrix[pos][i];
240 
241  return true;
242 }
243 
245  // Create a timer that will report times upon its destruction
246  // which means the end of the current scope.
247  StartTiming("int Alignment::sequencesMatrix::getSeqNumber(void) ");
248  return seqsNumber;
249 }
250 
252  // Create a timer that will report times upon its destruction
253  // which means the end of the current scope.
254  StartTiming("int Alignment::sequencesMatrix::getResidNumber(void) ");
255  return resNumber;
256 }
Alignment * alig
Pointer to the alignment this object is related to.
int getSeqNumber()
Number of sequences getter.
sequencesMatrix(string *alignmentMatrix, string *alignmentSeqsName, int sequences, int residues)
Manual constructor.
#define StartTiming(name)
int resNumber
Number of residues per sequence.
std::string * sequences
Vector containing the sequences.
Definition: Alignment.h:78
Class containing an alignment This class stores the alignment sequences with it&#39;s names...
Definition: Alignment.h:49
void getColumn(int column, int *numResidueseqMatrix)
Method to get a column out of the matrix.
int originalNumberOfResidues
Number of residues the alignment had when it was loaded.
Definition: Alignment.h:72
void initlVect(int *vector, int tam, int valor)
Vector initialization.
Definition: utils.cpp:40
sequencesMatrix & operator=(const sequencesMatrix &)
bool getSequence(string seqName, int *sequence)
Method to obtain a sequence based on its name.
int getResidNumber()
Number of residues getter.
std::string * seqsName
String vector containing the sequences names.
Definition: Alignment.h:80
int originalNumberOfSequences
Number of sequences the alignment had when it was loaded.
Definition: Alignment.h:68
sequencesMatrix()
Null constructor.
void getColumn(int value, int row, int *columnSeqMatrix)
Method that looks to value in a row and stores a column&#39;s, corresponding to row, sequences matrix in ...
void printMatrix()
Sequences Matrix printing method.
Utilities class. This class contains shared methods to be used in multiple parts of the code...
Definition: utils.h:50
Internal Alignment Class that represents a sequences matrix.
void setOrder(int *order)
Method that reorders the stored sequences with a given order list.
string * seqsName
Sequences names container.
int seqsNumber
Number of sequences in the matrix.
int ** matrix
Matrix container. It contains the sequences and residues of each sequence.
sequencesMatrix(Alignment *parent)
Automatic constructor.