| 0 | 1 package pairedendCrossPlot; | 
|  | 2 | 
|  | 3 import java.io.File; | 
|  | 4 import java.io.FileNotFoundException; | 
|  | 5 import java.io.PrintStream; | 
|  | 6 import java.sql.Timestamp; | 
|  | 7 import java.util.ArrayList; | 
|  | 8 import java.util.Date; | 
|  | 9 import java.util.Scanner; | 
|  | 10 | 
|  | 11 public class pairedendCrossPlot { | 
|  | 12 | 
|  | 13 	private static File READ1 = null; | 
|  | 14 	private static File READ2 = null; | 
|  | 15 	private static File OUTPUT = null; | 
|  | 16 	private static int WINDOW = 500; | 
|  | 17 | 
|  | 18 	private static int[] BP = null; | 
|  | 19 	private static int[] FOR = null; | 
|  | 20 	private static int[] REV = null; | 
|  | 21 | 
|  | 22 	private static ArrayList<String> chrName = null; | 
|  | 23 	private static ArrayList<Integer> chrStart = null; | 
|  | 24 | 
|  | 25 	private static int[] FHIST = null; | 
|  | 26 	private static int[] RHIST = null; | 
|  | 27 | 
|  | 28 	public static void main(String[] args) throws FileNotFoundException { | 
|  | 29 		loadConfig(args); //Load configuration data | 
|  | 30 		FHIST = new int[(WINDOW * 2) + 1]; //Initialize master histograms | 
|  | 31 		RHIST = new int[(WINDOW * 2) + 1]; //Initialize master histograms | 
|  | 32 		System.out.println("\n" + getTimeStamp()); | 
|  | 33 | 
|  | 34 		//Pass through the Read2 file once in order to identify the start and stops of all chromosomes present in file | 
|  | 35 		System.out.println("Indexing Read 2 File..."); | 
|  | 36 		indexFile(READ2); | 
|  | 37 		System.out.println("Indexing Complete\n" + getTimeStamp()); | 
|  | 38 | 
|  | 39 		//Parse read 1 | 
|  | 40 		System.out.println("\n" + getTimeStamp() + "\nParsing Read 1 File..."); | 
|  | 41 		parseRead1(READ1); | 
|  | 42 		System.out.println("\nRead 1 Parsed\n" + getTimeStamp()); | 
|  | 43 | 
|  | 44 		//Output histogram | 
|  | 45 		System.out.println("\nOutputing final histogram..."); | 
|  | 46 		outputHist(OUTPUT); | 
|  | 47 		System.out.println("Program Complete\n" + getTimeStamp()); | 
|  | 48 | 
|  | 49 	} | 
|  | 50 | 
|  | 51 	public static void outputHist(File out) throws FileNotFoundException { | 
|  | 52 		PrintStream OUT = new PrintStream(out); | 
|  | 53 		for(int x = WINDOW * -1; x <= WINDOW; x++) { | 
|  | 54 			OUT.print("\t" + x); | 
|  | 55 		} | 
|  | 56 		OUT.print("\nForward_Tags"); | 
|  | 57 		for(int x = 0; x < FHIST.length; x++) { | 
|  | 58 			OUT.print("\t" + FHIST[x]); | 
|  | 59 		} | 
|  | 60 		OUT.print("\nReverse_Tags"); | 
|  | 61 		for(int x = 0; x < RHIST.length; x++) { | 
|  | 62 			OUT.print("\t" + RHIST[x]); | 
|  | 63 		} | 
|  | 64 		OUT.close(); | 
|  | 65 	} | 
|  | 66 | 
|  | 67 	public static void parseRead1(File input) throws FileNotFoundException { | 
|  | 68 		Scanner scan = new Scanner(READ1); | 
|  | 69 		String currentChrom = ""; | 
|  | 70 		int currentIndex = 0; | 
|  | 71 		while (scan.hasNextLine()) { | 
|  | 72 			String temp = scan.nextLine(); | 
|  | 73 			if(!temp.contains("index") && !temp.contains("#")) { | 
|  | 74 				String[] array = temp.split("\t"); | 
|  | 75 				int POS = Integer.parseInt(array[1]); | 
|  | 76 				int F = Integer.parseInt(array[2]); | 
|  | 77 				int R = Integer.parseInt(array[3]); | 
|  | 78 | 
|  | 79 				//Check if newline's chrom is equal to current chrom in memory, reload new chrom if unequal | 
|  | 80 				if(!currentChrom.equals(array[0])) { | 
|  | 81 					BP = null; //reset BP immediately | 
|  | 82 					int newIndex = chrName.indexOf(array[0]); | 
|  | 83 					if(newIndex >= 0) { | 
|  | 84 						System.out.println("\nLoading: " + chrName.get(newIndex)); | 
|  | 85 						loadRead2(READ2, chrName.get(newIndex), chrStart.get(newIndex), chrStart.get(newIndex + 1)); | 
|  | 86 						currentChrom = array[0]; | 
|  | 87 						currentIndex = 0; | 
|  | 88 						System.out.println("Loading Complete\n" + getTimeStamp()); | 
|  | 89 					} | 
|  | 90 				} | 
|  | 91 				if(BP != null) { | 
|  | 92 					//Update local indexes to only upload data from local tag region defined by window | 
|  | 93 					while(POS - WINDOW > BP[currentIndex] && currentIndex < BP.length - 1) { currentIndex++; } //Find furthest upstream index in read2 | 
|  | 94 					int currentStop = currentIndex; | 
|  | 95 			        while(POS + WINDOW > BP[currentStop] && currentStop < BP.length - 1) { currentStop++; } //Find furthest downstream index in read2 | 
|  | 96 | 
|  | 97 			        //Populate histogram based on read2 tags flanking current position | 
|  | 98 			        int[] temp_F = new int[(WINDOW * 2) + 1]; | 
|  | 99 			        int[] temp_R = new int[(WINDOW * 2) + 1]; | 
|  | 100 			        for(int x = currentIndex; x <= currentStop; x++) { | 
|  | 101 			        	int index = BP[x] - POS + WINDOW; | 
|  | 102 			        	if(index >= 0 && index < temp_F.length) { | 
|  | 103 			        		temp_F[index] += FOR[x]; | 
|  | 104 			        		temp_R[index] += REV[x]; | 
|  | 105 			        	} | 
|  | 106 			        } | 
|  | 107 | 
|  | 108 			        //Populate master histogram based on score | 
|  | 109 			        for(int x = 0; x < temp_F.length; x++) { | 
|  | 110 			                FHIST[x] += (temp_F[x] * F); | 
|  | 111 			                RHIST[x] += (temp_R[x] * F); | 
|  | 112 			        } | 
|  | 113 			        //Reverse arrays | 
|  | 114 			        for(int x = 0; x < FHIST.length / 2; x++) { | 
|  | 115 			        	int tempF = temp_F[x]; | 
|  | 116 			        	int tempR = temp_R[x]; | 
|  | 117 			        	temp_F[x] = temp_F[temp_F.length - x - 1]; | 
|  | 118 			            temp_F[temp_F.length - x - 1] = tempF; | 
|  | 119 			            temp_R[x] = temp_R[temp_R.length - x - 1]; | 
|  | 120 			            temp_R[temp_R.length - x - 1] = tempR; | 
|  | 121 			        } | 
|  | 122 			        for(int x = 0; x < FHIST.length; x++) { | 
|  | 123 			                FHIST[x] += (temp_R[x] * R); | 
|  | 124 			                RHIST[x] += (temp_F[x] * R); | 
|  | 125 			        } | 
|  | 126 				} | 
|  | 127 			} | 
|  | 128 		} | 
|  | 129 		scan.close(); | 
|  | 130 	} | 
|  | 131 | 
|  | 132 	private static void loadRead2(File input, String currentChrom, int lineStart, int lineStop) throws FileNotFoundException { | 
|  | 133 		BP = new int[lineStop - lineStart - 1]; | 
|  | 134 		FOR = new int[lineStop - lineStart - 1]; | 
|  | 135 		REV = new int[lineStop - lineStart - 1]; | 
|  | 136 | 
|  | 137 		int counter = 0; | 
|  | 138 		Scanner scan = new Scanner(READ2); | 
|  | 139 		while (scan.hasNextLine()) { | 
|  | 140 			String temp = scan.nextLine(); | 
|  | 141 				if(counter > lineStart) { | 
|  | 142 					String[] array = temp.split("\t"); | 
|  | 143 					if(array[0].equals(currentChrom)) { | 
|  | 144 						BP[counter - lineStart - 1] = Integer.parseInt(array[1]); | 
|  | 145 						FOR[counter - lineStart - 1] = Integer.parseInt(array[2]); | 
|  | 146 						REV[counter - lineStart - 1] = Integer.parseInt(array[3]); | 
|  | 147 					} | 
|  | 148 				} | 
|  | 149 			counter++; | 
|  | 150 		} | 
|  | 151 		scan.close(); | 
|  | 152 		//System.out.println(BP.length + "\t" + FOR.length + "\t" + REV.length); | 
|  | 153 	} | 
|  | 154 | 
|  | 155 	//Index Read 2 file for faster processing of chromosomes | 
|  | 156 	public static void indexFile(File input) throws FileNotFoundException { | 
|  | 157 		chrName = new ArrayList<String>(); | 
|  | 158 		chrStart = new ArrayList<Integer>(); | 
|  | 159 		Scanner scan = new Scanner(input); | 
|  | 160 		int linenumber = 0; | 
|  | 161 		while (scan.hasNextLine()) { | 
|  | 162 			String temp = scan.nextLine(); | 
|  | 163 			if(!temp.contains("index") && !temp.contains("#")) { | 
|  | 164 				String[] array = temp.split("\t"); | 
|  | 165 				if(!chrName.contains(array[0])) { | 
|  | 166 					chrName.add(array[0]); | 
|  | 167 					chrStart.add(new Integer(linenumber)); | 
|  | 168 				} | 
|  | 169 			} | 
|  | 170 			linenumber++; | 
|  | 171 		} | 
|  | 172 		chrStart.add(new Integer(linenumber)); | 
|  | 173 		scan.close(); | 
|  | 174 	} | 
|  | 175 | 
|  | 176 	public static void loadConfig(String[] command){ | 
|  | 177 		for (int i = 0; i < command.length; i++) { | 
|  | 178 			switch (command[i].charAt((1))) { | 
|  | 179 				case 'f': | 
|  | 180 					READ1 = new File(command[i + 1]); | 
|  | 181 					i++; | 
|  | 182 					break; | 
|  | 183 				case 'r': | 
|  | 184 					READ2 = new File(command[i + 1]); | 
|  | 185 					i++; | 
|  | 186 					break; | 
|  | 187 				case 'o': | 
|  | 188 					OUTPUT = new File(command[i + 1]); | 
|  | 189 					i++; | 
|  | 190 					break; | 
|  | 191 				case 'w': | 
|  | 192 					WINDOW = Integer.parseInt(command[i + 1]); | 
|  | 193 					i++; | 
|  | 194 					break; | 
|  | 195 			} | 
|  | 196 		} | 
|  | 197 		if(READ1 == null) { | 
|  | 198 			System.out.println("No Read 1 File loaded!!!\n"); | 
|  | 199 			printUsage(); | 
|  | 200 			System.exit(0); | 
|  | 201 		} | 
|  | 202 		if(READ2 == null) { | 
|  | 203 			System.out.println("No Read 2 File loaded!!!\n"); | 
|  | 204 			printUsage(); | 
|  | 205 			System.exit(0); | 
|  | 206 		} | 
|  | 207 		if(WINDOW < 1) { | 
|  | 208 			System.out.println("Invalid Window Size Selected!!!\n"); | 
|  | 209 			printUsage(); | 
|  | 210 			System.exit(0); | 
|  | 211 		} | 
|  | 212 | 
|  | 213 		if(OUTPUT == null) { | 
|  | 214 			OUTPUT = new File(READ1.getName().split("\\.")[0] + ".out"); | 
|  | 215 		} | 
|  | 216 | 
|  | 217 		System.out.println("-----------------------------------------\nCommand Line Arguments:"); | 
|  | 218 		System.out.println("Read 1 file: " + READ1); | 
|  | 219 		System.out.println("Read 2 file: " + READ2); | 
|  | 220 		System.out.println("Output: " + OUTPUT); | 
|  | 221 		System.out.println("Window Size: " + WINDOW); | 
|  | 222 	} | 
|  | 223 | 
|  | 224 	public static void printUsage() { | 
|  | 225 		System.out.println("Usage: java -jar pairedendCrossPlot.jar -f [Read1File] -r [Read2File] [Options]"); | 
|  | 226 		System.out.println("-----------------------------------------"); | 
|  | 227 		System.out.println("\nRequired Parameter:"); | 
|  | 228 		System.out.println("Read 1 File:\t\t\t-f\tRead 1 scIDX file"); | 
|  | 229 		System.out.println("Read 2 File:\t\t\t-r\tRead 1 scIDX file"); | 
|  | 230 		System.out.println("\nOptional Parameters:"); | 
|  | 231 		System.out.println("Output File Name:\t\t-o\tOutput file"); | 
|  | 232 		System.out.println("Window Size +/- Reference:\t-w\t(Default = 500)"); | 
|  | 233 	} | 
|  | 234 | 
|  | 235 	private static String getTimeStamp() { | 
|  | 236 		Date date= new Date(); | 
|  | 237 		String time = new Timestamp(date.getTime()).toString(); | 
|  | 238 		return time; | 
|  | 239 	} | 
|  | 240 | 
|  | 241 } |