Skip to content

Instantly share code, notes, and snippets.

@agiertli
Created December 15, 2014 15:24
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/4212580a669c5b96d961 to your computer and use it in GitHub Desktop.
Save agiertli/4212580a669c5b96d961 to your computer and use it in GitHub Desktop.
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
}
}
for (int i = 0; i < tuples.length; 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