Page MenuHomec4science

Reader.java
No OneTemporary

File Metadata

Created
Sat, Feb 1, 22:35

Reader.java

package io.raw;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import java.sql.SQLException;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import crypto.CryptoException;
import crypto.paillier.PaillierScheme;
import database.DBconnector_client;
import database.DBconnector_server;
public class Reader implements Runnable {
private String path;
private DBconnector_client conn_mu;
private DBconnector_server conn_s;
private File input_f;
private BufferedReader br;
private BigInteger[] pbk;
private int id_p;
private String rs_code;
private String[] alleles;
private BigInteger genotype;
private BigInteger[] cipherGenotype;
private String[] tokens;
private String line;
private PaillierScheme cryptoScheme;
private JTextPane tp;
private StyledDocument doc;
public Reader(String raw_file_path, JTextPane txp){
path = raw_file_path;
conn_mu = new DBconnector_client();
conn_s = new DBconnector_server();
tp = txp;
doc = tp.getStyledDocument();
}
@Override
public void run() {
try{
//check extension file
String[] pathCheck = path.split("\\.");
if(!pathCheck[pathCheck.length-1].equalsIgnoreCase("raw")) {
throw new IOException("Wrong Format for RAW File!");
}
SimpleAttributeSet attribute = new SimpleAttributeSet();
StyleConstants.setForeground(attribute, Color.BLACK);
try {
doc.insertString(doc.getLength(), "Parsing RAW File and encrypting genotypes for each patient, please wait..." + "\n", attribute);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
input_f = new File(path);
br = new BufferedReader(new FileReader(input_f));
conn_s.openConnection();
conn_mu.openConnection();
int i = 0;
long start = System.currentTimeMillis();
while((line = br.readLine()) != null ){
i++;
System.out.println(i);
tokens = line.split("\t");
if(tokens[0].matches("^(rs|chr|seq|Seq|SEQ).*$")){
int pat = Integer.parseInt(tokens[1]);
if(pat != id_p){
id_p = pat;
pbk = conn_s.getPublicKey(id_p);
cryptoScheme = new PaillierScheme(pbk[0], pbk[1], pbk[2]);
try {
doc.insertString(doc.getLength(), "Encrypting genotypes for patient " + id_p + "\n", attribute);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
//rs_code
rs_code = tokens[0];
if(rs_code.contains("seq") || rs_code.contains("Seq") || rs_code.contains("SEQ")){
String[] string = rs_code.split("-");
rs_code = string[1];
}
alleles = conn_mu.getAlleles(rs_code);
if(alleles[0] == null){
throw new NullPointerException();
}
if(tokens[2].equals(alleles[0]) && tokens[3].equals(alleles[0])){
genotype = BigInteger.valueOf(2);
}else if(tokens[2].equals(alleles[1]) && tokens[3].equals(alleles[1])){
genotype = BigInteger.valueOf(0);
}else{
genotype = BigInteger.valueOf(1);
}
if(alleles[2] == null){
System.out.println(rs_code);
}
cipherGenotype = cryptoScheme.encrypt(genotype);
conn_s.insertEncryptedMarkers(id_p, Integer.parseInt(alleles[2]), cipherGenotype[0], cipherGenotype[1], null, null);
cipherGenotype = null;
}
}
long end = System.currentTimeMillis();
conn_s.closeConnection();
conn_mu.closeConnection();
try {
doc.insertString(doc.getLength(), "Parsing RAW File and encryption, completed!" + "\n", attribute);
doc.insertString(doc.getLength(), (end-start) + " ms\n", attribute);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}catch(IOException | CryptoException | NullPointerException e ){
e.printStackTrace();
SimpleAttributeSet error = new SimpleAttributeSet();
StyleConstants.setForeground(error, Color.RED);
try {
doc.insertString(doc.getLength(), e.getMessage() + "\n", error);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
SimpleAttributeSet error = new SimpleAttributeSet();
StyleConstants.setForeground(error, Color.RED);
try {
doc.insertString(doc.getLength(), "No Connection with the Server" + "\n", error);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
}
}

Event Timeline