From c1bfcc47304d63daebe01519f466e753c567afe4 Mon Sep 17 00:00:00 2001 From: jprocter Date: Tue, 29 Jan 2008 10:17:07 +0000 Subject: [PATCH] add methods to read from current position of existing fileParse object. --- src/jalview/io/AppletFormatAdapter.java | 105 +++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/src/jalview/io/AppletFormatAdapter.java b/src/jalview/io/AppletFormatAdapter.java index d9b0b55..b37c60e 100755 --- a/src/jalview/io/AppletFormatAdapter.java +++ b/src/jalview/io/AppletFormatAdapter.java @@ -137,6 +137,7 @@ public class AppletFormatAdapter public Alignment readFile(String inFile, String type, String format) throws java.io.IOException { + // TODO: generalise mapping between format string and io. class instances using Constructor.invoke reflection this.inFile = inFile; try { @@ -227,7 +228,111 @@ public class AppletFormatAdapter throw new java.io.IOException(SUPPORTED_FORMATS); } } + /** + * Constructs the correct filetype parser for an already open datasource + * + * @param source an existing datasource + * @param format File format of data that will be provided by datasource + * + * @return DOCUMENT ME! + */ + public Alignment readFromFile(FileParse source, String format) + throws java.io.IOException + { + // TODO: generalise mapping between format string and io. class instances using Constructor.invoke reflection + // This is exactly the same as the readFile method except we substitute 'inFile, type' with 'source' + this.inFile = source.getInFile(); + String type = source.type; + try + { + if (format.equals("FASTA")) + { + afile = new FastaFile(source); + } + else if (format.equals("MSF")) + { + afile = new MSFfile(source); + } + else if (format.equals("PileUp")) + { + afile = new PileUpfile(source); + } + else if (format.equals("CLUSTAL")) + { + afile = new ClustalFile(source); + } + else if (format.equals("BLC")) + { + afile = new BLCFile(source); + } + else if (format.equals("PIR")) + { + afile = new PIRFile(source); + } + else if (format.equals("PFAM")) + { + afile = new PfamFile(source); + } + else if (format.equals("JnetFile")) + { + afile = new JPredFile(source); + ( (JPredFile) afile).removeNonSequences(); + } + else if (format.equals("PDB")) + { + afile = new MCview.PDBfile(source); + } + else if (format.equals("STH")) + { + afile = new StockholmFile(source); + } + Alignment al = new Alignment(afile.getSeqsAsArray()); + + afile.addAnnotations(al); + + return al; + } + catch (Exception e) + { + e.printStackTrace(); + System.err.println("Failed to read alignment using the '" + format + + "' reader.\n" + e); + + if (e.getMessage() != null && + e.getMessage().startsWith(INVALID_CHARACTERS)) + { + throw new java.io.IOException(e.getMessage()); + } + + // Finally test if the user has pasted just the sequence, no id + if (type.equalsIgnoreCase("Paste")) + { + try + { + // Possible sequence is just residues with no label + afile = new FastaFile(">UNKNOWN\n" + inFile, "Paste"); + Alignment al = new Alignment(afile.getSeqsAsArray()); + afile.addAnnotations(al); + return al; + + } + catch (Exception ex) + { + if (ex.toString().startsWith(INVALID_CHARACTERS)) + { + throw new java.io.IOException(e.getMessage()); + } + + ex.printStackTrace(); + } + } + + // If we get to this stage, the format was not supported + throw new java.io.IOException(SUPPORTED_FORMATS); + } + } + /** * Construct an output class for an alignment in a particular filetype * -- 1.7.10.2