Source

sleipnir / src / seekdataset.h

  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
/*****************************************************************************
* This file is provided under the Creative Commons Attribution 3.0 license.
*
* You are free to share, copy, distribute, transmit, or adapt this work
* PROVIDED THAT you attribute the work to the authors listed below.
* For more information, please see the following web page:
* http://creativecommons.org/licenses/by/3.0/
*
* This file is a component of the Sleipnir library for functional genomics,
* authored by:
* Curtis Huttenhower (chuttenh@princeton.edu)
* Mark Schroeder
* Maria D. Chikina
* Olga G. Troyanskaya (ogt@princeton.edu, primary contact)
*
* If you use this library, the included executable tools, or any related
* code in your work, please cite the following publication:
* Curtis Huttenhower, Mark Schroeder, Maria D. Chikina, and
* Olga G. Troyanskaya.
* "The Sleipnir library for computational functional genomics"
*****************************************************************************/
#ifndef SEEKDATASET_H
#define SEEKDATASET_H

#include "seekbasic.h"
#include "seekmap.h"
#include "datapair.h"
#include "seekplatform.h"

namespace Sleipnir {

class CSeekDBSetting{
public:
	CSeekDBSetting(const string &gvar,
		const string &sinfo, const string &plat,
		const string &prep, const string &db,
		const string &gene, const string &quant,
		const string &dset, const ushort &numDB){
		m_gvarDirectory = gvar;
		m_sinfoDirectory = sinfo;
		m_platformDirectory = plat;
		m_prepDirectory = prep;
		m_dbDirectory = db;
		m_geneMapFile = gene;
		m_quantFile = quant;
		m_dsetFile = dset;
		m_numDB = numDB;
	}
	CSeekDBSetting(const char *gvar,
		const char* sinfo, const char* plat,
		const char* prep, const char* db,
		const char* gene, const char* quant,
		const char* dset, const ushort &numDB){
		m_gvarDirectory = gvar;
		m_sinfoDirectory = sinfo;
		m_platformDirectory = plat;
		m_prepDirectory = prep;
		m_dbDirectory = db;
		m_geneMapFile = gene;
		m_quantFile = quant;
		m_dsetFile = dset;
		m_numDB = numDB;
	}

	~CSeekDBSetting(){
	}

	string GetValue(const string &str){
		if(str=="gene")
			return m_geneMapFile;
		else if(str=="dset")
			return m_dsetFile;
		else if(str=="quant")
			return m_quantFile;
		else if(str=="gvar")
			return m_gvarDirectory;
		else if(str=="sinfo")
			return m_sinfoDirectory;
		else if(str=="db")
			return m_dbDirectory;
		else if(str=="prep")
			return m_prepDirectory;
		else if(str=="platform")
			return m_platformDirectory;
		else
			return "NULL";
	}

	ushort GetNumDB(){
		return m_numDB;
	}

private:
	string m_gvarDirectory;
	string m_sinfoDirectory;
	string m_platformDirectory;
	string m_prepDirectory;
	string m_dbDirectory;
	string m_geneMapFile;
	string m_quantFile;
	string m_dsetFile;
	ushort m_numDB;
};



/*!
 * \brief Representation of a microarray dataset that is used by Seek
 *
 * A \c CSeekDataset encapsulates the following information about the dataset:
 *
 * \li The gene-gene \a correlation matrix
 * \li Each gene's expression variance
 * \li Each gene's average \a correlation
 * \li The genes in the query that are present in the dataset
 * \li The genes in the genome that are present in the dataset
 * \li The platform which the dataset belongs to
 * \li The weight of the dataset that is assigned by the search algorithm
 *
 * This dataset structure is designed to be used by Seek.
 * \remarks
 * The word \a correlation refers to the standardized z-scores of Pearson correlations, which
 * is derived from a 2-step process:
 * \f[f(x,y)=\frac{1}{2}ln\frac{1+p(x,y)}{1-p(x,y)}\f]
 * where \f$p(x,y)\f$ is the Pearson correlation, \f$f(x,y)\f$ is the Fisher's transformed score.
 * \f[z(x,y)=\frac{f(x,y) - \bar{f}}{\sigma_{f}}\f]
 * where \f$z(x,y)\f$ is the z-score, \f$\bar{f}\f$ is the mean, and \f$\sigma_{f}\f$ is
 * the standard deviation.
 * 
 * From here on, \a correlation always refers to the above z-score definition.
 */

class CSeekDataset{
public:

