Debian Codegraph @

uk/ac/babraham/FastQC/Sequence/Fast5File.java

raw symbols SCIP
1 /**
2 * Copyright Copyright 2010-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 package uk.ac.babraham.FastQC.Sequence;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import ch.systemsx.cisd.hdf5.HDF5Factory;
28 import ch.systemsx.cisd.hdf5.IHDF5SimpleReader;
29
30 public class Fast5File implements SequenceFile {
31
32 private Sequence nextSequence = null;
33 private File file;
34 private String name;
35 private IHDF5SimpleReader reader;
36 private String [] readPaths = new String[] {""};
37
38 private int readPathsIndexPosition = 0;
39
40 private String [] rdfPaths = new String [] {
41 "Analyses/Basecall_2D_000/BaseCalled_template/Fastq",
42 "Analyses/Basecall_2D_000/BaseCalled_2D/Fastq",
43 "Analyses/Basecall_1D_000/BaseCalled_template/Fastq",
44 "Analyses/Basecall_1D_000/BaseCalled_1D/Fastq"
45 };
46
47
48
49 protected Fast5File(File file) throws SequenceFormatException, IOException {
50 this.file = file;
51 name = file.getName();
52
53 reader = HDF5Factory.openForReading(file);
54
55 // These files have changed structure over time. Originally they contained
56 // a single read per file where the base of the heirarchy was the read
57 // itself.
58 //
59 // Later the files moved to having multiple reads per file. Now there is
60 // an additional top level folder per read with the sub-structure being the
61 // same as it used to be for the individual reads.
62 //
63 // We need to account for both of these structures.
64
65
66 // See if we can see a bunch of paths starting with "read_" at the top of the
67 // heirarchy. If we can then we substitute the read paths for these.
68
69 List<String> topLevelFolders = reader.getGroupMembers("/");
70
71 List<String> readFolders = new ArrayList<String>();
72
73 for (String folder : topLevelFolders) {
74 System.err.println("Looking at "+folder);
75
76 if (folder.startsWith("read_")) {
77 readFolders.add(folder+"/");
78 }
79 }
80
81 if (readFolders.size() > 0) {
82 // We have read folders so we'll replace the default readPaths with
83 // the list we made
84
85 readPaths = readFolders.toArray(new String[0]);
86 }
87
88 }
89
90 public String name() {
91 return name;
92 }
93
94 public int getPercentComplete() {
95 return (readPathsIndexPosition*100) / readPaths.length;
96 }
97
98 public boolean isColorspace() {
99 return false;
100 }
101
102 public boolean hasNext() {
103 return readPathsIndexPosition < readPaths.length;
104 }
105
106 public Sequence next() throws SequenceFormatException {
107
108 for (int r=0;r<rdfPaths.length;r++) {
109
110 if (reader.exists(readPaths[readPathsIndexPosition]+rdfPaths[r])) {
111
112 String fastq = reader.readString(readPaths[readPathsIndexPosition]+rdfPaths[r]);
113
114 String [] sections = fastq.split("\\n");
115
116 if (sections.length != 4) {
117 throw new SequenceFormatException("Didn't get 4 sections from "+fastq);
118 }
119
120 Sequence seq = new Sequence(this, sections[1].toUpperCase(),sections[3], sections[0]);
121 ++readPathsIndexPosition;
122
123 if(readPathsIndexPosition >= readPaths.length) {
124 reader.close();
125 }
126
127 return(seq);
128 }
129 }
130
131 throw new SequenceFormatException("No valid fastq paths found in "+file);
132 }
133
134 public void remove() {
135 // No action here
136 }
137
138 public File getFile() {
139 return file;
140 }
141
142 }