|
16 | 16 | * Created by tombrouws on 15/06/15.
|
17 | 17 | */
|
18 | 18 | public class MetaParser {
|
19 |
| - |
| 19 | + |
20 | 20 | /**
|
21 | 21 | * Parses a metadata file and puts the information in genomes if they exist.
|
22 | 22 | *
|
23 | 23 | * @param metadata
|
24 |
| - * the file to parse |
| 24 | + * the file to parse |
25 | 25 | * @param genomeMap
|
26 |
| - * the map of genomes to put the data in |
| 26 | + * the map of genomes to put the data in |
27 | 27 | * @throws FileNotFoundException
|
28 |
| - * when the file cannot be found |
| 28 | + * when the file cannot be found |
29 | 29 | */
|
30 | 30 | 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) { |
33 | 50 | try {
|
34 | 51 | if (br.ready()) {
|
35 | 52 | br.readLine();
|
36 | 53 | }
|
37 | 54 | while (br.ready()) {
|
38 | 55 | String[] data = br.readLine().split("\t");
|
39 | 56 | 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); |
59 | 58 | }
|
60 | 59 | } catch (IOException e) {
|
61 | 60 | e.printStackTrace();
|
62 | 61 | }
|
63 | 62 | }
|
| 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 | + } |
64 | 83 | }
|
0 commit comments