Skip to content

Commit

Permalink
Silentcode first code upload
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardKoll committed Oct 18, 2019
1 parent b40e2aa commit a80adb5
Show file tree
Hide file tree
Showing 4 changed files with 324 additions and 0 deletions.
Binary file added Silentcode.jar
Binary file not shown.
178 changes: 178 additions & 0 deletions src/CalculatorMain.java
@@ -0,0 +1,178 @@
import java.io.*;
import java.nio.Buffer;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

public class CalculatorMain implements Runnable{
public final static String[][] codesun = new String[][]{
{"Stop", "TAA", "TAG", "TGA"},
{"Ala", "GCT", "GCC", "GCA", "GCG"},
{"Arg", "CGT", "CGC", "CGA", "CGG", "AGA", "AGG"},
{"Asn", "AAT", "AAC"},
{"Asp", "GAT", "GAC"},
{"Cys", "TGT", "TGC"},
{"Gln", "CAA", "CAG"},
{"Glu", "GAA", "GAG"},
{"Gly", "GGT", "GGC", "GGA", "GGG"},
{"His", "CAT", "CAC"},
{"Ile", "ATT", "ATC", "ATA"},
{"Leu", "TTA", "TTG", "CTT", "CTC", "CTA", "CTG"},
{"Lys", "AAA", "AAG"},
{"Met", "ATG"},
{"Phe", "TTT", "TTC"},
{"Pro", "CCT", "CCC", "CCA", "CCG"},
{"Ser", "TCT", "TCC", "TCA", "TCG", "AGT", "AGC"},
{"Thr", "ACT", "ACC", "ACA", "ACG"},
{"Trp", "TGG"},
{"Tyr", "TAT", "TAC"},
{"Val", "GTT", "GTC", "GTA", "GTG"}
};
public final static int windowsrange = 4;
public final static double gcMinThreshold = 0.4;
public final static double gcMaxThreshold = 0.6;
public final static int minPoss = 10;
public static DecimalFormat df = new DecimalFormat();
public String myRawSequence;
public BufferedWriter myOutfile;
public boolean vocal = true;

public static void main(String[] args){
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
generate(args[0], out, true);
}

public CalculatorMain(String rawseq, BufferedWriter outfile, boolean vocal){
myRawSequence = rawseq;
myOutfile = outfile;
this.vocal = vocal;
}

@Override
public void run() {
generate(myRawSequence, myOutfile, vocal);
}

public static boolean generate(String rawseq, BufferedWriter outfile, boolean vocal){
df.setMaximumFractionDigits(2);
BufferedReader strR = new BufferedReader(new StringReader(rawseq));
ArrayList<String[]> fasta = new ArrayList<>();
String header = "";
String body = "";
String line = "";
try{
while ((line = strR.readLine()) != null){
if(line.length()>0 && line.charAt(0)=='>'){
if(body.length()>0){
fasta.add(new String[]{header, body});
}
header = line;
body = "";
}else{
body = body + line;
}
}
if(body.length()>0){
if(body.length()>0){
fasta.add(new String[]{header, body});
}
}
}catch(IOException e){}
for (int f = 0; f < fasta.size(); f++) {
write(fasta.get(f)[0], outfile);
StringBuilder sequence = new StringBuilder(fasta.get(f)[1]);
if(vocal) {
write("sequence: " + sequence, outfile);
write("bases: " + sequence.length(), outfile);
}
for (int i = 0; 3+(4*i) < sequence.length(); i++) {
sequence.insert(3 + (4 * i), ' ');
}
//System.out.println(sequence.toString());
String[] triplets = sequence.toString().split("\\s");
if(vocal) {
write("triplets: " + triplets.length, outfile);
}

//GC content around base
int[][] windowGCContent = new int[triplets.length][8];
for (int i = 0; i < windowGCContent.length; i++) {
for (int j = 0; j < triplets[i].length(); j++) {
if (triplets[i].charAt(j) == 'A') {
windowGCContent[i][0]++;
} else if (triplets[i].charAt(j) == 'C') {
windowGCContent[i][1]++;
} else if (triplets[i].charAt(j) == 'G') {
windowGCContent[i][2]++;
} else if (triplets[i].charAt(j) == 'T') {
windowGCContent[i][3]++;
}
}
}
for (int i = 0; i < windowGCContent.length; i++) {
for (int j = 1; j < windowsrange; j++) {
for (int k = 0; k < 4; k++) {
if (i - j >= 0) {
windowGCContent[i][k + 4] += windowGCContent[i - j][k];
}
if (i + j < windowGCContent.length) {
windowGCContent[i][k + 4] += windowGCContent[i + j][k];
}
}
}
}

//number of silent variants
double possibilities = 1;
boolean stop = false;
int lastNum = 0;
if (vocal) {
write("AA \t BP \t Triplet (AA name)\t codons " /*+ "(" + possibilities + " total)" */ + "\tGC", outfile);
}
for (int i = 0; i < triplets.length; i++) {
stop = false;
for (int j = 0; !stop && j < codesun.length; j++) {
for (int k = 1; !stop && k < codesun[j].length; k++) {
if (triplets[i].equals(codesun[j][k])) {
possibilities = possibilities * (codesun[j].length - 1);
double myGC = gcContent(windowGCContent[i]);
if (vocal) {
write(i + "\t" + 3 * i + "\t" + ((lastNum + codesun[j].length > minPoss) ? ((myGC >= gcMinThreshold && myGC <= gcMaxThreshold) ? "\033[31m" : "\033[33m") : ((myGC >= gcMinThreshold && myGC <= gcMaxThreshold) ? "\033[32m" : "")) + triplets[i] + " (" + codesun[j][0] + "):\t\t" + (codesun[j].length - 1) + " " /*+ "(" + possibilities + " total)" */ + "\t" + df.format(myGC) + "\033[0m", outfile);
}
lastNum = codesun[j].length - 1;
stop = true;
}
}
}
}
write("Possible combinations of silent mutations: " + possibilities + " = 2^" + (int) Math.floor(Math.log(possibilities) / Math.log(2)) + " of 2^" + 2 * rawseq.length(), outfile);
write(("Storage capacity: " + (Math.floor(Math.log(possibilities) / Math.log(2))) / 8) + " Byte of " + rawseq.length() / 4 + " Byte", outfile);
}

try {
outfile.flush();
outfile.close();
}catch(Exception e){}
return true;
}

public static boolean write(String string, BufferedWriter file){
try {
file.write(string);
file.write('\n');
}catch(Exception e){
e.printStackTrace();
}
return true;
}

public static double gcContent(int[] counts){
double out;
if(counts.length == 8){
out = ((double)(counts[5]+counts[6]))/((double)(counts[4]+counts[5]+counts[6]+counts[7]));
}else{
out = Double.NaN;
}
return out;
}
}
88 changes: 88 additions & 0 deletions src/MainWindow.java
@@ -0,0 +1,88 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;

