2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.api.AlignExportSettingI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.AlignmentView;
29 import jalview.datamodel.PDBEntry.Type;
30 import jalview.datamodel.SequenceI;
31 import jalview.ext.jmol.JmolParser;
32 import jalview.structure.StructureImportSettings;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.util.List;
40 * A low level class for alignment and feature IO with alignment formatting
41 * methods used by both applet and application for generating flat alignment
42 * files. It also holds the lists of magic format names that the applet and
43 * application will allow the user to read or write files with.
48 public class AppletFormatAdapter
50 private AlignmentViewPanel viewpanel;
53 * add jalview-derived non-secondary structure annotation from PDB structure
55 boolean annotFromStructure = false;
58 * add secondary structure from PDB data with built-in algorithms
60 boolean localSecondaryStruct = false;
63 * process PDB data with web services
65 boolean serviceSecondaryStruct = false;
67 private AlignmentFileI alignFile = null;
72 * character used to write newlines
74 protected String newline = System.getProperty("line.separator");
76 private AlignExportSettingI exportSettings;
79 * List of valid format strings used in the isValidFormat method
81 // public static final String[] READABLE_FORMATS = new String[] { "BLC",
82 // "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "STH", "PDB",
83 // "JnetFile", "RNAML", "PHYLIP", "JSON",
84 // IdentifyFile.FeaturesFile, "HTML", "mmCIF" };
87 * List of readable format file extensions by application in order
88 * corresponding to READABLE_FNAMES
90 // public static final String[] READABLE_EXTENSIONS = new String[] {
91 // "fa, fasta, mfa, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa",
92 // "sto,stk", "xml,rnaml", "phy", "json", ".gff2,gff3", "jar,jvp",
97 // * List of readable formats by application in order corresponding to
98 // * READABLE_EXTENSIONS
100 // public static final String[] READABLE_FNAMES = new String[] { "Fasta",
101 // "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Stockholm", "RNAML",
102 // "PHYLIP", "JSON", IdentifyFile.FeaturesFile,
103 // "Jalview", HtmlFile.FILE_DESC, "mmCIF" };
106 * List of valid format strings for use by callers of the formatSequences
109 // public static final String[] WRITEABLE_FORMATS = new String[] { "BLC",
110 // "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "AMSA", "STH",
111 // "PHYLIP", "JSON" };
114 * List of extensions corresponding to file format types in WRITABLE_FNAMES
115 * that are writable by the application.
117 // public static final String[] WRITABLE_EXTENSIONS = new String[] {
118 // "fa, fasta, mfa, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa",
119 // "sto,stk", "phy", "json", "jvp" };
122 * List of writable formats by the application. Order must correspond with the
123 * WRITABLE_EXTENSIONS list of formats.
125 // public static final String[] WRITABLE_FNAMES = new String[] { "Fasta",
126 // "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "STH", "PHYLIP",
127 // "JSON", "Jalview" };
129 public static String INVALID_CHARACTERS = "Contains invalid characters";
131 // TODO: make these messages dynamic
132 public static String SUPPORTED_FORMATS = "Formats currently supported are\n"
133 + prettyPrint(FileFormat.getReadableFormats());
135 public AppletFormatAdapter()
139 public AppletFormatAdapter(AlignmentViewPanel viewpanel)
141 this.viewpanel = viewpanel;
144 public AppletFormatAdapter(AlignmentViewPanel alignPanel,
145 AlignExportSettingI settings)
147 viewpanel = alignPanel;
148 exportSettings = settings;
152 * Formats a grammatically correct(ish) list consisting of the given objects
157 public static String prettyPrint(List<? extends Object> things)
159 StringBuffer list = new StringBuffer();
160 for (int i = 0, iSize = things.size() - 1; i < iSize; i++)
162 list.append(things.get(i).toString());
165 // could i18n 'and' here
166 list.append(" and " + things.get(things.size() - 1).toString() + ".");
167 return list.toString();
170 public void setNewlineString(String nl)
175 public String getNewlineString()
181 * Constructs the correct filetype parser for a characterised datasource
191 public AlignmentI readFile(String file, DataSourceType sourceType,
192 FileFormatI fileFormat) throws IOException
197 if (fileFormat == FileFormat.PDB || fileFormat == FileFormat.MMCif)
199 // TODO obtain config value from preference settings.
200 // Set value to 'true' to test PDB processing with Jmol: JAL-1213
201 boolean isParseWithJMOL = StructureImportSettings
202 .getDefaultPDBFileParser().equalsIgnoreCase(
203 StructureImportSettings.StructureParser.JMOL_PARSER
207 StructureImportSettings.addSettings(annotFromStructure,
208 localSecondaryStruct, serviceSecondaryStruct);
209 alignFile = new JmolParser(annotFromStructure,
210 localSecondaryStruct, serviceSecondaryStruct, inFile,
215 StructureImportSettings.addSettings(annotFromStructure,
216 localSecondaryStruct, serviceSecondaryStruct);
217 StructureImportSettings.setShowSeqFeatures(true);
218 alignFile = new MCview.PDBfile(annotFromStructure,
219 localSecondaryStruct, serviceSecondaryStruct, inFile,
222 ((StructureFile) alignFile)
223 .setDbRefType(fileFormat == FileFormat.PDB ? Type.PDB
228 alignFile = fileFormat.getAlignmentFile(inFile, sourceType);
230 // new FastaFile(inFile, sourceType);
231 // new MSFfile(inFile, sourceType);
232 // new PileUpfile(inFile, sourceType);
233 // new ClustalFile(inFile, sourceType);
234 // new BLCFile(inFile, sourceType);
235 // new PIRFile(inFile, sourceType);
236 // new PfamFile(inFile, sourceType);
237 // alignFile = new JPredFile(inFile, sourceType);
238 // ((JPredFile) alignFile).removeNonSequences();
239 // new StockholmFile(inFile, sourceType);
240 // new SimpleBlastFile(inFile, sourceType);
241 // new PhylipFile(inFile, sourceType);
242 // new JSONFile(inFile, sourceType);
243 // new HtmlFile(inFile, sourceType);
244 // new RnamlFile(inFile, sourceType);
245 // alignFile = new FeaturesFile(true, inFile, sourceType);
246 return buildAlignmentFromFile();
247 } catch (Exception e)
250 System.err.println("Failed to read alignment using the '"
251 + fileFormat + "' reader.\n" + e);
253 if (e.getMessage() != null
254 && e.getMessage().startsWith(INVALID_CHARACTERS))
256 throw new IOException(e.getMessage());
259 // Finally test if the user has pasted just the sequence, no id
260 if (sourceType == DataSourceType.PASTE)
264 // Possible sequence is just residues with no label
265 alignFile = new FastaFile(">UNKNOWN\n" + inFile,
266 DataSourceType.PASTE);
267 return buildAlignmentFromFile();
269 } catch (Exception ex)
271 if (ex.toString().startsWith(INVALID_CHARACTERS))
273 throw new IOException(e.getMessage());
276 ex.printStackTrace();
279 if (fileFormat == FileFormat.Html)
281 throw new IOException(e.getMessage());
284 throw new FileFormatException(SUPPORTED_FORMATS);
288 * Constructs the correct filetype parser for an already open datasource
291 * an existing datasource
293 * File format of data that will be provided by datasource
297 public AlignmentI readFromFile(FileParse source, FileFormatI format)
300 this.inFile = source.getInFile();
301 DataSourceType type = source.dataSourceType;
304 if (format == FileFormat.PDB || format == FileFormat.MMCif)
306 // TODO obtain config value from preference settings
307 boolean isParseWithJMOL = false;
310 StructureImportSettings.addSettings(annotFromStructure,
311 localSecondaryStruct, serviceSecondaryStruct);
312 alignFile = new JmolParser(annotFromStructure,
313 localSecondaryStruct, serviceSecondaryStruct, source);
317 StructureImportSettings.setShowSeqFeatures(true);
318 alignFile = new MCview.PDBfile(annotFromStructure,
319 localSecondaryStruct, serviceSecondaryStruct, source);
321 ((StructureFile) alignFile).setDbRefType(Type.PDB);
325 alignFile = format.getAlignmentFile(source);
328 return buildAlignmentFromFile();
330 } catch (Exception e)
333 System.err.println("Failed to read alignment using the '" + format
334 + "' reader.\n" + e);
336 if (e.getMessage() != null
337 && e.getMessage().startsWith(INVALID_CHARACTERS))
339 throw new FileFormatException(e.getMessage());
342 // Finally test if the user has pasted just the sequence, no id
343 if (type == DataSourceType.PASTE)
347 // Possible sequence is just residues with no label
348 alignFile = new FastaFile(">UNKNOWN\n" + inFile,
349 DataSourceType.PASTE);
350 return buildAlignmentFromFile();
352 } catch (Exception ex)
354 if (ex.toString().startsWith(INVALID_CHARACTERS))
356 throw new IOException(e.getMessage());
359 ex.printStackTrace();
363 // If we get to this stage, the format was not supported
364 throw new FileFormatException(SUPPORTED_FORMATS);
369 * boilerplate method to handle data from an AlignFile and construct a new
370 * alignment or import to an existing alignment
372 * @return AlignmentI instance ready to pass to a UI constructor
374 private AlignmentI buildAlignmentFromFile()
376 // Standard boilerplate for creating alignment from parser
377 // alignFile.configureForView(viewpanel);
379 AlignmentI al = new Alignment(alignFile.getSeqsAsArray());
381 alignFile.addAnnotations(al);
383 alignFile.addGroups(al);
389 * create an alignment flatfile from a Jalview alignment view
394 * @param selectedOnly
395 * @return flatfile in a string
397 public String formatSequences(FileFormatI format, boolean jvsuffix,
398 AlignmentViewPanel ap, boolean selectedOnly)
401 AlignmentView selvew = ap.getAlignViewport().getAlignmentView(
402 selectedOnly, false);
403 AlignmentI aselview = selvew.getVisibleAlignment(ap.getAlignViewport()
405 List<AlignmentAnnotation> ala = (ap.getAlignViewport()
406 .getVisibleAlignmentAnnotation(selectedOnly));
409 for (AlignmentAnnotation aa : ala)
411 aselview.addAnnotation(aa);
415 return formatSequences(format, aselview, jvsuffix);
419 * Construct an output class for an alignment in a particular filetype TODO:
420 * allow caller to detect errors and warnings encountered when generating
424 * string name of alignment format
426 * the alignment to be written out
428 * passed to AlnFile class controls whether /START-END is added to
431 * @return alignment flat file contents
433 public String formatSequences(FileFormatI format, AlignmentI alignment,
438 AlignmentFileI afile = format.getAlignmentFile(alignment);
440 afile.setNewlineString(newline);
441 afile.setExportSettings(exportSettings);
442 afile.configureForView(viewpanel);
444 // check whether we were given a specific alignment to export, rather than
445 // the one in the viewpanel
446 SequenceI[] seqs = null;
447 if (viewpanel == null || viewpanel.getAlignment() == null
448 || viewpanel.getAlignment() != alignment)
450 seqs = alignment.getSequencesArray();
454 seqs = viewpanel.getAlignment().getSequencesArray();
457 String afileresp = afile.print(seqs, jvsuffix);
458 if (afile.hasWarningMessage())
460 System.err.println("Warning raised when writing as " + format
461 + " : " + afile.getWarningMessage());
464 } catch (Exception e)
466 System.err.println("Failed to write alignment as a '" + format
474 public static DataSourceType checkProtocol(String file)
476 DataSourceType protocol = DataSourceType.FILE;
477 String ft = file.toLowerCase().trim();
478 if (ft.indexOf("http:") == 0 || ft.indexOf("https:") == 0
479 || ft.indexOf("file:") == 0)
481 protocol = DataSourceType.URL;
486 public static void main(String[] args)
489 while (i < args.length)
491 File f = new File(args[i]);
496 System.out.println("Reading file: " + f);
497 AppletFormatAdapter afa = new AppletFormatAdapter();
498 Runtime r = Runtime.getRuntime();
500 long memf = -r.totalMemory() + r.freeMemory();
501 long t1 = -System.currentTimeMillis();
503 .readFile(args[i], DataSourceType.FILE,
504 new IdentifyFile().identify(args[i],
505 DataSourceType.FILE));
506 t1 += System.currentTimeMillis();
508 memf += r.totalMemory() - r.freeMemory();
511 System.out.println("Alignment contains " + al.getHeight()
512 + " sequences and " + al.getWidth() + " columns.");
515 System.out.println(new AppletFormatAdapter().formatSequences(
516 FileFormat.Fasta, al, true));
517 } catch (Exception e)
520 .println("Couln't format the alignment for output as a FASTA file.");
521 e.printStackTrace(System.err);
526 System.out.println("Couldn't read alignment");
528 System.out.println("Read took " + (t1 / 1000.0) + " seconds.");
530 .println("Difference between free memory now and before is "
531 + (memf / (1024.0 * 1024.0) * 1.0) + " MB");
532 } catch (Exception e)
534 System.err.println("Exception when dealing with " + i
535 + "'th argument: " + args[i] + "\n" + e);
540 System.err.println("Ignoring argument '" + args[i] + "' (" + i
541 + "'th)- not a readable file.");
548 * try to discover how to access the given file as a valid datasource that
549 * will be identified as the given type.
553 * @return protocol that yields the data parsable as the given type
555 public static DataSourceType resolveProtocol(String file,
558 return resolveProtocol(file, format, false);
561 public static DataSourceType resolveProtocol(String file,
562 FileFormatI format, boolean debug)
564 // TODO: test thoroughly!
565 DataSourceType protocol = null;
568 System.out.println("resolving datasource started with:\n>>file\n"
569 + file + ">>endfile");
572 // This might throw a security exception in certain browsers
573 // Netscape Communicator for instance.
577 InputStream is = System.getSecurityManager().getClass()
578 .getResourceAsStream("/" + file);
586 System.err.println("Resource '" + file + "' was "
587 + (rtn ? "" : "not") + " located by classloader.");
591 protocol = DataSourceType.CLASSLOADER;
594 } catch (Exception ex)
597 .println("Exception checking resources: " + file + " " + ex);
600 if (file.indexOf("://") > -1)
602 protocol = DataSourceType.URL;
606 // skipping codebase prepend check.
607 protocol = DataSourceType.FILE;
614 System.out.println("Trying to get contents of resource as "
617 fp = new FileParse(file, protocol);
626 System.out.println("Successful.");
629 } catch (Exception e)
633 System.err.println("Exception when accessing content: " + e);
641 System.out.println("Accessing as paste.");
643 protocol = DataSourceType.PASTE;
647 fp = new FileParse(file, protocol);
652 } catch (Exception e)
654 System.err.println("Failed to access content as paste!");
671 FileFormatI idformat = new IdentifyFile().identify(file, protocol);
672 if (idformat == null)
676 System.out.println("Format not identified. Inaccessible file.");
682 System.out.println("Format identified as " + idformat
683 + "and expected as " + format);
685 if (idformat.equals(format))
689 System.out.println("Protocol identified as " + protocol);
698 .println("File deemed not accessible via " + protocol);
703 } catch (Exception e)
707 System.err.println("File deemed not accessible via " + protocol);
715 public AlignmentFileI getAlignFile()