Debian Codegraph @

uk/ac/babraham/FastQC/Utilities/CasavaBasename.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 CasavaBasename {
28
29 /**
30 * This method finds the core name from a CASAVA 1.8 fastq 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 getCasavaBasename (String originalName) throws NameFormatException {
43
44 // Find the base name. We need to remove the 123 numbers from
45 // files of the form:
46 //
47 // anyold_text_123.fastq.gz
48 //
49 // where the base name will be
50 //
51 // anyoldtext.fastq.gz
52
53 // The file must usually end with .fastq.gz, but you can tell cassava to not
54 // compress, in which case the .gz is missing.
55
56
57 if (originalName.endsWith(".fastq.gz")) {
58
59 // They must have an _ 13 chars before the end
60 if (originalName.substring(originalName.length()-13, originalName.length()-12).equals("_")) {
61
62 // They must have numbers for the 3 positions before .fastq
63 try {
64 Integer.parseInt(originalName.substring(originalName.length()-12, originalName.length()-9));
65
66 // If we get here then everything is OK to use the base name from this file
67 String baseName = originalName.substring(0,originalName.length()-13)+".fastq.gz";
68 return baseName;
69 }
70 catch (NumberFormatException nfe) {}
71 }
72 }
73
74
75 else if (originalName.endsWith(".fastq")) {
76
77 // They must have an _ 10 chars before the end
78 if (originalName.substring(originalName.length()-10, originalName.length()-9).equals("_")) {
79
80 // They must have numbers for the 3 positions before .fastq
81 try {
82 Integer.parseInt(originalName.substring(originalName.length()-9, originalName.length()-6));
83
84 // If we get here then everything is OK to use the base name from this file
85 String baseName = originalName.substring(0,originalName.length()-10)+".fastq";
86 return baseName;
87 }
88 catch (NumberFormatException nfe) {}
89 }
90 }
91
92
93 throw new NameFormatException();
94 }
95
96 public static File [][] getCasavaGroups (File [] files) {
97 Hashtable<String, Vector<File>> fileBases = new Hashtable<String, Vector<File>>();
98
99 for (int f=0;f<files.length;f++) {
100
101 // If a file forms part of a CASAVA group then put it into that
102 // group.
103 try {
104 String baseName = CasavaBasename.getCasavaBasename(files[f].getName());
105 if (! fileBases.containsKey(baseName)) {
106 fileBases.put(baseName,new Vector<File>());
107 }
108 fileBases.get(baseName).add(files[f]);
109
110 }
111
112 // If the file name doesn't appear to be part of a CASAVA group
113 // then add it as a singleton
114 catch (NameFormatException nfe) {
115
116 System.err.println("File '"+files[f].getName()+"' didn't look like part of a CASAVA group");
117 Vector<File> newVector = new Vector<File>();
118 newVector.add(files[f]);
119 fileBases.put(files[f].getName(), newVector);
120 }
121
122 }
123
124 String [] baseNames = fileBases.keySet().toArray(new String [0]);
125
126 File [][] fileGroups = new File[baseNames.length][];
127
128 for (int i=0;i<baseNames.length;i++) {
129 fileGroups[i] = fileBases.get(baseNames[i]).toArray(new File[0]);
130 }
131
132 return fileGroups;
133 }
134
135 }