public class MainWindow {
public static void main(String[] args){
JFrame mainWindow = MainWindow.createMainWindow();
}

public static JFrame createMainWindow(){
JFrame mainWindow = new JFrame();
mainWindow.setTitle("Silent Mutation Calculator");
mainWindow.setSize(new Dimension(800, 800));
mainWindow.setLayout(new GridBagLayout());


GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridwidth = 2;
c.gridheight = 1;

c.gridx = 0;
c.gridy = 1;

JTextArea inField = new JTextArea("ATGTAA");
inField.setLineWrap(true);
JScrollPane scrollPaneINPUT = new JScrollPane (inField, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
mainWindow.add(scrollPaneINPUT, c);


c.gridy = 3;
JEditorPane outArea = new JEditorPane();
outArea.setEditable(false);
//outArea.setContentType("text/plain");
JScrollPane scrollPane = new JScrollPane (outArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
/*scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
e.getAdjustable().setValue(e.getAdjustable().getMaximum());
}
});*/


mainWindow.add(scrollPane, c);

c.weighty = 0.1;
c.gridy = 0;
JLabel inLabel = new JLabel("Please enter your sequence here:");
mainWindow.add(inLabel, c);

c.gridy = 2;
JLabel outLabel = new JLabel("Results (You might need to scroll down a bit when finished):");
mainWindow.add(outLabel, c);

c.gridwidth = 1;
JCheckBox vocalcheck = new JCheckBox("vocal", true);
c.gridy = 4;
mainWindow.add(vocalcheck, c);

JButton trigger = new JButton("Launch");
trigger.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
new Thread( new Runnable() {
@Override
public void run() {
outArea.setText("");
CalculatorMain c = new CalculatorMain(inField.getText(), new BufferedWriter(new OutputStreamWriter(new TextAreaOutputStream(outArea, scrollPane))), vocalcheck.isSelected());
c.run();
}
}).start();
}
});
trigger.setBackground(Color.RED);
c.gridx = 1;
mainWindow.add(trigger, c);

mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setLocationRelativeTo(null);
mainWindow.setVisible(true);
return mainWindow;
}
}
58 changes: 58 additions & 0 deletions src/TextAreaOutputStream.java
@@ -0,0 +1,58 @@
import java.awt.*;
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.*;
import javax.swing.text.*;


public class TextAreaOutputStream extends OutputStream{
private JEditorPane myTextArea;
JScrollPane pane;
Color c = Color.RED;
static boolean readingColor = false;
static String colorVal = "";

public TextAreaOutputStream(JEditorPane myTextArea, JScrollPane pane) {
this.myTextArea = myTextArea;
this.pane = pane;
}

public void write(int b) throws IOException {
char mychar = (char) b;
if(b != 27 && !readingColor) {
StyleContext sc = StyleContext.getDefaultStyleContext();
//System.out.println("real color" + c.toString());
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
//System.out.println(aset.toString());
int len = myTextArea.getDocument().getLength();
try {
myTextArea.getDocument().insertString(len, String.valueOf(mychar), aset);
pane.getVerticalScrollBar().setValue(pane.getVerticalScrollBar().getMaximum());
} catch (BadLocationException e) {
e.printStackTrace();
}
}else{
if(b == 27) {
readingColor = true;
colorVal = "";
}
colorVal += mychar;
if(b == 109){
readingColor = false;
//System.out.println("" + colorVal + "Color" + colorVal.length() + " " + colorVal.charAt(1) + " " + colorVal.charAt(2) + " " + colorVal.charAt(3) + "\033[0m");
if(colorVal.equals("\033[0m")){
c=Color.BLACK;
} else if(colorVal.equals("\033[31m")){
c=Color.RED;
//System.out.println("Print red");
} else if(colorVal.equals("\033[32m")){
c=Color.green;
//System.out.println("Print yellow");
} else if(colorVal.equals("\033[33m")){
c=Color.YELLOW;
//System.out.println("Print green");
}
}
}
}
}

0 comments on commit a80adb5

Please sign in to comment.