Beispiel #1
0
int main (int argc, char ** argv)
{
	//const clock_t begin_time = clock();
	
	if (argc<3) {
		print_usage ();
	}
	
	////////////////////////////////////////////////////////////
	// Init parameters
	//
	std::string input_file_name;
	std::string bv_file_name;
	std::string output_file_name;
	bool compress = false;
	
	////////////////////////////////////////////////////////////
	// Read command line arguments
	//
	int arg_pos = 1;
	while (arg_pos < argc){
		std::string flag = argv[arg_pos];
		if (flag[0] != '-') {
			if (input_file_name.empty()) {
				input_file_name = flag;
			} else if (bv_file_name.empty()){
				bv_file_name = flag;
			} else {
				std::cerr << "The mandatory files are already set, unknown file " << flag << " -> ignore\n";
			}
		} else if (flag.compare("-o") == 0) {
			arg_pos++;
			output_file_name = argv[arg_pos];
		} else if (flag.compare("-h") == 0) {
			print_usage ();
			return 0;
		} else if (flag.compare("-v") == 0) {
			std::cout << "\nextract_reads version " << version << "\n";
			return 0;
		} else {
			std::cerr << "Unknown option " << flag << "\n";
            print_usage ();
			return (0);
		}
		arg_pos++;
	}
	
	if (input_file_name.empty()) {
		std::cerr << "Error: An input file name is needed -> exit\n";
		print_usage ();
		return (0);
	} else if (bv_file_name.empty()) {
		std::cerr << "Error: A bv file name is needed -> exit\n";
		print_usage ();
		return (0);
	}
	
	////////////////////////////////////////////////////////////
	// Open the given file to check its type (fasta, fastq, gzip ?)
	//
	ReadFile * read_file = NULL;
	
	std::ifstream infile;
	infile.open(input_file_name.c_str());
	if (!infile.good()) {
		std::cerr << "Cannot open file file " << input_file_name << " -> ignore\n";
	}
	// Check the first char
	std::string basename = input_file_name.substr(input_file_name.rfind("/")+1);
	char c = infile.get();
	if (c == '>') {
		infile.close();
		read_file = new FastaFile(input_file_name, bv_file_name);
	} else if (c == '@') {
		infile.close();
		read_file = new FastqFile(input_file_name, bv_file_name);
	} else {
		infile.close();
		gzFile tmp_gz_file = (gzFile) gzopen(input_file_name.c_str(), "r");
		if (!tmp_gz_file) {
			std::cerr << "Cannot open file " << input_file_name << " -> ignore\n";
			exit(1);
		}
		c = gzgetc(tmp_gz_file);
		if (c == '>') {
			gzclose(tmp_gz_file);
			compress = true;
			read_file = new GzFastaFile(input_file_name, bv_file_name);
		} else if (c == '@') {
			gzclose(tmp_gz_file);
			compress = true;
			read_file = new GzFastqFile(input_file_name, bv_file_name);
		} else {
			std::cerr << "Unknown format: " << input_file_name << " -> ignore\n";
		}
	}
	
	////////////////////////////////////////////////////////////
	// Open the output file and write selected reads in it
	// Different behaviors if : - output file name is given
	//                          - compress is true or not
	//
	if (compress) {
		if (output_file_name.empty()) {
			std::cerr << "Error, try to compress results but no output file name is given\n";
			exit(1);
		}
		gzFile filetmp = (gzFile) gzopen(output_file_name.c_str(), "w6");
		if (filetmp == NULL) {
			std::cerr << "Error, cannot open file " << output_file_name << "\n";
		}
		std::string & current_read = read_file->get_next_read();
		while (!current_read.empty()) {
			gzprintf(filetmp, "%s", read_file->get_data().c_str());
			current_read = read_file->get_next_read();
		}
		gzclose(filetmp);
	} else {
		if (!output_file_name.empty()) {
			std::ofstream outfile;
			outfile.open (output_file_name.c_str());
			if (!outfile.good()) {
				std::cerr << "Cannot write on file " << output_file_name << "\n";
				return 1;
			}
			std::string & current_read = read_file->get_next_read();
			while (!current_read.empty()) {
				outfile << read_file->get_data();
				current_read = read_file->get_next_read();
			}
			outfile.close();
		} else {
			std::string & current_read = read_file->get_next_read();
			while (!current_read.empty()) {
				std::cout << read_file->get_data();
				current_read = read_file->get_next_read();
			}
		}
	}
	
	if (read_file != NULL) {
		delete read_file;
	}
	return 0;
}