X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FAppletFormatAdapter.java;h=c7b057578fbccdb3974aec814a330f0fe3ab9ba3;hb=cd5b2de469fb4c09242955cb4b74279e2da348d6;hp=a91a1b085024e8a37b6c97bfb43c28592d5cd727;hpb=0ba2cdf46e967155efab9b457598f2112e2e2033;p=jalview.git diff --git a/src/jalview/io/AppletFormatAdapter.java b/src/jalview/io/AppletFormatAdapter.java index a91a1b0..c7b0575 100755 --- a/src/jalview/io/AppletFormatAdapter.java +++ b/src/jalview/io/AppletFormatAdapter.java @@ -113,10 +113,21 @@ public class AppletFormatAdapter */ public static final boolean isValidFormat(String format) { + return isValidFormat(format, false); + } + /** + * validate format is valid for IO + * @param format a format string to be compared with either READABLE_FORMATS or WRITEABLE_FORMATS + * @param forwriting when true, format is checked for containment in WRITEABLE_FORMATS + * @return true if format is valid + */ + public static final boolean isValidFormat(String format, boolean forwriting) + { boolean valid = false; - for (int i = 0; i < READABLE_FORMATS.length; i++) + String[] format_list = (forwriting) ? WRITEABLE_FORMATS : READABLE_FORMATS; + for (int i = 0; i < format_list.length; i++) { - if (READABLE_FORMATS[i].equalsIgnoreCase(format)) + if (format_list[i].equalsIgnoreCase(format)) { return true; } @@ -124,7 +135,7 @@ public class AppletFormatAdapter return valid; } - + /** * Constructs the correct filetype parser for a characterised datasource * @@ -137,6 +148,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 +239,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 * @@ -308,8 +424,25 @@ public class AppletFormatAdapter if (f.exists()) { try { + System.out.println("Reading file: "+f); AppletFormatAdapter afa = new AppletFormatAdapter(); - afa.readFile(args[i], FILE, new IdentifyFile().Identify(args[i], FILE)); + Runtime r = Runtime.getRuntime(); + System.gc(); + long memf = -r.totalMemory()+r.freeMemory(); + long t1 = -System.currentTimeMillis(); + Alignment al = afa.readFile(args[i], FILE, new IdentifyFile().Identify(args[i], FILE)); + t1 +=System.currentTimeMillis(); + System.gc(); + memf += r.totalMemory()-r.freeMemory(); + if (al!=null) + { + System.out.println("Alignment contains "+al.getHeight()+" sequences and "+al.getWidth()+" columns."); + } else { + System.out.println("Couldn't read alignment"); + } + System.out.println("Read took "+(t1/1000.0)+" seconds."); + System.out.println("Difference between free memory now and before is "+(memf/(1024.0*1024.0)*1.0)+" MB"); + } catch (Exception e) { System.err.println("Exception when dealing with "+i+"'th argument: "+args[i]+"\n"+e);