Skip to content

Commit

Permalink
added CNV compare feature
Browse files Browse the repository at this point in the history
  • Loading branch information
manojbhosale committed Sep 14, 2020
1 parent ad1fa18 commit a6682d5
Show file tree
Hide file tree
Showing 8 changed files with 581 additions and 22 deletions.
7 changes: 4 additions & 3 deletions src/bedUtils/BedUtils.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package bedUtils;
import java.beans.IntrospectionException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import liftoverutils.*;
import liftoverutils.BedInterval;
import liftoverutils.Interval;
import liftoverutils.IntervalTree;
import liftoverutils.IntervalUtils;

public class BedUtils {

Expand Down
145 changes: 145 additions & 0 deletions src/cnv_compare/CnvCompare.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package cnv_compare;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import liftoverutils.Interval;
import liftoverutils.IntervalNode;;

public class CnvCompare {


public static void main(String[] args) {
File cnvFile1 = new File("C:\\Users\\manojkumar_bhosale\\Desktop\\ToDelete\\forCNV\\test1.txt");
File cnvFile2 = new File("C:\\Users\\manojkumar_bhosale\\Desktop\\ToDelete\\forCNV\\test2.txt");
Path outDir = Paths.get("C:\\Users\\manojkumar_bhosale\\Desktop\\ToDelete\\forCNV");
}

public static void compareCnvFilesToFolder(File cnvFile1, File cnvFile2, Path outDir) {
CnvCompare cc = new CnvCompare();
cc.compareCnv(cnvFile1, cnvFile2, outDir);
}


void compareCnv(File cnvFile1, File cnvFile2, Path outDir) {
List<Interval<CnvInterval>> intervalList = new ArrayList<Interval<CnvInterval>>();
File comparisonFile = outDir.resolve(cnvFile1.getName().replace(".", "_")+""+cnvFile2.getName()).toFile();
int leftNumIntervals = 0;
int rightNumIntervals = 0;
int leftOverlap = 0;
int rightOverlap = 0;
int exactMatch = 0;
int within = 0;
int covering = 0;
int noMatch = 0;

try(BufferedReader br = new BufferedReader(new FileReader(cnvFile1));BufferedReader br1 = new BufferedReader(new FileReader(cnvFile2)); PrintWriter pw = new PrintWriter(comparisonFile)){
String line = "";
while((line = br1.readLine()) != null) {
if(line.startsWith("#"))
continue;
leftNumIntervals++;
String[] splitedRec = line.split("\t");
String chr = splitedRec[0];
int start = Integer.parseInt(splitedRec[1]);
int stop = Integer.parseInt(splitedRec[2]);
String type = splitedRec[6];
CnvInterval cnvInterval = new CnvInterval(chr, start, stop, type);
Interval<CnvInterval> interval = new Interval<CnvInterval>(start, stop, cnvInterval);
intervalList.add(interval);
}

IntervalNode<CnvInterval> masterNode = new IntervalNode<>(intervalList);
line = "";

while((line = br.readLine()) != null) {
if(line.startsWith("#"))
continue;
rightNumIntervals++;
String[] splitedRec = line.split("\t");
String chr = splitedRec[0];
int start = Integer.parseInt(splitedRec[1]);
int stop = Integer.parseInt(splitedRec[2]);
String type = splitedRec[6];
CnvInterval cnvInterval = new CnvInterval(chr, start, stop, type);
Interval<CnvInterval> interval = new Interval<CnvInterval>(start, stop, cnvInterval);
List<Interval<CnvInterval>> intersectList = masterNode.query(interval);
boolean noMatchFlag = true;
for(Interval<CnvInterval> intersect : intersectList){
int res = getVerdict(cnvInterval, intersect.getData());
if(res == 0) {
exactMatch++;
noMatchFlag = false;
}else if(res == 1) {
covering++;
noMatchFlag = false;
}else if(res == 2) {
within++;
noMatchFlag = false;
}else if(res == 3) {
leftOverlap++;
noMatchFlag = false;
}else if(res == 4){
rightOverlap++;
noMatchFlag = false;
}
}

if(noMatchFlag)
noMatch++;

}

pw.println("File1\tFile2\t#Intervals in File1\t#Intervals in File2\tExact matches\tCovering\tWithin\tLeft overlap\tRight Overlap\tNo overlap");
pw.println(cnvFile1+"\t"+cnvFile2+"\t"+rightNumIntervals+"\t"+leftNumIntervals+"\t"+exactMatch+"\t"+covering+"\t"+within+"\t"+leftOverlap+"\t"+rightOverlap+"\t"+noMatch);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

int getVerdict(CnvInterval inter1, CnvInterval inter2){
if(cnvOfSameType(inter1, inter2) && isSameChr(inter1, inter2)) {
if(inter1.getStartPosition() == inter2.getStartPosition() && inter1.getStopPosition() == inter2.getStopPosition()) {
return 0; //exact match
}else if(inter1.getStartPosition() <= inter2.getStartPosition() && inter1.getStopPosition() >= inter2.getStopPosition()) {
return 1; //covering
}else if(inter1.getStartPosition() >= inter2.getStartPosition() && inter1.getStopPosition() <= inter2.getStopPosition()){
return 2; //within
}else if(inter1.getStartPosition() <= inter2.getStartPosition() && inter1.getStopPosition() <= inter2.getStopPosition()){
return 3; //left overlap
}else if(inter1.getStartPosition() >= inter2.getStartPosition() && inter1.getStopPosition() >= inter2.getStopPosition()){
return 4; //right overlap
}
}
return 5;
}

boolean cnvOfSameType(CnvInterval inter1, CnvInterval inter2) {
return (inter1.getType()).equals(inter2.getType());
}

boolean isSameChr(CnvInterval inter1, CnvInterval inter2) {
return (inter1.getChromosome()).equals(inter2.getChromosome());
}


public static void readCnvFile(File cnvFile) {



}

}
91 changes: 91 additions & 0 deletions src/cnv_compare/CnvInterval.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package cnv_compare;

public class CnvInterval {

private String chromosome;
private int startPosition;
private int stopPosition;
private String type;
private float logRatio;
private int copyNumber;

public CnvInterval(String chromosome, int startPosition, int stopPosition, String type, float logRatio,
int copyNumber) {
super();
this.chromosome = chromosome;
this.startPosition = startPosition;
this.stopPosition = stopPosition;
this.type = type;
this.logRatio = logRatio;
this.copyNumber = copyNumber;
}

public CnvInterval(String chromosome, int startPosition, int stopPosition, String type) {
super();
this.chromosome = chromosome.toUpperCase();
this.startPosition = startPosition;
this.stopPosition = stopPosition;
this.type = type.toUpperCase();
this.logRatio = 0;
this.copyNumber = 0;
}

public CnvInterval(String chromosome, int startPosition, int stopPosition) {
super();
this.chromosome = chromosome;
this.startPosition = startPosition;
this.stopPosition = stopPosition;
this.type = "";
this.logRatio = 0;
this.copyNumber = 0;
}

public String getChromosome() {
return chromosome;
}

public void setChromosome(String chromosome) {
this.chromosome = chromosome;
}

public int getStartPosition() {
return startPosition;
}

public void setStartPosition(int startPosition) {
this.startPosition = startPosition;
}

public int getStopPosition() {
return stopPosition;
}

public void setStopPosition(int stopPosition) {
this.stopPosition = stopPosition;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public float getLogRatio() {
return logRatio;
}

public void setLogRatio(float logRatio) {
this.logRatio = logRatio;
}

public int getCopyNumber() {
return copyNumber;
}

public void setCopyNumber(int copyNumber) {
this.copyNumber = copyNumber;
}

}
10 changes: 5 additions & 5 deletions src/com/psl/automation/main/MainGui.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package com.psl.automation.main;

import hsutils.BamSorter;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Expand Down Expand Up @@ -39,11 +34,14 @@

import com.psl.automation.panels.BarcodeMetricsPanel;
import com.psl.automation.panels.BedUtilitiesPanel;
import com.psl.automation.panels.CnvComparePanel;
import com.psl.automation.panels.CompareVcfPanel;
import com.psl.automation.panels.QCComparePanel;
import com.psl.automation.panels.TsTvMetricsPanel;
import com.psl.automation.panels.VcfBedIntersectionPanel;

import hsutils.BamSorter;

public class MainGui implements ActionListener,Runnable{
//Log4j logger
private static Logger log4jLog = Logger.getLogger(MainGui.class.getName());
Expand Down Expand Up @@ -81,12 +79,14 @@ public void run(){
QCComparePanel qcp = new QCComparePanel();
VcfBedIntersectionPanel vbedp = new VcfBedIntersectionPanel();
BedUtilitiesPanel bup = new BedUtilitiesPanel();
CnvComparePanel ccp = new CnvComparePanel();
jtp.addTab("VCF comparator", cvp.createCompareVcfPanel());
jtp.addTab("HS Util", bmp.createFileConfigPanel());
jtp.addTab("VCF Util", tstvp.createVcfUtilPanel());
jtp.addTab("QC compare", qcp.createCompareQcPanel());
jtp.addTab("VCF BED intersect", vbedp.createIntersectVcfPanel());
jtp.addTab("BED intersect", bup.createIntersectBedPanel());
jtp.addTab("CNV intersect", ccp.createCnvComparePanel());

jtp.addChangeListener(new TabSelected());

Expand Down
11 changes: 0 additions & 11 deletions src/com/psl/automation/panels/BedUtilitiesPanel.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package com.psl.automation.panels;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.Box;
Expand All @@ -19,17 +13,12 @@
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

import bedUtils.BedUtils;
import vcfutils.CompareUtils;
import vcfutils.ComparisonResult;
import vcfutils.VCFBedIntersect;

public class BedUtilitiesPanel {

Expand Down
Loading

0 comments on commit a6682d5

Please sign in to comment.