Skip to content

Commit 3cd4ced

Browse files
committed
Merge pull request #97 from ProgrammingLife3/refactor/siginput
Refactor/siginput
2 parents c9a04da + fc38557 commit 3cd4ced

File tree

10 files changed

+500
-317
lines changed

10 files changed

+500
-317
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,19 @@
44
* Created by tombrouws on 15/06/15.
55
*/
66
public enum Gender {
7-
MALE, FEMALE
7+
MALE, FEMALE;
8+
9+
/**
10+
* Parses a string to {@code Gender}.
11+
*
12+
* @param string
13+
* the string to parse
14+
* @return the gender parsed
15+
*/
16+
public static Gender parse(String string) {
17+
if (string.equalsIgnoreCase("Male")) {
18+
return MALE;
19+
}
20+
return FEMALE;
21+
}
822
}

src/main/java/tudelft/ti2806/pl3/data/meta/MetaParser.java

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,68 @@
1616
* Created by tombrouws on 15/06/15.
1717
*/
1818
public class MetaParser {
19-
19+
2020
/**
2121
* Parses a metadata file and puts the information in genomes if they exist.
2222
*
2323
* @param metadata
24-
* the file to parse
24+
* the file to parse
2525
* @param genomeMap
26-
* the map of genomes to put the data in
26+
* the map of genomes to put the data in
2727
* @throws FileNotFoundException
28-
* when the file cannot be found
28+
* when the file cannot be found
2929
*/
3030
public static void parseMeta(File metadata, Map<String, Genome> genomeMap) throws FileNotFoundException {
31-
BufferedReader br = new BufferedReader(new InputStreamReader(
32-
new BufferedInputStream(new FileInputStream(metadata))));
31+
BufferedReader br = new BufferedReader(new InputStreamReader(new BufferedInputStream(new FileInputStream(
32+
metadata))));
33+
parseMeta(br, genomeMap);
34+
try {
35+
br.close();
36+
} catch (IOException e) {
37+
e.printStackTrace();
38+
}
39+
}
40+
41+
/**
42+
* Parses a metadata file and puts the information in genomes if they exist.
43+
*
44+
* @param br
45+
* the {@link BufferedReader} to read from
46+
* @param genomeMap
47+
* the map of genomes to put the data in
48+
*/
49+
private static void parseMeta(BufferedReader br, Map<String, Genome> genomeMap) {
3350
try {
3451
if (br.ready()) {
3552
br.readLine();
3653
}
3754
while (br.ready()) {
3855
String[] data = br.readLine().split("\t");
3956
data[0] = data[0].replaceAll("-", "_");
40-
if (genomeMap.containsKey(data[0])) {
41-
Genome g = genomeMap.get(data[0]);
42-
43-
if (data[1].equals("Negative")) {
44-
g.setHivStatus(false);
45-
} else {
46-
g.setHivStatus(true);
47-
}
48-
g.setAge(Integer.parseInt(data[2]));
49-
50-
if (data[3].equals("Male")) {
51-
g.setGender(Gender.MALE);
52-
} else {
53-
g.setGender(Gender.FEMALE);
54-
}
55-
56-
g.setLocation(data[4]);
57-
g.setIsolationDate(data[5]);
58-
}
57+
readData(data, genomeMap);
5958
}
6059
} catch (IOException e) {
6160
e.printStackTrace();
6261
}
6362
}
63+
64+
/**
65+
* Parses a piece of meta data for a single {@link Genome}.
66+
*
67+
* @param data
68+
* the string array to parse
69+
* @param genomeMap
70+
* the map of genomes to put the data in
71+
*/
72+
private static void readData(String[] data, Map<String, Genome> genomeMap) {
73+
if (!genomeMap.containsKey(data[0])) {
74+
return;
75+
}
76+
Genome g = genomeMap.get(data[0]);
77+
g.setHivStatus(!data[1].equals("Negative"));
78+
g.setAge(Integer.parseInt(data[2]));
79+
g.setGender(Gender.parse(data[3]));
80+
g.setLocation(data[4]);
81+
g.setIsolationDate(data[5]);
82+
}
6483
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package tudelft.ti2806.pl3.data.wrapper.util;
2+
3+
import tudelft.ti2806.pl3.data.wrapper.CombineWrapper;
4+
import tudelft.ti2806.pl3.data.wrapper.SingleWrapper;
5+
import tudelft.ti2806.pl3.data.wrapper.Wrapper;
6+
7+
import java.util.ArrayList;
8+
import java.util.Collections;
9+
import java.util.HashMap;
10+
import java.util.HashSet;
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
public final class CombineWrapUtil {
15+
private CombineWrapUtil(){
16+
}
17+
18+
/**
19+
* Wraps a list into a new layer and reconnects the new layer.
20+
*
21+
* @param nonCombinedNodes
22+
* the nodes that are not combined
23+
* @param combinedNodes
24+
* the nodes that are combined, and are already of the new layer
25+
* @return a list containing a new layer over the previous layer
26+
*/
27+
public static List<Wrapper> wrapAndReconnect(List<Wrapper> nonCombinedNodes, List<CombineWrapper> combinedNodes) {
28+
Map<Wrapper, Wrapper> map = wrapList(nonCombinedNodes, combinedNodes);
29+
reconnectLayer(nonCombinedNodes, combinedNodes, map);
30+
List<Wrapper> list = new ArrayList<>(new HashSet<>(map.values()));
31+
Collections.sort(list);
32+
return list;
33+
}
34+
35+
/**
36+
* Reconnects the given layer, using the connections from the previous layer and applying them to the new layer.
37+
*
38+
* @param nonCombinedNodes
39+
* the nodes that are not combined
40+
* @param combinedNodes
41+
* the nodes that are combined, and are already of the new layer
42+
* @param map
43+
* a map mapping all nodes from the previous layer to the new layer
44+
*/
45+
public static void reconnectLayer(List<Wrapper> nonCombinedNodes, List<CombineWrapper> combinedNodes,
46+
Map<Wrapper, Wrapper> map) {
47+
reconnectNonCombinedNodes(nonCombinedNodes, map);
48+
reconnectCombinedNodes(combinedNodes, map);
49+
}
50+
51+
/**
52+
* Reconnects the nodes which where not combined to the given layer.
53+
*
54+
* @param nonCombinedNodes
55+
* the nodes that are not combined
56+
* @param map
57+
* a map mapping all nodes from the previous layer to the new layer
58+
*/
59+
private static void reconnectNonCombinedNodes(List<Wrapper> nonCombinedNodes, Map<Wrapper, Wrapper> map) {
60+
for (Wrapper node : nonCombinedNodes) {
61+
Wrapper newWrapper = map.get(node);
62+
node.getIncoming().stream().filter(in -> !newWrapper.getIncoming().contains(map.get(in)))
63+
.forEach(in -> newWrapper.getIncoming().add(map.get(in)));
64+
node.getOutgoing().stream().filter(out -> !newWrapper.getOutgoing().contains(map.get(out)))
65+
.forEach(out -> newWrapper.getOutgoing().add(map.get(out)));
66+
}
67+
}
68+
69+
70+
/**
71+
* Reconnects the nodes which where combined to the given layer.
72+
*
73+
* @param combinedNodes
74+
* the nodes that are combined, and are already of the new layer
75+
* @param map
76+
* a map mapping all nodes from the previous layer to the new layer
77+
*/
78+
private static void reconnectCombinedNodes(List<CombineWrapper> combinedNodes, Map<Wrapper, Wrapper> map) {
79+
for (CombineWrapper comNode : combinedNodes) {
80+
comNode.getFirst().getIncoming().stream().filter(in -> !comNode.getIncoming()
81+
.contains(map.get(in))) .forEach(in -> comNode.getIncoming().add(map.get(in)));
82+
comNode.getLast().getOutgoing().stream().filter(out -> !comNode.getOutgoing()
83+
.contains(map.get(out))) .forEach(out -> comNode.getOutgoing()
84+
.add(map.get(out)));
85+
}
86+
}
87+
88+
/**
89+
* Creates a new layer from the given nodes and creates a map mapping all nodes from the previous layer
90+
* to the new layer.
91+
*
92+
* @param nonCombinedNodes
93+
* the nodes that are not combined
94+
* @param combinedNodes
95+
* the nodes that are combined, and are already of the new layer
96+
* @return a map mapping all nodes from the previous layer to the new layer
97+
*/
98+
private static Map<Wrapper, Wrapper> wrapList(List<Wrapper> nonCombinedNodes,
99+
List<CombineWrapper> combinedNodes) {
100+
Map<Wrapper, Wrapper> map = new HashMap<>();
101+
for (Wrapper node : nonCombinedNodes) {
102+
SingleWrapper newWrapper = new SingleWrapper(node);
103+
map.put(node, newWrapper);
104+
}
105+
for (CombineWrapper verNode : combinedNodes) {
106+
for (Wrapper node : verNode.getNodeList()) {
107+
map.put(node, verNode);
108+
}
109+
}
110+
return map;
111+
}
112+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package tudelft.ti2806.pl3.data.wrapper.util;
2+
3+
import tudelft.ti2806.pl3.data.Genome;
4+
import tudelft.ti2806.pl3.data.wrapper.FixWrapper;
5+
import tudelft.ti2806.pl3.data.wrapper.Wrapper;
6+
7+
import java.util.HashSet;
8+
import java.util.List;
9+
import java.util.Set;
10+
11+
public final class FixWrapUtil {
12+
private FixWrapUtil(){
13+
}
14+
15+
/**
16+
* Adds the {@link FixWrapper}s to the given node list and connects them.
17+
*
18+
* @param nodes
19+
* the nodes in the remaining layer
20+
* @param startFix
21+
* the {@link FixWrapper} on the left
22+
* @param endFix
23+
* the {@link FixWrapper} on the right
24+
*
25+
*/
26+
public static void addFixNodesToGraph(List<Wrapper> nodes, FixWrapper startFix, FixWrapper endFix) {
27+
startFix.getOutgoing().add(endFix);
28+
endFix.getIncoming().add(startFix);
29+
Set<Genome> genomeSet = new HashSet<>();
30+
31+
connectFixNodes(genomeSet, nodes, startFix, endFix);
32+
33+
startFix.setGenome(genomeSet);
34+
endFix.setGenome(genomeSet);
35+
36+
nodes.add(startFix);
37+
nodes.add(endFix);
38+
}
39+
40+
/**
41+
* Connects the {@link FixWrapper}s to the graph.
42+
*
43+
* @param genomeSet
44+
* the set of genomes the fix wrappers should connect
45+
* @param nodes
46+
* the nodes in the remaining layer
47+
* @param startFix
48+
* the {@link FixWrapper} on the left
49+
* @param endFix
50+
* the {@link FixWrapper} on the right
51+
*/
52+
private static void connectFixNodes(Set<Genome> genomeSet, List<Wrapper> nodes, FixWrapper startFix,
53+
FixWrapper endFix) {
54+
for (Wrapper node : nodes) {
55+
Set<Genome> genome = node.getGenome();
56+
genomeSet.addAll(genome);
57+
final Set<Genome> set = new HashSet<>();
58+
node.getIncoming().stream().map(Wrapper::getGenome).forEach(set::addAll);
59+
if (set.size() != genome.size() || !set.containsAll(genome)) {
60+
node.getIncoming().add(startFix);
61+
startFix.getOutgoing().add(node);
62+
}
63+
set.clear();
64+
node.getOutgoing().stream().map(Wrapper::getGenome).forEach(set::addAll);
65+
if (set.size() != genome.size() || !set.containsAll(genome)) {
66+
node.getOutgoing().add(endFix);
67+
endFix.getIncoming().add(node);
68+
}
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)