1 package com.github.celldynamics.quimp.filesystem.converter;
2
3 import java.io.IOException;
4 import java.nio.file.Path;
5 import java.nio.file.Paths;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.List;
9
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 import com.github.celldynamics.quimp.CellStats;
14 import com.github.celldynamics.quimp.FrameStatistics;
15 import com.github.celldynamics.quimp.filesystem.FileExtensions;
16
17
18
19
20
21
22
23 public class StatFileParser {
24 static final int MAX_CELLS = 65535;
25
26
27
28 static final Logger LOGGER = LoggerFactory.getLogger(StatFileParser.class.getName());
29
30 private String name;
31 private ArrayList<CellStats> stats;
32
33
34
35
36
37
38 public ArrayList<CellStats> getStats() {
39 return stats;
40 }
41
42
43
44
45
46
47 public StatFileParser(String name) {
48 stats = new ArrayList<>();
49 this.name = name;
50 }
51
52
53
54
55
56
57
58
59
60 public ArrayList<CellStats> importStQp() throws IOException {
61 List<Path> files = getAllFiles();
62 for (Path p : files) {
63 FrameStatistics[] fs = FrameStatistics.read(p.toFile());
64 stats.add(new CellStats(new ArrayList<FrameStatistics>(Arrays.asList(fs))));
65 }
66 return getStats();
67 }
68
69
70
71
72
73
74 List<Path> getAllFiles() {
75 ArrayList<Path> ret = new ArrayList<>();
76 Path pa = Paths.get(name);
77 Path filename = pa.getFileName();
78 Path folder = pa.getParent();
79 int i = 0;
80 while (i < MAX_CELLS) {
81 Path testFile;
82 if (folder != null) {
83 testFile = folder
84 .resolve(Paths.get(filename.toString() + "_" + i + FileExtensions.statsFileExt));
85 } else {
86 testFile = Paths.get(filename.toString() + "_" + i + FileExtensions.statsFileExt);
87 }
88 if (testFile.toFile().isFile()) {
89 ret.add(testFile);
90 i++;
91 } else {
92 break;
93 }
94 }
95 if (i >= MAX_CELLS) {
96 LOGGER.warn("Reached maximal number of statistic files: " + MAX_CELLS);
97 }
98 return ret;
99
100 }
101
102 }