+
+ public static String checkProtocol(String file)
+ {
+ String protocol = FILE;
+ String ft = file.toLowerCase().trim();
+ if (ft.indexOf("http:") == 0 || ft.indexOf("https:") == 0
+ || ft.indexOf("file:") == 0)
+ {
+ protocol = URL;
+ }
+ return protocol;
+ }
+
+ public static void main(String[] args)
+ {
+ int i = 0;
+ while (i < args.length)
+ {
+ File f = new File(args[i]);
+ if (f.exists())
+ {
+ try
+ {
+ System.out.println("Reading file: " + f);
+ AppletFormatAdapter afa = new AppletFormatAdapter();
+ 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.");
+ try
+ {
+ System.out.println(new AppletFormatAdapter().formatSequences(
+ "FASTA", al, true));
+ } catch (Exception e)
+ {
+ System.err
+ .println("Couln't format the alignment for output as a FASTA file.");
+ e.printStackTrace(System.err);
+ }
+ }
+ 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);
+ }
+ }
+ else
+ {
+ System.err.println("Ignoring argument '" + args[i] + "' (" + i
+ + "'th)- not a readable file.");
+ }
+ i++;
+ }
+ }
+
+ /**
+ * try to discover how to access the given file as a valid datasource that
+ * will be identified as the given type.
+ *
+ * @param file
+ * @param format
+ * @return protocol that yields the data parsable as the given type
+ */
+ public static String resolveProtocol(String file, String format)
+ {
+ return resolveProtocol(file, format, false);
+ }
+
+ public static String resolveProtocol(String file, String format,
+ boolean debug)
+ {
+ // TODO: test thoroughly!
+ String protocol = null;
+ if (debug)
+ {
+ System.out.println("resolving datasource started with:\n>>file\n"
+ + file + ">>endfile");
+ }
+
+ // This might throw a security exception in certain browsers
+ // Netscape Communicator for instance.
+ try
+ {
+ boolean rtn = false;
+ InputStream is = System.getSecurityManager().getClass()
+ .getResourceAsStream("/" + file);
+ if (is != null)
+ {
+ rtn = true;
+ is.close();
+ }
+ if (debug)
+ {
+ System.err.println("Resource '" + file + "' was "
+ + (rtn ? "" : "not") + " located by classloader.");
+ }
+ ;
+ if (rtn)
+ {
+ protocol = AppletFormatAdapter.CLASSLOADER;
+ }
+
+ } catch (Exception ex)
+ {
+ System.err
+ .println("Exception checking resources: " + file + " " + ex);
+ }
+
+ if (file.indexOf("://") > -1)
+ {
+ protocol = AppletFormatAdapter.URL;
+ }
+ else
+ {
+ // skipping codebase prepend check.
+ protocol = AppletFormatAdapter.FILE;
+ }
+ FileParse fp = null;
+ try
+ {
+ if (debug)
+ {
+ System.out.println("Trying to get contents of resource as "
+ + protocol + ":");
+ }
+ fp = new FileParse(file, protocol);
+ if (!fp.isValid())
+ {
+ fp = null;
+ }
+ else
+ {
+ if (debug)
+ {
+ System.out.println("Successful.");
+ }
+ }
+ } catch (Exception e)
+ {
+ if (debug)
+ {
+ System.err.println("Exception when accessing content: " + e);
+ }
+ fp = null;
+ }
+ if (fp == null)
+ {
+ if (debug)
+ {
+ System.out.println("Accessing as paste.");
+ }
+ protocol = AppletFormatAdapter.PASTE;
+ fp = null;
+ try
+ {
+ fp = new FileParse(file, protocol);
+ if (!fp.isValid())
+ {
+ fp = null;
+ }
+ } catch (Exception e)
+ {
+ System.err.println("Failed to access content as paste!");
+ e.printStackTrace();
+ fp = null;
+ }
+ }
+ if (fp == null)
+ {
+ return null;
+ }
+ if (format == null || format.length() == 0)
+ {
+ return protocol;
+ }
+ else
+ {
+ try
+ {
+ String idformat = new jalview.io.IdentifyFile().Identify(file,
+ protocol);
+ if (idformat == null)
+ {
+ if (debug)
+ {
+ System.out.println("Format not identified. Inaccessible file.");
+ }
+ return null;
+ }
+ if (debug)
+ {
+ System.out.println("Format identified as " + idformat
+ + "and expected as " + format);
+ }
+ if (idformat.equals(format))
+ {
+ if (debug)
+ {
+ System.out.println("Protocol identified as " + protocol);
+ }
+ return protocol;
+ }
+ else
+ {
+ if (debug)
+ {
+ System.out
+ .println("File deemed not accessible via " + protocol);
+ }
+ fp.close();
+ return null;
+ }
+ } catch (Exception e)
+ {
+ if (debug)
+ {
+ System.err.println("File deemed not accessible via " + protocol);
+ e.printStackTrace();
+ }
+ ;
+
+ }
+ }
+ return null;
+ }