sleipnir / tools / SeekMiner / SeekMiner.cpp

  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
/*****************************************************************************
* 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"
*****************************************************************************/
#include "stdafx.h"
#include "cmdline.h"


int main( int iArgs, char** aszArgs ) {
	static const size_t	c_iBuffer	= 1024;
#ifdef WIN32
	pthread_win32_process_attach_np( );
#endif // WIN32
	gengetopt_args_info	sArgs;
	const int lineSize = 1024;

	if( cmdline_parser( iArgs, aszArgs, &sArgs ) ) {
		cmdline_parser_print_help( );
		return 1;
	}

	string method = sArgs.weighting_method_arg;
	string cv = sArgs.CV_partition_arg;
	int cv_fold = sArgs.CV_fold_arg;
	float rbp_p = sArgs.CV_rbp_p_arg;
	string dist_measure = sArgs.dist_measure_arg;

	if(dist_measure=="pearson"){
		string sinfo_dir = sArgs.dir_sinfo_arg;
		if(sinfo_dir=="NA"){
			fprintf(stderr, "Pearson selected. Please give the sinfo directory (-u).\n");
			return 1;
		}
		if(!!sArgs.norm_subavg_flag){
			fprintf(stderr, "Warning: -m flag is ignored due to --dist_measure pearson.\n");
		}
		if(!!sArgs.norm_subavg_plat_flag){
			fprintf(stderr, "Warning: -M flag is ignored due to --dist_measure pearson.\n");
		}
		
	}else if(dist_measure=="z_score"){
		if(!!sArgs.norm_subavg_plat_flag && !(!!sArgs.norm_subavg_flag)){
			fprintf(stderr, "Please enable -m flag. --norm_subavg_plat requires -m flag.\n");
			return 1;
		}
	}

	if(!sArgs.input_arg || !sArgs.quant_arg ||
		!sArgs.dset_arg ||
		!sArgs.query_arg || !sArgs.dir_platform_arg ||
		!sArgs.dir_in_arg || !sArgs.dir_prep_in_arg){
		fprintf(stderr, "Arguments missing!\n");
		return 1;
	}

	bool useNibble = false;
	if(sArgs.is_nibble_flag==1){
		fprintf(stderr, "Nibble integration is not supported! Please use a non-nibble CDatabase.\n");
		useNibble = true;
		return 1;
	}

	bool bOutputWeightComponent = !!sArgs.output_w_comp_flag;
	bool bSimulateWeight = !!sArgs.simulate_w_flag;

	// Random Number Generator Initializations
	gsl_rng_env_setup();

	const gsl_rng_type *T;
	T = gsl_rng_default;

	gsl_rng *rnd = gsl_rng_alloc(T);

	const gsl_rng_type *T2;
	T2 = gsl_rng_default;

	gsl_rng *random_ranking_rnd = gsl_rng_alloc(T2);

	float RATE = rbp_p;
	utype FOLD = (utype) cv_fold;
	enum CSeekQuery::PartitionMode PART_M;
	if(cv=="LOI"){
		PART_M = CSeekQuery::LEAVE_ONE_IN;
	}else if(cv=="LOO"){
		PART_M = CSeekQuery::LEAVE_ONE_OUT;
	}else if(cv=="XFOLD"){
		PART_M = CSeekQuery::CUSTOM_PARTITION;
	}

	utype i,j;
	//utype TOP = 1000;
	//utype TOP = 0;

/*
	CSeekCentral *func = new CSeekCentral();
	if(!func->Initialize(sArgs.input_arg, sArgs.func_quant_arg,
		sArgs.func_dset_arg, sArgs.func_dset_arg, sArgs.query_arg,
		sArgs.func_prep_arg, sArgs.func_db_arg, sArgs.func_prep_arg,
		useNibble, sArgs.func_n_arg, sArgs.buffer_arg,
		"fn", false, false, false, false,
		sArgs.score_cutoff_arg, sArgs.per_q_required_arg)){
		return -1;
	}

	func->EqualWeightSearch();
	const vector< vector<AResultFloat> > &vfunc = func->GetAllResult();
	const vector<CSeekQuery> &vq = func->GetAllQuery();

	vector<vector<string> > origQuery;
	vector<vector<string> > newQuery;
	newQuery.resize(vfunc.size());
	origQuery.resize(vfunc.size());

	for(i=0; i<vfunc.size(); i++){
		newQuery[i] = vector<string>();
		origQuery[i] = vector<string>();
		const vector<utype> &queryGenes = vq[i].GetQuery();
		for(j=0; j<queryGenes.size(); j++){
			origQuery[i].push_back(func->GetGene(queryGenes[j]));
			newQuery[i].push_back(func->GetGene(queryGenes[j]));
		}
		for(j=0; j<200; j++)
			newQuery[i].push_back(func->GetGene(vfunc[i][j].i));
	}

	func->Destruct();
	delete func;
	
	CSeekTools::Write2DArrayText("/tmp/ex_query.txt", newQuery);
*/
/*	
	CSeekCentral *csk = new CSeekCentral();

	if(!csk->Initialize(sArgs.input_arg, sArgs.quant_arg, sArgs.dset_arg,
		sArgs.search_dset_arg, sArgs.query_arg,
		sArgs.dir_platform_arg,
		sArgs.dir_in_arg, sArgs.dir_prep_in_arg, useNibble, sArgs.num_db_arg,
		sArgs.buffer_arg, "normal",
		!!sArgs.norm_subavg_flag, !!sArgs.norm_platsubavg_flag,
		!!sArgs.norm_platstdev_flag, false,
		sArgs.score_cutoff_arg, sArgs.per_q_required_arg))
			return -1;


	//csk->CVCustomSearch(newQuery, rnd, PART_M, FOLD, RATE);
	csk->CVSearch(rnd, PART_M, FOLD, RATE);
	const vector<vector<float> > &csk_weight = csk->GetAllWeight();

	vector<vector<float> > csk_weight_copy;
	csk_weight_copy.resize(csk_weight.size());
	for(i=0; i<csk_weight.size(); i++){
		csk_weight_copy[i] = vector<float>();
		for(j=0; j<csk_weight[i].size(); j++)
			csk_weight_copy[i].push_back(csk_weight[i][j]);
	}

	const vector< vector<AResultFloat> > &vcsk = csk->GetAllResult();
	vector< vector<string> > vcNew;
	vcNew.resize(vcsk.size());
	for(i=0; i<vcsk.size(); i++){
		vcNew[i] = vector<string>();
		for(j=0; j<TOP; j++){
			vcNew[i].push_back(csk->GetGene(vcsk[i][j].i));
		}
	}
	csk->Destruct();
	delete csk;
*/
/*
	vector< vector<string> > vcIntersect;
	vcIntersect.resize(vcNew.size());
	for(i=0; i<vcNew.size(); i++){
		vcIntersect[i] = vector<string>();
		vector<string> s1, s2;
		vector<string> intersect;
		intersect.resize(TOP);
		vector<string>::iterator it;

		for(j=0; j<origQuery[i].size(); j++)
			vcIntersect[i].push_back(origQuery[i][j]);

		//int G = max((int)1, (int)(origQuery[i].size()*0.3));
		//int G = max((int)1, (int)(20 - origQuery[i].size()));

		for(j=0; j<TOP; j++)
			s1.push_back(vcNew[i][j]);
		for(j=0; j<20; j++)
			s2.push_back(newQuery[i][j]);

		sort(s1.begin(), s1.end());
		sort(s2.begin(), s2.end());

		//fprintf(stderr, "G: %d\n", G);
		//for(j=0; j<TOP; j++){
		//	s1.push_back(vcNew[i][j]);
		//	s2.push_back(newQuery[i][j]);
		//	sort(s1.begin(), s1.end());
		//	sort(s2.begin(), s2.end());
		//	it = set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
		//		intersect.begin());
		//	//if((int)(it - intersect.begin()) > G) break;
		//}
		it = set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
			intersect.begin());

		int size2 = (int) (it - intersect.begin());
		for(j=0; j<size2; j++) vcIntersect[i].push_back(intersect[j]);
	}

	CSeekTools::Write2DArrayText("/tmp/ex_query.txt", vcIntersect);
*/

	//vector<vector<string> > newQ;
	//CSeekTools::ReadMultipleQueries("/tmp/ex_query2.txt", newQ);

	enum CSeekDataset::DistanceMeasure eDistMeasure;
	if(dist_measure=="pearson"){
		eDistMeasure = CSeekDataset::CORRELATION;
	}else{
		eDistMeasure = CSeekDataset::Z_SCORE;
	}

	/*fprintf(stderr, "input: %s quant: %s dset: %s, search_dset: %s\n", sArgs.input_arg, sArgs.quant_arg, sArgs.dset_arg, sArgs.search_dset_arg);
	fprintf(stderr, "query: %s dir_plat: %s dir_in: %s, dir_prep: %s\n", sArgs.query_arg, sArgs.dir_platform_arg, sArgs.dir_in_arg, sArgs.dir_prep_in_arg);
	fprintf(stderr, "dir_gvar: %s dir_sinfo: %s useNibble: %d, num_db: %s\n", sArgs.dir_gvar_arg, sArgs.dir_sinfo_arg, sArgs.is_nibble_flag, sArgs.num_db_arg);
	getchar();*/

	CSeekCentral *csfinal = new CSeekCentral();
	CSeekDBSetting *dbSetting = new CSeekDBSetting(sArgs.dir_gvar_arg,
		sArgs.dir_sinfo_arg, sArgs.dir_platform_arg, sArgs.dir_prep_in_arg,
		sArgs.dir_in_arg, sArgs.input_arg, sArgs.quant_arg, sArgs.dset_arg,
		sArgs.num_db_arg);
	vector<CSeekDBSetting*> cc;
	cc.push_back(dbSetting);

	string add_db = sArgs.additional_db_arg;
	if(add_db!="NA"){
		ifstream ifsm;
		ifsm.open(add_db.c_str());
		if(!ifsm.is_open()){
			fprintf(stderr, "Error opening file %s\n", add_db.c_str());
			return false;
		}
		char acBuffer[lineSize];
		utype c_iBuffer = lineSize;
		vector<map<string,string> > parameters; //an array of CDatabase's
		while(!ifsm.eof()){
			ifsm.getline(acBuffer, c_iBuffer-1);
			if(acBuffer[0]==0) break;
			acBuffer[c_iBuffer-1]=0;
			string strB = acBuffer;
			if(strB=="START"){
				map<string,string> p;
				while(!ifsm.eof()){
					ifsm.getline(acBuffer, c_iBuffer-1);
					if(acBuffer[0]==0){
						fprintf(stderr, "Invalid line (empty)\n");
						return 1;
					}
					strB = acBuffer;
					if(strB=="END") break;
					vector<string> tok;
					CMeta::Tokenize(acBuffer, tok); //separator is tab
					p[tok[0]] = tok[1];
				}
				parameters.push_back(p);
			}
		}
		ifsm.close();
		if(parameters.size()==0){
			fprintf(stderr, "Error, extra_db setting file must begin with START and end with END lines\n");
			return 1;
		}

		//i=0;
		for(i=0; i<parameters.size(); i++){
		string sinfo_dir = "NA";
		string gvar_dir = "NA";
		string platform_dir = "NA";
		string prep_dir = "NA";
		string db_dir = "NA";
		string dset_map_file = "NA";
		string gene_map_file = "NA";
		string quant_file = "NA";
		int num_db = -1;

		if(eDistMeasure==CSeekDataset::CORRELATION){
			if(parameters[i].find("SINFO_DIR")==parameters[i].end() ||
				parameters[i].find("SINFO_DIR")->second=="NA"){
				fprintf(stderr, "Please specify an sinfo directory for the extra db\n");
				return false;
			}
			sinfo_dir = parameters[i].find("SINFO_DIR")->second;
		}
		if(parameters[i].find("GVAR_DIR")!=parameters[i].end())
			gvar_dir = parameters[i].find("GVAR_DIR")->second;
		if(parameters[i].find("PREP_DIR")==parameters[i].end() ||
			parameters[i].find("PLATFORM_DIR")==parameters[i].end() ||
			parameters[i].find("DB_DIR")==parameters[i].end() ||
			parameters[i].find("DSET_MAP_FILE")==parameters[i].end() ||
			parameters[i].find("GENE_MAP_FILE")==parameters[i].end() ||
			parameters[i].find("QUANT_FILE")==parameters[i].end() ||
			parameters[i].find("NUMBER_OF_DB")==parameters[i].end()){
			fprintf(stderr, "Some arguments are missing. Please make sure the following are provided:\n");
			fprintf(stderr, "PREP_DIR, DB_DIR, DSET_MAP_FILE, GENE_MAP_FILE, QUANT_FILE, NUMBER_OF_DB\n");
		}

		platform_dir = parameters[i].find("PLATFORM_DIR")->second;
		db_dir = parameters[i].find("DB_DIR")->second;
		prep_dir = parameters[i].find("PREP_DIR")->second;
		dset_map_file = parameters[i].find("DSET_MAP_FILE")->second;
		gene_map_file = parameters[i].find("GENE_MAP_FILE")->second;
		quant_file = parameters[i].find("QUANT_FILE")->second;
		num_db = atoi(parameters[i].find("NUMBER_OF_DB")->second.c_str());

		CSeekDBSetting *dbSetting2 = new CSeekDBSetting(gvar_dir, sinfo_dir,
			platform_dir, prep_dir, db_dir, gene_map_file, quant_file, dset_map_file,
			num_db);
		cc.push_back(dbSetting2);
		}
	}

	if(!csfinal->Initialize(cc,
		sArgs.search_dset_arg, 
		//"/tmp/ex_query2.txt", 
		sArgs.query_arg,
		sArgs.output_dir_arg,
		sArgs.buffer_arg, !!sArgs.output_text_flag,
		bOutputWeightComponent, bSimulateWeight,
		eDistMeasure,
		!!sArgs.norm_subavg_flag, !!sArgs.norm_subavg_plat_flag,
		false,
		sArgs.score_cutoff_arg, sArgs.per_q_required_arg, !!sArgs.square_z_flag,
		!!sArgs.random_flag, sArgs.num_random_arg, random_ranking_rnd, useNibble))
		return -1;

	if(method=="CV"){
		csfinal->CVSearch(rnd, PART_M, FOLD, RATE);
	}else if(method=="EQUAL"){
		csfinal->EqualWeightSearch();
	}else if(method=="ORDER_STAT"){
		csfinal->OrderStatistics();
	}else if(method=="USER"){
		string uw = sArgs.user_weight_list_arg;
		vector<string> uww;
		if(!CSeekTools::ReadListOneColumn(uw.c_str(), uww)){
			fprintf(stderr, "Error reading user weight list\n");
			return -1;
		}
		vector<vector<float> > fw;
		fw.resize(uww.size());
		for(i=0; i<uww.size(); i++){
			if(!CSeekTools::ReadArray(uww[i].c_str(), fw[i])){
				return -1;
			}
		}
		csfinal->WeightSearch(fw);
	}else if(method=="VAR"){
		for(i=0; i<cc.size(); i++){
			CSeekDBSetting* pc = cc[i];
			if(pc->GetValue("gvar")=="NULL"){
				fprintf(stderr, "Must specify gvar directory!\n");
				return -1;
			}
		}
		if(bSimulateWeight){
			fprintf(stderr, "simulate weight option is not supported for variance-based weighting\n");
			return -1;
		}
		csfinal->VarianceWeightSearch();
	}else if(method=="AVERAGE_Z"){
		csfinal->AverageWeightSearch();
	}
	//csfinal->WeightSearch(csk_weight_copy);
	//csfinal->CVCustomSearch(newQ, rnd, PART_M, FOLD, RATE);
	//csfinal->EqualWeightSearch();
	//csfinal->CVSearch(rnd, PART_M, FOLD, RATE);
	//csfinal->OrderStatistics();
	fprintf(stderr, "Destructing...\n");
	csfinal->Destruct();
	fprintf(stderr, "Deleting...\n");
	delete csfinal;

	fprintf(stderr, "Deleting DBSetting...\n");
	if(add_db!="NA"){
		for(i=0; i<cc.size(); i++){
			delete cc[i];
		}
	}
	fprintf(stderr, "Finished deleting DBSetting...\n");

	cc.clear();

#ifdef WIN32
	pthread_win32_process_detach_np( );
#endif // WIN32
	return 0; 
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.