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.PDB.equals(fileFormat)
198 || FileFormat.MMCif.equals(fileFormat))
200 boolean isParseWithJMOL = StructureImportSettings
201 .getDefaultPDBFileParser().equalsIgnoreCase(
202 StructureImportSettings.StructureParser.JMOL_PARSER
206 StructureImportSettings.addSettings(annotFromStructure,
207 localSecondaryStruct, serviceSecondaryStruct);
208 alignFile = new JmolParser(annotFromStructure,
209 localSecondaryStruct, serviceSecondaryStruct, inFile,
214 StructureImportSettings.addSettings(annotFromStructure,
215 localSecondaryStruct, serviceSecondaryStruct);
216 StructureImportSettings.setShowSeqFeatures(true);
217 alignFile = new MCview.PDBfile(annotFromStructure,
218 localSecondaryStruct, serviceSecondaryStruct, inFile,
221 ((StructureFile) alignFile).setDbRefType(FileFormat.PDB
222 .equals(fileFormat) ? Type.PDB : Type.MMCIF);
226 alignFile = fileFormat.getAlignmentFile(inFile, sourceType);
228 // new FastaFile(inFile, sourceType);
229 // new MSFfile(inFile, sourceType);
230 // new PileUpfile(inFile, sourceType);
231 // new ClustalFile(inFile, sourceType);
232 // new BLCFile(inFile, sourceType);
233 // new PIRFile(inFile, sourceType);
234 // new PfamFile(inFile, sourceType);
235 // alignFile = new JPredFile(inFile, sourceType);
236 // ((JPredFile) alignFile).removeNonSequences();
237 // new StockholmFile(inFile, sourceType);
238 // new SimpleBlastFile(inFile, sourceType);
239 // new PhylipFile(inFile, sourceType);
240 // new JSONFile(inFile, sourceType);
241 // new HtmlFile(inFile, sourceType);
242 // new RnamlFile(inFile, sourceType);
243 // alignFile = new FeaturesFile(true, inFile, sourceType);
244 return buildAlignmentFromFile();
245 } catch (Exception e)
248 System.err.println("Failed to read alignment using the '"
249 + fileFormat + "' reader.\n" + e);
251 if (e.getMessage() != null
252 && e.getMessage().startsWith(INVALID_CHARACTERS))
254 throw new IOException(e.getMessage());
257 // Finally test if the user has pasted just the sequence, no id
258 if (sourceType == DataSourceType.PASTE)
262 // Possible sequence is just residues with no label
263 alignFile = new FastaFile(">UNKNOWN\n" + inFile,
264 DataSourceType.PASTE);
265 return buildAlignmentFromFile();
267 } catch (Exception ex)
269 if (ex.toString().startsWith(INVALID_CHARACTERS))
271 throw new IOException(e.getMessage());
274 ex.printStackTrace();
277 if (FileFormat.Html.equals(fileFormat))
279 throw new IOException(e.getMessage());
282 throw new FileFormatException(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
295 public AlignmentI readFromFile(FileParse source, FileFormatI format)
298 this.inFile = source.getInFile();
299 DataSourceType type = source.dataSourceType;
302 if (FileFormat.PDB.equals(format) || FileFormat.MMCif.equals(format))
304 // TODO obtain config value from preference settings
305 boolean isParseWithJMOL = false;
308 StructureImportSettings.addSettings(annotFromStructure,
309 localSecondaryStruct, serviceSecondaryStruct);
310 alignFile = new JmolParser(annotFromStructure,
311 localSecondaryStruct, serviceSecondaryStruct, source);
315 StructureImportSettings.setShowSeqFeatures(true);
316 alignFile = new MCview.PDBfile(annotFromStructure,
317 localSecondaryStruct, serviceSecondaryStruct, source);
319 ((StructureFile) alignFile).setDbRefType(Type.PDB);
323 alignFile = format.getAlignmentFile(source);
326 return buildAlignmentFromFile();
328 } catch (Exception e)
331 System.err.println("Failed to read alignment using the '" + format
332 + "' reader.\n" + e);
334 if (e.getMessage() != null
335 && e.getMessage().startsWith(INVALID_CHARACTERS))
337 throw new FileFormatException(e.getMessage());
340 // Finally test if the user has pasted just the sequence, no id
341 if (type == DataSourceType.PASTE)
345 // Possible sequence is just residues with no label
346 alignFile = new FastaFile(">UNKNOWN\n" + inFile,
347 DataSourceType.PASTE);
348 return buildAlignmentFromFile();
350 } catch (Exception ex)
352 if (ex.toString().startsWith(INVALID_CHARACTERS))
354 throw new IOException(e.getMessage());
357 ex.printStackTrace();
361 // If we get to this stage, the format was not supported
362 throw new FileFormatException(SUPPORTED_FORMATS);
367 * boilerplate method to handle data from an AlignFile and construct a new
368 * alignment or import to an existing alignment
370 * @return AlignmentI instance ready to pass to a UI constructor
372 private AlignmentI buildAlignmentFromFile()
374 // Standard boilerplate for creating alignment from parser
375 // alignFile.configureForView(viewpanel);
377 AlignmentI al = new Alignment(alignFile.getSeqsAsArray());
379 alignFile.addAnnotations(al);
381 alignFile.addGroups(al);
387 * create an alignment flatfile from a Jalview alignment view
392 * @param selectedOnly
393 * @return flatfile in a string
395 public String formatSequences(FileFormatI format, boolean jvsuffix,
396 AlignmentViewPanel ap, boolean selectedOnly)
399 AlignmentView selvew = ap.getAlignViewport().getAlignmentView(
400 selectedOnly, false);
401 AlignmentI aselview = selvew.getVisibleAlignment(ap.getAlignViewport()
403 List<AlignmentAnnotation> ala = (ap.getAlignViewport()
404 .getVisibleAlignmentAnnotation(selectedOnly));
407 for (AlignmentAnnotation aa : ala)
409 aselview.addAnnotation(aa);
413 return formatSequences(format, aselview, jvsuffix);
417 * Construct an output class for an alignment in a particular filetype TODO:
418 * allow caller to detect errors and warnings encountered when generating
422 * string name of alignment format
424 * the alignment to be written out
426 * passed to AlnFile class controls whether /START-END is added to
429 * @return alignment flat file contents
431 public String formatSequences(FileFormatI format, AlignmentI alignment,
436 AlignmentFileI afile = format.getAlignmentFile(alignment);
438 afile.setNewlineString(newline);
439 afile.setExportSettings(exportSettings);
440 afile.configureForView(viewpanel);
442 // check whether we were given a specific alignment to export, rather than
443 // the one in the viewpanel
444 SequenceI[] seqs = null;
445 if (viewpanel == null || viewpanel.getAlignment() == null
446 || viewpanel.getAlignment() != alignment)
448 seqs = alignment.getSequencesArray();
452 seqs = viewpanel.getAlignment().getSequencesArray();
455 String afileresp = afile.print(seqs, jvsuffix);
456 if (afile.hasWarningMessage())
458 System.err.println("Warning raised when writing as " + format
459 + " : " + afile.getWarningMessage());
462 } catch (Exception e)
464 System.err.println("Failed to write alignment as a '" + format
472 public static DataSourceType checkProtocol(String file)
474 DataSourceType protocol = DataSourceType.FILE;
475 String ft = file.toLowerCase().trim();
476 if (ft.indexOf("http:") == 0 || ft.indexOf("https:") == 0
477 || ft.indexOf("file:") == 0)
479 protocol = DataSourceType.URL;
484 public static void main(String[] args)
487 while (i < args.length)
489 File f = new File(args[i]);
494 System.out.println("Reading file: " + f);
495 AppletFormatAdapter afa = new AppletFormatAdapter();
496 Runtime r = Runtime.getRuntime();
498 long memf = -r.totalMemory() + r.freeMemory();
499 long t1 = -System.currentTimeMillis();
501 .readFile(args[i], DataSourceType.FILE,
502 new IdentifyFile().identify(args[i],
503 DataSourceType.FILE));
504 t1 += System.currentTimeMillis();
506 memf += r.totalMemory() - r.freeMemory();
509 System.out.println("Alignment contains " + al.getHeight()
510 + " sequences and " + al.getWidth() + " columns.");
513 System.out.println(new AppletFormatAdapter().formatSequences(
514 FileFormat.Fasta, al, true));
515 } catch (Exception e)
518 .println("Couln't format the alignment for output as a FASTA file.");
519 e.printStackTrace(System.err);
524 System.out.println("Couldn't read alignment");
526 System.out.println("Read took " + (t1 / 1000.0) + " seconds.");
528 .println("Difference between free memory now and before is "
529 + (memf / (1024.0 * 1024.0) * 1.0) + " MB");
530 } catch (Exception e)
532 System.err.println("Exception when dealing with " + i
533 + "'th argument: " + args[i] + "\n" + e);
538 System.err.println("Ignoring argument '" + args[i] + "' (" + i
539 + "'th)- not a readable file.");
546 * try to discover how to access the given file as a valid datasource that
547 * will be identified as the given type.
551 * @return protocol that yields the data parsable as the given type
553 public static DataSourceType resolveProtocol(String file,
556 return resolveProtocol(file, format, false);
559 public static DataSourceType resolveProtocol(String file,
560 FileFormatI format, boolean debug)
562 // TODO: test thoroughly!
563 DataSourceType protocol = null;
566 System.out.println("resolving datasource started with:\n>>file\n"
567 + file + ">>endfile");
570 // This might throw a security exception in certain browsers
571 // Netscape Communicator for instance.
575 InputStream is = System.getSecurityManager().getClass()
576 .getResourceAsStream("/" + file);
584 System.err.println("Resource '" + file + "' was "
585 + (rtn ? "" : "not") + " located by classloader.");
589 protocol = DataSourceType.CLASSLOADER;
592 } catch (Exception ex)
595 .println("Exception checking resources: " + file + " " + ex);
598 if (file.indexOf("://") > -1)
600 protocol = DataSourceType.URL;
604 // skipping codebase prepend check.
605 protocol = DataSourceType.FILE;
612 System.out.println("Trying to get contents of resource as "
615 fp = new FileParse(file, protocol);
624 System.out.println("Successful.");
627 } catch (Exception e)
631 System.err.println("Exception when accessing content: " + e);
639 System.out.println("Accessing as paste.");
641 protocol = DataSourceType.PASTE;
645 fp = new FileParse(file, protocol);
650 } catch (Exception e)
652 System.err.println("Failed to access content as paste!");
669 FileFormatI idformat = new IdentifyFile().identify(file, protocol);
670 if (idformat == null)
674 System.out.println("Format not identified. Inaccessible file.");
680 System.out.println("Format identified as " + idformat
681 + "and expected as " + format);
683 if (idformat.equals(format))
687 System.out.println("Protocol identified as " + protocol);
696 .println("File deemed not accessible via " + protocol);
701 } catch (Exception e)
705 System.err.println("File deemed not accessible via " + protocol);
713 public AlignmentFileI getAlignFile()