Diff of /src/utils/utils.java [000000] .. [ce076b]

Switch to unified view

a b/src/utils/utils.java
1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package utils;
7
8
import biodiscml.Main;
9
import java.io.BufferedReader;
10
import java.io.File;
11
import java.io.FileInputStream;
12
import java.io.FileOutputStream;
13
import java.io.FileReader;
14
import java.io.IOException;
15
import java.io.InputStream;
16
import java.io.OutputStream;
17
import java.nio.channels.FileChannel;
18
import java.text.DecimalFormat;
19
import java.text.DecimalFormatSymbols;
20
import java.util.ArrayList;
21
import java.util.HashMap;
22
import java.util.Iterator;
23
import java.util.TreeMap;
24
import org.apache.commons.math3.stat.descriptive.moment.Mean;
25
import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation;
26
27
/**
28
 *
29
 * @author Administrator
30
 */
31
public class utils {
32
33
    public static HashMap<String, ArrayList<String>> convertDataColumnsToHashMap(ArrayList<String[]> altable) {
34
        HashMap<String, ArrayList<String>> hmData = new HashMap<>();
35
        for (int i = 1; i < altable.get(0).length; i++) { //for column
36
            String header = altable.get(0)[i]; //get column header
37
            ArrayList<String> al = new ArrayList<>(); //data list of the column
38
            for (int j = 1; j < altable.size(); j++) { //for each value in the column
39
                try {
40
                    al.add(altable.get(j)[i]);
41
                } catch (Exception e) {
42
                    al.add("");
43
                }
44
            }
45
            hmData.put(header, al);
46
        }
47
        return hmData;
48
    }
49
50
    public static ArrayList<String[]> readTable(String file, String separator) {
51
        if (separator.isEmpty()) {
52
            separator = detectSeparator(file);
53
        }
54
        ArrayList<String[]> altable = new ArrayList<>();
55
        int cpt = 1;
56
        try {
57
            BufferedReader br = new BufferedReader(new FileReader(file));
58
            while (br.ready()) {
59
                if (Main.debug2) {
60
                    System.out.println("Reading line " + cpt++);
61
                }
62
                altable.add(br.readLine().split(separator));
63
            }
64
            br.close();
65
        } catch (Exception e) {
66
            e.printStackTrace();
67
        }
68
        return altable;
69
    }
70
71
    public static ArrayList<String[]> readTable(String file) {
72
        String separator = detectSeparator(file);
73
        if (Main.debug) {
74
            System.out.println("reading table " + file);
75
        }
76
        ArrayList<String[]> altable = new ArrayList<>();
77
        try {
78
            BufferedReader br = new BufferedReader(new FileReader(file));
79
            while (br.ready()) {
80
                altable.add(br.readLine().split(separator));
81
            }
82
        } catch (Exception e) {
83
            e.printStackTrace();
84
        }
85
        return altable;
86
    }
87
88
    public static String[][] transposeMatrix(String[][] m) {
89
        String[][] temp = new String[m[0].length][m.length];
90
        for (int i = 0; i < m.length; i++) {
91
            for (int j = 0; j < m[0].length; j++) {
92
                temp[j][i] = m[i][j];
93
            }
94
        }
95
        return temp;
96
    }
97
98
    /**
99
     *
100
     * @param doubles
101
     * @return
102
     */
103
    public static double[] convertStringListToDoubles(ArrayList<String> doubles) {
104
        double[] ret = new double[doubles.size()];
105
        Iterator<String> iterator = doubles.iterator();
106
        int i = 0;
107
        while (iterator.hasNext()) {
108
            ret[i] = Double.valueOf(iterator.next());
109
            i++;
110
        }
111
        return ret;
112
    }
113
114
    /**
115
     * auto detection of the delimiter
116
     *
117
     * @param infile
118
     * @return
119
     */
120
    public static String detectSeparator(String infile) {
121
        if (Main.debug) {
122
            //System.out.println("Delimiter not specified, auto-detection 10 first lines among these delimiters: \"\\t\" \" \" \";\" \",\" \"~\" \":\" \"/\" \"\\|\"");
123
            System.out.print("Delimiter not specified, auto-detection in the 10 first lines...");
124
        }
125
        String delimiter = "";
126
        String potentialDelimiters[] = {"\\t", " ", ";", ",", "~", ":", "/", "\\|"};
127
        for (String potentialDelimiter : potentialDelimiters) {
128
            try {
129
                BufferedReader br = new BufferedReader(new FileReader(infile));
130
                boolean sameNumberAsPreviousLine = true;
131
                String line = br.readLine();
132
                int init = line.split(potentialDelimiter).length;
133
                if (init > 1) {
134
                    int cpt = 0;
135
                    while (br.ready() & sameNumberAsPreviousLine && cpt < 10) {
136
                        cpt++;
137
                        line = br.readLine();
138
                        if (!line.trim().isEmpty()) {
139
                            int split = line.split(potentialDelimiter).length;
140
                            if (split == init) {
141
                                init = split;
142
                            } else {
143
                                sameNumberAsPreviousLine = false;
144
                            }
145
                        }
146
                    }
147
                    if (sameNumberAsPreviousLine) {
148
                        delimiter = potentialDelimiter;
149
                        break;
150
                    }
151
                }
152
            } catch (Exception e) {
153
                e.printStackTrace();
154
            }
155
        }
156
        if (delimiter.isEmpty()) {
157
            System.err.print("[error] CSV separator not detected. Guessing there is just one column. "
158
                    + "If not, change your CSV separator to a standard one (ex: tabulation or comma), or check the file consistency");
159
            delimiter = " ";
160
        }
161
        if (Main.debug) {
162
            System.out.println("Delimiter found:" + delimiter);
163
        }
164
        return delimiter;
165
    }
166
167
    public static void copyFileUsingStream(File source, File dest) throws IOException {
168
        InputStream is = null;
169
        OutputStream os = null;
170
        try {
171
            is = new FileInputStream(source);
172
            os = new FileOutputStream(dest);
173
            byte[] buffer = new byte[1024];
174
            int length;
175
            while ((length = is.read(buffer)) > 0) {
176
                os.write(buffer, 0, length);
177
            }
178
        } finally {
179
            is.close();
180
            os.close();
181
        }
182
    }
183
184
    public static void copyFileUsingChannel(File source, File dest) throws IOException {
185
        FileChannel sourceChannel = null;
186
        FileChannel destChannel = null;
187
        try {
188
            sourceChannel = new FileInputStream(source).getChannel();
189
            destChannel = new FileOutputStream(dest).getChannel();
190
            destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
191
        } finally {
192
            sourceChannel.close();
193
            destChannel.close();
194
        }
195
    }
196
197
    public static String arrayToString(String[] array, String separator) {
198
        String s = "";
199
        for (int i = 0; i < array.length; i++) {
200
            if (array[i] != null) {
201
                s += array[i] + separator;
202
            }
203
        }
204
        return s.substring(0, s.length() - 1);
205
    }
206
207
    public static String arrayToString(ArrayList<Integer> array, String separator) {
208
        String s = "";
209
        for (int i = 0; i < array.size(); i++) {
210
            s += array.get(i) + separator;
211
        }
212
        return s.substring(0, s.length() - 1);
213
    }
214
215
    public static double[] arrayToDouble(ArrayList<Double> al) {
216
        return al.stream().mapToDouble(Double::doubleValue).toArray();
217
    }
218
219
    public static String getStandardDeviation(ArrayList<Double> al) {
220
        DecimalFormat df = new DecimalFormat();
221
        df.setMaximumFractionDigits(3);
222
        DecimalFormatSymbols dfs = new DecimalFormatSymbols();
223
        dfs.setDecimalSeparator('.');
224
        df.setDecimalFormatSymbols(dfs);
225
226
        StandardDeviation s = new StandardDeviation();
227
        s.setData(utils.arrayToDouble(al));
228
229
        return df.format(s.evaluate());
230
    }
231
232
    /**
233
     * Table Object to manipulate table
234
     */
235
    public static class TableObject {
236
237
        public HashMap<String, ArrayList<String>> hmData = new HashMap<>();//<ColName,Values>
238
        public HashMap<String, Integer> hmIDsList = new HashMap<>(); //<ID,index>
239
240
        public TableObject(ArrayList<String[]> altable) {
241
            if (Main.debug) {
242
                System.out.println("converting table");
243
            }
244
            hmData = convertDataColumnsToHashMap(altable);
245
            int ID_index = getIdIndex(altable);
246
            for (int i = 1; i < altable.size(); i++) { //for each ID
247
                //hmIDsList.put(altable.get(i)[ID_index].toLowerCase(), i - 1);
248
                Integer j = hmIDsList.put(altable.get(i)[ID_index], i - 1);
249
                if (j != null) {
250
                    System.err.println("DUPLICATED ID DETECTED: " + altable.get(i)[ID_index]);
251
                    System.err.println("All IDs must be unique");
252
                    System.exit(0);
253
                }
254
            }
255
        }
256
257
        public ArrayList<String> getTheClass(String theclass) {
258
            ArrayList<String> toreturn = hmData.get(theclass);
259
            hmData.remove(theclass);
260
            return toreturn;
261
        }
262
263
        public boolean containsClass(String theClass) {
264
            return hmData.containsKey(theClass);
265
        }
266
267
        public int getIdIndex(ArrayList<String[]> altable) {
268
            int cpt = 0;
269
            for (String s : altable.get(0)) {
270
                if (s.equals(Main.mergingID)) {
271
                    return cpt;
272
                } else {
273
                    cpt++;
274
                }
275
            }
276
            return 0;
277
        }
278
279
        public ArrayList<String> getSortedHmDataKeyset() {
280
            ArrayList<String> al = new ArrayList<>();
281
            TreeMap<String, ArrayList<String>> tm = new TreeMap<>();
282
            tm.putAll(hmData);
283
            for (String key : tm.keySet()) {
284
                al.add(key);
285
            }
286
            return al;
287
        }
288
    }
289
290
    /**
291
     * calculate mean of an array of doubles
292
     *
293
     * @param al
294
     * @return
295
     */
296
    public static String getMean(ArrayList<Double> al) {
297
298
        if (!al.isEmpty()) {
299
            double d[] = new double[al.size()];
300
            for (int i = 0; i < al.size(); i++) {
301
                if (!al.get(i).isNaN()) {
302
                    d[i] = (double) al.get(i);
303
                }
304
            }
305
306
            Mean m = new Mean();
307
            DecimalFormat df = new DecimalFormat();
308
            df.setMaximumFractionDigits(3);
309
            DecimalFormatSymbols dfs = new DecimalFormatSymbols();
310
            dfs.setDecimalSeparator('.');
311
            df.setDecimalFormatSymbols(dfs);
312
            return df.format(m.evaluate(d));
313
        } else {
314
            return "";
315
        }
316
    }
317
318
}