Skip to content

Commit 8fd1d56

Browse files
committed
Merge pull request #5 from ProgrammingLife3/NodeSplit
"TomBrouws confirmed fixes on slack"
2 parents 6ab633e + 9d5c89b commit 8fd1d56

26 files changed

+1226
-343
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
<version>4.12</version>
1515
<scope>test</scope>
1616
</dependency>
17+
<dependency>
18+
<groupId>org.graphstream</groupId>
19+
<artifactId>gs-core</artifactId>
20+
<version>1.2</version>
21+
</dependency>
1722
</dependencies>
1823
<build>
1924
<plugins>

src/main/java/tudelft/ti2806/pl3/data/AminoAcid.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public enum AminoAcid {
99
Arg, Gly, Ochre, Amber, Opal;
1010

1111
private static final int CODON_SIZE = 3;
12-
private static final int CODONS = (int) Math.pow(Gene.values().length - 1,
12+
private static final int CODONS = (int) Math.pow(BasePair.MAX_GENE,
1313
CODON_SIZE);
1414
private static final AminoAcid[] TRANSLATION_TABLE = fillTranslationTable();
1515
private static final String TRANSLATION_TABLE_FILE = "data/translationTable";
@@ -26,7 +26,6 @@ public static AminoAcid get(int index) {
2626
if (index == -1) {
2727
return null;
2828
}
29-
System.out.println(CODONS + " - " + CODON_SIZE);
3029
return TRANSLATION_TABLE[index];
3130
}
3231

@@ -43,7 +42,7 @@ private static AminoAcid[] fillTranslationTable() {
4342
for (int i = 0; i < CODONS; i++) {
4443
String[] data = scanner.nextLine().split(" ");
4544
if (!data[0].startsWith("#")) {
46-
table[Gene.getCodon(Gene.getGeneString(data[0]))]
45+
table[BasePair.getCodon(BasePair.toEnumString(data[0]))]
4746
= valueOf(data[1]);
4847
}
4948
}
@@ -61,11 +60,11 @@ private static AminoAcid[] fillTranslationTable() {
6160
* an array of the genes to be read
6261
* @return an array of all acids read
6362
*/
64-
public static AminoAcid[] getAcids(Gene[] gene) {
63+
public static AminoAcid[] getAcids(BasePair[] gene) {
6564
int size = gene.length / 3;
6665
AminoAcid[] array = new AminoAcid[size];
6766
for (int i = 0; i < size; i++) {
68-
array[i] = get(Gene.getCodon(gene, i * 3));
67+
array[i] = get(BasePair.getCodon(gene, i * 3));
6968
}
7069
return array;
7170
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package tudelft.ti2806.pl3.data;
2+
3+
public enum BasePair {
4+
N(0, -1), T(1, 0), U(2, 0), C(3, 1), A(4, 2), G(5, 3);
5+
6+
private int value;
7+
public byte storeByte;
8+
private static BasePair[] storeByteToBasePair = new BasePair[] { N, T, U, C, A, G };
9+
public static final int MAX_GENE = 4;
10+
11+
BasePair(int storeByte, int value) {
12+
this.value = value;
13+
this.storeByte = (byte) storeByte;
14+
}
15+
16+
/**
17+
* Translates a three bit string of quaternary numbers into a six bit string
18+
* of binary numbers.
19+
*
20+
* @param basePair
21+
* the string of basePairs
22+
* @return the encoding of the three given basePair bits <br>
23+
* -1 if the codon contains an unknown basePair (N)
24+
*/
25+
public static int getCodon(BasePair[] basePair) {
26+
if (basePair[0] == N || basePair[1] == N || basePair[2] == N) {
27+
return -1;
28+
}
29+
return (basePair[0].value << 4) + (basePair[1].value << 2) + basePair[2].value;
30+
}
31+
32+
/**
33+
* Translates the first three bits of the basePair string from the given index.
34+
*
35+
* @param basePair
36+
* the array of basePairs to be read
37+
* @param index
38+
* index from where to read
39+
* @return encoding of the three basePair bits
40+
* @return -1 if the codon contains an unknown basePair (N)
41+
*/
42+
public static int getCodon(BasePair[] basePair, int index) {
43+
return getCodon(new BasePair[] { basePair[index], basePair[index + 1],
44+
basePair[index + 2] });
45+
}
46+
47+
/**
48+
* Reads a string and translates it into a byte array.
49+
*
50+
* @param string
51+
* string of basePair characters
52+
* @return an array of basePairs
53+
*/
54+
public static byte[] getBasePairString(String string) {
55+
String[] charArray = string.split("");
56+
byte[] array = new byte[charArray.length];
57+
for (int i = 0; i < charArray.length; i++) {
58+
array[i] = valueOf(charArray[i]).storeByte;
59+
}
60+
return array;
61+
}
62+
63+
/**
64+
* Reads a string and translates it into a basePair array.
65+
*
66+
* @param string
67+
* string of basePair characters
68+
* @return an array of basePairs
69+
*/
70+
public static BasePair[] toEnumString(String string) {
71+
byte[] str = getBasePairString(string);
72+
BasePair[] result = new BasePair[str.length];
73+
for (int i = 0; i < str.length; i++) {
74+
result[i] = getBasePair(str[i]);
75+
}
76+
return result;
77+
}
78+
79+
private static BasePair getBasePair(byte pair) {
80+
return storeByteToBasePair[pair];
81+
}
82+
}

src/main/java/tudelft/ti2806/pl3/data/Gene.java

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package tudelft.ti2806.pl3.data;
2+
3+
public class Genome {
4+
public final String identifier;
5+
6+
public Genome(String identifier) {
7+
this.identifier = identifier;
8+
}
9+
10+
@Override
11+
public int hashCode() {
12+
final int prime = 31;
13+
int result = 1;
14+
result = prime * result
15+
+ ((identifier == null) ? 0 : identifier.hashCode());
16+
return result;
17+
}
18+
19+
@Override
20+
public boolean equals(Object obj) {
21+
if (this == obj) {
22+
return true;
23+
}
24+
if (obj == null) {
25+
return false;
26+
}
27+
if (getClass() != obj.getClass()) {
28+
return false;
29+
}
30+
Genome other = (Genome) obj;
31+
if (identifier == null) {
32+
if (other.identifier != null) {
33+
return false;
34+
}
35+
} else if (!identifier.equals(other.identifier)) {
36+
return false;
37+
}
38+
return true;
39+
}
40+
41+
}

src/main/java/tudelft/ti2806/pl3/data/GraphParser.java

Lines changed: 0 additions & 105 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package tudelft.ti2806.pl3.data.filter;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Filter is an interface for filters. The filter is supposed to be calculated
8+
* on initialising of the instance over the entire data set, so the filters can
9+
* be applied individually in any order.
10+
*
11+
* @author Sam Smulders
12+
*
13+
* @param <T>
14+
* the element type to filter
15+
*/
16+
public abstract class Filter<T> {
17+
protected List<T> filter = new ArrayList<T>();
18+
19+
/**
20+
* Calculates what elements have to be removed from the list and stores it.
21+
*
22+
* @param list
23+
* a list of the entire data set to filter
24+
*/
25+
public abstract void calculateFilter(List<T> list);
26+
27+
public final void filter(List<T> list) {
28+
list.removeAll(filter);
29+
}
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package tudelft.ti2806.pl3.data.filter;
2+
3+
import tudelft.ti2806.pl3.data.Genome;
4+
import tudelft.ti2806.pl3.data.graph.Node;
5+
6+
import java.util.List;
7+
8+
public class GenomeFilter extends Filter<Node> {
9+
protected final String genome;
10+
11+
public GenomeFilter(String genome) {
12+
this.genome = genome;
13+
}
14+
15+
@Override
16+
public void calculateFilter(List<Node> list) {
17+
for (Node node : list) {
18+
for (Genome source : node.getSource()) {
19+
if (source.equals(genome)) {
20+
filter.add(node);
21+
}
22+
}
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)