// CSI 2131 Assignment 1 Part 1.1 Removing low-complexity segments
// Winter 2006 - Prof. L. Moura
// Solution by TA Naim R. El-Far (naim@discover.uottawa.ca)

// File:	lowcomp.cpp 
// Desc:	Driver file containing the whole project's source code
// Func:	This program, all contained in this cpp file, reads a genome sequence from a file replacing low
//			complexity segments with the character 'X' in the original file

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
	//Print welcome message
	//---------------------
	cout << "CSI 2131 Asst 1 Part 1.1" << endl
		 << "Solution by TA Naim R. El-Far (naim@discover.uottawa.ca)" << endl
		 << endl;

	//Prompt user to input genome file name (default: genome1.txt)
	//------------------------------------------------------------
	cout << "Please enter genome file name (default: genome1.txt): ";
	string genomeFileName = "";
	getline(cin, genomeFileName, '\n');
	if (genomeFileName == "")
		genomeFileName = "genome1.txt";

	//Open genome file
	//----------------
	fstream genomeFile(genomeFileName.c_str(), ios::in  | ios::out);
	if (!genomeFile) {
		cerr << endl
			 << "-------------------------------------------------" << endl
			 << "Error! Specified genome file could not be opened!" << endl
			 << "-------------------------------------------------" << endl
			 << endl;
		return 1;
	}

	//Prompt user to input lowcomp file name (default: lowcomp1.txt)
	//--------------------------------------------------------------
	cout << "Please enter lowcomp file name (default: lowcomp1.txt): ";
	string lowcompFileName = "";
	getline(cin, lowcompFileName, '\n');
	if (lowcompFileName == "")
		lowcompFileName = "lowcomp1.txt";
	
	//Open lowcomp file
	//-----------------
	fstream lowcompFile(lowcompFileName.c_str(), ios::in);
	if (!lowcompFile) {
		cerr << endl
			 << "--------------------------------------------------" << endl
			 << "Error! Specified lowcomp file could not be opened!" << endl
			 << "--------------------------------------------------" << endl
			 << endl;
		return 1;
	}
	
	//Read from lowcompFile changing genomeFile as specified in the assignment description
	//------------------------------------------------------------------------------------
	int begPos = -1;							//This variable will hold the beginning position of the segment to be replaced
	int endPos = -1;							//This variable will hold the end position of the segment to be replaced
	char replacedChar = ' ';					//This variable will hold the character that is to be replaced
	const char replacingChar = 'X';				//Replace characters in the sequence with this character
	lowcompFile >> begPos >> endPos;			//Read in the beginning and ending positions from the file
	while (!lowcompFile.eof()) {	
		genomeFile.seekp(begPos);				
		cout << "Replacing: ";
		for (int i = 0 ; i < (endPos - begPos + 1) ; i++) {	//For as many times as endPos - begPos + 1 ...
			int posBeforeRead = genomeFile.tellp();		
			genomeFile.read(&replacedChar, 1);				//... read a character and
			cout << replacedChar;							//... print it to the screen, then
			int posAfterRead = genomeFile.tellp();
			
			genomeFile.seekp(posBeforeRead);
			genomeFile.write(&replacingChar, 1);			//... write it to the file.
			genomeFile.seekp(posAfterRead);
		}
		cout << endl;
		lowcompFile >> begPos >> endPos;		//Read in the beginning and ending positions from the file
	}

	//Housekeeping
	//------------
	genomeFile.close();		//Closing a file is not necessary since the fstream destructor does that for you
	lowcompFile.close();	//but it's a good programming habit
	
	//Successful termination of main
	//------------------------------
	return 0;
}


