2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
21 import java.io.InputStream;
23 import jalview.datamodel.*;
26 * A low level class for alignment and feature IO with alignment formatting
27 * methods used by both applet and application for generating flat alignment
28 * files. It also holds the lists of magic format names that the applet and
29 * application will allow the user to read or write files with.
34 public class AppletFormatAdapter
37 * List of valid format strings used in the isValidFormat method
39 public static final String[] READABLE_FORMATS = new String[]
40 { "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "STH",
41 "PDB", "JnetFile" }; // , "SimpleBLAST" };
44 * List of valid format strings for use by callers of the formatSequences
47 public static final String[] WRITEABLE_FORMATS = new String[]
48 { "BLC", "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "AMSA" };
51 * List of extensions corresponding to file format types in WRITABLE_FNAMES
52 * that are writable by the application.
54 public static final String[] WRITABLE_EXTENSIONS = new String[]
55 { "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa", "jar" };
58 * List of writable formats by the application. Order must correspond with the
59 * WRITABLE_EXTENSIONS list of formats.
61 public static final String[] WRITABLE_FNAMES = new String[]
62 { "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Jalview" };
65 * List of readable format file extensions by application in order
66 * corresponding to READABLE_FNAMES
68 public static final String[] READABLE_EXTENSIONS = new String[]
69 { "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa", "jar",
76 * List of readable formats by application in order corresponding to
79 public static final String[] READABLE_FNAMES = new String[]
80 { "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Jalview",
86 public static String INVALID_CHARACTERS = "Contains invalid characters";
88 // TODO: make these messages dynamic
89 public static String SUPPORTED_FORMATS = "Formats currently supported are\n"
90 + prettyPrint(READABLE_FORMATS);
95 * @return grammatically correct(ish) list consisting of els elements.
97 public static String prettyPrint(String[] els)
99 StringBuffer list = new StringBuffer();
100 for (int i = 0, iSize = els.length - 1; i < iSize; i++)
105 list.append(" and " + els[els.length - 1] + ".");
106 return list.toString();
109 public static String FILE = "File";
111 public static String URL = "URL";
113 public static String PASTE = "Paste";
115 public static String CLASSLOADER = "ClassLoader";
117 AlignFile afile = null;
122 * character used to write newlines
124 protected String newline = System.getProperty("line.separator");
126 public void setNewlineString(String nl)
131 public String getNewlineString()
137 * check that this format is valid for reading
140 * a format string to be compared with READABLE_FORMATS
141 * @return true if format is readable
143 public static final boolean isValidFormat(String format)
145 return isValidFormat(format, false);
149 * validate format is valid for IO
152 * a format string to be compared with either READABLE_FORMATS or
155 * when true, format is checked for containment in WRITEABLE_FORMATS
156 * @return true if format is valid
158 public static final boolean isValidFormat(String format,
161 boolean valid = false;
162 String[] format_list = (forwriting) ? WRITEABLE_FORMATS
164 for (int i = 0; i < format_list.length; i++)
166 if (format_list[i].equalsIgnoreCase(format))
176 * Constructs the correct filetype parser for a characterised datasource
183 * File format of data provided by datasource
185 * @return DOCUMENT ME!
187 public Alignment readFile(String inFile, String type, String format)
188 throws java.io.IOException
190 // TODO: generalise mapping between format string and io. class instances
191 // using Constructor.invoke reflection
192 this.inFile = inFile;
195 if (format.equals("FASTA"))
197 afile = new FastaFile(inFile, type);
199 else if (format.equals("MSF"))
201 afile = new MSFfile(inFile, type);
203 else if (format.equals("PileUp"))
205 afile = new PileUpfile(inFile, type);
207 else if (format.equals("CLUSTAL"))
209 afile = new ClustalFile(inFile, type);
211 else if (format.equals("BLC"))
213 afile = new BLCFile(inFile, type);
215 else if (format.equals("PIR"))
217 afile = new PIRFile(inFile, type);
219 else if (format.equals("PFAM"))
221 afile = new PfamFile(inFile, type);
223 else if (format.equals("JnetFile"))
225 afile = new JPredFile(inFile, type);
226 ((JPredFile) afile).removeNonSequences();
228 else if (format.equals("PDB"))
230 afile = new MCview.PDBfile(inFile, type);
232 else if (format.equals("STH"))
234 afile = new StockholmFile(inFile, type);
236 else if (format.equals("SimpleBLAST"))
238 afile = new SimpleBlastFile(inFile, type);
241 Alignment al = new Alignment(afile.getSeqsAsArray());
243 afile.addAnnotations(al);
246 } catch (Exception e)
249 System.err.println("Failed to read alignment using the '" + format
250 + "' reader.\n" + e);
252 if (e.getMessage() != null
253 && e.getMessage().startsWith(INVALID_CHARACTERS))
255 throw new java.io.IOException(e.getMessage());
258 // Finally test if the user has pasted just the sequence, no id
259 if (type.equalsIgnoreCase("Paste"))
263 // Possible sequence is just residues with no label
264 afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
265 Alignment al = new Alignment(afile.getSeqsAsArray());
266 afile.addAnnotations(al);
269 } catch (Exception ex)
271 if (ex.toString().startsWith(INVALID_CHARACTERS))
273 throw new java.io.IOException(e.getMessage());
276 ex.printStackTrace();
280 // If we get to this stage, the format was not supported
281 throw new java.io.IOException(SUPPORTED_FORMATS);
286 * Constructs the correct filetype parser for an already open datasource
289 * an existing datasource
291 * File format of data that will be provided by datasource
293 * @return DOCUMENT ME!
295 public Alignment readFromFile(FileParse source, String format)
296 throws java.io.IOException
298 // TODO: generalise mapping between format string and io. class instances
299 // using Constructor.invoke reflection
300 // This is exactly the same as the readFile method except we substitute
301 // 'inFile, type' with 'source'
302 this.inFile = source.getInFile();
303 String type = source.type;
306 if (format.equals("FASTA"))
308 afile = new FastaFile(source);
310 else if (format.equals("MSF"))
312 afile = new MSFfile(source);
314 else if (format.equals("PileUp"))
316 afile = new PileUpfile(source);
318 else if (format.equals("CLUSTAL"))
320 afile = new ClustalFile(source);
322 else if (format.equals("BLC"))
324 afile = new BLCFile(source);
326 else if (format.equals("PIR"))
328 afile = new PIRFile(source);
330 else if (format.equals("PFAM"))
332 afile = new PfamFile(source);
334 else if (format.equals("JnetFile"))
336 afile = new JPredFile(source);
337 ((JPredFile) afile).removeNonSequences();
339 else if (format.equals("PDB"))
341 afile = new MCview.PDBfile(source);
343 else if (format.equals("STH"))
345 afile = new StockholmFile(source);
347 else if (format.equals("SimpleBLAST"))
349 afile = new SimpleBlastFile(source);
352 Alignment al = new Alignment(afile.getSeqsAsArray());
354 afile.addAnnotations(al);
357 } catch (Exception e)
360 System.err.println("Failed to read alignment using the '" + format
361 + "' reader.\n" + e);
363 if (e.getMessage() != null
364 && e.getMessage().startsWith(INVALID_CHARACTERS))
366 throw new java.io.IOException(e.getMessage());
369 // Finally test if the user has pasted just the sequence, no id
370 if (type.equalsIgnoreCase("Paste"))
374 // Possible sequence is just residues with no label
375 afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste");
376 Alignment al = new Alignment(afile.getSeqsAsArray());
377 afile.addAnnotations(al);
380 } catch (Exception ex)
382 if (ex.toString().startsWith(INVALID_CHARACTERS))
384 throw new java.io.IOException(e.getMessage());
387 ex.printStackTrace();
391 // If we get to this stage, the format was not supported
392 throw new java.io.IOException(SUPPORTED_FORMATS);
397 * Construct an output class for an alignment in a particular filetype TODO:
398 * allow caller to detect errors and warnings encountered when generating
402 * string name of alignment format
404 * the alignment to be written out
406 * passed to AlnFile class controls whether /START-END is added to
409 * @return alignment flat file contents
411 public String formatSequences(String format, AlignmentI alignment,
416 AlignFile afile = null;
418 if (format.equalsIgnoreCase("FASTA"))
420 afile = new FastaFile();
422 else if (format.equalsIgnoreCase("MSF"))
424 afile = new MSFfile();
426 else if (format.equalsIgnoreCase("PileUp"))
428 afile = new PileUpfile();
430 else if (format.equalsIgnoreCase("CLUSTAL"))
432 afile = new ClustalFile();
434 else if (format.equalsIgnoreCase("BLC"))
436 afile = new BLCFile();
438 else if (format.equalsIgnoreCase("PIR"))
440 afile = new PIRFile();
442 else if (format.equalsIgnoreCase("PFAM"))
444 afile = new PfamFile();
446 else if (format.equalsIgnoreCase("STH"))
448 afile = new StockholmFile();
450 else if (format.equalsIgnoreCase("AMSA"))
452 afile = new AMSAFile(alignment);
457 "Implementation error: Unknown file format string");
459 afile.setNewlineString(newline);
460 afile.addJVSuffix(jvsuffix);
462 afile.setSeqs(alignment.getSequencesArray());
464 String afileresp = afile.print();
465 if (afile.hasWarningMessage())
467 System.err.println("Warning raised when writing as " + format
468 + " : " + afile.getWarningMessage());
471 } catch (Exception e)
473 System.err.println("Failed to write alignment as a '" + format
481 public static String checkProtocol(String file)
483 String protocol = FILE;
484 String ft = file.toLowerCase().trim();
485 if (ft.indexOf("http:") == 0 || ft.indexOf("https:") == 0
486 || ft.indexOf("file:") == 0)
493 public static void main(String[] args)
496 while (i < args.length)
498 File f = new File(args[i]);
503 System.out.println("Reading file: " + f);
504 AppletFormatAdapter afa = new AppletFormatAdapter();
505 Runtime r = Runtime.getRuntime();
507 long memf = -r.totalMemory() + r.freeMemory();
508 long t1 = -System.currentTimeMillis();
509 Alignment al = afa.readFile(args[i], FILE,
510 new IdentifyFile().Identify(args[i], FILE));
511 t1 += System.currentTimeMillis();
513 memf += r.totalMemory() - r.freeMemory();
516 System.out.println("Alignment contains " + al.getHeight()
517 + " sequences and " + al.getWidth() + " columns.");
520 System.out.println(new AppletFormatAdapter().formatSequences(
522 } catch (Exception e)
525 .println("Couln't format the alignment for output as a FASTA file.");
526 e.printStackTrace(System.err);
531 System.out.println("Couldn't read alignment");
533 System.out.println("Read took " + (t1 / 1000.0) + " seconds.");
535 .println("Difference between free memory now and before is "
536 + (memf / (1024.0 * 1024.0) * 1.0) + " MB");
538 } catch (Exception e)
540 System.err.println("Exception when dealing with " + i
541 + "'th argument: " + args[i] + "\n" + e);
546 System.err.println("Ignoring argument '" + args[i] + "' (" + i
547 + "'th)- not a readable file.");
554 * try to discover how to access the given file as a valid datasource that
555 * will be identified as the given type.
559 * @return protocol that yields the data parsable as the given type
561 public static String resolveProtocol(String file, String format)
563 return resolveProtocol(file, format, false);
566 public static String resolveProtocol(String file, String format,
569 // TODO: test thoroughly!
570 String protocol = null;
573 System.out.println("resolving datasource started with:\n>>file\n"
574 + file + ">>endfile");
577 // This might throw a security exception in certain browsers
578 // Netscape Communicator for instance.
582 InputStream is = System.getSecurityManager().getClass()
583 .getResourceAsStream("/" + file);
591 System.err.println("Resource '" + file + "' was "
592 + (rtn ? "" : "not") + " located by classloader.");
597 protocol = AppletFormatAdapter.CLASSLOADER;
600 } catch (Exception ex)
603 .println("Exception checking resources: " + file + " " + ex);
606 if (file.indexOf("://") > -1)
608 protocol = AppletFormatAdapter.URL;
612 // skipping codebase prepend check.
613 protocol = AppletFormatAdapter.FILE;
620 System.out.println("Trying to get contents of resource as "
623 fp = new FileParse(file, protocol);
632 System.out.println("Successful.");
635 } catch (Exception e)
639 System.err.println("Exception when accessing content: " + e);
647 System.out.println("Accessing as paste.");
649 protocol = AppletFormatAdapter.PASTE;
653 fp = new FileParse(file, protocol);
658 } catch (Exception e)
660 System.err.println("Failed to access content as paste!");
669 if (format == null || format.length() == 0)
677 String idformat = new jalview.io.IdentifyFile().Identify(file,
679 if (idformat == null)
683 System.out.println("Format not identified. Inaccessible file.");
689 System.out.println("Format identified as " + idformat
690 + "and expected as " + format);
692 if (idformat.equals(format))
696 System.out.println("Protocol identified as " + protocol);
705 .println("File deemed not accessible via " + protocol);
710 } catch (Exception e)
714 System.err.println("File deemed not accessible via " + protocol);