/* * Jalview - A Sequence Alignment Editor and Viewer * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ package jalview.io; import java.io.*; import jalview.datamodel.*; import java.util.*; import jalview.schemes.UserColourScheme; import java.net.URL; public class AnnotationFile { public String printAnnotations(AlignmentAnnotation [] annotations) { StringBuffer text = new StringBuffer( "JALVIEW_ANNOTATION\n" +"# Created: " +new java.util.Date()+"\n\n"); AlignmentAnnotation row; String comma; String seqref = null; StringBuffer colours = new StringBuffer(); StringBuffer graphLine = new StringBuffer(); Hashtable graphGroup = new Hashtable(); java.awt.Color color; for(int i=0; i-1) { String key = String.valueOf(row.graphGroup); if(graphGroup.containsKey(key)) graphGroup.put(key, graphGroup.get(key) +"\t"+row.label); else graphGroup.put(key, row.label); } } text.append(row.label+"\t"); for(int j=0; j 0 && !row.annotations[j].displayCharacter.equals(" ")) { text.append(comma + row.annotations[j].displayCharacter); comma = ","; } if (row.annotations[j].value!=0f) { color = row.annotations[j].colour; text.append(comma + row.annotations[j].value); } } text.append("|"); } text.append("\n"); if(color!=null && color!=java.awt.Color.black) { colours.append("COLOUR\t" +row.label+"\t" +jalview.util.Format.getHexString(color)+"\n"); } } text.append("\n"); text.append(colours.toString()); text.append(graphLine.toString()); if(graphGroup.size()>0) { text.append("COMBINE\t"); Enumeration en = graphGroup.elements(); while(en.hasMoreElements()) { text.append(en.nextElement()+"\n"); } } return text.toString(); } public boolean readAnnotationFile(AlignmentI al, String file) { try { BufferedReader in = null; java.io.InputStream is = getClass().getResourceAsStream("/" + file); if (is != null) { in = new BufferedReader(new java.io.InputStreamReader(is)); } else { try { URL url = new URL(file); in = new BufferedReader(new InputStreamReader(url.openStream())); } catch (java.net.MalformedURLException ex) { in = new BufferedReader(new FileReader(file)); } } String line, label, description, token; int graphStyle, index; SequenceI refSeq = null; int refSeqIndex = 1; int existingAnnotations = 0; if(al.getAlignmentAnnotation()!=null) existingAnnotations = al.getAlignmentAnnotation().length; int alWidth = al.getWidth(); StringTokenizer st; Annotation[] annotations; AlignmentAnnotation annotation = null; // First confirm this is an Annotation file boolean jvAnnotationFile = false; while ( (line = in.readLine()) != null) { if (line.indexOf("#") == 0 ) continue; if (line.indexOf("JALVIEW_ANNOTATION") > -1) { jvAnnotationFile = true; break; } } if(!jvAnnotationFile) { in.close(); return false; } while ( (line = in.readLine()) != null) { if(line.indexOf("#")==0 || line.indexOf("JALVIEW_ANNOTATION")>-1 || line.length()==0) continue; st = new StringTokenizer(line, "\t"); token = st.nextToken(); if(token.equalsIgnoreCase("COLOUR")) { colourAnnotations(al, st.nextToken(), st.nextToken()); continue; } if(token.equalsIgnoreCase("COMBINE") ) { combineAnnotations(al, st); continue; } if (token.equalsIgnoreCase("GRAPHLINE")) { addLine(al, st); continue; } if(token.equalsIgnoreCase("SEQUENCE_REF") ) { refSeq = al.findName(st.nextToken()); try{ refSeqIndex = Integer.parseInt(st.nextToken()); } catch(Exception ex) { refSeqIndex = 1; } continue; } graphStyle = AlignmentAnnotation.getGraphValueFromString(token); label = description = st.nextToken(); line = st.nextToken(); st = new StringTokenizer(line, "|", true); annotations = new Annotation[alWidth]; index = 0; boolean emptyColumn = true; while (st.hasMoreElements() && index