2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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", "STH", "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", "sto,stk" };
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", "STH"};
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(alignment);
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 String fName = f.getName();
506 String extension = fName.substring(fName.lastIndexOf(".") + 1, fName.length());
507 if (extension.equals("stk") || extension.equals("sto"))
513 Runtime r = Runtime.getRuntime();
515 long memf = -r.totalMemory() + r.freeMemory();
516 long t1 = -System.currentTimeMillis();
517 Alignment al = afa.readFile(args[i], FILE,
518 new IdentifyFile().Identify(args[i], FILE));
519 t1 += System.currentTimeMillis();
521 memf += r.totalMemory() - r.freeMemory();
524 System.out.println("Alignment contains " + al.getHeight()
525 + " sequences and " + al.getWidth() + " columns.");
528 System.out.println(new AppletFormatAdapter().formatSequences(
530 } catch (Exception e)
533 .println("Couln't format the alignment for output as a FASTA file.");
534 e.printStackTrace(System.err);
539 System.out.println("Couldn't read alignment");
541 System.out.println("Read took " + (t1 / 1000.0) + " seconds.");
543 .println("Difference between free memory now and before is "
544 + (memf / (1024.0 * 1024.0) * 1.0) + " MB");
546 } catch (Exception e)
548 System.err.println("Exception when dealing with " + i
549 + "'th argument: " + args[i] + "\n" + e);
555 System.err.println("Ignoring argument '" + args[i] + "' (" + i
556 + "'th)- not a readable file.");
562 private void test(File f) {
563 System.out.println("Reading file: " + f);
564 String ff = f.getPath();
567 Alignment al = readFile(ff, FILE, new IdentifyFile().Identify(ff, FILE));
568 for (int i = 0; i < al.getSequencesArray().length; ++i) {
569 al.getSequenceAt(i).setDatasetSequence(al.getSequenceAt(i));
571 AlignFile stFile = new StockholmFile(al);
572 stFile.setSeqs(al.getSequencesArray());
574 String stockholmoutput = stFile.print();
575 Alignment al_input = readFile(stockholmoutput, AppletFormatAdapter.PASTE, "STH");
576 if (al != null && al_input!= null)
578 System.out.println("Alignment contains: " + al.getHeight() + " and " + al_input.getHeight()
579 + " sequences; " + al.getWidth() + " and " + al_input.getWidth() + " columns.");
580 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
581 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
583 // check Alignment annotation
584 if (aa_new != null && aa_original != null)
586 System.out.println("Alignment contains: " + aa_new.length
587 + " and " + aa_original.length + " alignment annotation(s)");
588 for (int i = 0; i < aa_original.length; i++)
590 if (!equalss(aa_original[i], aa_new[i]))
591 System.out.println("Different alignment annotation");
595 // check sequences, annotation and features
596 SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
597 seq_original = al.getSequencesArray();
598 SequenceI[] seq_new = new SequenceI[al_input.getSequencesArray().length];
599 seq_new = al_input.getSequencesArray();
600 SequenceFeature[] sequenceFeatures_original,sequenceFeatures_new;
601 AlignmentAnnotation annot_original, annot_new;
603 for (int i = 0; i < al.getSequencesArray().length; i++)
605 String name = seq_original[i].getName();
606 int start = seq_original[i].getStart();
607 int end = seq_original[i].getEnd();
608 System.out.println("Check sequence: " + name + "/" + start + "-" + end);
610 // search equal sequence
611 for (int in = 0; in < al_input.getSequencesArray().length; in++) {
612 if (name.equals(seq_new[in].getName()) &&
613 start == seq_new[in].getStart() &&
614 end ==seq_new[in].getEnd())
616 String ss_original = seq_original[i].getSequenceAsString();
617 String ss_new = seq_new[in].getSequenceAsString();
618 if (!ss_original.equals(ss_new))
620 System.out.println("The sequences " + name + "/" + start + "-" + end + " are not equal");
623 // compare sequence features
624 if (seq_original[i].getSequenceFeatures() != null && seq_new[in].getSequenceFeatures() != null)
626 System.out.println("There are feature!!!");
627 sequenceFeatures_original = new SequenceFeature[seq_original[i].getSequenceFeatures().length];
628 sequenceFeatures_original = seq_original[i].getSequenceFeatures();
629 sequenceFeatures_new = new SequenceFeature[seq_new[in].getSequenceFeatures().length];
630 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
632 if (seq_original[i].getSequenceFeatures().length == seq_new[in].getSequenceFeatures().length)
634 for (int feat = 0; feat < seq_original[i].getSequenceFeatures().length; feat++) {
635 if (!sequenceFeatures_original[feat].equals(sequenceFeatures_new[feat])) {
636 System.out.println("Different features");
642 System.out.println("different number of features");
644 } else if (seq_original[i].getSequenceFeatures() == null && seq_new[in].getSequenceFeatures() == null)
646 System.out.println("No sequence features");
647 } else if (seq_original[i].getSequenceFeatures() != null && seq_new[in].getSequenceFeatures() == null)
649 System.out.println("Coudn't compare sequence features new one");
651 // compare alignment annotation
652 if (al.getSequenceAt(i).getAnnotation() != null && al_input.getSequenceAt(in).getAnnotation() != null)
654 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
656 if (al.getSequenceAt(i).getAnnotation()[j] != null &&
657 al_input.getSequenceAt(in).getAnnotation()[j] != null)
659 annot_original = al.getSequenceAt(i).getAnnotation()[j];
660 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
661 if (!equalss(annot_original, annot_new))
662 System.out.println("Different annotation");
665 } else if (al.getSequenceAt(i).getAnnotation() == null && al_input.getSequenceAt(in).getAnnotation() == null)
667 System.out.println("No annotations");
668 } else if (al.getSequenceAt(i).getAnnotation() != null && al_input.getSequenceAt(in).getAnnotation() == null)
670 System.out.println("Coudn't compare annotations new one");
678 System.out.println("Couldn't read alignment");
680 } catch (Exception e)
682 System.err.println("Couln't format the alignment for output file.");
683 e.printStackTrace(System.err);
688 * compare annotations
690 private boolean equalss(AlignmentAnnotation annot_or, AlignmentAnnotation annot_new)
692 if (annot_or.annotations.length != annot_new.annotations.length)
696 for (int i = 0; i < annot_or.annotations.length; i++)
698 if (annot_or.annotations[i] != null && annot_new.annotations[i] != null)
700 if (!annot_or.annotations[i].displayCharacter.equals(annot_new.annotations[i].displayCharacter) &&
701 annot_or.annotations[i].secondaryStructure != annot_new.annotations[i].secondaryStructure &&
702 !annot_or.annotations[i].description.equals(annot_new.annotations[i].description))
706 } else if (annot_or.annotations[i] == null && annot_new.annotations[i] == null)
717 * try to discover how to access the given file as a valid datasource that
718 * will be identified as the given type.
722 * @return protocol that yields the data parsable as the given type
724 public static String resolveProtocol(String file, String format)
726 return resolveProtocol(file, format, false);
729 public static String resolveProtocol(String file, String format,
732 // TODO: test thoroughly!
733 String protocol = null;
736 System.out.println("resolving datasource started with:\n>>file\n"
737 + file + ">>endfile");
740 // This might throw a security exception in certain browsers
741 // Netscape Communicator for instance.
745 InputStream is = System.getSecurityManager().getClass()
746 .getResourceAsStream("/" + file);
754 System.err.println("Resource '" + file + "' was "
755 + (rtn ? "" : "not") + " located by classloader.");
760 protocol = AppletFormatAdapter.CLASSLOADER;
763 } catch (Exception ex)
766 .println("Exception checking resources: " + file + " " + ex);
769 if (file.indexOf("://") > -1)
771 protocol = AppletFormatAdapter.URL;
775 // skipping codebase prepend check.
776 protocol = AppletFormatAdapter.FILE;
783 System.out.println("Trying to get contents of resource as "
786 fp = new FileParse(file, protocol);
795 System.out.println("Successful.");
798 } catch (Exception e)
802 System.err.println("Exception when accessing content: " + e);
810 System.out.println("Accessing as paste.");
812 protocol = AppletFormatAdapter.PASTE;
816 fp = new FileParse(file, protocol);
821 } catch (Exception e)
823 System.err.println("Failed to access content as paste!");
832 if (format == null || format.length() == 0)
840 String idformat = new jalview.io.IdentifyFile().Identify(file,
842 if (idformat == null)
846 System.out.println("Format not identified. Inaccessible file.");
852 System.out.println("Format identified as " + idformat
853 + "and expected as " + format);
855 if (idformat.equals(format))
859 System.out.println("Protocol identified as " + protocol);
868 .println("File deemed not accessible via " + protocol);
873 } catch (Exception e)
877 System.err.println("File deemed not accessible via " + protocol);