	/*!
	 * \enum DistanceMeasure
	 * \brief Distance measure (see main section for descriptions)
	 */
	enum DistanceMeasure{
		CORRELATION = 0, /**< Pearson correlations */
		Z_SCORE = CORRELATION + 1 /**< Z-score of Pearson correlations */
	};

	/*!
	 * \brief
	 * Constructor
	 */
	CSeekDataset();

	/*!
	 * \brief
	 * Destructor
	 */
	~CSeekDataset();

	/*!
	 * \brief
	 * Read the \c *.sinfo file
	 *
	 * \param strFileName The file name
	 *
	 * The \c *.sinfo file contains the mean and the standard deviation
	 * of the global gene-gene Pearson distribution for this dataset.
	 */
	bool ReadDatasetAverageStdev(const string &);

	/*!
	 * \brief
	 * Read the gene average \a correlation file \c *.gavg
	 *
	 * \param strFileName The file name
	 *
	 * The \c *.gavg is an array that stores the average \a correlation of each
	 * gene.
	 */
	bool ReadGeneAverage(const string &);

	/*!
	 * \brief
	 * Read the gene variance file \c *.gvar
	 *
	 * \param strFileName The file name
	 *
	 * The \c *.gvar file is an array that stores the expression variance of each
	 * gene.
	 */
	bool ReadGeneVariance(const string &);

	/*!
	 * \brief
	 * Read the gene presence file \c *.gpres
	 *
	 * \param strFileName The file name
	 *
	 * The \c *.gpres is a 2-value array that contains the presence (1), absence (0)
	 * status of genes.
	 */
	bool ReadGenePresence(const string &);

	/*!
	 * \brief
	 * Initialize the genome presence map
	 *
	 * Indicates which genes of the genome are present in the dataset.
	 */
	bool InitializeGeneMap();

	/*!
	 * \brief
	 * Initialize the query presence map
	 *
	 * \param query The query genes
	 *
	 * Indicates which query genes are present in the dataset.
	 */
	bool InitializeQuery(const vector<ushort> &);

	/*!
	 * \brief
	 * Initialize a presence map for a block of queries
	 *
	 * \param queryBlock A vector of queries
	 *
	 * Flattens all the queries into one vector that contains only the unique query genes, then
	 * constructs a presence map based on this vector.
	 */
	bool InitializeQueryBlock(const vector<ushort> &);

	/*!
	 * \brief
	 * Delete the query
	 *
	 * Resets all query-related data, such as dataset weight, query presence map, etc.
	 */
	bool DeleteQuery();

	/*!
	 * \brief
	 * Delete query block
	 *
	 * Resets all query-block related data.
	 */
	bool DeleteQueryBlock();

	/*!
	 * \brief
	 * Initialize the gene-gene \a correlation matrix
	 *
	 * \param rD A two-dimensional array storing the discretized gene-gene \a correlations
	 * \param quant The discretization function
	 * \param iRows The number of rows for the \a correlation matrix
	 * \param iColumns The number of columns for the \a correlation matrix
	 * \param bSubtractAvg If true, subtract the \a correlation by the dataset average
	 * \param bNormPlatform If true, subtract the \a correlation by the platform average and divide by standard deviation
	 * \param logit If true, apply the logit transform on \a correlations
	 * \param dist_measure Distance measure: z-score or correlations
	 * \param cutoff Apply a hard cutoff on \a correlations
	 * \param bRandom If true, shuffle the \a correlation vector
	 * \param rand The random generator for the shuffling operation above
	 * \remarks
	 * The discretized \a correlation in the matrix \c rD is bounded by 0 to 255 (the limit of
	 * \c unsigned \c char). The parameter \c quant specifies how a \a correlation is
	 * discretized. For example, if the \c quant has 5 bins:
	 * \code 
	 * [0, 1, 2, 3, 4]
	 * \endcode
	 * Then if a \a correlation is 2.5, the discretized value would be 2.
	 */
	bool InitializeDataMatrix(ushort**, const vector<float> &,
		const ushort&, const ushort&, const bool=true, 
		const bool=false, const bool=false,
		const enum DistanceMeasure=Z_SCORE,
		const float cutoff=-1.0*CMeta::GetNaN(), 
		const bool=false, gsl_rng *rand=NULL);

