Skip to content

Instantly share code, notes, and snippets.

@agiertli
Created December 15, 2014 15:32
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/459a5a56cccb840d10c9 to your computer and use it in GitHub Desktop.
Save agiertli/459a5a56cccb840d10c9 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
public class tupples {
public static void main(String args[]) {
String gen = "agcaaagagagactgtttagtccacataaagcgtcgttggacgcaaattacgc" + "ggggggtgcaaacgaacgagaggaggtttgagatagctgtatattc";
String[][] tuples = new String[gen.length()][2];
int globalCounter = 0;
for (int i = 0; i < gen.length() - 2; i++) { // loop through the whole string
String tuple = gen.substring(i, i + 3);
int tupleIndex = findTuple(tuple, tuples); // tupleIndex will be -1 if not found, and real value if found
if (tupleIndex != -1) { // if tuple WAS found, then increase counter on that index
Integer myCount = Integer.valueOf(tuples[tupleIndex][1]);
myCount++;
tuples[tupleIndex][1] = String.valueOf(myCount);
}
else { // if it wasn't found
tuples[globalCounter][1] = String.valueOf(1); // mark 1 as its first occurence of the tuple
tuples[globalCounter][0] = tuple; // add the tuple
globalCounter++; // move tuple conuter by one
}
}
// Keeps track of the smallest string's index
int shortestStringIndex;
// /I reduced the upper bound from Array.length to (Array.length - 1)
for (int j = 0; j < globalCounter - 1; j++) {
shortestStringIndex = j;
for (int i = j + 1; i < globalCounter; i++) {
// We keep track of the index to the smallest string
if (tuples[i][0].trim().compareTo(tuples[shortestStringIndex][0].trim()) < 0) {
shortestStringIndex = i;
}
}
// We only swap with the smallest string
if (shortestStringIndex != j) {
String temp = tuples[j][0];
tuples[j][0] = tuples[shortestStringIndex][0];
tuples[shortestStringIndex][0] = temp;
}
}
for (int i = 0; i < globalCounter; i++) {
if (tuples[i][0] == null)
break;
if (tuples[i][0].isEmpty())
break;
System.out.println("tuple:" + tuples[i][0] + ",counts:" + String.valueOf(tuples[i][1]));
}
}
private static int findTuple(String tuple, String[][] tuppleArray) {
int found = -1;
for (int i = 0; i < tuppleArray.length; i++) {
if (tuppleArray[i][0] == null)
break;
if (tuppleArray[i][0].isEmpty())
break;
if (tuppleArray[i][0].equals(tuple)) {
found = i;
break;
}
}
return found;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment