Skip to content

Instantly share code, notes, and snippets.

@agiertli
Created December 15, 2014 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agiertli/b59be182f252e4f0bc1e to your computer and use it in GitHub Desktop.
Save agiertli/b59be182f252e4f0bc1e to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package skuska.zapocet;
/**
*
* @author Mišel
*/
public class SkuskaZapocet {
/**
* @param args
* the command line arguments
*/
static String genom = "agaactttagagag";
public static Integer globalCounter = 0;
public static void main(String[] args) {
sortAndPrintCodons(statistics(genom),globalCounter);
}
public static int findCodon(String codon, String[][] codonArray) {
int found = -1;
for (int i = 0; i < codonArray.length; i++) {
if (codonArray[i][0] == null) {
break;
}
if (codonArray[i][0].isEmpty()) {
break;
}
if (codonArray[i][0].equals(codon)) {
found = i;
break;
}
}
return found;
}
public static void sortAndPrintCodons(String[][] codons, int globalCounter) {
int shortestStringIndex;
for (int j = 0; j < globalCounter - 1; j++) {
shortestStringIndex = j;
for (int i = j + 1; i < globalCounter; i++) {
if (codons[i][0].trim().compareTo(codons[shortestStringIndex][0].trim()) < 0) {
shortestStringIndex = i;
}
}
if (shortestStringIndex != j) {
String temp = codons[j][0];
codons[j][0] = codons[shortestStringIndex][0];
codons[shortestStringIndex][0] = temp;
}
}
System.out.println("Codons\t" + "Occurrence");
for (int i = 0; i < globalCounter; i++) {
if (codons[i][0] == null) {
break;
}
if (codons[i][0].isEmpty()) {
break;
}
System.out.println(codons[i][0] + "\t" + String.valueOf(codons[i][1]));
}
}
public static String[][] statistics(String genom) {
String[][] codons = new String[genom.length()][2];
for (int i = 0; i < genom.length() - 2; i++) {
String codon = genom.substring(i, i + 3);
int indexOfCodon = findCodon(codon, codons);
if (indexOfCodon != -1) {
Integer count = Integer.valueOf(codons[indexOfCodon][1]);
count++;
codons[indexOfCodon][1] = String.valueOf(count);
} else {
codons[globalCounter][1] = String.valueOf(1);
codons[globalCounter][0] = codon;
globalCounter++;
}
}
return codons;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment