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(inFile, sourceType);
212 StructureImportSettings.addSettings(annotFromStructure,
213 localSecondaryStruct, serviceSecondaryStruct);
214 StructureImportSettings.setShowSeqFeatures(true);
215 alignFile = new MCview.PDBfile(annotFromStructure,
216 localSecondaryStruct, serviceSecondaryStruct, inFile,
219 ((StructureFile) alignFile).setDbRefType(FileFormat.PDB
220 .equals(fileFormat) ? Type.PDB : Type.MMCIF);
224 alignFile = fileFormat.getAlignmentFile(inFile, sourceType);
226 // new FastaFile(inFile, sourceType);
227 // new MSFfile(inFile, sourceType);
228 // new PileUpfile(inFile, sourceType);
229 // new ClustalFile(inFile, sourceType);
230 // new BLCFile(inFile, sourceType);
231 // new PIRFile(inFile, sourceType);
232 // new PfamFile(inFile, sourceType);
233 // alignFile = new JPredFile(inFile, sourceType);
234 // ((JPredFile) alignFile).removeNonSequences();
235 // new StockholmFile(inFile, sourceType);
236 // new SimpleBlastFile(inFile, sourceType);
237 // new PhylipFile(inFile, sourceType);
238 // new JSONFile(inFile, sourceType);
239 // new HtmlFile(inFile, sourceType);
240 // new RnamlFile(inFile, sourceType);
241 // alignFile = new FeaturesFile(true, inFile, sourceType);
242 return buildAlignmentFromFile();
243 } catch (Exception e)
246 System.err.println("Failed to read alignment using the '"
247 + fileFormat + "' reader.\n" + e);
249 if (e.getMessage() != null
250 && e.getMessage().startsWith(INVALID_CHARACTERS))
252 throw new IOException(e.getMessage());
255 // Finally test if the user has pasted just the sequence, no id
256 if (sourceType == DataSourceType.PASTE)
260 // Possible sequence is just residues with no label
261 alignFile = new FastaFile(">UNKNOWN\n" + inFile,
262 DataSourceType.PASTE);
263 return buildAlignmentFromFile();
265 } catch (Exception ex)
267 if (ex.toString().startsWith(INVALID_CHARACTERS))
269 throw new IOException(e.getMessage());
272 ex.printStackTrace();
275 if (FileFormat.Html.equals(fileFormat))
277 throw new IOException(e.getMessage());
280 throw new FileFormatException(SUPPORTED_FORMATS);
284 * Constructs the correct filetype parser for an already open datasource
287 * an existing datasource
289 * File format of data that will be provided by datasource
293 public AlignmentI readFromFile(FileParse source, FileFormatI format)
296 this.inFile = source.getInFile();
297 DataSourceType type = source.dataSourceType;
300 if (FileFormat.PDB.equals(format) || FileFormat.MMCif.equals(format))
302 // TODO obtain config value from preference settings
303 boolean isParseWithJMOL = false;
306 StructureImportSettings.addSettings(annotFromStructure,
307 localSecondaryStruct, serviceSecondaryStruct);
308 alignFile = new JmolParser(source);
312 StructureImportSettings.setShowSeqFeatures(true);
313 alignFile = new MCview.PDBfile(annotFromStructure,
314 localSecondaryStruct, serviceSecondaryStruct, source);
316 ((StructureFile) alignFile).setDbRefType(Type.PDB);
320 alignFile = format.getAlignmentFile(source);
323 return buildAlignmentFromFile();
325 } catch (Exception e)
328 System.err.println("Failed to read alignment using the '" + format
329 + "' reader.\n" + e);
331 if (e.getMessage() != null
332 && e.getMessage().startsWith(INVALID_CHARACTERS))
334 throw new FileFormatException(e.getMessage());
337 // Finally test if the user has pasted just the sequence, no id
338 if (type == DataSourceType.PASTE)
342 // Possible sequence is just residues with no label
343 alignFile = new FastaFile(">UNKNOWN\n" + inFile,
344 DataSourceType.PASTE);
345 return buildAlignmentFromFile();
347 } catch (Exception ex)
349 if (ex.toString().startsWith(INVALID_CHARACTERS))
351 throw new IOException(e.getMessage());
354 ex.printStackTrace();
358 // If we get to this stage, the format was not supported
359 throw new FileFormatException(SUPPORTED_FORMATS);
364 * boilerplate method to handle data from an AlignFile and construct a new
365 * alignment or import to an existing alignment
367 * @return AlignmentI instance ready to pass to a UI constructor
369 private AlignmentI buildAlignmentFromFile()
371 // Standard boilerplate for creating alignment from parser
372 // alignFile.configureForView(viewpanel);
374 AlignmentI al = new Alignment(alignFile.getSeqsAsArray());
376 alignFile.addAnnotations(al);
378 alignFile.addGroups(al);
384 * create an alignment flatfile from a Jalview alignment view
389 * @param selectedOnly
390 * @return flatfile in a string
392 public String formatSequences(FileFormatI format, boolean jvsuffix,
393 AlignmentViewPanel ap, boolean selectedOnly)
396 AlignmentView selvew = ap.getAlignViewport().getAlignmentView(
397 selectedOnly, false);
398 AlignmentI aselview = selvew.getVisibleAlignment(ap.getAlignViewport()
400 List<AlignmentAnnotation> ala = (ap.getAlignViewport()
401 .getVisibleAlignmentAnnotation(selectedOnly));
404 for (AlignmentAnnotation aa : ala)
406 aselview.addAnnotation(aa);
410 return formatSequences(format, aselview, jvsuffix);
414 * Construct an output class for an alignment in a particular filetype TODO:
415 * allow caller to detect errors and warnings encountered when generating
419 * string name of alignment format
421 * the alignment to be written out
423 * passed to AlnFile class controls whether /START-END is added to
426 * @return alignment flat file contents
428 public String formatSequences(FileFormatI format, AlignmentI alignment,
433 AlignmentFileI afile = format.getAlignmentFile(alignment);
435 afile.setNewlineString(newline);
436 afile.setExportSettings(exportSettings);
437 afile.configureForView(viewpanel);
439 // check whether we were given a specific alignment to export, rather than
440 // the one in the viewpanel
441 SequenceI[] seqs = null;
442 if (viewpanel == null || viewpanel.getAlignment() == null
443 || viewpanel.getAlignment() != alignment)
445 seqs = alignment.getSequencesArray();
449 seqs = viewpanel.getAlignment().getSequencesArray();
452 String afileresp = afile.print(seqs, jvsuffix);
453 if (afile.hasWarningMessage())
455 System.err.println("Warning raised when writing as " + format
456 + " : " + afile.getWarningMessage());
459 } catch (Exception e)
461 System.err.println("Failed to write alignment as a '" + format
469 public static DataSourceType checkProtocol(String file)
471 DataSourceType protocol = DataSourceType.FILE;
472 String ft = file.toLowerCase().trim();
473 if (ft.indexOf("http:") == 0 || ft.indexOf("https:") == 0
474 || ft.indexOf("file:") == 0)
476 protocol = DataSourceType.URL;
481 public static void main(String[] args)
484 while (i < args.length)
486 File f = new File(args[i]);
491 System.out.println("Reading file: " + f);
492 AppletFormatAdapter afa = new AppletFormatAdapter();
493 Runtime r = Runtime.getRuntime();
495 long memf = -r.totalMemory() + r.freeMemory();
496 long t1 = -System.currentTimeMillis();
498 .readFile(args[i], DataSourceType.FILE,
499 new IdentifyFile().identify(args[i],
500 DataSourceType.FILE));
501 t1 += System.currentTimeMillis();
503 memf += r.totalMemory() - r.freeMemory();
506 System.out.println("Alignment contains " + al.getHeight()
507 + " sequences and " + al.getWidth() + " columns.");
510 System.out.println(new AppletFormatAdapter().formatSequences(
511 FileFormat.Fasta, al, true));
512 } catch (Exception e)
515 .println("Couln't format the alignment for output as a FASTA file.");
516 e.printStackTrace(System.err);
521 System.out.println("Couldn't read alignment");
523 System.out.println("Read took " + (t1 / 1000.0) + " seconds.");
525 .println("Difference between free memory now and before is "
526 + (memf / (1024.0 * 1024.0) * 1.0) + " MB");
527 } catch (Exception e)
529 System.err.println("Exception when dealing with " + i
530 + "'th argument: " + args[i] + "\n" + e);
535 System.err.println("Ignoring argument '" + args[i] + "' (" + i
536 + "'th)- not a readable file.");
543 * try to discover how to access the given file as a valid datasource that
544 * will be identified as the given type.
548 * @return protocol that yields the data parsable as the given type
550 public static DataSourceType resolveProtocol(String file,
553 return resolveProtocol(file, format, false);
556 public static DataSourceType resolveProtocol(String file,
557 FileFormatI format, boolean debug)
559 // TODO: test thoroughly!
560 DataSourceType protocol = null;
563 System.out.println("resolving datasource started with:\n>>file\n"
564 + file + ">>endfile");
567 // This might throw a security exception in certain browsers
568 // Netscape Communicator for instance.
572 InputStream is = System.getSecurityManager().getClass()
573 .getResourceAsStream("/" + file);
581 System.err.println("Resource '" + file + "' was "
582 + (rtn ? "" : "not") + " located by classloader.");
586 protocol = DataSourceType.CLASSLOADER;
589 } catch (Exception ex)
592 .println("Exception checking resources: " + file + " " + ex);
595 if (file.indexOf("://") > -1)
597 protocol = DataSourceType.URL;
601 // skipping codebase prepend check.
602 protocol = DataSourceType.FILE;
609 System.out.println("Trying to get contents of resource as "
612 fp = new FileParse(file, protocol);
621 System.out.println("Successful.");
624 } catch (Exception e)
628 System.err.println("Exception when accessing content: " + e);
636 System.out.println("Accessing as paste.");
638 protocol = DataSourceType.PASTE;
642 fp = new FileParse(file, protocol);
647 } catch (Exception e)
649 System.err.println("Failed to access content as paste!");
666 FileFormatI idformat = new IdentifyFile().identify(file, protocol);
667 if (idformat == null)
671 System.out.println("Format not identified. Inaccessible file.");
677 System.out.println("Format identified as " + idformat
678 + "and expected as " + format);
680 if (idformat.equals(format))
684 System.out.println("Protocol identified as " + protocol);
693 .println("File deemed not accessible via " + protocol);
698 } catch (Exception e)
702 System.err.println("File deemed not accessible via " + protocol);
710 public AlignmentFileI getAlignFile()