--- /dev/null
+/**
+ *
+ */
+package uk.ac.vamsas.objects.utils;
+import java.util.Vector;
+
+
+import uk.ac.vamsas.client.ClientHandle;
+import uk.ac.vamsas.client.UserHandle;
+import uk.ac.vamsas.client.simpleclient.VamsasArchive;
+import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
+import uk.ac.vamsas.objects.core.*;
+/**
+ * Form, accessors and validation for ApplicationData references in
+ * vamsas document.
+ * TODO: LATER:extend XML Schema to properly validate against the same forms required by this class
+ * TODO: VAMSAS: URNS for appDatas are supposed to be unique, aren't they ?
+ */
+public class AppDataReference {
+ /**
+ * search interface for collecting particular types of AppDatas in a vamsas document
+ * @author jimp
+ *
+ */
+ interface IAppDSearch {
+ /**
+ * process the appData Vobject d
+ * @param d
+ * @return true if appData should be collected
+ */
+ public boolean process(AppData d);
+ }
+ /**
+ * collect all appData reference strings in a vamsas document
+ * @param doc
+ * @return vector of String objects
+ */
+ static public Vector getAppDataReferences(VamsasDocument doc) {
+ if ((doc!=null) && (doc.getApplicationDataCount()>0)) {
+ Vector apdrefs = new Vector();
+ ApplicationData[] appdatas = doc.getApplicationData();
+ for (int q=0; q<appdatas.length; q++) {
+ String refstring=appdatas[q].getDataReference();
+ if (refstring!=null)
+ apdrefs.add(refstring);
+ User users[] = appdatas[q].getUser();
+
+ if (users!=null)
+ for (int u=0; u<users.length; u++) {
+ refstring=users[u].getDataReference();
+ if (refstring!=null)
+ apdrefs.add(new String(refstring)); // avoid referencing.
+ }
+ }
+ if (apdrefs.size()>0)
+ return apdrefs;
+ }
+ return null;
+ }
+ /**
+ * General search through the set of AppData objects for a particular profile of Client and User handle.
+ * @param doc
+ * @param test interface implemented by the filter selecting particular AppDatas.
+ * @param cascade if true only User objects for ApplicationData objects that test.process returned true will be tested.
+ * @return set of uk.ac.vamsas.objects.core.AppData objects for which test.process returned true
+ */
+ static public Vector searchAppDatas(VamsasDocument doc, IAppDSearch test, boolean cascade) {
+ if ((doc!=null) && (doc.getApplicationDataCount()>0)) {
+ Vector apdrefs = new Vector();
+ ApplicationData[] appdatas = doc.getApplicationData();
+ for (int q=0; q<appdatas.length; q++) {
+ boolean t;
+ if (t=test.process(appdatas[q]))
+ apdrefs.add(appdatas[q]);
+ if (t || cascade) {
+ User users[] = appdatas[q].getUser();
+ if (users!=null)
+ for (int u=0; u<users.length; u++)
+ if (test.process(users[u]))
+ apdrefs.add(users[u]);
+ }
+ }
+ if (apdrefs.size()>0)
+ return apdrefs;
+ }
+ return null;
+ }
+ static public boolean equals(User p, UserHandle u) {
+ if (p.getFullname().equals(u.getFullName())
+ && p.getOrganization().equals(u.getOrganization()))
+ return true;
+ return false;
+ }
+ /**
+ * returns true if Name matches in c and p, and Urn's match (or c.getUrn()==null) and Version's match (or c.getVersion()==null)
+ * @param p
+ * @param c
+ * @return match of p on template c.
+ */
+ static public boolean equals(ApplicationData p, ClientHandle c) {
+ if (
+ //((c.getClientUrn()==null) || p.getUrn().equals(c.getClientUrn()))
+ //&&
+ (p.getName().equals(c.getClientName()))
+ &&
+ ((c.getVersion()==null) || (p.getVersion().equals(c.getVersion())))
+ )
+ return true;
+ return false;
+ }
+ /**
+ * Searches document appData structure for particular combinations of client and user data
+ * @param doc the data
+ * @param user template user data to match against
+ * @see AppDataReference.equals(uk.ac.vamsas.objects.core.User, uk.ac.vamsas.client.UserHandle)
+ * @param app
+ * @see AppDataReference.equals(uk.ac.vamsas.objects.core.ApplicationData, uk.ac.vamsas.client.ClientHandle)
+ * @return set of matching client app datas for this client and user combination
+ */
+ static public Vector getUserandApplicationsData(VamsasDocument doc, UserHandle user, ClientHandle app) {
+ if (doc==null) {
+ return null;
+ }
+ final UserHandle u = user;
+ final ClientHandle c = app;
+
+ IAppDSearch match = new IAppDSearch() {
+ public boolean process(AppData p) {
+ if (p instanceof User) {
+ if (AppDataReference.equals((User) p, u))
+ return true;
+ } else
+ if (p instanceof ApplicationData) {
+ if (AppDataReference.equals((ApplicationData) p, c))
+ return true;
+ }
+ return false;
+ }
+ };
+
+ return searchAppDatas(doc, match, true); // only return AppDatas belonging to appdata app.
+ }
+ /**
+ * safely creates a new appData reference
+ * @param dest destination document Vobject
+ * @param entry base application reference to make unique
+ */
+ public static String uniqueAppDataReference(VamsasDocument dest,String base) {
+ String urn = new String(base);
+
+ for (int i=0, j=dest.getApplicationDataCount(); i<j; i++) {
+ ApplicationData o = dest.getApplicationData()[i];
+ // ensure new urn is really unique
+ int v = 1;
+ // TODO: while (o.getUrn().equals(urn)) {
+ urn = base+"/"+v++;
+ //}
+ }
+ return urn;
+ }
+}
--- /dev/null
+package uk.ac.vamsas.objects.utils;
+
+
+import uk.ac.vamsas.objects.core.*;
+import uk.ac.vamsas.objects.utils.document.VersionEntries;
+
+/**
+ * various vamsas-client independent helpers
+ * for creating and manipulating the vamsasDocument Vobject
+ * @author jimp
+ *
+ */
+public class DocumentStuff {
+ public static VamsasDocument newVamsasDocument(VAMSAS root[], String version) {
+ return newVamsasDocument(root, ProvenanceStuff.newProvenance(
+ "AUTO:org.vamsas.DocumentStuff.newVamsasDocument",
+ "Vamsas Document created"),
+ version);
+ }
+ public static VamsasDocument newVamsasDocument(VAMSAS root[]) {
+ return newVamsasDocument(root, ProvenanceStuff.newProvenance(
+ "AUTO:org.vamsas.DocumentStuff.newVamsasDocument",
+ "Vamsas Document created"),
+ VersionEntries.latestVersion());
+ }
+ public static VamsasDocument newVamsasDocument(VAMSAS root[], Provenance p, String version) {
+ VamsasDocument doc = new VamsasDocument();
+ for (int r=0; r<root.length; r++) {
+ doc.addVAMSAS(root[r]);
+ }
+ doc.setProvenance(p);
+ doc.setVersion(version);
+ return doc;
+ }
+}
--- /dev/null
+/*\r
+ * Cay S. Horstmann & Gary Cornell, Core Java\r
+ * Published By Sun Microsystems Press/Prentice-Hall\r
+ * Copyright (C) 1997 Sun Microsystems Inc.\r
+ * All Rights Reserved.\r
+ *\r
+ * Permission to use, copy, modify, and distribute this \r
+ * software and its documentation for NON-COMMERCIAL purposes\r
+ * and without fee is hereby granted provided that this \r
+ * copyright notice appears in all copies. \r
+ * \r
+ * THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR \r
+ * WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER \r
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE \r
+ * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A \r
+ * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS\r
+ * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED \r
+ * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING \r
+ * THIS SOFTWARE OR ITS DERIVATIVES.\r
+ */\r
+\r
+/**\r
+ * A class for formatting numbers that follows printf conventions.\r
+ * Also implements C-like atoi and atof functions\r
+ * @version 1.03 25 Oct 1997\r
+ * @author Cay Horstmann\r
+ */\r
+\r
+package uk.ac.vamsas.objects.utils;\r
+\r
+import java.io.*;\r
+\r
+public class Format { /**\r
+ * Formats the number following printf conventions.\r
+ * Main limitation: Can only handle one format parameter at a time\r
+ * Use multiple Format objects to format more than one number\r
+ * @param s the format string following printf conventions\r
+ * The string has a prefix, a format code and a suffix. The prefix and suffix\r
+ * become part of the formatted output. The format code directs the\r
+ * formatting of the (single) parameter to be formatted. The code has the\r
+ * following structure\r
+ * <ul>\r
+ * <li> a % (required)\r
+ * <li> a modifier (optional)\r
+ * <dl>\r
+ * <dt> + <dd> forces display of + for positive numbers\r
+ * <dt> 0 <dd> show leading zeroes\r
+ * <dt> - <dd> align left in the field\r
+ * <dt> space <dd> prepend a space in front of positive numbers\r
+ * <dt> # <dd> use "alternate" format. Add 0 or 0x for octal or hexadecimal numbers. Don't suppress trailing zeroes in general floating point format.\r
+ * </dl>\r
+ * <li> an integer denoting field width (optional)\r
+ * <li> a period followed by an integer denoting precision (optional)\r
+ * <li> a format descriptor (required)\r
+ * <dl>\r
+ * <dt>f <dd> floating point number in fixed format\r
+ * <dt>e, E <dd> floating point number in exponential notation (scientific format). The E format results in an uppercase E for the exponent (1.14130E+003), the e format in a lowercase e.\r
+ * <dt>g, G <dd> floating point number in general format (fixed format for small numbers, exponential format for large numbers). Trailing zeroes are suppressed. The G format results in an uppercase E for the exponent (if any), the g format in a lowercase e.\r
+ * <dt>d, i <dd> integer in decimal\r
+ * <dt>x <dd> integer in hexadecimal\r
+ * <dt>o <dd> integer in octal\r
+ * <dt>s <dd> string\r
+ * <dt>c <dd> character\r
+ * </dl>\r
+ * </ul>\r
+ * @exception IllegalArgumentException if bad format\r
+ */\r
+\r
+ public Format(String s) {\r
+ width = 0;\r
+ precision = -1;\r
+ pre = "";\r
+ post = "";\r
+ leading_zeroes = false;\r
+ show_plus = false;\r
+ alternate = false;\r
+ show_space = false;\r
+ left_align = false;\r
+ fmt = ' ';\r
+\r
+ int state = 0;\r
+ int length = s.length();\r
+ int parse_state = 0;\r
+ // 0 = prefix, 1 = flags, 2 = width, 3 = precision,\r
+ // 4 = format, 5 = end\r
+ int i = 0;\r
+\r
+ while (parse_state == 0) {\r
+ if (i >= length)\r
+ parse_state = 5;\r
+ else if (s.charAt(i) == '%') {\r
+ if (i < length - 1) {\r
+ if (s.charAt(i + 1) == '%') {\r
+ pre = pre + '%';\r
+ i++;\r
+ } else\r
+ parse_state = 1;\r
+ } else\r
+ throw new java.lang.IllegalArgumentException();\r
+ } else\r
+ pre = pre + s.charAt(i);\r
+ i++;\r
+ }\r
+ while (parse_state == 1) {\r
+ if (i >= length)\r
+ parse_state = 5;\r
+ else if (s.charAt(i) == ' ')\r
+ show_space = true;\r
+ else if (s.charAt(i) == '-')\r
+ left_align = true;\r
+ else if (s.charAt(i) == '+')\r
+ show_plus = true;\r
+ else if (s.charAt(i) == '0')\r
+ leading_zeroes = true;\r
+ else if (s.charAt(i) == '#')\r
+ alternate = true;\r
+ else {\r
+ parse_state = 2;\r
+ i--;\r
+ }\r
+ i++;\r
+ }\r
+ while (parse_state == 2) {\r
+ if (i >= length)\r
+ parse_state = 5;\r
+ else if ('0' <= s.charAt(i) && s.charAt(i) <= '9') {\r
+ width = width * 10 + s.charAt(i) - '0';\r
+ i++;\r
+ } else if (s.charAt(i) == '.') {\r
+ parse_state = 3;\r
+ precision = 0;\r
+ i++;\r
+ } else\r
+ parse_state = 4;\r
+ }\r
+ while (parse_state == 3) {\r
+ if (i >= length)\r
+ parse_state = 5;\r
+ else if ('0' <= s.charAt(i) && s.charAt(i) <= '9') {\r
+ precision = precision * 10 + s.charAt(i) - '0';\r
+ i++;\r
+ } else\r
+ parse_state = 4;\r
+ }\r
+ if (parse_state == 4) {\r
+ if (i >= length)\r
+ parse_state = 5;\r
+ else\r
+ fmt = s.charAt(i);\r
+ i++;\r
+ }\r
+ if (i < length)\r
+ post = s.substring(i, length);\r
+ }\r
+\r
+ /**\r
+ * prints a formatted number following printf conventions\r
+ * @param s a PrintStream\r
+ * @param fmt the format string\r
+ * @param x the double to print\r
+ */\r
+\r
+ public static void print(java.io.PrintStream s, String fmt, double x) {\r
+ s.print(new Format(fmt).form(x));\r
+ }\r
+\r
+ /**\r
+ * prints a formatted number following printf conventions\r
+ * @param s a PrintStream\r
+ * @param fmt the format string\r
+ * @param x the long to print\r
+ */\r
+ public static void print(java.io.PrintStream s, String fmt, long x) {\r
+ s.print(new Format(fmt).form(x));\r
+ }\r
+\r
+ /**\r
+ * prints a formatted number following printf conventions\r
+ * @param s a PrintStream\r
+ * @param fmt the format string\r
+ * @param x the character to \r
+ */\r
+\r
+ public static void print(java.io.PrintStream s, String fmt, char x) {\r
+ s.print(new Format(fmt).form(x));\r
+ }\r
+\r
+ /**\r
+ * prints a formatted number following printf conventions\r
+ * @param s a PrintStream, fmt the format string\r
+ * @param x a string that represents the digits to print\r
+ */\r
+\r
+ public static void print(java.io.PrintStream s, String fmt, String x) {\r
+ s.print(new Format(fmt).form(x));\r
+ }\r
+\r
+ /**\r
+ * Converts a string of digits (decimal, octal or hex) to an integer\r
+ * @param s a string\r
+ * @return the numeric value of the prefix of s representing a base 10 integer\r
+ */\r
+\r
+ public static int atoi(String s) {\r
+ return (int)atol(s);\r
+ }\r
+\r
+ /**\r
+ * Converts a string of digits (decimal, octal or hex) to a long integer\r
+ * @param s a string\r
+ * @return the numeric value of the prefix of s representing a base 10 integer\r
+ */\r
+\r
+ public static long atol(String s) {\r
+ int i = 0;\r
+\r
+ while (i < s.length() && Character.isWhitespace(s.charAt(i)))\r
+ i++;\r
+ if (i < s.length() && s.charAt(i) == '0') {\r
+ if (i + 1 < s.length() && (s.charAt(i + 1) == 'x' || s.charAt(i + 1) == 'X'))\r
+ return parseLong(s.substring(i + 2), 16);\r
+ else\r
+ return parseLong(s, 8);\r
+ } else\r
+ return parseLong(s, 10);\r
+ }\r
+\r
+ private static long parseLong(String s, int base) {\r
+ int i = 0;\r
+ int sign = 1;\r
+ long r = 0;\r
+\r
+ while (i < s.length() && Character.isWhitespace(s.charAt(i)))\r
+ i++;\r
+ if (i < s.length() && s.charAt(i) == '-') {\r
+ sign = -1;\r
+ i++;\r
+ } else if (i < s.length() && s.charAt(i) == '+') {\r
+ i++;\r
+ }\r
+ while (i < s.length()) {\r
+ char ch = s.charAt(i);\r
+ if ('0' <= ch && ch < '0' + base)\r
+ r = r * base + ch - '0';\r
+ else if ('A' <= ch && ch < 'A' + base - 10)\r
+ r = r * base + ch - 'A' + 10 ;\r
+ else if ('a' <= ch && ch < 'a' + base - 10)\r
+ r = r * base + ch - 'a' + 10 ;\r
+ else\r
+ return r * sign;\r
+ i++;\r
+ }\r
+ return r * sign;\r
+ }\r
+\r
+ /**\r
+ * Converts a string of digits to an double\r
+ * @param s a string\r
+ */\r
+\r
+ public static double atof(String s) {\r
+ int i = 0;\r
+ int sign = 1;\r
+ double r = 0; // integer part\r
+ double f = 0; // fractional part\r
+ double p = 1; // exponent of fractional part\r
+ int state = 0; // 0 = int part, 1 = frac part\r
+\r
+ while (i < s.length() && Character.isWhitespace(s.charAt(i)))\r
+ i++;\r
+ if (i < s.length() && s.charAt(i) == '-') {\r
+ sign = -1;\r
+ i++;\r
+ } else if (i < s.length() && s.charAt(i) == '+') {\r
+ i++;\r
+ }\r
+ while (i < s.length()) {\r
+ char ch = s.charAt(i);\r
+ if ('0' <= ch && ch <= '9') {\r
+ if (state == 0)\r
+ r = r * 10 + ch - '0';\r
+ else if (state == 1) {\r
+ p = p / 10;\r
+ r = r + p * (ch - '0');\r
+ }\r
+ } else if (ch == '.') {\r
+ if (state == 0)\r
+ state = 1;\r
+ else\r
+ return sign * r;\r
+ } else if (ch == 'e' || ch == 'E') {\r
+ long e = (int)parseLong(s.substring(i + 1), 10);\r
+ return sign * r * Math.pow(10, e);\r
+ } else\r
+ return sign * r;\r
+ i++;\r
+ }\r
+ return sign * r;\r
+ }\r
+\r
+ /**\r
+ * Formats a double into a string (like sprintf in C)\r
+ * @param x the number to format\r
+ * @return the formatted string \r
+ * @exception IllegalArgumentException if bad argument\r
+ */\r
+\r
+ public String form(double x) {\r
+ String r;\r
+ if (precision < 0)\r
+ precision = 6;\r
+ int s = 1;\r
+ if (x < 0) {\r
+ x = -x;\r
+ s = -1;\r
+ }\r
+ if (fmt == 'f')\r
+ r = fixed_format(x);\r
+ else if (fmt == 'e' || fmt == 'E' || fmt == 'g' || fmt == 'G')\r
+ r = exp_format(x);\r
+ else\r
+ throw new java.lang.IllegalArgumentException();\r
+\r
+ return pad(sign(s, r));\r
+ }\r
+\r
+ /**\r
+ * Formats a long integer into a string (like sprintf in C)\r
+ * @param x the number to format\r
+ * @return the formatted string \r
+ */\r
+\r
+ public String form(long x) {\r
+ String r;\r
+ int s = 0;\r
+ if (fmt == 'd' || fmt == 'i') {\r
+ if (x < 0) {\r
+ r = ("" + x).substring(1);\r
+ s = -1;\r
+ } else {\r
+ r = "" + x;\r
+ s = 1;\r
+ }\r
+ } else if (fmt == 'o')\r
+ r = convert(x, 3, 7, "01234567");\r
+ else if (fmt == 'x')\r
+ r = convert(x, 4, 15, "0123456789abcdef");\r
+ else if (fmt == 'X')\r
+ r = convert(x, 4, 15, "0123456789ABCDEF");\r
+ else\r
+ throw new java.lang.IllegalArgumentException();\r
+\r
+ return pad(sign(s, r));\r
+ }\r
+\r
+ /**\r
+ * Formats a character into a string (like sprintf in C)\r
+ * @param x the value to format\r
+ * @return the formatted string \r
+ */\r
+\r
+ public String form(char c) {\r
+ if (fmt != 'c')\r
+ throw new java.lang.IllegalArgumentException();\r
+\r
+ String r = "" + c;\r
+ return pad(r);\r
+ }\r
+\r
+ /**\r
+ * Formats a string into a larger string (like sprintf in C)\r
+ * @param x the value to format\r
+ * @return the formatted string \r
+ */\r
+\r
+ public String form(String s) {\r
+ if (fmt != 's')\r
+ throw new java.lang.IllegalArgumentException();\r
+ if (precision >= 0)\r
+ s = s.substring(0, precision);\r
+ return pad(s);\r
+ }\r
+\r
+\r
+ /**\r
+ * a test stub for the format class\r
+ */\r
+\r
+ public static void main(String[] a) {\r
+ double x = 1.23456789012;\r
+ double y = 123;\r
+ double z = 1.2345e30;\r
+ double w = 1.02;\r
+ double u = 1.234e-5;\r
+ int d = 0xCAFE;\r
+ Format.print(System.out, "x = |%f|\n", x);\r
+ Format.print(System.out, "u = |%20f|\n", u);\r
+ Format.print(System.out, "x = |% .5f|\n", x);\r
+ Format.print(System.out, "w = |%20.5f|\n", w);\r
+ Format.print(System.out, "x = |%020.5f|\n", x);\r
+ Format.print(System.out, "x = |%+20.5f|\n", x);\r
+ Format.print(System.out, "x = |%+020.5f|\n", x);\r
+ Format.print(System.out, "x = |% 020.5f|\n", x);\r
+ Format.print(System.out, "y = |%#+20.5f|\n", y);\r
+ Format.print(System.out, "y = |%-+20.5f|\n", y);\r
+ Format.print(System.out, "z = |%20.5f|\n", z);\r
+\r
+ Format.print(System.out, "x = |%e|\n", x);\r
+ Format.print(System.out, "u = |%20e|\n", u);\r
+ Format.print(System.out, "x = |% .5e|\n", x);\r
+ Format.print(System.out, "w = |%20.5e|\n", w);\r
+ Format.print(System.out, "x = |%020.5e|\n", x);\r
+ Format.print(System.out, "x = |%+20.5e|\n", x);\r
+ Format.print(System.out, "x = |%+020.5e|\n", x);\r
+ Format.print(System.out, "x = |% 020.5e|\n", x);\r
+ Format.print(System.out, "y = |%#+20.5e|\n", y);\r
+ Format.print(System.out, "y = |%-+20.5e|\n", y);\r
+\r
+ Format.print(System.out, "x = |%g|\n", x);\r
+ Format.print(System.out, "z = |%g|\n", z);\r
+ Format.print(System.out, "w = |%g|\n", w);\r
+ Format.print(System.out, "u = |%g|\n", u);\r
+ Format.print(System.out, "y = |%.2g|\n", y);\r
+ Format.print(System.out, "y = |%#.2g|\n", y);\r
+\r
+ Format.print(System.out, "d = |%d|\n", d);\r
+ Format.print(System.out, "d = |%20d|\n", d);\r
+ Format.print(System.out, "d = |%020d|\n", d);\r
+ Format.print(System.out, "d = |%+20d|\n", d);\r
+ Format.print(System.out, "d = |% 020d|\n", d);\r
+ Format.print(System.out, "d = |%-20d|\n", d);\r
+ Format.print(System.out, "d = |%20.8d|\n", d);\r
+ Format.print(System.out, "d = |%x|\n", d);\r
+ Format.print(System.out, "d = |%20X|\n", d);\r
+ Format.print(System.out, "d = |%#20x|\n", d);\r
+ Format.print(System.out, "d = |%020X|\n", d);\r
+ Format.print(System.out, "d = |%20.8x|\n", d);\r
+ Format.print(System.out, "d = |%o|\n", d);\r
+ Format.print(System.out, "d = |%020o|\n", d);\r
+ Format.print(System.out, "d = |%#20o|\n", d);\r
+ Format.print(System.out, "d = |%#020o|\n", d);\r
+ Format.print(System.out, "d = |%20.12o|\n", d);\r
+\r
+ Format.print(System.out, "s = |%-20s|\n", "Hello");\r
+ Format.print(System.out, "s = |%-20c|\n", '!');\r
+\r
+ // regression test to confirm fix of reported bugs\r
+\r
+ Format.print(System.out, "|%i|\n", Long.MIN_VALUE);\r
+\r
+ Format.print(System.out, "|%6.2e|\n", 0.0);\r
+ Format.print(System.out, "|%6.2g|\n", 0.0);\r
+\r
+ Format.print(System.out, "|%6.2f|\n", 9.99);\r
+ Format.print(System.out, "|%6.2f|\n", 9.999);\r
+\r
+ Format.print(System.out, "|%6.0f|\n", 9.999);\r
+ }\r
+\r
+ private static String repeat(char c, int n) {\r
+ if (n <= 0)\r
+ return "";\r
+ StringBuffer s = new StringBuffer(n);\r
+ for (int i = 0; i < n; i++)\r
+ s.append(c);\r
+ return s.toString();\r
+ }\r
+\r
+ private static String convert(long x, int n, int m, String d) {\r
+ if (x == 0)\r
+ return "0";\r
+ String r = "";\r
+ while (x != 0) {\r
+ r = d.charAt((int)(x & m)) + r;\r
+ x = x >>> n;\r
+ }\r
+ return r;\r
+ }\r
+\r
+ private String pad(String r) {\r
+ String p = repeat(' ', width - r.length());\r
+ if (left_align)\r
+ return pre + r + p + post;\r
+ else\r
+ return pre + p + r + post;\r
+ }\r
+\r
+ private String sign(int s, String r) {\r
+ String p = "";\r
+ if (s < 0)\r
+ p = "-";\r
+ else if (s > 0) {\r
+ if (show_plus)\r
+ p = "+";\r
+ else if (show_space)\r
+ p = " ";\r
+ } else {\r
+ if (fmt == 'o' && alternate && r.length() > 0 && r.charAt(0) != '0')\r
+ p = "0";\r
+ else if (fmt == 'x' && alternate)\r
+ p = "0x";\r
+ else if (fmt == 'X' && alternate)\r
+ p = "0X";\r
+ }\r
+ int w = 0;\r
+ if (leading_zeroes)\r
+ w = width;\r
+ else if ((fmt == 'd' || fmt == 'i' || fmt == 'x' || fmt == 'X' || fmt == 'o')\r
+ && precision > 0)\r
+ w = precision;\r
+\r
+ return p + repeat('0', w - p.length() - r.length()) + r;\r
+ }\r
+\r
+ private String fixed_format(double d) {\r
+ boolean removeTrailing\r
+ = (fmt == 'G' || fmt == 'g') && !alternate;\r
+ // remove trailing zeroes and decimal point\r
+\r
+ if (d > 0x7FFFFFFFFFFFFFFFL)\r
+ return exp_format(d);\r
+ if (precision == 0)\r
+ return (long)(d + 0.5) + (removeTrailing ? "" : ".");\r
+\r
+ long whole = (long)d;\r
+ double fr = d - whole; // fractional part\r
+ if (fr >= 1 || fr < 0)\r
+ return exp_format(d);\r
+\r
+ double factor = 1;\r
+ String leading_zeroes = "";\r
+ for (int i = 1; i <= precision && factor <= 0x7FFFFFFFFFFFFFFFL; i++) {\r
+ factor *= 10;\r
+ leading_zeroes = leading_zeroes + "0";\r
+ }\r
+ long l = (long) (factor * fr + 0.5);\r
+ if (l >= factor) {\r
+ l = 0;\r
+ whole++;\r
+ } // CSH 10-25-97\r
+\r
+ String z = leading_zeroes + l;\r
+ z = "." + z.substring(z.length() - precision, z.length());\r
+\r
+ if (removeTrailing) {\r
+ int t = z.length() - 1;\r
+ while (t >= 0 && z.charAt(t) == '0')\r
+ t--;\r
+ if (t >= 0 && z.charAt(t) == '.')\r
+ t--;\r
+ z = z.substring(0, t + 1);\r
+ }\r
+\r
+ return whole + z;\r
+ }\r
+\r
+ private String exp_format(double d) {\r
+ String f = "";\r
+ int e = 0;\r
+ double dd = d;\r
+ double factor = 1;\r
+ if (d != 0) {\r
+ while (dd > 10) {\r
+ e++;\r
+ factor /= 10;\r
+ dd = dd / 10;\r
+ }\r
+ while (dd < 1) {\r
+ e--;\r
+ factor *= 10;\r
+ dd = dd * 10;\r
+ }\r
+ }\r
+ if ((fmt == 'g' || fmt == 'G') && e >= -4 && e < precision)\r
+ return fixed_format(d);\r
+\r
+ d = d * factor;\r
+ f = f + fixed_format(d);\r
+\r
+ if (fmt == 'e' || fmt == 'g')\r
+ f = f + "e";\r
+ else\r
+ f = f + "E";\r
+\r
+ String p = "000";\r
+ if (e >= 0) {\r
+ f = f + "+";\r
+ p = p + e;\r
+ } else {\r
+ f = f + "-";\r
+ p = p + (-e);\r
+ }\r
+\r
+ return f + p.substring(p.length() - 3, p.length());\r
+ }\r
+\r
+ private int width;\r
+ private int precision;\r
+ private String pre;\r
+ private String post;\r
+ private boolean leading_zeroes;\r
+ private boolean show_plus;\r
+ private boolean alternate;\r
+ private boolean show_space;\r
+ private boolean left_align;\r
+ private char fmt; // one of cdeEfgGiosxXos\r
+}\r
+\r
+\r
+\r
+\r
+\r
--- /dev/null
+package uk.ac.vamsas.objects.utils;\r
+\r
+/**\r
+ * dict attribute values for glyph symbol sets found in uk.ac.vamsas.objects.core.AnnotationElement\r
+ * TODO: add validators and multilength symbols.\r
+ * @author JimP\r
+ *\r
+ */\r
+public class GlyphDictionary {\r
+ /**\r
+ * standard H, E, or C three state secondary structure assignment.\r
+ */\r
+ static final public String PROTEIN_SS_3STATE="aasecstr_3"; // HE, blank or C\r
+ /**\r
+ * default glyph type attribute indicates a UTF8 character\r
+ */\r
+ static final public String DEFAULT="utf8";\r
+ /**\r
+ * kyte and doolittle hydrophobicity\r
+ * TODO: specify this glyph set.\r
+ */\r
+ static final public String PROTEIN_HD_HYDRO="kd_hydrophobicity";\r
+}\r
--- /dev/null
+package uk.ac.vamsas.objects.utils;
+
+import java.util.Date;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import uk.ac.vamsas.objects.core.Entry;
+import uk.ac.vamsas.objects.core.Provenance;
+
+public class ProvenanceStuff {
+
+ /**
+ * stuff for making and doing things with provenance objects.
+ */
+ static Log log = LogFactory.getLog(ProvenanceStuff.class);
+
+ /**
+ * @param app TODO
+ * @param action
+ * text for action entry
+ * @return new Provenance entry for ArchiveWriter created docs.
+ * TODO: Verify and move to SimpleClient class for provenance handling
+ */
+ public static Entry newProvenanceEntry(String app, String user, String action) {
+ log.debug("Adding ProvenanceEntry("+user+","+action+")");
+ Entry e = new Entry();
+ e.setApp(app);
+ e.setAction(action);
+ e.setUser(user);
+ e.setDate(new org.exolab.castor.types.Date(new Date()));
+ return e;
+ }
+ public static Provenance newProvenance(Entry entry) {
+ Provenance list = new Provenance();
+ list.addEntry(entry);
+ return list;
+ }
+ public static Provenance newProvenance(String user, String action) {
+ return newProvenance(ProvenanceStuff.newProvenanceEntry("vamsasApp:ExampleVamsasClient/alpha", user, action));
+ }
+ public static Provenance newProvenance(String app, String user, String action) {
+ return newProvenance(ProvenanceStuff.newProvenanceEntry(app, user, action));
+ }
+
+}
--- /dev/null
+/*
+ * Created on 17-May-2005
+ *
+ * TODO To change the template for this generated file go to
+ * Window - Preferences - Java - Code Style - Code Templates
+ */
+package uk.ac.vamsas.objects.utils;
+
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.util.regex.Pattern;
+
+import uk.ac.vamsas.objects.core.AlignmentSequence;
+import uk.ac.vamsas.objects.core.Sequence;
+import uk.ac.vamsas.objects.core.SequenceType;
+
+/**
+ * @author jimp
+ *
+ * TODO To change the template for this generated type comment go to
+ * Window - Preferences - Java - Code Style - Code Templates
+ */
+public class Seq {
+
+ public static void write_PirSeq(OutputStream os, SequenceType seq, int wid) throws IOException {
+ BufferedWriter pir_out = new BufferedWriter(new OutputStreamWriter(os));
+ pir_out.write(">P1;"+seq.getName()+"\n");
+ int width = (wid<1) ? 80 : wid;
+ for (int j=0,k=seq.getSequence().length(); j<k; j+=width)
+ if (j+width<k)
+ pir_out.write(seq.getSequence().substring(j,j+width)+"\n");
+ else
+ pir_out.write(seq.getSequence().substring(j)+"\n");
+ pir_out.flush();
+ }
+
+ public static void write_FastaSeq(OutputStream os, SequenceType seq) throws IOException {
+ BufferedWriter fasta_out = new BufferedWriter(new OutputStreamWriter(os));
+ fasta_out.write(">"+seq.getName()+"\n");
+ fasta_out.write(seq.getSequence()+"\n");
+ fasta_out.flush();
+ }
+
+ public static void write_FastaSeq(OutputStream os, SequenceType seq, int wid) throws IOException {
+ BufferedWriter fasta_out = new BufferedWriter(new OutputStreamWriter(os));
+ fasta_out.write(">"+seq.getName()+"\n");
+ int width = (wid<1) ? 80 : wid;
+ for (int j=0,k=seq.getSequence().length(); j<k; j+=width)
+ if (j+width<k)
+ fasta_out.write(seq.getSequence().substring(j,j+width)+"\n");
+ else
+ fasta_out.write(seq.getSequence().substring(j)+"\n");
+ fasta_out.flush();
+ }
+ /**
+ *validate a SequenceType Vobject as an info:iubmb.org/aminoacid SequenceType
+ *This version resolves references to Sequence objects from AlignmentSequence
+ *TODO: Define info: urn for dictionary string (could also be regex of valid characters!)
+ * @param s
+ * @param dict TODO
+ * @return true if a valid amino acid sequence Vobject
+ */
+ private static boolean valid_aadictionary_string(String s, String dict) {
+ if (s==null)
+ return false;
+ // validate against dictionary
+ // TODO generalise to resolve dictionary against info: urn for dictionary type
+ Pattern aa_repl = Pattern.compile("[ARNDCQEGHILKMFPSTWYVUX]+", Pattern.CASE_INSENSITIVE);
+ String remnants = aa_repl.matcher(s).replaceAll("");
+ return !remnants.matches("//S+");
+ }
+
+ public static Sequence newSequence(String Name, String Sequence, String Dictionary, int start, int end) {
+ //TODO: make hierarchy reflecting the SeqType Vobject.
+ Sequence seq= new Sequence();
+ seq.setDictionary(Dictionary);
+ seq.setName(Name);
+ seq.setSequence(Sequence);
+ seq.setStart(start);
+ if (start<=end) {
+ if ((end-start)!=Sequence.length())
+ seq.setEnd(start+Sequence.length());
+ } else {
+ // reverse topology mapping. TODO: VAMSAS: decide if allowed to do start>end on Sequence Vobject
+ if ((start-end)!=Sequence.length())
+ seq.setEnd(end+Sequence.length());
+ }
+ return seq;
+ }
+ public static AlignmentSequence newAlignmentSequence(String name, String alSequence, Sequence refseq, int start, int end) {
+ if (refseq!=null) {
+ AlignmentSequence asq = new AlignmentSequence();
+ asq.setName(name);
+ asq.setSequence(alSequence);
+ asq.setRefid(refseq);
+ if (start<refseq.getStart())
+ start = refseq.getStart();
+ asq.setStart(start);
+ if (end>refseq.getEnd())
+ end = refseq.getEnd();
+ asq.setEnd(end);
+ return asq;
+ }
+ return null;
+ }
+ public static boolean is_valid_aa_seq(SequenceType s) {
+ Sequence q;
+ boolean validref=false;
+ if (s instanceof Sequence) {
+ q=(Sequence) s;
+ if (q.getDictionary()!=null
+
+ && q.getDictionary().length()>0
+ || !q.getDictionary().equals(SymbolDictionary.STANDARD_AA))
+ return false;
+ return valid_aadictionary_string(q.getSequence(), SymbolDictionary.STANDARD_AA);
+ }
+
+ // follow references
+ if (s instanceof AlignmentSequence) {
+ Object w = (((AlignmentSequence) s).getRefid());
+ if (w!=null && w!=s && w instanceof SequenceType)
+ return is_valid_aa_seq((SequenceType) w)
+ && valid_aadictionary_string(((AlignmentSequence) s).getSequence(), SymbolDictionary.STANDARD_AA);
+ }
+
+ return false;
+ }
+}
--- /dev/null
+/*
+ * Created on 17-May-2005
+ *
+ * TODO To change the template for this generated file go to
+ * Window - Preferences - Java - Code Style - Code Templates
+ */
+package uk.ac.vamsas.objects.utils;
+
+import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Hashtable;
+import java.util.StringTokenizer;
+import java.util.Vector;
+import java.util.regex.Pattern;
+
+import uk.ac.vamsas.objects.core.*;
+
+/**
+ * @author jimp
+ *
+ * TODO To change the template for this generated type comment go to Window -
+ * Preferences - Java - Code Style - Code Templates
+ */
+public class SeqAln extends uk.ac.vamsas.objects.core.Alignment {
+
+ public static Sequence[] ReadClustalFile(InputStream os) throws Exception {
+ System.err.println("NOT FULLY IMPLEMENTED!"); // TODO: Finish adapting this method
+ Pattern nonGap = Pattern.compile("[A-Z*0-9]", Pattern.CASE_INSENSITIVE);
+ String gapchars = "";
+ char gapchar = '-';
+
+ int i = 0;
+ boolean flag = false;
+
+ Vector headers = new Vector();
+ Hashtable seqhash = new Hashtable();
+ Sequence[] seqs = null;
+ int noSeqs = 0;
+ String line;
+
+ try {
+ BufferedReader ins = new BufferedReader(new InputStreamReader(os));
+ while ((line = ins.readLine()) != null) {
+ if (line.indexOf(" ") != 0) {
+ java.util.StringTokenizer str = new StringTokenizer(line, " ");
+ String id = "";
+
+ if (str.hasMoreTokens()) {
+ id = str.nextToken();
+ if (id.equals("CLUSTAL")) {
+ flag = true;
+ } else {
+ if (flag) {
+ StringBuffer tempseq;
+ if (seqhash.containsKey(id)) {
+ tempseq = (StringBuffer) seqhash.get(id);
+ } else {
+ tempseq = new StringBuffer();
+ seqhash.put(id, tempseq);
+ }
+
+ if (!(headers.contains(id))) {
+ headers.addElement(id);
+ }
+
+ tempseq.append(str.nextToken());
+ }
+ }
+ }
+ }
+ }
+
+ } catch (IOException e) {
+ throw (new Exception("Exception parsing clustal file ", e));
+ }
+
+ if (flag) {
+ noSeqs = headers.size();
+
+ // Add sequences to the hash
+ seqs = new Sequence[headers.size()];
+ for (i = 0; i < headers.size(); i++) {
+ if (seqhash.get(headers.elementAt(i)) != null) {
+ // TODO: develop automatic dictionary typing for sequences
+ Sequence newSeq = Seq.newSequence(headers.elementAt(i).toString(),
+ seqhash.get(headers.elementAt(i).toString()).toString(),
+ SymbolDictionary.STANDARD_AA,0,0);
+
+ seqs[i] = newSeq;
+
+ } else {
+ throw (new Exception("Bizarreness! Can't find sequence for "
+ + headers.elementAt(i)));
+ }
+ }
+ }
+ return seqs;
+ }
+
+ public static void WriteClustalWAlignment(java.io.OutputStream os,
+ Alignment seqAl) throws IOException {
+ System.err.println("NOT FULLY IMPLEMENTED!"); // TODO: Finish adapting this method
+ AlignmentSequence[] s = seqAl.getAlignmentSequence();
+
+ java.io.BufferedWriter out = new BufferedWriter(
+ new java.io.OutputStreamWriter(os));
+
+ out.write("CLUSTAL\n\n");
+
+ int max = 0;
+ int maxid = 0;
+
+ int i = 0;
+
+ while (i < s.length && s[i] != null) {
+ String tmp = s[i].getId();
+
+ if (s[i].getSequence().length() > max) {
+ max = s[i].getSequence().length();
+ }
+ if (tmp.length() > maxid) {
+ maxid = tmp.length();
+ }
+ i++;
+ }
+
+ if (maxid < 15) {
+ maxid = 15;
+ }
+ maxid++;
+ int len = 60;
+ int nochunks = max / len + 1;
+
+ for (i = 0; i < nochunks; i++) {
+ int j = 0;
+ while (j < s.length && s[j] != null) {
+ out.write(new Format("%-" + maxid + "s").form(s[j].getId() + " "));
+ int start = i * len;
+ int end = start + len;
+
+ if (end < s[j].getSequence().length() && start < s[j].getSequence().length()) {
+ out.write(s[j].getSequence().substring(start, end) + "\n");
+ } else {
+ if (start < s[j].getSequence().length()) {
+ out.write(s[j].getSequence().substring(start) + "\n");
+ }
+ }
+ j++;
+ }
+ out.write("\n");
+
+ }
+ }
+ /**
+ * manufacture an alignment/dataset from an array of sequences
+ * @param origin
+ * @param seqs
+ * @return
+ * @throws Exception
+ */
+ public static Alignment make_Alignment(Entry origin,
+ Sequence[] seqs) throws Exception {
+ System.err.println("NOT FULLY IMPLEMENTED!"); // TODO: Finish adapting this method
+ Alignment al = new Alignment();
+ al.setProvenance(ProvenanceStuff.newProvenance(origin));
+
+ Pattern nonGap = Pattern.compile("[A-Z*0-9]", Pattern.CASE_INSENSITIVE);
+ boolean gapsset = false;
+ char gapchar = '-';
+ int seqLength = 0;
+
+ for (int i = 0, nseq = seqs.length; i < nseq; i++) {
+ String seq = seqs[i].getSequence();
+ String gaps = nonGap.matcher(seq).replaceAll("");
+ if (seqLength == 0) {
+ seqLength = seq.length();
+ } else if (seqLength != seq.length())
+ throw (new Exception(i + "th Sequence (>" + seqs[i].getId()
+ + ") is not aligned.\n"));// TODO: move this to assertions part of
+ // Alignment
+
+ // common check for any sequence...
+ if (gaps != null && gaps.length() > 0) {
+ if (!gapsset)
+ gapchar = gaps.charAt(0);
+ for (int c = 0, gc = gaps.length(); c < gc; c++) {
+ if (gapchar != gaps.charAt(c)) {
+ throw (new IOException("Inconsistent gap characters in sequence "
+ + i + ": '" + seq + "'"));
+ }
+ }
+ }
+ AlignmentSequence sq = new AlignmentSequence();
+ // TODO: use as basis of default AlignSequence(Sequence) constructor.
+ sq.setSequence(seq);
+ sq.setName(seqs[i].getId());
+ sq.setRefid(seqs[i].getVorbaId());
+ sq.setStart(seqs[i].getStart());
+ sq.setEnd(seqs[i].getEnd());
+ al.addAlignmentSequence(sq);
+ }
+ al.setGapChar(String.valueOf(gapchar));
+ return al;
+ }
+
+ public static Alignment read_FastaAlignment(InputStream os, Entry entry)
+ throws Exception {
+ Sequence[] seqs;
+ System.err.println("NOT FULLY IMPLEMENTED!"); // TODO: Finish adapting this method
+ try {
+ seqs = SeqSet.read_SeqFasta(os);
+ if (seqs == null)
+ throw (new Exception("Empty alignment stream!\n"));
+ } catch (Exception e) {
+ throw new Exception("Invalid fasta alignment\n", e);
+ }
+
+ return make_Alignment(entry, seqs);
+ }
+
+ public static Alignment read_ClustalAlignment(InputStream os, Entry entry)
+ throws Exception {
+ Sequence[] seqs;
+ try {
+ seqs = SeqAln.ReadClustalFile(os);
+ if (seqs == null)
+ throw (new Exception("Empty alignment stream!\n"));
+ } catch (Exception e) {
+ throw new Exception("Invalid fasta alignment\n", e);
+ }
+ System.err.println("NOT FULLY IMPLEMENTED!"); // TODO: Finish adapting this method
+ return make_Alignment(entry, seqs);
+ }
+}
--- /dev/null
+/*
+ * Created on 17-May-2005
+ * Slurped into VamsasClient object set on 12th Jan 2006
+ *
+ * TODO To change the template for this generated file go to
+ * Window - Preferences - Java - Code Style - Code Templates
+ */
+package uk.ac.vamsas.objects.utils;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.util.Hashtable;
+import java.util.Vector;
+import java.util.regex.Pattern;
+
+import uk.ac.vamsas.objects.core.*;
+
+/**
+ * @author jimp
+ *
+ * TODO To change the template for this generated type comment go to
+ * Window - Preferences - Java - Code Style - Code Templates
+ */
+public class SeqSet {
+
+ public static void write_Fasta(OutputStream os, SequenceType[] seqs) throws IOException {
+ write_Fasta(os, seqs, 80);
+ }
+
+ public static void write_Fasta(OutputStream os, SequenceType[] seqs, boolean width80) throws IOException {
+ write_Fasta(os, seqs, (width80) ? 80 : 0);
+ }
+
+ public static void write_Fasta(OutputStream os, SequenceType[] seqs, int width) throws IOException {
+ int i, nseq = seqs.length;
+ BufferedWriter fasta_out = new BufferedWriter(new OutputStreamWriter(os));
+ System.err.println("NOT FULLY IMPLEMENTED!"); // TODO: Finish adapting this method
+ for (i=0; i<nseq; i++) {
+ fasta_out.write(">"+seqs[i].getName()+"\n");
+ if (width<=0) {
+ fasta_out.write(seqs[i].getSequence()+"\n");
+ } else {
+ // TODO: adapt to SymbolDictionary labelwidths
+ String tempseq = seqs[i].getSequence();
+ int j=0, k=tempseq.length();
+ while (j<k) {
+ int d = k-j;
+ if (d>=width) {
+ fasta_out.write(tempseq, j, width);
+ } else {
+ fasta_out.write(tempseq, j, d);
+ }
+ fasta_out.write("\n");
+ j+=width;
+ }
+ }
+ }
+ fasta_out.flush();
+ }
+ /**
+ * TODO: introduce a dictionary parameter for qualified sequence symbols
+ * Reads a sequence set from a stream - will only read prescribed amino acid
+ * symbols.
+ * @param os
+ * @return
+ * @throws IOException
+ */
+ public static Sequence[] read_SeqFasta(InputStream os) throws IOException {
+ Vector seqs = new Vector();
+ int nseq = 0;
+ BufferedReader infasta = new BufferedReader(new InputStreamReader(os));
+ System.err.println("NOT FULLY IMPLEMENTED!"); // TODO: Finish adapting this method
+ // TODO: decide on return type - SequenceType is a partly complete vamsas Vobject - either for a dataset or alignment sequence
+ // so could go in either!
+ String line;
+ Sequence seq = null;
+ Pattern aaMatch = Pattern.compile("[ARNDCQEGHILKMFPSTUWYV]", Pattern.CASE_INSENSITIVE);
+ String sname = "", seqstr=null;
+ do {
+ line = infasta.readLine();
+ if (line==null || line.startsWith(">")) {
+ if (seqstr!=null)
+ seqs.add((Object) Seq.newSequence(sname.substring(1), seqstr, SymbolDictionary.STANDARD_AA, 0,0));
+ sname = line; // remove >
+ seqstr="";
+ } else {
+ String subseq = Pattern.compile("//s+").matcher(line).replaceAll("");
+ seqstr += subseq;
+ }
+ } while (line!=null);
+ nseq = seqs.size();
+ if (nseq>0) {
+ // TODO:POSS: should really return a sequence if there's only one in the file.
+ Sequence[] seqset = new Sequence[nseq];
+ for (int i=0; i<nseq; i++) {
+ seqset[i] = (Sequence) seqs.elementAt(i);
+ }
+ return seqset;
+ }
+
+ return null;
+ }
+
+ public static Hashtable uniquify(SequenceType[] sequences) {
+ System.err.println("NOT FULLY IMPLEMENTED!"); // TODO: Finish adapting this method
+ // TODO: do we need this with vamsas sequences ?
+ // Generate a safely named sequence set and a hash to recover the sequence names
+ Hashtable map = new Hashtable();
+ for (int i = 0; i < sequences.length; i++) {
+ String safename = new String("Sequence" + i);
+ map.put(safename, sequences[i].getName());
+ sequences[i].setName(safename);
+ }
+ return map;
+ }
+
+ public static boolean deuniquify(Hashtable map, SequenceType[] sequences) {
+ System.err.println("NOT FULLY IMPLEMENTED!"); // TODO: Finish adapting this method
+ // TODO: do we need this with vamsas sequences ?
+ // recover unsafe sequence names for a sequence set
+ boolean allfound = true;
+ for (int i = 0; i < sequences.length; i++) {
+ if (map.containsKey(sequences[i].getName())) {
+ String unsafename = (String) map.get(sequences[i].getName());
+ sequences[i].setName(unsafename);
+ } else {
+ allfound = false;
+ }
+ }
+ return allfound;
+ }
+
+}
--- /dev/null
+package uk.ac.vamsas.objects.utils;
+
+public class SymbolDictionary {
+ /**
+ * defines standard names and properties for vamsas sequence dictionaries
+ */
+ static final public String STANDARD_AA="info:iubmb.org/aminoacids"; // strict 1 letter code
+ static final public String STANDARD_NA="info:iubmb.org/nucleosides";// strict 1 letter code (do not allow arbitrary rare nucleosides)
+ /**
+ * TODO: Vamsas Dictionary properties interface
+ * an interface for dictionary provides :
+ * validation for a string
+ * symbolwidth (or symbol next/previous)
+ * mappings to certain other dictionaries (one2three, etc)
+ * gap-character test
+ *
+ */
+
+}
--- /dev/null
+/**
+ *
+ */
+package uk.ac.vamsas.objects.utils.document;
+
+import java.util.Hashtable;
+
+/**
+ * enumerates versions for the VamsasDocument.Version string
+ * provides version comparison methods
+ * TODO: LATER: associate schema versions with these strings
+ */
+public class VersionEntries {
+ public static final String ALPHA_VERSION="alpha";
+ public static final String BETA_VERSION="beta";
+ protected static Hashtable versions;
+ static {
+ versions = new Hashtable();
+ // integers represent version hierarchy - 0 precedes 1
+ versions.put(ALPHA_VERSION, new Integer(0));
+ versions.put(BETA_VERSION, new Integer(1));
+ }
+ // TODO: LATER: decide on best pattern for enumeration classes (ie - need an ordered list of versions, and validator, plus explicit enum-strings)
+ public static boolean isVersion(String vstring) {
+ return versions.containsKey(vstring);
+ }
+ /**
+ * returns 0 if levels are equivalent,
+ * 1 if higher is valid and higher,
+ * 2 if lower is valid and higher
+ * -1 if both levels are invalid
+ * @param higher
+ * @param lower
+ * @return
+ */
+ public static int compare(String higher, String lower) {
+ int v_1 = versions.containsKey(higher) ? ((Integer) versions.get(higher)).intValue() : -1;
+ int v_2 = versions.containsKey(lower) ? ((Integer) versions.get(lower)).intValue() : -1;
+ int comp = v_1<v_2 ? 2 : v_1 == v_2 ? 0 : 1;
+ return (comp==0) ? (v_1!=-1 ? 0 : -1) : comp;
+ }
+ /**
+ * @return the latest version that this vamsas library supports
+ */
+ public static String latestVersion() {
+ return BETA_VERSION;
+ }
+
+}