Exemple #1
0
bool InputReader::OpenInputFile(std::string p) {

	
	std::vector<std::vector<int>*>   left_matrix;
	std::vector<std::vector<int>*>   right_matrix;
	unsigned int rows = 0;
	unsigned int cols = 0;


	/* MATRICE SINISTRA */
	std::ifstream left_file((p+"/left_side").c_str());

	/* is there the left matrix? */
	if (!left_file.is_open()) {
		perror("ERROR: cannot open input file");
		printf("%s\n", (p+"/left_side").c_str());
		return false;
	};

	/* read matrix */
	std::vector<int> totals_left;
	while (left_file.good()) {
		std::string riga ;
		std::getline(left_file, riga);		
		unsigned int total_left = 0;
		std::vector<int>* ret = split_string_integers(riga, '\t', &total_left);
		totals_left.push_back(total_left);		
		left_matrix.push_back(ret);
	};

	left_file.close();

	rows = left_matrix.size();
	cols = left_matrix.back()->size();
	
	/* Initialize compressed ODE matrix */
	for ( unsigned int i=0; i<cols; i++ ) {
		this->comp_ODE.push_back( new std::vector<int> );
	}

	/* 
		RE-parsing matrice sinistra 
	*/
	for (unsigned int r=0; r<rows; r++) {
		for (unsigned int c=0; c<cols; c++) {
			switch ( left_matrix[r]->at(c) ) {

			case 0:
				break;

			case 1:

				this->comp_ODE[ c ]->push_back( totals_left[ r ] +1 );
				this->comp_ODE[ c ]->push_back( -(r+1) );

				for ( unsigned int cl=0; cl<cols; cl++ ) {
					switch ( left_matrix[r]->at(cl) ) {

					case 0: 
						break;
					case 1:
						this->comp_ODE[ c ]->push_back( cl );
						break;
					case 2:
						this->comp_ODE[ c ]->push_back( cl );
						this->comp_ODE[ c ]->push_back( cl );
						break;
					default: 
						perror("ERROR: unsupported case");
						break;
					}
				}

				break;

			default:
				perror("ERROR: unsupported case");
				break;
			}
		}
	}

	
	/* 
		MATRICE DESTRA 
		La matrice destra genera direttamente i prodotti delle reazioni, 
		compaiono tutti con una costante positiva e possono essere aggiunti on-fly.	
	*/
	std::ifstream right_file((p+"/right_side").c_str());		

	/* is there the right matrix? */
	if (!right_file.is_open()) {
		perror("ERROR: cannot open input file");
		printf("%s\n", (p+"/right_side").c_str());
		return false;
	};

	/* read matrix */	
	unsigned int total_right = 0;
	while (right_file.good()) {
		std::string riga ;
		std::getline(right_file, riga);		
		std::vector<int>* ret = split_string_integers(riga, '\t', &total_right);
		right_matrix.push_back(ret);

		/* 
			ret is a single row of the stoichometric matrix.
			we parse it and put the corresponding value into the compressed matrix.
		*/
		for ( unsigned c=0; c<cols; c++ ) {
			
			switch ( ret->at(c) ) {

				case 0: 
					break;
				case 1: 
					/* 
						We have counted how many stoichiometric values are different from 0 
						in the left matrix. We use this information to fill the first 
						value of the compressed matrix. We make +1 because we also consider
						the (unknown) kinetic constant.
					*/
					this->comp_ODE[ c ]->push_back( totals_left[ right_matrix.size()-1 ] +1 );
					this->comp_ODE[ c ]->push_back( right_matrix.size() );
					for (unsigned int cl=0; cl<cols; cl++) {
						switch ( left_matrix[ right_matrix.size()-1 ]->at(cl) ) {
							case 0: 
								break;
							case 1:
								this->comp_ODE[ c ]->push_back( cl );
								break;
							case 2:
								this->comp_ODE[ c ]->push_back( cl );
								this->comp_ODE[ c ]->push_back( cl );
								break;
							default:
								perror("ERROR: unsupported case");
								break;
						} // end switch
					} // end for
					break;
				case 2:
					perror("ERROR: unsupported case");
					break;
				default:
					perror("ERROR: unsupported case");
					break;

			} // end switch		
		} // end for		
	}; // end while

	/* Terminators */
	for ( unsigned int i=0; i<cols; i++) 
		this->comp_ODE[ i ]->push_back( 0 );
	
	right_file.close();


	return true;
}
void SRAssembler::do_preprocessing(int lib_idx, int file_part){
	Library lib = this->libraries[lib_idx];

	logger->info("preprocessing lib " + int2str(lib_idx + 1) + ", reads file (" + int2str(file_part) + "/" + int2str(lib.get_num_parts()) + ")");
	char suffixc[3];
	suffixc[0] = (char)(((file_part-1) / 26) + 97);
	suffixc[1] = (char)(((file_part-1) % 26) + 97);
	suffixc[2] = '\0';
	string suffix(suffixc);
	string left_src_read = lib.get_prefix_split_src_file(lib.get_left_read()) + suffix;
	string right_src_read = "";
	if (lib.get_paired_end())
	    right_src_read = lib.get_prefix_split_src_file(lib.get_right_read()) + suffix;
	ifstream left_file(left_src_read.c_str());
	ifstream right_file;
	if (lib.get_paired_end()){
		right_file.open(right_src_read.c_str(), ios_base::in);
	}
	string left_header = "";
	string right_header = "";
	string left_seq = "";
	string right_seq = "";
	string left_qual = "";
	string right_qual = "";
	string plus;
	ofstream split_read_fasta_file;
	ofstream split_read_fastq_file;
	split_read_fasta_file.open(lib.get_split_file_name(file_part, FORMAT_FASTA).c_str(), ios_base::out);
	if (lib.get_format() == FORMAT_FASTQ)
	    split_read_fastq_file.open(lib.get_split_file_name(file_part, FORMAT_FASTQ).c_str(), ios_base::out);
	while (getline(left_file, left_header)) {
		// get read data point, which includes 4 lines
		string lead_chr = (lib.get_format() == FORMAT_FASTQ)? "@" : ">";
		if (left_header.substr(0,1) == lead_chr){
		 //save left-end reads
			getline(left_file, left_seq);
			if (lib.get_format() == FORMAT_FASTQ) {
				getline(left_file, plus);
				getline(left_file, left_qual);
			}
			if (lib.get_paired_end()){
				 //save right-end reads
				 while (getline(right_file, right_header))
					 if (right_header.substr(0,1) == lead_chr)
						 break;
				 getline(right_file, right_seq);
				 if (lib.get_format() == FORMAT_FASTQ) {
					 getline(right_file, plus);
					 getline(right_file, right_qual);
				 }
			}
			if (lib.get_format() == FORMAT_FASTQ) {
				split_read_fastq_file << left_header << endl
									  << left_seq << endl
									  << "+" << endl
									  << left_qual << endl;
			}
			split_read_fasta_file << ">" << left_header.substr(1) << endl << left_seq << endl;
			if (lib.get_paired_end()){
				if (lib.get_format() == FORMAT_FASTQ) {
					split_read_fastq_file << right_header << endl
								 << right_seq << endl
								 << "+" << endl
								 << right_qual << endl;
				}
				split_read_fasta_file << ">" << right_header.substr(1) << endl << right_seq << endl;
			}
		 }
	}
	split_read_fasta_file.close();
	if (lib.get_format() == FORMAT_FASTQ)
		split_read_fastq_file.close();
	left_file.close();
	if (lib.get_paired_end())
		right_file.close();
	string cmd = "rm " + left_src_read + " " + right_src_read;
	run_shell_command(cmd);
}