|
| 1 | +package cnv_compare; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileNotFoundException; |
| 6 | +import java.io.FileReader; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.PrintWriter; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.nio.file.Paths; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import liftoverutils.Interval; |
| 15 | +import liftoverutils.IntervalNode;; |
| 16 | + |
| 17 | +public class CnvCompare { |
| 18 | + |
| 19 | + |
| 20 | + public static void main(String[] args) { |
| 21 | + File cnvFile1 = new File("C:\\Users\\manojkumar_bhosale\\Desktop\\ToDelete\\forCNV\\test1.txt"); |
| 22 | + File cnvFile2 = new File("C:\\Users\\manojkumar_bhosale\\Desktop\\ToDelete\\forCNV\\test2.txt"); |
| 23 | + Path outDir = Paths.get("C:\\Users\\manojkumar_bhosale\\Desktop\\ToDelete\\forCNV"); |
| 24 | + } |
| 25 | + |
| 26 | + public static void compareCnvFilesToFolder(File cnvFile1, File cnvFile2, Path outDir) { |
| 27 | + CnvCompare cc = new CnvCompare(); |
| 28 | + cc.compareCnv(cnvFile1, cnvFile2, outDir); |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + void compareCnv(File cnvFile1, File cnvFile2, Path outDir) { |
| 33 | + List<Interval<CnvInterval>> intervalList = new ArrayList<Interval<CnvInterval>>(); |
| 34 | + File comparisonFile = outDir.resolve(cnvFile1.getName().replace(".", "_")+""+cnvFile2.getName()).toFile(); |
| 35 | + int leftNumIntervals = 0; |
| 36 | + int rightNumIntervals = 0; |
| 37 | + int leftOverlap = 0; |
| 38 | + int rightOverlap = 0; |
| 39 | + int exactMatch = 0; |
| 40 | + int within = 0; |
| 41 | + int covering = 0; |
| 42 | + int noMatch = 0; |
| 43 | + |
| 44 | + try(BufferedReader br = new BufferedReader(new FileReader(cnvFile1));BufferedReader br1 = new BufferedReader(new FileReader(cnvFile2)); PrintWriter pw = new PrintWriter(comparisonFile)){ |
| 45 | + String line = ""; |
| 46 | + while((line = br1.readLine()) != null) { |
| 47 | + if(line.startsWith("#")) |
| 48 | + continue; |
| 49 | + leftNumIntervals++; |
| 50 | + String[] splitedRec = line.split("\t"); |
| 51 | + String chr = splitedRec[0]; |
| 52 | + int start = Integer.parseInt(splitedRec[1]); |
| 53 | + int stop = Integer.parseInt(splitedRec[2]); |
| 54 | + String type = splitedRec[6]; |
| 55 | + CnvInterval cnvInterval = new CnvInterval(chr, start, stop, type); |
| 56 | + Interval<CnvInterval> interval = new Interval<CnvInterval>(start, stop, cnvInterval); |
| 57 | + intervalList.add(interval); |
| 58 | + } |
| 59 | + |
| 60 | + IntervalNode<CnvInterval> masterNode = new IntervalNode<>(intervalList); |
| 61 | + line = ""; |
| 62 | + |
| 63 | + while((line = br.readLine()) != null) { |
| 64 | + if(line.startsWith("#")) |
| 65 | + continue; |
| 66 | + rightNumIntervals++; |
| 67 | + String[] splitedRec = line.split("\t"); |
| 68 | + String chr = splitedRec[0]; |
| 69 | + int start = Integer.parseInt(splitedRec[1]); |
| 70 | + int stop = Integer.parseInt(splitedRec[2]); |
| 71 | + String type = splitedRec[6]; |
| 72 | + CnvInterval cnvInterval = new CnvInterval(chr, start, stop, type); |
| 73 | + Interval<CnvInterval> interval = new Interval<CnvInterval>(start, stop, cnvInterval); |
| 74 | + List<Interval<CnvInterval>> intersectList = masterNode.query(interval); |
| 75 | + boolean noMatchFlag = true; |
| 76 | + for(Interval<CnvInterval> intersect : intersectList){ |
| 77 | + int res = getVerdict(cnvInterval, intersect.getData()); |
| 78 | + if(res == 0) { |
| 79 | + exactMatch++; |
| 80 | + noMatchFlag = false; |
| 81 | + }else if(res == 1) { |
| 82 | + covering++; |
| 83 | + noMatchFlag = false; |
| 84 | + }else if(res == 2) { |
| 85 | + within++; |
| 86 | + noMatchFlag = false; |
| 87 | + }else if(res == 3) { |
| 88 | + leftOverlap++; |
| 89 | + noMatchFlag = false; |
| 90 | + }else if(res == 4){ |
| 91 | + rightOverlap++; |
| 92 | + noMatchFlag = false; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if(noMatchFlag) |
| 97 | + noMatch++; |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + pw.println("File1\tFile2\t#Intervals in File1\t#Intervals in File2\tExact matches\tCovering\tWithin\tLeft overlap\tRight Overlap\tNo overlap"); |
| 102 | + pw.println(cnvFile1+"\t"+cnvFile2+"\t"+rightNumIntervals+"\t"+leftNumIntervals+"\t"+exactMatch+"\t"+covering+"\t"+within+"\t"+leftOverlap+"\t"+rightOverlap+"\t"+noMatch); |
| 103 | + |
| 104 | + } catch (FileNotFoundException e) { |
| 105 | + // TODO Auto-generated catch block |
| 106 | + e.printStackTrace(); |
| 107 | + } catch (IOException e) { |
| 108 | + // TODO Auto-generated catch block |
| 109 | + e.printStackTrace(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + int getVerdict(CnvInterval inter1, CnvInterval inter2){ |
| 114 | + if(cnvOfSameType(inter1, inter2) && isSameChr(inter1, inter2)) { |
| 115 | + if(inter1.getStartPosition() == inter2.getStartPosition() && inter1.getStopPosition() == inter2.getStopPosition()) { |
| 116 | + return 0; //exact match |
| 117 | + }else if(inter1.getStartPosition() <= inter2.getStartPosition() && inter1.getStopPosition() >= inter2.getStopPosition()) { |
| 118 | + return 1; //covering |
| 119 | + }else if(inter1.getStartPosition() >= inter2.getStartPosition() && inter1.getStopPosition() <= inter2.getStopPosition()){ |
| 120 | + return 2; //within |
| 121 | + }else if(inter1.getStartPosition() <= inter2.getStartPosition() && inter1.getStopPosition() <= inter2.getStopPosition()){ |
| 122 | + return 3; //left overlap |
| 123 | + }else if(inter1.getStartPosition() >= inter2.getStartPosition() && inter1.getStopPosition() >= inter2.getStopPosition()){ |
| 124 | + return 4; //right overlap |
| 125 | + } |
| 126 | + } |
| 127 | + return 5; |
| 128 | + } |
| 129 | + |
| 130 | + boolean cnvOfSameType(CnvInterval inter1, CnvInterval inter2) { |
| 131 | + return (inter1.getType()).equals(inter2.getType()); |
| 132 | + } |
| 133 | + |
| 134 | + boolean isSameChr(CnvInterval inter1, CnvInterval inter2) { |
| 135 | + return (inter1.getChromosome()).equals(inter2.getChromosome()); |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + public static void readCnvFile(File cnvFile) { |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments