Debian Codegraph @

uk/ac/babraham/FastQC/Utilities/NanoporeBasename.java

raw symbols SCIP
1 /**
2 * Copyright Copyright 2011-17 Simon Andrews
3 *
4 * This file is part of FastQC.
5 *
6 * FastQC is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * FastQC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with FastQC; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 package uk.ac.babraham.FastQC.Utilities;
22
23 import java.io.File;
24 import java.util.Hashtable;
25 import java.util.Vector;
26
27 public class NanoporeBasename {
28
29 /**
30 * This method finds the core name from an ONT fast5 file. It strips off the
31 * part which indicates that this file is one of a set and returns the base name with
32 * this part removed.
33 *
34 * If the filename does not conform to standard CASAVA naming then a NameFormatException
35 * is thrown.
36 *
37 * @param originalName
38 * @return
39 * @throws NameFormatException
40 */
41
42 public static String getNanoporeBasename (String originalName) throws NameFormatException {
43
44 // Files from nanopores look like: Computer_Samplename_number_chXXX_fileXXX_strand.fast5
45 // We need to reduce this to Computer_Samplename_number
46
47 // Some more recent files have names which are just: Computer_Samplename_number.fast5 so
48 // we need to account for those too.
49
50 String [] subNames = originalName.replaceAll(".fast5$", "").split("_");
51
52 if (subNames.length < 3) {
53 throw new NameFormatException();
54 }
55
56 String basename = subNames[0]+"_"+subNames[1]+"_"+subNames[2];
57
58 System.err.println("Basename is "+basename);
59
60 return basename;
61
62 }
63
64 public static File [][] getNanoporeGroups (File [] files) {
65 Hashtable<String, Vector<File>> fileBases = new Hashtable<String, Vector<File>>();
66
67 for (int f=0;f<files.length;f++) {
68
69
70 if (files[f].getName().contains("muxscan")) continue; // Control files not containing real data.
71
72 // If a file forms part of a nanopore group then put it into that
73 // group.
74 try {
75 String baseName = NanoporeBasename.getNanoporeBasename(files[f].getName());
76 if (! fileBases.containsKey(baseName)) {
77 fileBases.put(baseName,new Vector<File>());
78 }
79 fileBases.get(baseName).add(files[f]);
80
81 }
82
83 // If the file name doesn't appear to be part of a nanopore group
84 // then add it as a singleton
85 catch (NameFormatException nfe) {
86
87 System.err.println("File '"+files[f].getName()+"' didn't look like part of a CASAVA group");
88 Vector<File> newVector = new Vector<File>();
89 newVector.add(files[f]);
90 fileBases.put(files[f].getName(), newVector);
91 }
92
93 }
94
95 String [] baseNames = fileBases.keySet().toArray(new String [0]);
96
97 File [][] fileGroups = new File[baseNames.length][];
98
99 for (int i=0;i<baseNames.length;i++) {
100 fileGroups[i] = fileBases.get(baseNames[i]).toArray(new File[0]);
101 }
102
103 return fileGroups;
104 }
105
106 }