View Javadoc
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   * Parse stQP files and convert it to StatsCollection.
19   * 
20   * @author p.baniukiewicz
21   *
22   */
23  public class StatFileParser {
24    static final int MAX_CELLS = 65535;
25    /**
26     * The Constant LOGGER.
27     */
28    static final Logger LOGGER = LoggerFactory.getLogger(StatFileParser.class.getName());
29  
30    private String name;
31    private ArrayList<CellStats> stats;
32  
33    /**
34     * CellStat getter.
35     * 
36     * @return the stats
37     */
38    public ArrayList<CellStats> getStats() {
39      return stats;
40    }
41  
42    /**
43     * Construct object.
44     * 
45     * @param name corename of file with path. E.g path/to/file/test for files test_0.stQP.csv,...
46     */
47    public StatFileParser(String name) {
48      stats = new ArrayList<>();
49      this.name = name;
50    }
51  
52    /**
53     * Read all files along given path and prefix used for constructing the object and convert them to
54     * CellStat objects.
55     * 
56     * @return Imported stats
57     * 
58     * @throws IOException on File read error
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     * Scan for stQP.csv files numbered consequently from _0.
71     * 
72     * @return List of full paths (related to path given in constructor) of stQP files or empty list.
73     */
74    List<Path> getAllFiles() {
75      ArrayList<Path> ret = new ArrayList<>();
76      Path pa = Paths.get(name);
77      Path filename = pa.getFileName(); // file name without extension
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()) { // check if _xx exists
89          ret.add(testFile); // store
90          i++; // go to next possible file
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 }