001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.bio.program.blast2html;
022
023import java.io.FileInputStream;
024import java.util.Iterator;
025import java.util.Map;
026import java.util.Properties;
027
028/**
029 * Simple implementation for specifying markup styles.
030 * Has 3 modes of operation: SHOW_ALL, SHOW_SAME & SHOW_DIFF.<p>
031 *
032 * SHOW_ALL  - returns the default style for all given residues.
033 * SHOW_SAME - only returns a markup style if the <B>styles</B> for both
034 *             characters are the same.
035 * SHOW_DIFF - only returns a markup style if the <B>styles</B> for both
036 *             are different.
037 *
038 * Styles can be easily defined in two ways.<BR>
039 * 
040 * 1. Add each style by calling <CODE>addStyle( poChar, poColour )</CODE>
041 *    For example,
042 * <CODE>
043 *      String oRed = "FFA2A2";
044 *      oStyler.addStyle( "-", oRed );
045 *      oStyler.addStyle( "N", oRed );
046 *      oStyler.addStyle( "A", oRed );
047 *      oStyler.addStyle( "T", oRed );
048 *      oStyler.addStyle( "C", oRed );
049 *      oStyler.addStyle( "G", oRed );
050 * </CODE><p>
051 *
052 * 2. Alternatively the styles could be specified in a java properties file
053 *    and loaded by calling <CODE>readColourMapFromProperties( poFilename )</CODE>,
054 *    or <CODE>readColourMap()</CODE> and setting the system property 'colourMap'
055 *    to the correct filename. <BR>
056 *
057 *    This file should be in java properties format, mapping 
058 *    characters to colours, specified in HEX RGB.
059 * 
060 * For example:
061 * <PRE>
062 * # set everything red
063 * - = FFA2A2
064 * N = FFA2A2
065 * A = FFA2A2
066 * T = FFA2A2
067 * C = FFA2A2
068 * G = FFA2A2
069 * </PRE> 
070 *
071 * Note this is simply character based, so if you want to colour gaps then
072 * you need to specify a colour for the gap character.<p>
073 *
074 * If no colour is specified for a character then it is uncoloured.
075 *
076 * Primary author -
077 *                 Colin Hardman      (CAT)
078 * Other authors  -
079 *                 Tim Dilks          (CAT)
080 *                 Simon Brocklehurst (CAT)
081 *                 Stuart Johnston    (CAT)
082 *                 Lawerence Bower    (CAT)
083 *                 Derek Crockford    (CAT)
084 *                 Neil Benn          (CAT)
085 *
086 * Copyright 2001 Cambridge Antibody Technology Group plc.
087 *
088 * This code released to the biojava project, May 2001
089 * under the LGPL license.
090 *
091 * @author Cambridge Antibody Technology Group plc
092 * @version 1.0
093 *
094 */
095public class SimpleAlignmentStyler extends AbstractAlignmentStyler {
096
097    /**
098     * Return default styles
099     */
100    public static int SHOW_ALL = 0;
101    /**
102     * Only return if the two colour classes for
103     * query and subject are the same
104     */
105    public static int SHOW_SAME = 1;
106    /**
107     * As NORMAL except only return if the two colour classes for
108     * query and subject are the different
109     */
110    public static int SHOW_DIFF = 2;
111
112    private int iStyle = 0;
113
114    /**
115     * Creates a new <CODE>SimpleAlignmentStyler</CODE> instance.<p>
116     *
117     * The int flag should be one of SimpleAlignmentStyler.SHOW_ALL,
118     * SimpleAlignmentStyler.SHOW_SAME or
119     * SimpleAlignmentStyler.SHOW_DIFF.
120     *
121     * @param piStyle (one of SimpleAlignmentStyler.SHOW_SAME or SimpleAlignmentStyler.SHOW_DIFF).
122     * @throws IllegalArgumentException - if style not one of allowed values
123     */
124    public SimpleAlignmentStyler( int piStyle ) {
125
126        if ( piStyle != SHOW_DIFF &&  piStyle != SHOW_ALL &&
127             piStyle != SHOW_SAME ) {
128            throw new IllegalArgumentException
129                ( "Style flag not one of SimpleAlignmentStyler.SHOW_DIFF, " +
130                  " SHOW_ALL or SHOW_SAME" );
131        }
132        iStyle = piStyle;
133    }
134
135    /**
136     * Setup styles from java property file.
137     *
138     * @param poFileName - the file name of the property file.
139     */
140    protected void readColourMapFromProperties( String poFileName ) {
141
142        // load in properties
143        Properties oColourProps = new Properties();
144        
145        try{
146            FileInputStream fis = new FileInputStream( poFileName );
147            oColourProps.load(fis);
148            fis.close();
149        } catch (java.lang.Exception e) {
150        
151            System.out.println("Failed to read properties file: " +
152                               poFileName );
153            System.out.println( e.getMessage() );
154            e.printStackTrace();
155            return;
156        }
157
158            for (Iterator i=oColourProps.entrySet().iterator(); 
159                 i.hasNext(); ) {
160                Map.Entry e = (Map.Entry) i.next();
161
162                String oColourClass  = this.getColourClass
163                    ( (String)e.getValue() );
164
165                oColourMap.put( e.getKey(), oColourClass );
166            }
167    }
168
169    /**
170     * Read the the properties file that specifies the character/colour mapping.
171     * The location of the property file is specified by the system property
172     * 'colourMap'.
173     *
174     */
175    protected void readColourMap() {
176
177        String oPropFileName = System.getProperty("colourMap");
178    
179        if( oPropFileName == null ) {
180            System.err.println
181                ("No ColourMap preference file specified " +
182                 "with -DcolourMap=<filename>" );
183
184        } else {
185            this.readColourMapFromProperties( oPropFileName );
186        }
187    }
188
189    /**
190     * Returns the styles for the two aligned characters in the form
191     * of predefined font classes.<p>
192     *
193     * Null is acceptable value for no style.
194     *
195     * @param poFirst - the first char in the alignment
196     * @param poSecond - the second char in the alignment
197     * @param poStyleHolder - an array to hold the styles, [0] = first etc
198     */
199    public void getStyle( String poFirst, String poSecond,
200                          String[] poStyleHolder ) {
201
202        poStyleHolder[0] = (String)oColourMap.get( poFirst  );
203        poStyleHolder[1] = (String)oColourMap.get( poSecond );
204
205        if ( iStyle == SimpleAlignmentStyler.SHOW_SAME ) {
206            
207            if ( poStyleHolder[0] != poStyleHolder[1] ) {
208                poStyleHolder[0] = null;
209                poStyleHolder[1] = null;
210            }
211        } else if ( iStyle == SimpleAlignmentStyler.SHOW_DIFF) {
212
213            if ( !(poStyleHolder[0] != poStyleHolder[1]) ) {
214                poStyleHolder[0] = null;
215                poStyleHolder[1] = null;
216            }
217        } 
218    }
219
220} // end class