Debian Codegraph @

uk/ac/babraham/FastQC/FastQCApplication.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;
21
22 import java.awt.BorderLayout;
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.Vector;
26
27 import javax.swing.ImageIcon;
28 import javax.swing.JFileChooser;
29 import javax.swing.JFrame;
30 import javax.swing.JLabel;
31 import javax.swing.JOptionPane;
32 import javax.swing.JPanel;
33 import javax.swing.JTabbedPane;
34 import javax.swing.UIManager;
35 import javax.swing.filechooser.FileFilter;
36
37 import uk.ac.babraham.FastQC.Analysis.AnalysisRunner;
38 import uk.ac.babraham.FastQC.Analysis.OfflineRunner;
39 import uk.ac.babraham.FastQC.Dialogs.WelcomePanel;
40 import uk.ac.babraham.FastQC.FileFilters.BAMFileFilter;
41 import uk.ac.babraham.FastQC.FileFilters.CasavaFastQFileFilter;
42 import uk.ac.babraham.FastQC.FileFilters.FastQFileFilter;
43 import uk.ac.babraham.FastQC.FileFilters.MappedBAMFileFilter;
44 import uk.ac.babraham.FastQC.FileFilters.SequenceFileFilter;
45 import uk.ac.babraham.FastQC.Modules.ModuleFactory;
46 import uk.ac.babraham.FastQC.Modules.QCModule;
47 import uk.ac.babraham.FastQC.Report.HTMLReportArchive;
48 import uk.ac.babraham.FastQC.Results.ResultsPanel;
49 import uk.ac.babraham.FastQC.Sequence.SequenceFactory;
50 import uk.ac.babraham.FastQC.Sequence.SequenceFile;
51 import uk.ac.babraham.FastQC.Sequence.SequenceFormatException;
52 import uk.ac.babraham.FastQC.Utilities.CasavaBasename;
53 import uk.ac.babraham.FastQC.Utilities.NanoporeBasename;
54
55 public class FastQCApplication extends JFrame {
56
57 public static final String VERSION = "0.12.1";
58
59 private JTabbedPane fileTabs;
60 private WelcomePanel welcomePanel;
61 private File lastUsedDir = null;
62
63 public FastQCApplication () {
64 setTitle("FastQC");
65 setIconImage(new ImageIcon(ClassLoader.getSystemResource("uk/ac/babraham/FastQC/Resources/fastqc_icon.png")).getImage());
66 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
67 // setSize(1280, 720);
68 setSize(800,600);
69 setLocationRelativeTo(null);
70
71 welcomePanel = new WelcomePanel();
72
73 fileTabs = new JTabbedPane(JTabbedPane.TOP);
74 setContentPane(welcomePanel);
75
76 setJMenuBar(new FastQCMenuBar(this));
77
78 }
79
80 public void close () {
81 if (fileTabs.getSelectedIndex() >=0) {
82 fileTabs.remove(fileTabs.getSelectedIndex());
83 }
84 if (fileTabs.getTabCount() == 0) {
85 setContentPane(welcomePanel);
86 validate();
87 repaint();
88 }
89 }
90
91 public void closeAll () {
92 fileTabs.removeAll();
93 setContentPane(welcomePanel);
94 validate();
95 repaint();
96 }
97
98 public void openFile () {
99 JFileChooser chooser;
100
101 if (lastUsedDir == null) {
102 chooser = new JFileChooser();
103 }
104 else {
105 chooser = new JFileChooser(lastUsedDir);
106 }
107 chooser.setMultiSelectionEnabled(true);
108 SequenceFileFilter sff = new SequenceFileFilter();
109 chooser.addChoosableFileFilter(sff);
110 chooser.addChoosableFileFilter(new FastQFileFilter());
111 chooser.addChoosableFileFilter(new CasavaFastQFileFilter());
112 chooser.addChoosableFileFilter(new BAMFileFilter());
113 chooser.addChoosableFileFilter(new MappedBAMFileFilter());
114 chooser.setFileFilter(sff);
115 int result = chooser.showOpenDialog(this);
116 if (result == JFileChooser.CANCEL_OPTION) return;
117
118 // See if they forced a file format
119 FileFilter chosenFilter = chooser.getFileFilter();
120 if (chosenFilter instanceof FastQFileFilter) {
121 FastQCConfig.getInstance().setSequenceFormat("fastq");
122 }
123 if (chosenFilter instanceof CasavaFastQFileFilter) {
124 FastQCConfig.getInstance().setSequenceFormat("fastq");
125 FastQCConfig.getInstance().setCasavaMode(true);
126
127 }
128 else if (chosenFilter instanceof BAMFileFilter) {
129 FastQCConfig.getInstance().setSequenceFormat("bam");
130 }
131 else if (chosenFilter instanceof MappedBAMFileFilter) {
132 FastQCConfig.getInstance().setSequenceFormat("bam_mapped");
133 System.setProperty("fastqc.sequence_format", "bam_mapped");
134 }
135
136 // If we're still showing the welcome panel switch this out for
137 // the file tabs panel
138 if (fileTabs.getTabCount() == 0) {
139 setContentPane(fileTabs);
140 validate();
141 repaint();
142 }
143
144 File [] files = chooser.getSelectedFiles();
145
146 if (FastQCConfig.getInstance().nano) {
147 // Some of the files we've been passed might be directories and we would need to
148 // list the fast5 files within those directories.
149
150 Vector<File> keptFiles = new Vector<File>();
151
152 for (int f=0;f<files.length;f++) {
153 if (files[f].isDirectory()) {
154 File [] fast5files = files[f].listFiles();
155 for (int i=0;i<fast5files.length;i++) {
156 if (fast5files[i].getName().endsWith(".fast5")) {
157 keptFiles.add(fast5files[i]);
158 }
159 }
160 }
161 else {
162 keptFiles.add(files[f]);
163 }
164 }
165
166 files = keptFiles.toArray(new File[0]);
167
168 }
169
170
171 File [][] fileGroups;
172
173 // See if we need to group together files from a casava group
174 if (FastQCConfig.getInstance().casava) {
175 fileGroups = CasavaBasename.getCasavaGroups(files);
176 }
177 else if (FastQCConfig.getInstance().nano) {
178 fileGroups = NanoporeBasename.getNanoporeGroups(files);
179 }
180 else {
181 fileGroups = new File [files.length][1];
182 for (int f=0;f<files.length;f++) {
183 fileGroups[f][0] = files[f];
184 }
185 }
186
187
188 for (int i=0;i<fileGroups.length;i++) {
189 File [] filesToProcess = fileGroups[i];
190 lastUsedDir = filesToProcess[0].getParentFile();
191 SequenceFile sequenceFile;
192
193
194 try {
195 sequenceFile = SequenceFactory.getSequenceFile(filesToProcess);
196 }
197 catch (SequenceFormatException e) {
198 JPanel errorPanel = new JPanel();
199 errorPanel.setLayout(new BorderLayout());
200 errorPanel.add(new JLabel("File format error: "+e.getLocalizedMessage(), JLabel.CENTER),BorderLayout.CENTER);
201 fileTabs.addTab(filesToProcess[0].getName(), errorPanel);
202 e.printStackTrace();
203 continue;
204 }
205 catch (IOException e) {
206 System.err.println("File broken");
207 e.printStackTrace();
208 JOptionPane.showMessageDialog(this, "Couldn't read file:"+e.getLocalizedMessage(), "Error reading file", JOptionPane.ERROR_MESSAGE);
209 continue;
210 }
211
212 AnalysisRunner runner = new AnalysisRunner(sequenceFile);
213 ResultsPanel rp = new ResultsPanel(sequenceFile);
214 runner.addAnalysisListener(rp);
215 fileTabs.addTab(sequenceFile.name(), rp);
216
217
218 QCModule [] module_list = ModuleFactory.getStandardModuleList();
219
220 runner.startAnalysis(module_list);
221 }
222 }
223
224 public void saveReport () {
225 JFileChooser chooser;
226
227 if (lastUsedDir == null) {
228 chooser = new JFileChooser();
229 }
230 else {
231 chooser = new JFileChooser(lastUsedDir);
232 }
233
234 if (fileTabs.getSelectedComponent() == null) {
235 JOptionPane.showMessageDialog(this, "No FastQ files are open yet", "Can't save report", JOptionPane.ERROR_MESSAGE);
236 return;
237 }
238 chooser.setSelectedFile(new File(((ResultsPanel)fileTabs.getSelectedComponent()).sequenceFile().getFile().getName().replaceAll("stdin:","").replaceAll(".gz$","").replaceAll(".bz2$","").replaceAll(".txt$","").replaceAll(".fastq$", "").replaceAll(".fq$", "").replaceAll(".sam$", "").replaceAll(".bam$", "").replaceAll(".ubam$", "")+"_fastqc.html"));
239 chooser.setMultiSelectionEnabled(false);
240 chooser.setFileFilter(new FileFilter() {
241
242 public String getDescription() {
243 return "HTML files";
244 }
245
246 public boolean accept(File f) {
247 if (f.isDirectory() || f.getName().toLowerCase().endsWith(".html")) {
248 return true;
249 }
250 else {
251 return false;
252 }
253 }
254
255 });
256
257 File reportFile;
258 while (true) {
259 int result = chooser.showSaveDialog(this);
260 if (result == JFileChooser.CANCEL_OPTION) return;
261
262 reportFile = chooser.getSelectedFile();
263 if (! reportFile.getName().toLowerCase().endsWith(".html")) {
264 reportFile = new File(reportFile.getAbsoluteFile()+".html");
265 }
266
267 // Check if we're overwriting something
268 if (reportFile.exists()) {
269 int reply = JOptionPane.showConfirmDialog(this, reportFile.getName()+" already exists. Overwrite?", "Overwrite existing file?", JOptionPane.YES_NO_OPTION);
270 if (reply == JOptionPane.NO_OPTION) {
271 continue;
272 }
273 else {
274 break;
275 }
276 }
277 else {
278 break;
279 }
280 }
281
282
283 ResultsPanel selectedPanel = (ResultsPanel)fileTabs.getSelectedComponent();
284
285 try {
286 new HTMLReportArchive(selectedPanel.sequenceFile(), selectedPanel.modules(), reportFile);
287 }
288 catch (Exception e) {
289 JOptionPane.showMessageDialog(this, "Failed to create archive: "+e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
290 e.printStackTrace();
291 }
292 }
293
294 public static void main(String[] args) {
295
296 // See if we just have to print out the version
297 if (System.getProperty("fastqc.show_version") != null && System.getProperty("fastqc.show_version").equals("true")) {
298 System.out.println("FastQC v"+VERSION);
299 System.exit(0);
300 }
301
302 if (args.length > 0) {
303 // Set headless to true so we don't get problems
304 // with people working without an X display.
305 System.setProperty("java.awt.headless", "true");
306
307 // We used to default to unzipping the zip file in
308 // non-interactive runs. As we now save an HTML
309 // report at the top level we no longer do this
310 // so unzip is false unless explicitly set to be true.
311
312 if (FastQCConfig.getInstance().do_unzip == null) {
313 FastQCConfig.getInstance().do_unzip = false;
314 }
315
316 new OfflineRunner(args);
317 System.exit(0);
318 }
319
320 else {
321 // Recent java themes for linux are just horribly broken with missing
322 // bits of UI. We're therefore not going to set a native look if
323 // we're on linux. See seqmonk bug #95 for details.
324
325 try {
326 if (! System.getProperty("os.name").toLowerCase().contains("linux")) {
327 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
328 }
329 } catch (Exception e) {}
330
331
332 // The interactive default is to not uncompress the
333 // reports after they have been generated
334 if (FastQCConfig.getInstance().do_unzip == null) {
335 FastQCConfig.getInstance().do_unzip = false;
336 }
337
338 FastQCApplication app = new FastQCApplication();
339
340 app.setVisible(true);
341 }
342 }
343
344 }