From 6750fadacf73cce0289645dbc387be9331016b1e Mon Sep 17 00:00:00 2001 From: jprocter Date: Mon, 21 Apr 2008 17:34:41 +0000 Subject: [PATCH] documented and refactored to facilitate headless feature file generation --- src/jalview/io/FeaturesFile.java | 232 ++++++++++++++++++++------------------ 1 file changed, 121 insertions(+), 111 deletions(-) diff --git a/src/jalview/io/FeaturesFile.java b/src/jalview/io/FeaturesFile.java index e961683..03a7a83 100755 --- a/src/jalview/io/FeaturesFile.java +++ b/src/jalview/io/FeaturesFile.java @@ -25,15 +25,15 @@ import jalview.datamodel.*; import jalview.schemes.*; /** - * Parse and create Jalview Features files - * Detects GFF format features files and parses. - * Does not implement standard print() - call specific printFeatures or printGFF. - * Uses AlignmentI.findSequence(String id) to find the sequence object for the features annotation - this normally works on an exact match. + * Parse and create Jalview Features files Detects GFF format features files and + * parses. Does not implement standard print() - call specific printFeatures or + * printGFF. Uses AlignmentI.findSequence(String id) to find the sequence object + * for the features annotation - this normally works on an exact match. + * * @author AMW * @version $Revision$ */ -public class FeaturesFile - extends AlignFile +public class FeaturesFile extends AlignFile { /** * Creates a new FeaturesFile object. @@ -44,47 +44,47 @@ public class FeaturesFile /** * Creates a new FeaturesFile object. - * - * @param inFile DOCUMENT ME! - * @param type DOCUMENT ME! - * - * @throws IOException DOCUMENT ME! + * + * @param inFile + * DOCUMENT ME! + * @param type + * DOCUMENT ME! + * + * @throws IOException + * DOCUMENT ME! */ - public FeaturesFile(String inFile, String type) - throws IOException + public FeaturesFile(String inFile, String type) throws IOException { super(inFile, type); } + public FeaturesFile(FileParse source) throws IOException { super(source); } /** - * The Application can render HTML, but the applet will - * remove HTML tags and replace links with %LINK% - * Both need to read links in HTML however - * - * @throws IOException DOCUMENT ME! + * The Application can render HTML, but the applet will remove HTML tags and + * replace links with %LINK% Both need to read links in HTML however + * + * @throws IOException + * DOCUMENT ME! */ - public boolean parse(AlignmentI align, - Hashtable colours, - boolean removeHTML) + public boolean parse(AlignmentI align, Hashtable colours, + boolean removeHTML) { return parse(align, colours, null, removeHTML); } /** - * The Application can render HTML, but the applet will - * remove HTML tags and replace links with %LINK% - * Both need to read links in HTML however - * - * @throws IOException DOCUMENT ME! + * The Application can render HTML, but the applet will remove HTML tags and + * replace links with %LINK% Both need to read links in HTML however + * + * @throws IOException + * DOCUMENT ME! */ - public boolean parse(AlignmentI align, - Hashtable colours, - Hashtable featureLink, - boolean removeHTML) + public boolean parse(AlignmentI align, Hashtable colours, + Hashtable featureLink, boolean removeHTML) { String line = null; try @@ -101,7 +101,7 @@ public class FeaturesFile boolean GFFFile = true; - while ( (line = nextLine()) != null) + while ((line = nextLine()) != null) { if (line.startsWith("#")) { @@ -124,8 +124,8 @@ public class FeaturesFile } else if (type.equalsIgnoreCase("endgroup")) { - //We should check whether this is the current group, - //but at present theres no way of showing more than 1 group + // We should check whether this is the current group, + // but at present theres no way of showing more than 1 group st.nextToken(); featureGroup = null; groupLink = null; @@ -162,24 +162,24 @@ public class FeaturesFile { desc = st.nextToken(); type = st.nextToken(); - try { - start = Integer.parseInt(st.nextToken()); + try + { + start = Integer.parseInt(st.nextToken()); } catch (NumberFormatException ex) { - start=0; + start = 0; } - try { + try + { end = Integer.parseInt(st.nextToken()); - } - catch (NumberFormatException ex) + } catch (NumberFormatException ex) { - end=-1; - } + end = -1; + } try { score = new Float(st.nextToken()).floatValue(); - } - catch (NumberFormatException ex) + } catch (NumberFormatException ex) { score = 0; } @@ -190,9 +190,9 @@ public class FeaturesFile { sf.setValue("STRAND", st.nextToken()); sf.setValue("FRAME", st.nextToken()); + } catch (Exception ex) + { } - catch (Exception ex) - {} if (st.hasMoreTokens()) { @@ -220,13 +220,15 @@ public class FeaturesFile } if (!st.hasMoreTokens()) { - System.err.println("DEBUG: Run out of tokens when trying to identify the destination for the feature.. giving up."); - // in all probability, this isn't a file we understand, so bail quietly. + System.err + .println("DEBUG: Run out of tokens when trying to identify the destination for the feature.. giving up."); + // in all probability, this isn't a file we understand, so bail + // quietly. return false; } - + token = st.nextToken(); - + if (!token.equals("ID_NOT_SPECIFIED")) { seq = align.findName(token, true); @@ -238,8 +240,7 @@ public class FeaturesFile { index = Integer.parseInt(st.nextToken()); seq = align.getSequenceAt(index); - } - catch (NumberFormatException ex) + } catch (NumberFormatException ex) { seq = null; } @@ -280,12 +281,11 @@ public class FeaturesFile parseDescriptionHTML(sf, removeHTML); - //If we got here, its not a GFFFile + // If we got here, its not a GFFFile GFFFile = false; } } - } - catch (Exception ex) + } catch (Exception ex) { System.out.println(line); System.out.println("Error parsing feature file: " + ex + "\n" + line); @@ -303,7 +303,8 @@ public class FeaturesFile return; } - if (removeHTML && sf.getDescription().toUpperCase().indexOf("") == -1) + if (removeHTML + && sf.getDescription().toUpperCase().indexOf("") == -1) { removeHTML = false; } @@ -370,41 +371,51 @@ public class FeaturesFile } +/** + * generate a features file for seqs + * @param seqs source of sequence features + * @param visible hash of feature types and colours + * @return features file contents + */ + public String printJalviewFormat(SequenceI[] seqs, Hashtable visible) + { + return printJalviewFormat(seqs, visible, true); + } + /** - * DOCUMENT ME! - * - * @param s DOCUMENT ME! - * @param len DOCUMENT ME! - * @param gaps DOCUMENT ME! - * @param displayId DOCUMENT ME! - * - * @return DOCUMENT ME! + * generate a features file for seqs with colours from visible (if any) + * @param seqs source of features + * @param visible hash of Colours for each feature type + * @param visOnly when true only feature types in 'visible' will be output + * @return features file contents */ - public String printJalviewFormat(SequenceI[] seqs, - Hashtable visible) + public String printJalviewFormat(SequenceI[] seqs, Hashtable visible, + boolean visOnly) { StringBuffer out = new StringBuffer(); SequenceFeature[] next; - if (visible == null || visible.size() < 1) + if (visOnly && (visible == null || visible.size() < 1)) { return "No Features Visible"; } - - Enumeration en = visible.keys(); - String type; - int color; - while (en.hasMoreElements()) + if (visible != null && visOnly) { - type = en.nextElement().toString(); - color = Integer.parseInt(visible.get(type).toString()); - out.append(type + "\t" - + jalview.util.Format.getHexString( - new java.awt.Color(color)) - + "\n"); + // write feature colours only if we're given them and we are generating viewed features + Enumeration en = visible.keys(); + String type; + int color; + while (en.hasMoreElements()) + { + type = en.nextElement().toString(); + color = Integer.parseInt(visible.get(type).toString()); + out.append(type + + "\t" + + jalview.util.Format + .getHexString(new java.awt.Color(color)) + "\n"); + } } - - //Work out which groups are both present and visible + // Work out which groups are both present and visible Vector groups = new Vector(); int groupIndex = 0; @@ -415,13 +426,13 @@ public class FeaturesFile { for (int j = 0; j < next.length; j++) { - if (!visible.containsKey(next[j].type)) + if (visOnly && !visible.containsKey(next[j].type)) { continue; } if (next[j].featureGroup != null - && !groups.contains(next[j].featureGroup)) + && !groups.contains(next[j].featureGroup)) { groups.addElement(next[j].featureGroup); } @@ -451,15 +462,14 @@ public class FeaturesFile { for (int j = 0; j < next.length; j++) { - if (!visible.containsKey(next[j].type)) + if (visOnly && !visible.containsKey(next[j].type)) { continue; } if (group != null - && (next[j].featureGroup == null - || !next[j].featureGroup.equals(group)) - ) + && (next[j].featureGroup == null || !next[j].featureGroup + .equals(group))) { continue; } @@ -469,14 +479,15 @@ public class FeaturesFile continue; } - if (next[j].description == null || next[j].description.equals("")) + if (next[j].description == null + || next[j].description.equals("")) { out.append(next[j].type + "\t"); } else { if (next[j].links != null - && next[j].getDescription().indexOf("") == -1) + && next[j].getDescription().indexOf("") == -1) { out.append(""); } @@ -492,11 +503,9 @@ public class FeaturesFile if (next[j].description.indexOf(href) == -1) { - out.append("" - + label - + ""); + out + .append("" + label + + ""); } } @@ -509,11 +518,8 @@ public class FeaturesFile out.append("\t"); } - out.append(seqs[i].getName() + "\t-1\t" - + next[j].begin + "\t" - + next[j].end + "\t" - + next[j].type + "\n" - ); + out.append(seqs[i].getName() + "\t-1\t" + next[j].begin + "\t" + + next[j].end + "\t" + next[j].type + "\n"); } } } @@ -528,14 +534,19 @@ public class FeaturesFile break; } - } - while (groupIndex < groups.size() + 1); + } while (groupIndex < groups.size() + 1); return out.toString(); } public String printGFFFormat(SequenceI[] seqs, Hashtable visible) { + return printGFFFormat(seqs, visible, true); + } + + public String printGFFFormat(SequenceI[] seqs, Hashtable visible, + boolean visOnly) + { StringBuffer out = new StringBuffer(); SequenceFeature[] next; String source; @@ -547,7 +558,7 @@ public class FeaturesFile next = seqs[i].getSequenceFeatures(); for (int j = 0; j < next.length; j++) { - if (!visible.containsKey(next[j].type)) + if (visOnly && !visible.containsKey(next[j].type)) { continue; } @@ -558,13 +569,9 @@ public class FeaturesFile source = next[j].getDescription(); } - out.append(seqs[i].getName() + "\t" - + source + "\t" - + next[j].type + "\t" - + next[j].begin + "\t" - + next[j].end + "\t" - + next[j].score + "\t" - ); + out.append(seqs[i].getName() + "\t" + source + "\t" + + next[j].type + "\t" + next[j].begin + "\t" + + next[j].end + "\t" + next[j].score + "\t"); if (next[j].getValue("STRAND") != null) { @@ -598,15 +605,18 @@ public class FeaturesFile return out.toString(); } + /** + * this is only for the benefit of object polymorphism - method does nothing. + */ public void parse() { - //IGNORED + // IGNORED } /** - * DOCUMENT ME! - * - * @return DOCUMENT ME! + * this is only for the benefit of object polymorphism - method does nothing. + * + * @return error message */ public String print() { -- 1.7.10.2