	/*!
	 * \brief
	 * Copy constructor
	 * \param src A given dataset
	 */
	bool Copy(CSeekDataset *);

	/*!
	 * \brief
	 * Get the gene-gene \a correlation matrix
	 *
	 * \return A two-dimensional array of type \c ushort. Note that the
	 * \a correlation has been scaled to a integer range from 0 to 640.
	 * See CSeekDataset::InitializeDataMatrix.
	 *
	 */
	ushort** GetDataMatrix();

	/*!
	 * \brief
	 * Get the gene-gene \a correlation matrix
	 *
	 * \return A two-dimensional array of type \c unsigned \c char**.
	 */
	unsigned char** GetMatrix();

	/*!
	 * \brief Get the genome presence map
	 * \return The genome presence map
	 */
	CSeekIntIntMap* GetGeneMap();

	/*!
	 * \brief Get the query-block presence map
	 * \return The query-block presence map
	 */
	CSeekIntIntMap* GetDBMap();

	/*!
	 * \brief Get the query presence map
	 * \return The query presence map
	 */
	CSeekIntIntMap* GetQueryMap();

	/*!
	 * \brief Get the query genes
	 * \return A vector of queries
	 */
	const vector<ushort>& GetQuery() const;

	/*!
	 * \brief Get the query gene indices
	 * \return A vector of query gene indices
	 */
	const vector<ushort>& GetQueryIndex() const;

	/*!
	 * \brief Get the gene expression variance vector
	 * \return The variance vector
	 */
	float GetGeneVariance(const ushort&) const;
	/*!
	 * \brief Get the gene average \a correlation vector
	 * \return The average \a correlation vector
	 */
	float GetGeneAverage(const ushort&) const;
	/*!
	 * \brief Get the mean of the global gene-gene Pearson distribution
	 * \return The mean Pearson for the dataset
	 */
	float GetDatasetAverage() const;
	/*!
	 * \brief Get the standard deviation of the global gene-gene Pearson distribution
	 * \return The standard deviation of the Pearson distribution
	 */
	float GetDatasetStdev() const;
	/*!
	 * \brief Get the genome size
	 * \return The genome size
	 */
	ushort GetNumGenes() const;

	/*!
	 * \brief Initialize the weight of the dataset
	 *
	 * \param i The number of cross-validations
	 *
	 * Initializes the total dataset weight, and the score of the
	 * individual cross-validation (CV) runs. 
	 */
	bool InitializeCVWeight(const ushort&);

	/*!
	 * \brief Set the score for a particular cross-validation
	 * \param i The index
	 * \param f The validation score
	 */
	bool SetCVWeight(const ushort&, const float&);

	/*!
	 * \brief Get the score for a particular cross-validation
	 * \param i The index
	 */
	float GetCVWeight(const ushort&);

	/*!
	 * \brief Get all the cross-validation scores
	 * \return A vector of cross-validation scores
	 */
	const vector<float>& GetCVWeight() const;

	/*!
	 * \brief Get the dataset weight
	 * \return The dataset weight
	 */
	float GetDatasetSumWeight();

	/*!
	 * \brief Set the platform
	 * \param cp The platform
	 */
	void SetPlatform(CSeekPlatform &);
	/*!
	 * \brief Get the platform
	 * \return The platform of this dataset
	 */
	CSeekPlatform& GetPlatform() const;

private:
	CSeekPlatform *platform;
	vector<float> geneAverage;
	vector<float> geneVariance;
	vector<char> genePresence;
	CSeekIntIntMap *geneMap;

	/* previously known as sinfo file */
	float m_fDsetAverage;
	float m_fDsetStdev;

	CSeekIntIntMap *dbMap;
	CSeekIntIntMap *queryMap;
	vector<ushort> query;
	vector<ushort> queryIndex;

	ushort iQuerySize;
	ushort iNumGenes;
	ushort iDBSize;

	vector<float> weight;

	ushort **rData;
	unsigned char **r;

	float sum_weight;
	bool m_bIsNibble;
};



}
#endif