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.SequenceI;
30 import jalview.structure.StructureImportSettings;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.util.List;
38 * A low level class for alignment and feature IO with alignment formatting
39 * methods used by both applet and application for generating flat alignment
40 * files. It also holds the lists of magic format names that the applet and
41 * application will allow the user to read or write files with.
46 public class AppletFormatAdapter
48 private AlignmentViewPanel viewpanel;
51 * add jalview-derived non-secondary structure annotation from PDB structure
53 boolean annotFromStructure = false;
56 * add secondary structure from PDB data with built-in algorithms
58 boolean localSecondaryStruct = false;
61 * process PDB data with web services
63 boolean serviceSecondaryStruct = false;
65 private AlignmentFileI alignFile = null;
70 * character used to write newlines
72 protected String newline = System.getProperty("line.separator");
74 private AlignExportSettingI exportSettings;
77 * List of valid format strings used in the isValidFormat method
79 public static final String[] READABLE_FORMATS = new String[] { "BLC",
80 "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "STH", "PDB",
81 "JnetFile", "RNAML", "PHYLIP", "JSON",
82 IdentifyFile.FeaturesFile, "HTML", "mmCIF" };
85 * List of readable format file extensions by application in order
86 * corresponding to READABLE_FNAMES
88 public static final String[] READABLE_EXTENSIONS = new String[] {
89 "fa, fasta, mfa, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa",
90 "sto,stk", "xml,rnaml", "phy", "json",
91 ".gff2,gff3", "jar,jvp", "html", "cif" };
94 * List of readable formats by application in order corresponding to
97 public static final String[] READABLE_FNAMES = new String[] { "Fasta",
98 "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "Stockholm", "RNAML",
99 "PHYLIP", "JSON", IdentifyFile.FeaturesFile,
100 "Jalview", HtmlFile.FILE_DESC, "mmCIF" };
103 * List of valid format strings for use by callers of the formatSequences
106 public static final String[] WRITEABLE_FORMATS = new String[] { "BLC",
107 "CLUSTAL", "FASTA", "MSF", "PileUp", "PIR", "PFAM", "AMSA", "STH",
111 * List of extensions corresponding to file format types in WRITABLE_FNAMES
112 * that are writable by the application.
114 public static final String[] WRITABLE_EXTENSIONS = new String[] {
115 "fa, fasta, mfa, fastq", "aln", "pfam", "msf", "pir", "blc", "amsa",
116 "sto,stk", "phy", "json", "jvp" };
119 * List of writable formats by the application. Order must correspond with the
120 * WRITABLE_EXTENSIONS list of formats.
122 public static final String[] WRITABLE_FNAMES = new String[] { "Fasta",
123 "Clustal", "PFAM", "MSF", "PIR", "BLC", "AMSA", "STH",
124 "PHYLIP", "JSON", "Jalview" };
126 public static String INVALID_CHARACTERS = "Contains invalid characters";
128 // TODO: make these messages dynamic
129 public static String SUPPORTED_FORMATS = "Formats currently supported are\n"
130 + prettyPrint(READABLE_FORMATS);
132 public AppletFormatAdapter()
136 public AppletFormatAdapter(AlignmentViewPanel viewpanel)
138 this.viewpanel = viewpanel;
141 public AppletFormatAdapter(AlignmentViewPanel alignPanel,
142 AlignExportSettingI settings)
144 viewpanel = alignPanel;
145 exportSettings = settings;
151 * @return grammatically correct(ish) list consisting of els elements.
153 public static String prettyPrint(String[] els)
155 StringBuffer list = new StringBuffer();
156 for (int i = 0, iSize = els.length - 1; i < iSize; i++)
161 list.append(" and " + els[els.length - 1] + ".");
162 return list.toString();
165 public void setNewlineString(String nl)
170 public String getNewlineString()
176 * Constructs the correct filetype parser for a characterised datasource
186 public AlignmentI readFile(String file, DataSourceType sourceType,
187 FileFormatI fileFormat) throws IOException
192 if (fileFormat == FileFormat.PDB || fileFormat == FileFormat.MMCif)
194 StructureImportSettings.addSettings(annotFromStructure,
195 localSecondaryStruct, serviceSecondaryStruct);
196 alignFile = fileFormat.getAlignmentFile(inFile, sourceType);
200 alignFile = fileFormat.getAlignmentFile(inFile, sourceType);
202 // new FastaFile(inFile, sourceType);
203 // new MSFfile(inFile, sourceType);
204 // new PileUpfile(inFile, sourceType);
205 // new ClustalFile(inFile, sourceType);
206 // new BLCFile(inFile, sourceType);
207 // new PIRFile(inFile, sourceType);
208 // new PfamFile(inFile, sourceType);
209 // alignFile = new JPredFile(inFile, sourceType);
210 // ((JPredFile) alignFile).removeNonSequences();
211 // new StockholmFile(inFile, sourceType);
212 // new SimpleBlastFile(inFile, sourceType);
213 // new PhylipFile(inFile, sourceType);
214 // new JSONFile(inFile, sourceType);
215 // new HtmlFile(inFile, sourceType);
216 // new RnamlFile(inFile, sourceType);
217 // alignFile = new FeaturesFile(true, inFile, sourceType);
218 return buildAlignmentFromFile();
219 } catch (Exception e)
222 System.err.println("Failed to read alignment using the '"
224 + "' reader.\n" + e);
226 if (e.getMessage() != null
227 && e.getMessage().startsWith(INVALID_CHARACTERS))
229 throw new IOException(e.getMessage());
232 // Finally test if the user has pasted just the sequence, no id
233 if (sourceType == DataSourceType.PASTE)
237 // Possible sequence is just residues with no label
238 alignFile = new FastaFile(">UNKNOWN\n" + inFile,
239 DataSourceType.PASTE);
240 return buildAlignmentFromFile();
242 } catch (Exception ex)
244 if (ex.toString().startsWith(INVALID_CHARACTERS))
246 throw new IOException(e.getMessage());
249 ex.printStackTrace();
252 if (fileFormat == FileFormat.Html)
254 throw new IOException(e.getMessage());
257 throw new FileFormatException(SUPPORTED_FORMATS);
261 * Constructs the correct filetype parser for an already open datasource
264 * an existing datasource
266 * File format of data that will be provided by datasource
270 public AlignmentI readFromFile(FileParse source, FileFormatI format)
273 this.inFile = source.getInFile();
274 DataSourceType type = source.dataSourceType;
277 if (format == FileFormat.PDB || format == FileFormat.MMCif)
279 StructureImportSettings.addSettings(annotFromStructure,
280 localSecondaryStruct, serviceSecondaryStruct);
281 alignFile = format.getAlignmentFile(source);
285 alignFile = format.getAlignmentFile(source);
288 return buildAlignmentFromFile();
290 } catch (Exception e)
293 System.err.println("Failed to read alignment using the '" + format
294 + "' reader.\n" + e);
296 if (e.getMessage() != null
297 && e.getMessage().startsWith(INVALID_CHARACTERS))
299 throw new FileFormatException(e.getMessage());
302 // Finally test if the user has pasted just the sequence, no id
303 if (type == DataSourceType.PASTE)
307 // Possible sequence is just residues with no label
308 alignFile = new FastaFile(">UNKNOWN\n" + inFile,
309 DataSourceType.PASTE);
310 return buildAlignmentFromFile();
312 } catch (Exception ex)
314 if (ex.toString().startsWith(INVALID_CHARACTERS))
316 throw new IOException(e.getMessage());
319 ex.printStackTrace();
323 // If we get to this stage, the format was not supported
324 throw new FileFormatException(SUPPORTED_FORMATS);
329 * boilerplate method to handle data from an AlignFile and construct a new
330 * alignment or import to an existing alignment
332 * @return AlignmentI instance ready to pass to a UI constructor
334 private AlignmentI buildAlignmentFromFile()
336 // Standard boilerplate for creating alignment from parser
337 // alignFile.configureForView(viewpanel);
339 AlignmentI al = new Alignment(alignFile.getSeqsAsArray());
341 alignFile.addAnnotations(al);
343 alignFile.addGroups(al);
349 * create an alignment flatfile from a Jalview alignment view
354 * @param selectedOnly
355 * @return flatfile in a string
357 public String formatSequences(FileFormatI format, boolean jvsuffix,
358 AlignmentViewPanel ap, boolean selectedOnly)
361 AlignmentView selvew = ap.getAlignViewport().getAlignmentView(
362 selectedOnly, false);
363 AlignmentI aselview = selvew.getVisibleAlignment(ap.getAlignViewport()
365 List<AlignmentAnnotation> ala = (ap.getAlignViewport()
366 .getVisibleAlignmentAnnotation(selectedOnly));
369 for (AlignmentAnnotation aa : ala)
371 aselview.addAnnotation(aa);
375 return formatSequences(format, aselview, jvsuffix);
379 * Construct an output class for an alignment in a particular filetype TODO:
380 * allow caller to detect errors and warnings encountered when generating
384 * string name of alignment format
386 * the alignment to be written out
388 * passed to AlnFile class controls whether /START-END is added to
391 * @return alignment flat file contents
393 public String formatSequences(FileFormatI format, AlignmentI alignment,
398 AlignmentFileI afile = format.getAlignmentFile();
400 afile.setNewlineString(newline);
401 afile.setExportSettings(exportSettings);
402 afile.configureForView(viewpanel);
404 // check whether we were given a specific alignment to export, rather than
405 // the one in the viewpanel
406 SequenceI[] seqs = null;
407 if (viewpanel == null || viewpanel.getAlignment() == null
408 || viewpanel.getAlignment() != alignment)
410 seqs = alignment.getSequencesArray();
414 seqs = viewpanel.getAlignment().getSequencesArray();
417 String afileresp = afile.print(seqs, jvsuffix);
418 if (afile.hasWarningMessage())
420 System.err.println("Warning raised when writing as " + format
421 + " : " + afile.getWarningMessage());
424 } catch (Exception e)
426 System.err.println("Failed to write alignment as a '" + format
434 public static DataSourceType checkProtocol(String file)
436 DataSourceType protocol = DataSourceType.FILE;
437 String ft = file.toLowerCase().trim();
438 if (ft.indexOf("http:") == 0 || ft.indexOf("https:") == 0
439 || ft.indexOf("file:") == 0)
441 protocol = DataSourceType.URL;
446 public static void main(String[] args)
449 while (i < args.length)
451 File f = new File(args[i]);
456 System.out.println("Reading file: " + f);
457 AppletFormatAdapter afa = new AppletFormatAdapter();
458 Runtime r = Runtime.getRuntime();
460 long memf = -r.totalMemory() + r.freeMemory();
461 long t1 = -System.currentTimeMillis();
463 .readFile(args[i], DataSourceType.FILE,
464 new IdentifyFile().identify(args[i],
465 DataSourceType.FILE));
466 t1 += System.currentTimeMillis();
468 memf += r.totalMemory() - r.freeMemory();
471 System.out.println("Alignment contains " + al.getHeight()
472 + " sequences and " + al.getWidth() + " columns.");
475 System.out.println(new AppletFormatAdapter().formatSequences(
476 FileFormat.Fasta, al, true));
477 } catch (Exception e)
480 .println("Couln't format the alignment for output as a FASTA file.");
481 e.printStackTrace(System.err);
486 System.out.println("Couldn't read alignment");
488 System.out.println("Read took " + (t1 / 1000.0) + " seconds.");
490 .println("Difference between free memory now and before is "
491 + (memf / (1024.0 * 1024.0) * 1.0) + " MB");
492 } catch (Exception e)
494 System.err.println("Exception when dealing with " + i
495 + "'th argument: " + args[i] + "\n" + e);
500 System.err.println("Ignoring argument '" + args[i] + "' (" + i
501 + "'th)- not a readable file.");
508 * try to discover how to access the given file as a valid datasource that
509 * will be identified as the given type.
513 * @return protocol that yields the data parsable as the given type
515 public static DataSourceType resolveProtocol(String file,
518 return resolveProtocol(file, format, false);
521 public static DataSourceType resolveProtocol(String file,
522 FileFormatI format, boolean debug)
524 // TODO: test thoroughly!
525 DataSourceType protocol = null;
528 System.out.println("resolving datasource started with:\n>>file\n"
529 + file + ">>endfile");
532 // This might throw a security exception in certain browsers
533 // Netscape Communicator for instance.
537 InputStream is = System.getSecurityManager().getClass()
538 .getResourceAsStream("/" + file);
546 System.err.println("Resource '" + file + "' was "
547 + (rtn ? "" : "not") + " located by classloader.");
551 protocol = DataSourceType.CLASSLOADER;
554 } catch (Exception ex)
557 .println("Exception checking resources: " + file + " " + ex);
560 if (file.indexOf("://") > -1)
562 protocol = DataSourceType.URL;
566 // skipping codebase prepend check.
567 protocol = DataSourceType.FILE;
574 System.out.println("Trying to get contents of resource as "
577 fp = new FileParse(file, protocol);
586 System.out.println("Successful.");
589 } catch (Exception e)
593 System.err.println("Exception when accessing content: " + e);
601 System.out.println("Accessing as paste.");
603 protocol = DataSourceType.PASTE;
607 fp = new FileParse(file, protocol);
612 } catch (Exception e)
614 System.err.println("Failed to access content as paste!");
631 FileFormatI idformat = new IdentifyFile().identify(file,
633 if (idformat == null)
637 System.out.println("Format not identified. Inaccessible file.");
643 System.out.println("Format identified as " + idformat
644 + "and expected as " + format);
646 if (idformat.equals(format))
650 System.out.println("Protocol identified as " + protocol);
659 .println("File deemed not accessible via " + protocol);
664 } catch (Exception e)
668 System.err.println("File deemed not accessible via " + protocol);
676 public AlignmentFileI getAlignFile()