Ejemplo n.º 1
0
int main(int argc, char **argv) 
{ 
	AllScoreModels model;

	int i;
	char ann_file[256];
	char out_file[256];
	char input_file[256];
	char inspect_results_file[256];
	char list_file[256];
	char model_file[256];
	char initial_model[256];
	char model_dir[256];
	char PTM_string[256];
	char mgf_out_dir[256];
	char neg_spec_list[256];
	char tag_string[64];
	char tag_suffix[64];
	
	bool got_input_file=false,got_model_file=false, got_list_file=false;
	bool got_model_dir=false, got_initial_model=false, got_PTM_string = false, got_neg_spec_list=false;
	bool prm_only=false;
	bool prm_norm=false;
	bool pmcsqs_only = false;
	bool sqs_only = false;
	bool got_filter_spectra = false;
	bool pmcsqs_and_prm = false;
	bool train_flag = false;
	bool correct_pm = false;
	bool use_spectrum_charge = false;
	bool use_spectrum_mz     = false;
	bool perform_filter		 = true;
	bool output_aa_probs	 = false;
	bool output_cumulative_probs = false;
	bool make_inspect_tags   = false;
	bool make_training_fa	 = false;
	bool test_tags			 = false;
	bool got_make_ann_mgf	 = false;
	bool got_make_training_mgf = false;
	bool got_rescore_inspect = false;
	bool got_recalibrate_inspect = false;
	bool got_make_peak_examples  = false;

	int start_train_idx=0;
	int end_train_idx = POS_INF;
	int specific_charge=-1;
	int specific_size=-1;
	int specific_region=-1;

	int specific_idx = -1;
	
	int file_start_idx =0;
	int tag_length = 0;
	int num_solutions = 20;
	int digest_type = TRYPSIN_DIGEST;
	mass_t train_tolerance;
	float min_pmcsqs_prob = -1.0;
	mass_t fragment_tolerance = -1.0;
	mass_t pm_tolerance = -1.0;
	float sqs_filter_thresh = 0.0;
	float min_filter_prob = 0.0;
	int   num_test_cases=-1;
	int	  num_training_spectra=-1;

	seedRandom(112233);
	strcpy(tag_suffix,"tags");

	// read command line arguments
	i=1;
	while (i<argc)
	{

		if (! strcmp(argv[i],"-make_ann_mgf"))
		{
			if (++i == argc)
				print_help("Missing file ann file!");

			strcpy(ann_file,argv[i]);	

			if (++i == argc)
				print_help("Missing file out file!");

			strcpy(out_file,argv[i]);	

			got_make_ann_mgf=true;
		}
		else
		if (! strcmp(argv[i],"-make_training_mgf"))
		{
			if (++i == argc)
				print_help("Missing file out file!");

			strcpy(out_file,argv[i]);	

			if (++i == argc)
				print_help("Missing num training spectra!");

			num_training_spectra = atoi(argv[i]);
			if (num_training_spectra<=0)
				print_help("Error: -make_training_mgf [out_file] [num spectra>0]\n");
			
			got_make_training_mgf=true;
		}
		else if (!strcmp(argv[i],"-file"))
		{
			if (++i == argc)
				print_help("Missing file name!");

			strcpy(input_file,argv[i]);
			got_input_file=true;
		}
		else
		if (!strcmp(argv[i],"-list"))
		{
			if (++i == argc)
				print_help("Missing list name!");

			strcpy(list_file,argv[i]);
			got_list_file=true;
		}
		else if  (!strcmp(argv[i],"-file_start_idx"))
		{
			if (++i == argc)
				print_help("Missing file start idx!");

			file_start_idx = atoi(argv[i]);
		}
		else if (!strcmp(argv[i],"-model")) 
		{
			if (++i == argc)
				print_help("Missing model name!");

			strcpy(model_file,argv[i]);
			got_model_file=true;
		}
		else if (! strcmp(argv[i],"-model_dir"))
		{
			if (++i == argc)
				print_help("Missing model dir name!");

			strcpy(model_dir,argv[i]);
			got_model_dir=true;
		}
		else if (! strcmp(argv[i],"-fragment_tolerance"))
		{
			if (++i == argc)
				print_help("Missing model dir name!");

			fragment_tolerance = atof(argv[i]);
			if (fragment_tolerance<0 || fragment_tolerance>0.75)
				print_help("Error: -fragment_toelerance should be 0-0.75\n");
		}
		else if (! strcmp(argv[i],"-pm_tolerance"))
		{
			if (++i == argc)
				print_help("Missing model dir name!");

			pm_tolerance = atof(argv[i]);
			if (pm_tolerance<0 || pm_tolerance>5.0)
				print_help("Error: -pm_toelerance should be 0-5.0\n");
		}
		else if  (!strcmp(argv[i],"-num_solutions"))
		{
			if (++i == argc)
				print_help("Missing number of solutions!");

			num_solutions = atoi(argv[i]);
			if (num_solutions<=0 || num_solutions> 2000)
				print_help("Error: -num_solutions should be 1-2000\n");
		}
		else if (!strcmp(argv[i],"-tag_length"))
		{
			if (++i == argc)
				print_help("Missing minimum length parameter!");

			tag_length = atoi(argv[i]);
			if (tag_length<3 || tag_length>6)
				print_help("Error: -tag_length value must be 3-6\n");

		}
		else if (!strcmp(argv[i],"-digest"))
		{
			if (++i == argc)
				print_help("Missing digest type parameter : NON_SPECIFIC, TRYPSIN\n");

			if (! strcmp(argv[i],"NON_SPECIFIC"))
			{
				digest_type = NON_SPECIFIC_DIGEST;
			}
			else if (! strcmp(argv[i],"TRYPSIN"))
			{
				digest_type = TRYPSIN_DIGEST;
			}
			else
			{
				printf("Error: bad digest type: %s\n",argv[i]);
				print_help("Supported digest types: NON_SPECIFIC, TRYPSIN.");
			}
		}
		else if (! strcmp(argv[i],"-use_spectrum_charge"))
		{
			use_spectrum_charge = true;
		}
		else if (! strcmp(argv[i],"-use_spectrum_mz"))
		{
			use_spectrum_mz = true;
		}
		else if (! strcmp(argv[i],"-no_quality_filter"))
		{
			perform_filter = false;
		}
		else if (! strcmp(argv[i],"-correct_pm"))
		{
			correct_pm = true;
		}
		else if (! strcmp(argv[i],"-prm")) 
		{
			prm_only = true;
		}
		else if (! strcmp(argv[i],"-prm_norm")) 
		{
			prm_norm = true;
			prm_only = true;
		}
		else if (! strcmp(argv[i],"-output_aa_probs"))
		{
			output_aa_probs=true;
		}
		else if (! strcmp(argv[i],"-output_cumulative_probs"))
		{
			output_cumulative_probs=true;
		}
		else if (! strcmp(argv[i],"-pmcsqs_only"))
		{
			pmcsqs_only = true;
		}
		else if (! strcmp(argv[i],"-sqs_only"))
		{
			sqs_only = true;
		}
		else if (! strcmp(argv[i],"-min_filter_prob"))
		{
			if (++i == argc)
				print_help("Missing minimum probability parmater after -min_filter_prob !\n");

			min_filter_prob = -1.0;
			min_filter_prob = atof(argv[i]);
			if (min_filter_prob<0.0 || min_filter_prob>=1.0 || argv[i][0] != '0')
			{
				print_help("The flag -min_filter_prob should be followed by a minimal probability value [0-1.0]\n");
				exit(1);
			}
		}
		else if ( ! strcmp(argv[i],"-filter_spectra"))
		{
			got_filter_spectra = true;
			if (++i == argc)
				print_help("Missing minimum probability parmater after -filter_spectra !\n");
			
			sqs_filter_thresh=atof(argv[i]);

			if (sqs_filter_thresh <0 || sqs_filter_thresh>1.0)
				print_help("Error: the sqs threshold should be in the range 0-1 (recommended below 0.1)\n");
			
			if (++i == argc)
				print_help("Missing output directory for MGF files (second argument after -filter_spectra)!\n");
		
			strcpy(mgf_out_dir,argv[i]);
		}
		else if (! strcmp(argv[i],"-specific_idx"))
		{
			if (++i == argc)
				print_help("Missing idx!");
			specific_idx=atoi(argv[i]);
		}
		else if (! strcmp(argv[i],"-train_model"))
		{
			train_flag = true;
			if (++i == argc)
				print_help("Missing training tolerance!");

			train_tolerance = atof(argv[i]);
			if (train_tolerance<0.001 || train_tolerance>1.0)
				print_help("Error: training tolerance should be in the range 0.001 - 1.0\n");
		}
		else if (! strcmp(argv[i],"-start_train_idx"))
		{
			if (++i == argc)
				print_help("Missing start_train_idx!");

			start_train_idx = atoi(argv[i]);
		}
		else if (! strcmp(argv[i],"-end_train_idx"))
		{
			if (++i == argc)
				print_help("end_train_idx!");

			end_train_idx = atoi(argv[i]);
		}
		else if (! strcmp(argv[i],"-specific_reigon_model"))
		{
			if (++i == argc)
				print_help("specific_reigon_model!");

			specific_charge = atoi(argv[i++]);
			specific_size	= atoi(argv[i++]);
			specific_region = atoi(argv[i]);

		}
		else if (! strcmp(argv[i],"-specific_charge"))
		{
			if (++i == argc)
				print_help("specific_charge!");

			specific_charge = atoi(argv[i]);
		}
		else if (! strcmp(argv[i],"-specific_size"))
		{
			if (++i == argc)
				print_help("specific_size!");

			specific_size = atoi(argv[i]);
		}
		else if (! strcmp(argv[i],"-initial_model"))
		{
			got_initial_model = true;
			if (++i == argc)
				print_help("Missing initial model name!");
			strcpy(initial_model,argv[i]);
		}
		else if (! strcmp(argv[i],"-neg_spec_list"))
		{
			got_neg_spec_list = true;
			if (++i == argc)
				print_help("Missing neg spec list!");
			strcpy(neg_spec_list,argv[i]);
		}
		else if (! strcmp(argv[i],"-PTMs"))
		{
			got_PTM_string = true;
			if (++i == argc)
				print_help("Missing PTM list!");
			strcpy(PTM_string,argv[i]);
		}
		else if (! strcmp(argv[i],"-inspect_tags"))
		{
			make_inspect_tags=true;
			if (++i == argc)
				print_help("inspect_tags!");

			strcpy(tag_string,argv[i]);
		}
		else if (! strcmp(argv[i],"-rescore_inspect"))
		{
			got_rescore_inspect = true;
			if (++i == argc)
				print_help("Missing results file!");

			strcpy(inspect_results_file,argv[i]);

			if (++i == argc)
				print_help("Missing new results file!");

			strcpy(out_file,argv[i]);
		}
		else if (! strcmp(argv[i],"-recalibrate_inspect"))
		{
			got_recalibrate_inspect = true;
			if (++i == argc)
				print_help("Missing results file!");

			strcpy(inspect_results_file,argv[i]);

			if (++i == argc)
				print_help("Missing new results file!");

			strcpy(out_file,argv[i]); 		
		}
		else if ( ! strcmp(argv[i],"-make_peak_examples"))
		{
			got_make_peak_examples=true;
		}
		else if (! strcmp(argv[i],"-make_training_fa"))
		{
			make_training_fa=true;
		}
		else if (! strcmp(argv[i],"-test_tags"))
		{
			test_tags=true;
			if (++i == argc)
				print_help("test_tags!");

			strcpy(tag_string,argv[i]);
		}
		else if (! strcmp(argv[i],"-num_test_cases"))
		{
			if (++i == argc)
				print_help("num_test_cases!");

			num_test_cases = atoi(argv[i]);
		}
		else if (! strcmp(argv[i],"-tag_suffix"))
		{
			if (++i == argc)
				print_help("tag suffix!");
			strcpy(tag_suffix,argv[i]);
		}
		else
		{
			printf("**********************************************************\n");
			printf("\nError: Unkown command line option: %s\n\n",argv[i]);
			print_help("");
			exit(0); 
		}
		i++;
	}


	if (! got_model_file) 
		print_help("Error: Missing model name!");


	if (!got_input_file && ! got_list_file)
		print_help("Error: missing input file (either -file or -list must be used).");

	Config *config = model.get_config();

	if (got_model_dir)
	{
		config->set_resource_dir(string(model_dir));
	}

	

	//////////////////////////////////////////////////////////////////
	// Model Training
	if (train_flag)
	{	
		if (got_initial_model)
		{
			model.read_model(initial_model);
			if (got_PTM_string)
				config->apply_selected_PTMs(PTM_string);
			model.read_rank_models(initial_model,true);
			model.read_cum_seq_prob_models(initial_model,true);
		}
		else
		{
			config->init_with_defaults();
			config->set_tolerance(train_tolerance);
			config->set_digest_type(digest_type);
			if (got_PTM_string)
				config->apply_selected_PTMs(PTM_string);
		}

		model.set_model_name(string(model_file));
	
		SpectraAggregator sa;
		if (! got_list_file)
		{
			if (got_input_file)
			{
		//		fm.init_from_mgf(config,input_file);
				sa.initializeFromSpectraFilePath(input_file, config);
			}
			else
			{
				printf("Must supply a list of annotated spectra for training!\n");
				exit(0);
			}
		}
		else
		{
		//	fm.init_from_list_file(config,list_file);
			sa.initializeFromTextFile(list_file, config);
		}
		
		
		model.trainModelsInStages(model_file, 
								  sa,
									train_tolerance, 
									start_train_idx, 
									end_train_idx,
									specific_charge, 
									specific_size, 
									specific_region,
									(got_neg_spec_list ? neg_spec_list : NULL));

	

		model.write_model();
		exit(0);
	}
	
	///////////////////////////////////////////////////////////////////
	// Model initializing (running some sort of de novo, need a model)
	// 
	const time_t start_time = time(NULL);

	cout << "PepNovo V3. Build " << build_name << endl;
	cout << "Copyright 2008, The Regents of the University of California. All Rights Reserved." << endl;
	cout << "Created by Ari Frank ([email protected])" << endl << endl;
	cout << "Initializing models (this might take a few seconds)... " << flush;

	// TODO: incorporate PTM line into the model reading and also the other model stuff below
	model.read_model(model_file,true); 
	if (got_PTM_string)
		config->apply_selected_PTMs(PTM_string);
	model.getPeptideCompositionAssigner().init_aa_translations();
	model.read_rank_models(model_file,true);
	model.read_cum_seq_prob_models(model_file,true);

	cout << "Done." << endl;

	config = model.get_config();
	config->set_digest_type(digest_type);

	if (fragment_tolerance>0)
		config->set_tolerance(fragment_tolerance);

	if (pm_tolerance>0)
		config->setPrecursorMassTolerance(pm_tolerance);

	if (correct_pm)
		config->set_need_to_estimate_pm(1);

	if (use_spectrum_mz)
		config->set_use_spectrum_mz(1);

	if (use_spectrum_charge)
		config->set_use_spectrum_charge(1);

	if (! perform_filter)
		config->set_filter_flag(0);

	if (config->get_pm_tolerance()<0.1)
		config->set_need_to_estimate_pm(0);

	cout << setprecision(4) << fixed;
	cout << "Fragment tolerance : " << config->getTolerance() << endl;
	cout << "PM tolernace       : " << config->get_pm_tolerance() << endl;
	cout << "PTMs considered    : " ;
	if (got_PTM_string)
	{
		cout << PTM_string << endl;
	}
	else
	{
		cout << "None" << endl;
	}
	


	///////////////////////////////////////////////////////////////////
	// Training fa
	if (make_training_fa)
	{
		make_denovo_training_fa(model,input_file);
		exit(0);
	}

	///////////////////////////////////////////////////////////////////
	// Inspect tags

	if (make_inspect_tags)
	{
		create_tag_file_for_inspect(model,input_file,tag_string,tag_suffix);
		exit(0);
	}

	if (test_tags)
	{
		benchmark_tags(model,list_file,tag_string,num_test_cases);
		exit(0);
	}


	////////////////////////////////////////////////////////////////////
	// Rescore InsPecT
	if (got_rescore_inspect)
	{
		PeptideRankScorer *db_score = (PeptideRankScorer *)model.get_rank_model_ptr(0);
		db_score->rescore_inspect_results(input_file,inspect_results_file,out_file);
		exit(0);
	}

	if (got_recalibrate_inspect)
	{
		cout << "Recalibrating delta scores in " << input_file << endl;
		PeptideRankScorer *db_score = (PeptideRankScorer *)model.get_rank_model_ptr(0);
		db_score->recalibrate_inspect_delta_scores(input_file,inspect_results_file,out_file);
		exit(0);
	}

	if (got_make_peak_examples)
	{
		cout << "Making peak examples " << input_file << endl;
		PeptideRankScorer *db_score = (PeptideRankScorer *)model.get_rank_model_ptr(0);
		//db_score->make_peak_table_examples(input_file);
		exit(0);
	}



	///////////////////////////////////////////////////////////////////
	// Make input file list
	vector<string> list_vector;
	if (got_list_file)
	{
		readListOfPaths(list_file, list_vector);
	}
	else
		list_vector.push_back(input_file);

	int correct_benchmark =0;
	int total_benchmark =0;
	int counter=0;

	if (got_make_training_mgf)
	{
	//	make_training_mgf(config,list_file,num_training_spectra,out_file);
		exit(0);
	}


	if (sqs_only)
	{
		PMCSQS_Scorer *pmcsqs = (PMCSQS_Scorer *)model.get_pmcsqs_ptr();
		if (! pmcsqs ||  ! pmcsqs->getIndInitializedSqs())
		{
			cout << "Error: no spectrum quality score (SQS) for this model!" << endl;
			exit(1);
		}
	}
	else
	if (got_filter_spectra ||  pmcsqs_only)
	{
		PMCSQS_Scorer *pmcsqs = (PMCSQS_Scorer *)model.get_pmcsqs_ptr();
		if (! pmcsqs || ! pmcsqs->getIndInitializedPmc() || ! pmcsqs->getIndInitializedSqs())
		{
			cout << "Error: no parent mass correction (PMC) and/or quality score (SQS) for this model!" << endl;
			exit(1);
		}
	}




	///////////////////////////////////////////////////////////////////
	// FILTER SPECTRA
	if (got_filter_spectra)
	{
		int num_written =0;
		int num_read = 0;
		PMCSQS_Scorer *pmcsqs = (PMCSQS_Scorer *)model.get_pmcsqs_ptr();

	//	pmcsqs->output_filtered_spectra_to_mgfs(config, list_vector, mgf_out_dir, sqs_filter_thresh, num_written, num_read);
		
		time_t curr_time = time(NULL);
		double elapsed_time = (curr_time - start_time);
		cout << "Processed " << list_vector.size() << " (" << num_read << " spectra)." << endl;
		cout << "Wrote " << num_written << " spectra to mgfs in " << mgf_out_dir << endl;
		cout << "Elapsed time " << fixed << elapsed_time << " seconds." << endl;
		return 0;
	}

	//////////////////////////////////////////////////////////////////
	// PRM
	if (prm_only)
	{
		

		perform_prm_on_list_of_files(model, list_vector, min_filter_prob, file_start_idx, prm_norm);
	//	prm_benchmark(model, list_vector, min_pmcsqs_prob, file_start_idx);

	//	FileManager fm;
	//	fm.init_from_list(config,list_vector);
	//	model.learn_prm_normalizer_values(fm);
	//	model.write_prm_normalizer_values();
		return 0;
	}

	if (fabs(config->get_aa2mass()[Cys]-103.0)<1)
	{
		cout << endl <<"*** Warning: searching with unmodified cystine, usually the PTM C+57 should be included ***" << endl << endl;
	}
	cout << endl;

	//////////////////////////////////////////////////////////////////
	// PMCSQS
	if (pmcsqs_only)
	{
	//	perform_pmcsqs_on_list_of_files(model, list_vector, file_start_idx);
		return 0;
	}
 
	//////////////////////////////////////////////////////////////////
	// SQS
	if (sqs_only)
	{
	//	perform_sqs_on_list_of_files(model, list_vector, file_start_idx);
		return 0;
	}  
	
	//////////////////////////////////////////////////////////////////
	// DENOVO AND TAGS

	if (tag_length<=0)
	{
	//	perform_denovo_on_list_of_files(model, list_vector, file_start_idx, num_solutions, 7, 16, 
	//		false, min_filter_prob, output_aa_probs,  output_cumulative_probs, cout);
		new_perform_denovo_on_list_of_files(model, list_vector, file_start_idx, num_solutions, 7, 16, 
			false, min_filter_prob, output_aa_probs,  output_cumulative_probs, cout);
	}
	else
	{
		perform_tags_on_list_of_files(model,list_vector,file_start_idx,num_solutions,tag_length,
			false, min_filter_prob, output_aa_probs, output_cumulative_probs, cout);	
	}
	

#ifdef WIN32
	system("pause");
#endif

	return 0;
}
Ejemplo n.º 2
0
/***************************************************************************************
This function touches up inspect search results by rescoring the sequences returned by
inspect. The function produces a new inspect results file with the scores (and delta scores)
replaced.
****************************************************************************************/
void PeptideRankScorer::recalibrate_inspect_delta_scores(char *spectra_file, 
											   char *inspect_res, 
											   char *new_res_file) const
{
	AllScoreModels* allScoreModels = static_cast<AllScoreModels*>(this->allScoreModelsPtr_);
	Config *config = allScoreModels->get_config();

	ifstream org_res(inspect_res);

	if (!  org_res.is_open() || ! org_res.good())
	{
		cout << "Error: couldn't open original inspect results file for reading:" << inspect_res << endl;
		exit(1);
	}

	ofstream new_res(new_res_file);
	if (! new_res.is_open() || ! new_res.good())
	{
			cout << "Error: couldn't open new inspect results file for writing:" << new_res << endl;
		exit(1);
	}

	char line_buff[1024];
	org_res.getline(line_buff,1024);

	bool read_line  = true;
	vector<string> field_names;
	if (line_buff[0] != '#')
	{
		read_line = false;
	}
	else
	{
		string header = string(line_buff);
		split_string(header,field_names);

		int i;
		for (i=0; i<field_names.size(); i++)
			cout << i << "\t" << field_names[i] << endl;
	}


	vector<ScanCandidateSet> cand_sets;
	vector<int> scan_mapping;
	cand_sets.clear();
	scan_mapping.resize(100000,-1);
	
	while (! org_res.eof())
	{
		vector<string> fields;

		if (read_line)
		{
			org_res.getline(line_buff,1024);
			if (org_res.gcount() < 5)
				continue;
		}
		else
		{
			read_line = true;
		}

		split_string(line_buff,fields);
		InspectResultsLine res;

		res.parse_from_fields(config,fields);

		if (cand_sets.size()==0 || ! cand_sets[cand_sets.size()-1].add_new_line(res))
		{
			ScanCandidateSet new_set;
			new_set.add_new_line(res);
			
			if (new_set.scan>=scan_mapping.size())
				scan_mapping.resize(2*scan_mapping.size(),-1);

			scan_mapping[new_set.scan]=cand_sets.size();
			cand_sets.push_back(new_set);
		}
	}
	org_res.close();

	cout << "Read results for " << cand_sets.size() << " scans..." << endl;

	FileManager fm;
	FileSet     fs;
	fm.init_from_file(config,spectra_file);
	fs.select_all_files(fm);
	const vector<SingleSpectrumFile *>& all_ssfs = fs.get_ssf_pointers();

	cout << "Read " <<  all_ssfs.size() << " spectra headers..." << endl;

	BasicSpecReader bsr;
	QCPeak *peaks = new QCPeak[5000];

	vector<bool> spectrum_indicators;
	spectrum_indicators.resize(cand_sets.size(),false);

	int num_found =0;
	int i;
	for (i=0; i<all_ssfs.size(); i++)
	{
		SingleSpectrumFile *ssf = all_ssfs[i];
		
		const int scan_number = ssf->get_scan();
		if (scan_mapping[scan_number]<0)
			continue;

		const int num_peaks = bsr.read_basic_spec(config,fm,ssf,peaks);

		spectrum_indicators[scan_mapping[scan_number]]=true;
		num_found++;

		ScanCandidateSet& cand_set = cand_sets[scan_mapping[scan_number]];
		
		cand_set.recalbirate_scores(config);

		cand_set.output_to_stream(new_res,10);
	}

	if (num_found<cand_sets.size())
	{
		cout << "Warning: found only " << num_found << "/" << cand_sets.size() << " of the scans scored by InsPecT!" << endl;
	}
	else
	{
		cout << "All scored scans found in spectrum file." << endl;
	}


	delete [] peaks;
}
Ejemplo n.º 3
0
void PrmNodeScoreModel::learnPrmNormalizerValue(void* allScoreModelsVoidPointer, const SpectraAggregator& sa)
{
	AllScoreModels* allScoreModels = static_cast<AllScoreModels*>(allScoreModelsVoidPointer);
	const float step = 0.5;
	const float min_delta = -1.0;
	const float max_delta = 7.0;
	const float target_mid_ratio = 0.96;
	const float target_side_ratio = 0.94;


	config_->set_use_spectrum_charge(1);

	regional_prm_normalizers.resize(RegionalPrmNodeScoreModels_.size());
	int c;
	for (c=0; c<RegionalPrmNodeScoreModels_.size(); c++)
	{
		regional_prm_normalizers[c].resize(RegionalPrmNodeScoreModels_[c].size());
		int s;
		for (s=0; s<RegionalPrmNodeScoreModels_[c].size(); s++)
			regional_prm_normalizers[c][s].resize(RegionalPrmNodeScoreModels_[c][s].size(),0);
	}
	

	const vector< vector<mass_t> >& mass_threshes = config_->get_size_thresholds();
	for (c=1; c<regional_prm_normalizers.size(); c++)
	{
		int s;
		for (s=0; s<regional_prm_normalizers[c].size(); s++)
		{
			const mass_t min_mass = (s == 0 ? 0 : mass_threshes[c][s-1]);
			const mass_t max_mass =  mass_threshes[c][s];
			const int num_regions = regional_prm_normalizers[c][s].size();
			
			cout << "Finding normalizers for charge " << c << " size " << s << "  (masses " << min_mass << " - " <<
				max_mass << ")" << endl;

			SpectraList sl(sa);
			sl.selectHeaders(min_mass/c,max_mass/c,c,c);
			sl.randomlyReduceListToSize(2000);

			if (sl.getNumHeaders()<50)
			{
				cout << "Insufficient number of spectra... skipping" << endl;
				continue;
			}

			vector< vector< NodeType > > all_prms;
			int sc;
			for (sc=0; sc<sl.getNumHeaders(); sc++)
			{
				const SingleSpectrumHeader* header = sl.getSpectrumHeader(sc);
				PrmGraph prm;

				Spectrum s;
				if (! s.readSpectrum(sa, header))
					continue;
				
				vector<mass_t> pms_with_19;
				vector<int>    charges;

				// output m/z and prob values for the different charge states
				allScoreModels->selectPrecursorMassesAndCharges(config_, s, pms_with_19, charges);
				if (pms_with_19.size()<=0)
					continue;
			
				s.setCharge(charges[0]);
				prm.create_graph_from_spectrum(allScoreModels, &s,pms_with_19[0]);

				vector<NodeType> spec_prms;
				vector<mass_t>   exp_masses;
				const mass_t true_mass_with_19 = s.get_true_mass_with_19();
				s.getPeptide().calc_expected_breakage_masses(config_,exp_masses);

				int i;
				for (i=1; i<prm.get_num_nodes()-1; i++)
				{
					const Node& node = prm.get_node(i);
					if (node.score == 0)
						continue;
					
					NodeType nt;

					nt.type = 0;
					int j;
					for (j=0; j<exp_masses.size(); j++)
						if (fabs(exp_masses[j]-node.mass)<config_->getTolerance())
						{
							nt.type=1;
							break;
						}
					
					if (nt.type<=0)
					{
						int j;
						for (j=0; j<exp_masses.size(); j++)
							if (fabs(true_mass_with_19 - exp_masses[j] -node.mass-MASS_PROTON)<config_->getTolerance())
							{
								nt.type=2;
								break;
							}
					}

					nt.org_score = node.score;
					nt.mod_score = node.score;
					nt.region = node.breakage.region_idx;
					spec_prms.push_back(nt);
				}
				all_prms.push_back(spec_prms);
			}
		
	
			vector< vector< double > > per_pre, per_suf, per_covered;
			vector<float> deltas;

			per_pre.resize(num_regions);
			per_suf.resize(num_regions);
			per_covered.resize(num_regions);

			float delta;
			for (delta = min_delta; delta<=max_delta; delta+=step )
			{
				// perform mods
				int a;
				for (a=0; a<all_prms.size(); a++)
				{
					int b;
					for (b=0; b<all_prms[a].size(); b++)
					{
						NodeType& nt = all_prms[a][b];
						if (nt.org_score< -delta)
						{
							nt.mod_score = NEG_INF;
							continue;
						}
						nt.mod_score = nt.org_score + delta;
					}
				}

				// compute stats (if score is negative treat as 0)
				vector<double> num_pre,num_suf;
				vector<double> num_pre_wpos, num_suf_wpos;
				vector<double> score_pre, score_suf, total_score;
			

				num_pre.resize(num_regions,0);
				num_suf.resize(num_regions,0);
				num_pre_wpos.resize(num_regions,0);
				num_suf_wpos.resize(num_regions,0);
				score_pre.resize(num_regions,0);
				score_suf.resize(num_regions,0);
				total_score.resize(num_regions,0);
				
				for (a=0; a<all_prms.size(); a++)
				{
					int b;
					for (b=0; b<all_prms[a].size(); b++)
					{
						const int   type =    all_prms[a][b].type;
						const float score =   all_prms[a][b].mod_score;
						const int   region =  all_prms[a][b].region;

						if (type == 1)
						{
							num_pre[region]++;
							if (score>0)
							{
								num_pre_wpos[region]++;
								score_pre[region]+= score;
							}
						}

						if (type == 2)
						{
							num_suf[region]++;
							if (score>0)
							{
								num_suf_wpos[region]++;
								score_suf[region]+=score;
							}
						}

						if (score>0)
							total_score[region]+=score;
					}
				}

				
				deltas.push_back(delta);
				int r;
				for (r=0; r<num_regions; r++)
				{
					per_pre[r].push_back(num_pre_wpos[r]/num_pre[r]);
					per_suf[r].push_back(num_suf_wpos[r]/num_suf[r]);
					per_covered[r].push_back((score_pre[r]+score_suf[r])/total_score[r]);
				}
			}

			// report
			int r;
			for (r=0; r<num_regions; r++)
			{
				cout << endl << "Region " << r << endl;
				int d;
				for (d=0; d<deltas.size(); d++)
					cout << "\t" << deltas[d];
				cout << endl << "% Pre";
				for (d=0; d<per_pre[r].size(); d++)
					cout << "\t" << per_pre[r][d];
				cout << endl << "% Suf";
				for (d=0; d<per_suf[r].size(); d++)
					cout << "\t" << per_suf[r][d];
				cout << endl << "% Cov";
				for (d=0; d<per_covered[r].size(); d++)
					cout << "\t" << per_covered[r][d];
				cout << endl;

				// select
				float target_val = target_mid_ratio;
				if (r==0 || r == num_regions-1)
					target_val = target_side_ratio;

				float best_val=POS_INF;
				float best_delta=0;

				for (d=0; d<deltas.size(); d++)
					if (fabs(per_pre[r][d]-target_val)<best_val)
					{
						best_val = fabs(per_pre[r][d]-target_val);
						best_delta = deltas[d];
					}
				
				cout << "Chose delta = " << best_delta << endl << endl;
				regional_prm_normalizers[c][s][r]=best_delta;
			}	
		}
	}

	indNormzlizersInitialized_ = true;
	write_prm_normalizer_values();
}
Ejemplo n.º 4
0
/***************************************************************************************
This function touches up inspect search results by rescoring the sequences returned by
inspect. The function produces a new inspect results file with the scores (and delta scores)
replaced.
****************************************************************************************/
void PeptideRankScorer::rescore_inspect_results(char *spectra_file, 
											   char *inspect_res, 
											   char *new_res_file) const
{
	AllScoreModels* allScoreModels = static_cast<AllScoreModels*>(this->allScoreModelsPtr_);
	Config *config = allScoreModels->get_config();

	ifstream org_res(inspect_res);

	if (!  org_res.is_open() || ! org_res.good())
	{
		cout << "Error: couldn't open original inspect results file for reading:" << inspect_res << endl;
		exit(1);
	}

	ofstream new_res(new_res_file);
	if (! new_res.is_open() || ! new_res.good())
	{
			cout << "Error: couldn't open new inspect results file for writing:" << new_res << endl;
		exit(1);
	}

	char line_buff[1024];
	org_res.getline(line_buff,1024);

	bool read_line  = true;
	vector<string> field_names;
	if (line_buff[0] != '#')
	{
		read_line = false;
	}
	else
	{
		string header = string(line_buff);
		split_string(header,field_names);

	//	int i;
	//	for (i=0; i<field_names.size(); i++)
	//		cout << i << "\t" << field_names[i] << endl;
		cout << "Header:" << endl << line_buff << endl;
	}


	vector<ScanCandidateSet> cand_sets;
	vector<int> scan_mapping;
	cand_sets.clear();
	scan_mapping.resize(100000,-1);
	
	while (! org_res.eof())
	{
		vector<string> fields;

		if (read_line)
		{
			org_res.getline(line_buff,1024);
			if (org_res.gcount() < 5)
				continue;
		}
		else
		{
			read_line = true;
		}

		split_string(line_buff,fields);
		InspectResultsLine res;

		res.parse_from_fields(config,fields);

		if (cand_sets.size()==0 || ! cand_sets[cand_sets.size()-1].add_new_line(res))
		{
			ScanCandidateSet new_set;
			new_set.add_new_line(res);
			
			if (new_set.scan>=scan_mapping.size())
				scan_mapping.resize(2*scan_mapping.size(),-1);

			scan_mapping[new_set.scan]=cand_sets.size();
			cand_sets.push_back(new_set);
		}
	}
	org_res.close();

	cout << "Read results for " << cand_sets.size() << " scans..." << endl;


	SpectraAggregator sa;
	sa.initializeFromSpectraFilePath(spectra_file, config);

	SpectraList sl(sa);
	sl.selectAllAggregatorHeaders();

	cout << "Read " <<  sl.getNumHeaders() << " spectra headers." << endl;

	if (sl.getNumHeaders() == 0)
	{
		cout << "Error: read not spectra headers from " << spectra_file << endl;
		return;
	}

	
	vector<bool> spectrum_indicators;
	spectrum_indicators.resize(cand_sets.size(),false);

	int num_found =0;
	int sc;
	for (sc=0; sc<sl.getNumHeaders(); sc++)
	{
		const SingleSpectrumHeader* header = sl.getSpectrumHeader(sc);		
		int scan_number = (header->getScanNumber() >=0 ? header->getScanNumber() : header->getIndexInFile());
		
		if (header->getFileType() == IFT_MGF)
			scan_number = header->getIndexInFile();

		assert(scan_number>=0);
		if (scan_mapping[scan_number]<0)
			continue;

		AnnotatedSpectrum as;
		if (! as.readSpectrum(sa, header))
		{
			continue;
		}

		spectrum_indicators[scan_mapping[scan_number]]=true;
		num_found++;

		ScanCandidateSet& cand_set = cand_sets[scan_mapping[scan_number]];
		
		vector<PeptideSolution> peptide_sols;
		peptide_sols.resize(cand_set.results.size());

		int j;
		for (j=0; j<cand_set.results.size(); j++)
		{
			InspectResultsLine& inspect_res = cand_set.results[j];
			PeptideSolution& sol = peptide_sols[j];

			sol.pep = inspect_res.pep;
			sol.pm_with_19 = sol.pep.get_mass_with_19();
			sol.charge = inspect_res.Charge;
			sol.reaches_n_terminal = true;
			sol.reaches_c_terminal = true;
		}

		vector<score_pair> scores;
	//	score_complete_sequences(peptide_sols,ssf,peaks,num_peaks,scores);
		scoreCompleteSequences(peptide_sols, as, scores);

		for (j=0; j<scores.size(); j++)
			cand_set.results[j].Score = scores[j].score;

		cand_set.recalbirate_scores(config);

		vector<string> pep_strings;
		pep_strings.resize(scores.size());
		int max_len =0;
		for (j=0; j<cand_set.results.size(); j++)
		{
			pep_strings[j]=cand_set.results[j].pep.as_string(config);
			if (pep_strings[j].length()>max_len)
				max_len = pep_strings[j].length();
		}

		if (1)
		{
			cand_set.output_to_stream(new_res,10);
		}
		else
		{
			for (j=0; j<cand_set.results.size(); j++)
			{
				cout << cand_set.scan << " " << cand_set.results[j].Charge << "\t";

				cout << cand_set.results[j].Protein.substr(0,3) << " " << pep_strings[j];
				if (pep_strings[j].length()<max_len)
				{
					int k;
					for (k=pep_strings[j].length(); k<max_len; k++)
						cout << " ";
				}
				cout << "\t" << cand_set.results[j].MQScore << "\t" << cand_set.results[j].Score << "\t" <<
				cand_set.results[j].DeltaScore << "\t" << cand_set.results[j].DeltaScoreOther << endl;
			}
			cout << endl;
		}
	}

	if (num_found<cand_sets.size())
	{
		cout << "Warning: found only " << num_found << "/" << cand_sets.size() << " of the scans scored by InsPecT!" << endl;
	}
	else
	{
		cout << "All scored scans found in spectrum file." << endl;
	}
}
Ejemplo n.º 5
0
void PrmNodeScoreModel::trainNodeScoreModels(void* allScoreModelsVoidPointer,
											 const char *name, 
											 const SpectraAggregator& sa,
											 int specificCharge, 
											 int specificSize, 
											 int specificRegion)
{
	AllScoreModels* allScoreModels = static_cast<AllScoreModels*>(allScoreModelsVoidPointer);
	config_ = allScoreModels->get_config();
	// resize regional breakage score models according to regional fragment sets
	const vector< vector< vector< RegionalFragments > > >& all_rfs = config_->get_regional_fragment_sets();

	int c;
	RegionalPrmNodeScoreModels_.resize(all_rfs.size());
	for (c=0; c<all_rfs.size(); c++)
	{
		RegionalPrmNodeScoreModels_[c].resize(all_rfs[c].size());
		int s;
		for (s=0; s<all_rfs[c].size(); s++)
		{
			RegionalPrmNodeScoreModels_[c][s].resize(all_rfs[c][s].size());
			int r;
			for (r=0; r<RegionalPrmNodeScoreModels_[c][s].size(); r++)
				if (! RegionalPrmNodeScoreModels_[c][s][r].get_was_initialized())
					RegionalPrmNodeScoreModels_[c][s][r].init(config_,c,s,r);
		}
	}


	// train models
	for (c=1; c<RegionalPrmNodeScoreModels_.size(); c++)
	{
		if (RegionalPrmNodeScoreModels_.size() == 0 || (specificCharge>0 && specificCharge != c))
			continue;

		if (sa.getNumSpectraWithCharge(c)<200)
		{
			cout << "WARNING: insufficient number of spectra to train breakage model for charge " << c << endl;
			cout <<	"		  only " << sa.getNumSpectraWithCharge(c) << " spectra were found so this charge is being skipped!" << endl << endl;
			continue;
		}

		int s;
		for (s=0; s<RegionalPrmNodeScoreModels_[c].size(); s++)
		{
			if (specificSize>=0 && s != specificSize)
				continue;

			int r;
			for (r=0; r<RegionalPrmNodeScoreModels_[c][s].size(); r++)
			{
				if (specificRegion>=0 && r != specificRegion)
					continue;
				
				RegionalPrmNodeScoreModels_[c][s][r].trainRegionalScoreModel(allScoreModelsVoidPointer, name, sa);
			}
		}
	}

	// train PRM normalizer values
//	cout << endl << "Training PRM normalizer vlaues..." << endl;

// TODO fix this issue, it needs to use the AllScoreModels class
//	learn_prm_normalizer_values(fm);

	ind_was_initialized=true;
}