001    package org.jaga.individualRepresentation.proteinLocation;
002    
003    import java.util.*;
004    import java.io.*;
005    
006    /**
007     * TODO: Complete these comments.
008     *
009     * <p><u>Project:</u> JAGA - Java API for Genetic Algorithms.</p>
010     *
011     * <p><u>Company:</u> University College London and JAGA.Org
012     *    (<a href="http://www.jaga.org" target="_blank">http://www.jaga.org</a>).
013     * </p>
014     *
015     * <p><u>Copyright:</u> (c) 2004 by G. Paperin.<br/>
016     *    This program is free software; you can redistribute it and/or modify
017     *    it under the terms of the GNU General Public License as published by
018     *    the Free Software Foundation, ONLY if you include a note of the original
019     *    author(s) in any redistributed/modified copy.<br/>
020     *    This program is distributed in the hope that it will be useful,
021     *    but WITHOUT ANY WARRANTY; without even the implied warranty of
022     *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
023     *    GNU General Public License for more details.<br/>
024     *    You should have received a copy of the GNU General Public License
025     *    along with this program; if not, write to the Free Software
026     *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
027     *    or see http://www.gnu.org/licenses/gpl.html</p>
028     *
029     * @author Greg Paperin (greg@jaga.org)
030     *
031     * @version JAGA public release 1.0 beta
032     */
033    
034    public class Blosum62 extends SubstitutionScoringMatrix {
035    
036            private static final String dataFile = "D:\\Courseworks\\4C58\\cw\\data\\blosum62.dat";
037            private HashMap logScores = null;
038            private HashMap actualScores = null;
039    
040            public Blosum62() {
041                    logScores = new HashMap(21);
042                    actualScores = new HashMap(21);
043                    BufferedReader in = null;
044                    try {
045                            in = new BufferedReader(new InputStreamReader(new FileInputStream(dataFile)));
046                    } catch (IOException e) {
047                            throw new RuntimeException(e.getMessage());
048                    }
049                    try {
050                            try {
051                                    // Read headline:
052                                    String head = in.readLine();
053                                    StringTokenizer tok = new StringTokenizer(head);
054                                    String [] names = new String[25];
055                                    int i = 0;
056                                    while (tok.hasMoreTokens()) {
057                                            String name = tok.nextToken();
058                                            names[i++] = name.trim();
059                                    }
060                                    // Read data lines:
061                                    i = 0;
062                                    String line = in.readLine();
063                                    while (null != line) {
064                                            //System.out.println("LINE " + i + ": " + line + " / " + names[i]);
065                                            if (names[i].equalsIgnoreCase("B")         // dont know what these
066                                                            || names[i].equalsIgnoreCase("Z")  // codes stand for.
067                                                            || names[i].equalsIgnoreCase("X")) {
068                                                    i++;
069                                                    line = in.readLine();
070                                                    continue;
071                                            }
072                                            AminoAcid aminoAcid = null;
073                                            if (!names[i].equalsIgnoreCase("*"))
074                                                    aminoAcid = AminoAcidFactory.getResidueByCode(names[i]);
075                                            tok = new StringTokenizer(line);
076                                            HashMap log = new HashMap(21);
077                                            HashMap act = new HashMap(21);
078                                            int j = 0;
079                                            while (tok.hasMoreTokens()) {
080                                                    String num = tok.nextToken();
081                                                    //System.out.println(j + " / " + num);
082                                                    if (names[j].equalsIgnoreCase("B")         // dont know what these
083                                                                    || names[j].equalsIgnoreCase("Z")  // codes stand for.
084                                                                    || names[j].equalsIgnoreCase("X")) {
085                                                            j++;
086                                                            continue;
087                                                    }
088                                                    AminoAcid aa = null;
089                                                    if (!names[j].equalsIgnoreCase("*"))
090                                                            aa = AminoAcidFactory.getResidueByCode(names[j]);
091                                                    int p = Integer.parseInt(num);
092                                                    log.put(aa, new Integer(p));
093                                                    act.put(aa, new Double(Math.pow(2, p)));
094                                                    j++;
095                                            }
096                                            logScores.put(aminoAcid, log);
097                                            actualScores.put(aminoAcid, act);
098                                            ++i;
099                                            line = in.readLine();
100                                    }
101                            } finally {
102                                    in.close();
103                            }
104                    } catch (IOException e) {
105                            throw new RuntimeException(e.getMessage());
106                    }
107            }
108    
109            public int getLogScore(AminoAcid aminoAcid1, AminoAcid aminoAcid2) {
110                    HashMap map = (HashMap) logScores.get(aminoAcid1);
111                    Integer val = (Integer) map.get(aminoAcid2);
112                    return val.intValue();
113            }
114    
115            public double getActualScore(AminoAcid aminoAcid1, AminoAcid aminoAcid2) {
116                    return ((Double) ((HashMap) actualScores.get(aminoAcid1)).get(aminoAcid2)).doubleValue();
117            }
118    
119            /* TEST: */
120            public static void main(String[] args) {
121                    Blosum62 m = new Blosum62();
122                    String aas = "ARNDCQEGHILKMFPSTWYV*";
123                    System.out.println();
124                    for (int i = 0; i < 21; i++) {
125                            System.out.print(aas.substring(i, i + 1));
126                            System.out.print("\t");
127                    }
128                    System.out.println();
129                    for (int i = 0; i < 20; i++) {
130                            for (int j = 0; j < 20; j++) {
131                                    System.out.print(m.getLogScore(aas.substring(i, i + 1), aas.substring(j, j + 1)));
132                                    System.out.print("\t");
133                            }
134                            System.out.print(m.getLogScore(aas.substring(i, i + 1), null));
135                            System.out.print("\t");
136                            System.out.println();
137                    }
138                    for (int j = 0; j < 20; j++) {
139                            System.out.print(m.getLogScore(null, aas.substring(j, j + 1)));
140                            System.out.print("\t");
141                    }
142                    System.out.print(m.getLogScore((String) null, (String) null));
143                    System.out.print("\t");
144                    System.out.println();
145            }
146    
147    }