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.structure.StructureImportSettings;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.util.List;
37 * A low level class for alignment and feature IO with alignment formatting
38 * methods used by both applet and application for generating flat alignment
39 * files. It also holds the lists of magic format names that the applet and
40 * application will allow the user to read or write files with.
45 public class AppletFormatAdapter
47 private AlignmentViewPanel viewpanel;
50 * add jalview-derived non-secondary structure annotation from PDB structure
52 boolean annotFromStructure = false;
55 * add secondary structure from PDB data with built-in algorithms
57 boolean localSecondaryStruct = false;
60 * process PDB data with web services
62 boolean serviceSecondaryStruct = false;
64 private AlignmentFileI alignFile = null;
69 * character used to write newlines
71 protected String newline = System.getProperty("line.separator");
73 private AlignExportSettingI exportSettings;
76 * List of valid format strings used in the isValidFormat method
78 public static final String[] READABLE_FORMATS = new String[] { "BLC",
79 "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "STH", "PDB",
80 "JnetFile", "RNAML", "PHYLIP", "JSON",
81 IdentifyFile.FeaturesFile, "HTML", "mmCIF" };
84 * List of readable format file extensions by application in order
85 * corresponding to READABLE_FNAMES
87 public static final String[] READABLE_EXTENSIONS = new String[] {
88 "fa, fasta, mfa, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa",
89 "sto,stk", "xml,rnaml", "phy", "json",
90 ".gff2,gff3", "jar,jvp", HtmlFile.FILE_EXT, "cif" };
93 * List of readable formats by application in order corresponding to
96 public static final String[] READABLE_FNAMES = new String[] { "Fasta",
97 "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Stockholm", "RNAML",
98 "PHYLIP", "JSON", IdentifyFile.FeaturesFile,
99 "Jalview", HtmlFile.FILE_DESC, "mmCIF" };
102 * List of valid format strings for use by callers of the formatSequences
105 public static final String[] WRITEABLE_FORMATS = new String[] { "BLC",
106 "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "AMSA", "STH",
110 * List of extensions corresponding to file format types in WRITABLE_FNAMES
111 * that are writable by the application.
113 public static final String[] WRITABLE_EXTENSIONS = new String[] {
114 "fa, fasta, mfa, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa",
115 "sto,stk", "phy", "json", "jvp" };
118 * List of writable formats by the application. Order must correspond with the
119 * WRITABLE_EXTENSIONS list of formats.
121 public static final String[] WRITABLE_FNAMES = new String[] { "Fasta",
122 "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "STH",
123 "PHYLIP", "JSON", "Jalview" };
125 public static String INVALID_CHARACTERS = "Contains invalid characters";
127 // TODO: make these messages dynamic
128 public static String SUPPORTED_FORMATS = "Formats currently supported are\n"
129 + prettyPrint(READABLE_FORMATS);
131 public AppletFormatAdapter()
135 public AppletFormatAdapter(AlignmentViewPanel viewpanel)
137 this.viewpanel = viewpanel;
140 public AppletFormatAdapter(AlignmentViewPanel alignPanel,
141 AlignExportSettingI settings)
143 viewpanel = alignPanel;
144 exportSettings = settings;
150 * @return grammatically correct(ish) list consisting of els elements.
152 public static String prettyPrint(String[] els)
154 StringBuffer list = new StringBuffer();
155 for (int i = 0, iSize = els.length - 1; i < iSize; i++)
160 list.append(" and " + els[els.length - 1] + ".");
161 return list.toString();
164 public void setNewlineString(String nl)
169 public String getNewlineString()
175 * check that this format is valid for reading
178 * a format string to be compared with READABLE_FORMATS
179 * @return true if format is readable
181 public static final boolean isValidFormat(String format)
183 return isValidFormat(format, false);
187 * validate format is valid for IO
190 * a format string to be compared with either READABLE_FORMATS or
193 * when true, format is checked for containment in WRITEABLE_FORMATS
194 * @return true if format is valid
196 public static final boolean isValidFormat(String format,
203 boolean valid = false;
204 String[] format_list = (forwriting) ? WRITEABLE_FORMATS
206 for (String element : format_list)
208 if (element.equalsIgnoreCase(format))
218 * Constructs the correct filetype parser for a characterised datasource
228 public AlignmentI readFile(String file, DataSourceType sourceType,
229 FileFormatI fileFormat) throws IOException
234 if (fileFormat == FileFormat.PDB || fileFormat == FileFormat.MMCif)
236 StructureImportSettings.addSettings(annotFromStructure,
237 localSecondaryStruct, serviceSecondaryStruct);
238 alignFile = fileFormat.getAlignmentFile(inFile, sourceType);
242 alignFile = fileFormat.getAlignmentFile(inFile, sourceType);
244 // new FastaFile(inFile, sourceType);
245 // new MSFfile(inFile, sourceType);
246 // new PileUpfile(inFile, sourceType);
247 // new ClustalFile(inFile, sourceType);
248 // new BLCFile(inFile, sourceType);
249 // new PIRFile(inFile, sourceType);
250 // new PfamFile(inFile, sourceType);
251 // alignFile = new JPredFile(inFile, sourceType);
252 // ((JPredFile) alignFile).removeNonSequences();
253 // new StockholmFile(inFile, sourceType);
254 // new SimpleBlastFile(inFile, sourceType);
255 // new PhylipFile(inFile, sourceType);
256 // new JSONFile(inFile, sourceType);
257 // new HtmlFile(inFile, sourceType);
258 // new RnamlFile(inFile, sourceType);
259 // alignFile = new FeaturesFile(true, inFile, sourceType);
260 return buildAlignmentFromFile();
261 } catch (Exception e)
264 System.err.println("Failed to read alignment using the '"
266 + "' reader.\n" + e);
268 if (e.getMessage() != null
269 && e.getMessage().startsWith(INVALID_CHARACTERS))
271 throw new IOException(e.getMessage());
274 // Finally test if the user has pasted just the sequence, no id
275 if (sourceType == DataSourceType.PASTE)
279 // Possible sequence is just residues with no label
280 alignFile = new FastaFile(">UNKNOWN\n" + inFile,
281 DataSourceType.PASTE);
282 return buildAlignmentFromFile();
284 } catch (Exception ex)
286 if (ex.toString().startsWith(INVALID_CHARACTERS))
288 throw new IOException(e.getMessage());
291 ex.printStackTrace();
294 if (fileFormat == FileFormat.Html)
296 throw new IOException(e.getMessage());
299 throw new FileFormatException(SUPPORTED_FORMATS);
303 * Constructs the correct filetype parser for an already open datasource
306 * an existing datasource
308 * File format of data that will be provided by datasource
312 public AlignmentI readFromFile(FileParse source, FileFormatI format)
315 this.inFile = source.getInFile();
316 DataSourceType type = source.dataSourceType;
319 if (format == FileFormat.PDB || format == FileFormat.MMCif)
321 StructureImportSettings.addSettings(annotFromStructure,
322 localSecondaryStruct, serviceSecondaryStruct);
323 alignFile = format.getAlignmentFile(source);
327 alignFile = format.getAlignmentFile(source);
330 return buildAlignmentFromFile();
332 } catch (Exception e)
335 System.err.println("Failed to read alignment using the '" + format
336 + "' reader.\n" + e);
338 if (e.getMessage() != null
339 && e.getMessage().startsWith(INVALID_CHARACTERS))
341 throw new FileFormatException(e.getMessage());
344 // Finally test if the user has pasted just the sequence, no id
345 if (type == DataSourceType.PASTE)
349 // Possible sequence is just residues with no label
350 alignFile = new FastaFile(">UNKNOWN\n" + inFile,
351 DataSourceType.PASTE);
352 return buildAlignmentFromFile();
354 } catch (Exception ex)
356 if (ex.toString().startsWith(INVALID_CHARACTERS))
358 throw new IOException(e.getMessage());
361 ex.printStackTrace();
365 // If we get to this stage, the format was not supported
366 throw new FileFormatException(SUPPORTED_FORMATS);
371 * boilerplate method to handle data from an AlignFile and construct a new
372 * alignment or import to an existing alignment
374 * @return AlignmentI instance ready to pass to a UI constructor
376 private AlignmentI buildAlignmentFromFile()
378 // Standard boilerplate for creating alignment from parser
379 // alignFile.configureForView(viewpanel);
381 AlignmentI al = new Alignment(alignFile.getSeqsAsArray());
383 alignFile.addAnnotations(al);
385 alignFile.addGroups(al);
391 * create an alignment flatfile from a Jalview alignment view
396 * @param selectedOnly
397 * @return flatfile in a string
399 public String formatSequences(FileFormatI format, boolean jvsuffix,
400 AlignmentViewPanel ap, boolean selectedOnly)
403 AlignmentView selvew = ap.getAlignViewport().getAlignmentView(
404 selectedOnly, false);
405 AlignmentI aselview = selvew.getVisibleAlignment(ap.getAlignViewport()
407 List<AlignmentAnnotation> ala = (ap.getAlignViewport()
408 .getVisibleAlignmentAnnotation(selectedOnly));
411 for (AlignmentAnnotation aa : ala)
413 aselview.addAnnotation(aa);
417 return formatSequences(format, aselview, jvsuffix);
421 * Construct an output class for an alignment in a particular filetype TODO:
422 * allow caller to detect errors and warnings encountered when generating
426 * string name of alignment format
428 * the alignment to be written out
430 * passed to AlnFile class controls whether /START-END is added to
433 * @return alignment flat file contents
435 public String formatSequences(FileFormatI format, AlignmentI alignment,
440 AlignmentFileI afile = format.getAlignmentFile();
442 afile.setNewlineString(newline);
443 afile.addJVSuffix(jvsuffix);
444 afile.setExportSettings(exportSettings);
445 afile.configureForView(viewpanel);
447 // check whether we were given a specific alignment to export, rather than
448 // the one in the viewpanel
449 if (viewpanel == null || viewpanel.getAlignment() == null
450 || viewpanel.getAlignment() != alignment)
452 afile.setSeqs(alignment.getSequencesArray());
456 afile.setSeqs(viewpanel.getAlignment().getSequencesArray());
459 String afileresp = afile.print();
460 if (afile.hasWarningMessage())
462 System.err.println("Warning raised when writing as " + format
463 + " : " + afile.getWarningMessage());
466 } catch (Exception e)
468 System.err.println("Failed to write alignment as a '" + format
476 public static DataSourceType checkProtocol(String file)
478 DataSourceType protocol = DataSourceType.FILE;
479 String ft = file.toLowerCase().trim();
480 if (ft.indexOf("http:") == 0 || ft.indexOf("https:") == 0
481 || ft.indexOf("file:") == 0)
483 protocol = DataSourceType.URL;
488 public static void main(String[] args)
491 while (i < args.length)
493 File f = new File(args[i]);
498 System.out.println("Reading file: " + f);
499 AppletFormatAdapter afa = new AppletFormatAdapter();
500 Runtime r = Runtime.getRuntime();
502 long memf = -r.totalMemory() + r.freeMemory();
503 long t1 = -System.currentTimeMillis();
505 .readFile(args[i], DataSourceType.FILE,
506 new IdentifyFile().identify(args[i],
507 DataSourceType.FILE));
508 t1 += System.currentTimeMillis();
510 memf += r.totalMemory() - r.freeMemory();
513 System.out.println("Alignment contains " + al.getHeight()
514 + " sequences and " + al.getWidth() + " columns.");
517 System.out.println(new AppletFormatAdapter().formatSequences(
518 FileFormat.Fasta, al, true));
519 } catch (Exception e)
522 .println("Couln't format the alignment for output as a FASTA file.");
523 e.printStackTrace(System.err);
528 System.out.println("Couldn't read alignment");
530 System.out.println("Read took " + (t1 / 1000.0) + " seconds.");
532 .println("Difference between free memory now and before is "
533 + (memf / (1024.0 * 1024.0) * 1.0) + " MB");
534 } catch (Exception e)
536 System.err.println("Exception when dealing with " + i
537 + "'th argument: " + args[i] + "\n" + e);
542 System.err.println("Ignoring argument '" + args[i] + "' (" + i
543 + "'th)- not a readable file.");
550 * try to discover how to access the given file as a valid datasource that
551 * will be identified as the given type.
555 * @return protocol that yields the data parsable as the given type
557 public static DataSourceType resolveProtocol(String file,
560 return resolveProtocol(file, format, false);
563 public static DataSourceType resolveProtocol(String file,
564 FileFormatI format, boolean debug)
566 // TODO: test thoroughly!
567 DataSourceType protocol = null;
570 System.out.println("resolving datasource started with:\n>>file\n"
571 + file + ">>endfile");
574 // This might throw a security exception in certain browsers
575 // Netscape Communicator for instance.
579 InputStream is = System.getSecurityManager().getClass()
580 .getResourceAsStream("/" + file);
588 System.err.println("Resource '" + file + "' was "
589 + (rtn ? "" : "not") + " located by classloader.");
593 protocol = DataSourceType.CLASSLOADER;
596 } catch (Exception ex)
599 .println("Exception checking resources: " + file + " " + ex);
602 if (file.indexOf("://") > -1)
604 protocol = DataSourceType.URL;
608 // skipping codebase prepend check.
609 protocol = DataSourceType.FILE;
616 System.out.println("Trying to get contents of resource as "
619 fp = new FileParse(file, protocol);
628 System.out.println("Successful.");
631 } catch (Exception e)
635 System.err.println("Exception when accessing content: " + e);
643 System.out.println("Accessing as paste.");
645 protocol = DataSourceType.PASTE;
649 fp = new FileParse(file, protocol);
654 } catch (Exception e)
656 System.err.println("Failed to access content as paste!");
673 FileFormatI idformat = new IdentifyFile().identify(file,
675 if (idformat == null)
679 System.out.println("Format not identified. Inaccessible file.");
685 System.out.println("Format identified as " + idformat
686 + "and expected as " + format);
688 if (idformat.equals(format))
692 System.out.println("Protocol identified as " + protocol);
701 .println("File deemed not accessible via " + protocol);
706 } catch (Exception e)
710 System.err.println("File deemed not accessible via " + protocol);
718 public AlignmentFileI getAlignFile()