/* Author: Amay Agrawal
 * Project: Bacteria Cloud
 * Mentor: Prof. Manish K Gupta
 */
import java.util.*;
import java.io.*;
public class Data_insertion1 {
	static String finalstring[][];
	File file = new File("C:\\Users\\HP\\Desktop\\Plasmids\\final_output1.txt");
	BufferedWriter bw = null;
	public Data_insertion1(String ab[][]){
		finalstring = ab;
	}
	
	public void insertsequence() throws IOException{
	
		FileWriter fw = new FileWriter(file);
		  bw = new BufferedWriter(fw);
		BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\Users\\HP\\Desktop\\Plasmids\\encoded.txt"));
		BufferedReader bufferedReader1 = new BufferedReader(new FileReader("C:\\Users\\HP\\Desktop\\Plasmids\\output.txt"));
		BufferedReader bufferedReader2 = new BufferedReader(new FileReader("C:\\Users\\HP\\Desktop\\Plasmids\\pBR322.txt"));
		BufferedReader bufferedReader3 = new BufferedReader(new FileReader("C:\\Users\\HP\\Desktop\\Plasmids\\output.txt"));
		String line = null;
		String pbr322 = null;
		String line1 = null;
		String encoded = null;
		String line2 = null;
		int length1=0;
		int length2=0;
		int length=0;
		int endlength=0;
		int counter = 0;
		String line3 = null;
		String final2 = "";
		while ((line = bufferedReader2.readLine()) != null){
			pbr322 = line;
		}
		
		while ((line1 = bufferedReader.readLine()) != null){
			encoded = line1;
		}
		
		int search = Integer.parseInt(finalstring[0][0]);
		final2+=pbr322.substring(0, search);//length,search
		while ((line2 = bufferedReader1.readLine()) != null){
			
			String parts [] = line2.split(" ");
			if(search==Integer.parseInt(parts[0])){
				int ab = Integer.parseInt(parts[2]);
				String bc = parts[3];
				String sub = bc.substring(ab, bc.length());
				length=sub.length();
				String final1 = sub+encoded;
				final2+=final1;
				int total = search+final1.length();
				for(int j=0;j<finalstring.length;j++){
					if(Integer.parseInt(finalstring[j][0])>total){
						counter = Integer.parseInt(finalstring[j][0]);
						endlength = counter;
						break;
					}
				}
				
				while ((line3 = bufferedReader3.readLine()) != null){
					String parts1[] = line3.split(" ");
					int gh = Integer.parseInt(parts1[0]);
					if(gh==counter){
						int hh = Integer.parseInt(parts1[2]);
						String hc = parts1[3];
						String sub1 = hc.substring(hh, hc.length());
						length1=counter-total-sub1.length();
						length2=sub1.length();
						for(int y=0;y<(counter-total-sub1.length());y++){
							final2+='A';
						}
						final2+=sub1;
						final2+=pbr322.substring(counter);
						bw.write(final2);
					}
				}
			}

		}
		bw.close();
		Declone d = new Declone(search,endlength,length,length1,length2);
		d.amay();
	}
}
