001/*
002 *  @(#)ModelLoader.java        1.0 June 2010
003 *
004 *  Copyright (c) 2010 Peter Troshin
005 *
006 *  JRONN version: 3.1
007 *
008 *        BioJava development code
009 *
010 * This code may be freely distributed and modified under the
011 * terms of the GNU Lesser General Public Licence.  This should
012 * be distributed with the code.  If you do not have a copy,
013 * see:
014 *
015 *      http://www.gnu.org/copyleft/lesser.html
016 *
017 * Copyright for this code is held jointly by the individual
018 * authors.  These should be listed in @author doc comments.
019 *
020 * For more information on the BioJava project and its aims,
021 * or to join the biojava-l mailing list, visit the home page
022 * at:
023 *
024 *      http://www.biojava.org/
025 *
026 */
027
028package org.biojava.nbio.ronn;
029
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033import java.io.BufferedReader;
034import java.io.IOException;
035import java.io.InputStreamReader;
036import java.util.Arrays;
037import java.util.HashMap;
038import java.util.Map;
039
040/**
041 * Class that loads data from the model files into {@link Model} objects
042 *
043 * @author Peter Troshin
044 * @version 1.0
045 * @since 3.0.2
046
047 */
048public final class ModelLoader {
049
050        private static final Logger logger = LoggerFactory.getLogger(ModelLoader.class);
051
052        /**
053         * Represents a Threshold
054         *
055         */
056        public final static class Threshold {
057
058        final float mu0;
059        final float mu1;
060        final float sigma0;
061        final float sigma1;
062
063        public Threshold(final int modelNum) {
064                final float[] values = RonnConstraint.Threshold
065                        .getTreshold(modelNum);
066                mu0 = values[0];
067                mu1 = values[1];
068                sigma0 = values[2];
069                sigma1 = values[3];
070        }
071
072        }
073
074        /**
075         * Represent a RONN model
076         *
077         */
078        public static class Model {
079
080        /**
081         * Stores encoded sequences from the model similar to seqAA
082         *
083         * 190 is a maximum length of the sequence in the model
084         */
085        final short[][] dbAA;// = new short[RonnConstraint.maxD][190];
086        /**
087         * This array contains the length of each sequence from the model file
088         * The length of this array vary with the number of sequences in the
089         * mode
090         */
091        final short[] Length;// = new int[RonnConstraint.maxD];
092        /**
093         * Holds the values from the second column of model file
094         */
095        final float[] W;// = new float[RonnConstraint.maxD];
096
097        int numOfDBAAseq;
098        int modelNum = -1;
099
100        public Model(final int modelNum, final int numberofSequence) {
101                this.modelNum = modelNum;
102                numOfDBAAseq = numberofSequence;
103                dbAA = new short[numberofSequence][190];
104                Length = new short[numberofSequence];
105                W = new float[numberofSequence];
106        }
107
108        @Override
109        public int hashCode() {
110                final int prime = 31;
111                int result = 1;
112                result = prime * result + Arrays.hashCode(Length);
113                result = prime * result + Arrays.hashCode(W);
114                result = prime * result + Arrays.hashCode(dbAA);
115                result = prime * result + modelNum;
116                result = prime * result + numOfDBAAseq;
117                return result;
118        }
119
120        @Override
121        public boolean equals(final Object obj) {
122                if (this == obj) {
123                return true;
124                }
125                if (obj == null) {
126                return false;
127                }
128                if (getClass() != obj.getClass()) {
129                return false;
130                }
131                final Model other = (Model) obj;
132                if (!Arrays.equals(Length, other.Length)) {
133                return false;
134                }
135                if (!Arrays.equals(W, other.W)) {
136                return false;
137                }
138                if (!Arrays.equals(dbAA, other.dbAA)) {
139                return false;
140                }
141                if (modelNum != other.modelNum) {
142                return false;
143                }
144                if (numOfDBAAseq != other.numOfDBAAseq) {
145                return false;
146                }
147                return true;
148        }
149
150        @Override
151        public String toString() {
152                return "Model [modelNum=" + modelNum + ", numOfDBAAseq="
153                        + numOfDBAAseq + "]";
154        }
155
156        }
157
158        private static final Map<Integer, Model> models = new HashMap<Integer, Model>();
159
160        public Model getModel(final int modelNum) {
161        return ModelLoader.models.get(modelNum);
162        }
163
164        void loadModels() throws NumberFormatException, IOException {
165
166                for (int i = 0; i < 10; i++) {
167                        final BufferedReader bfr = new BufferedReader(
168                                        new InputStreamReader(ModelLoader.class.getResourceAsStream(
169                                                        "model" + i + ".rec"),
170                                                        "ISO-8859-1"));
171                        String line = null;
172                        line = bfr.readLine().trim();
173                        final int numberOfSeqs = Integer.parseInt(line);
174                        final Model model = new Model(i, numberOfSeqs);
175                        // ignore this one, its always 19 defined in RonnConstrain
176                        line = bfr.readLine();
177                        for (int j = 0; j < numberOfSeqs; j++) {
178                                line = bfr.readLine();
179                                final char[] dbseq = line.trim().toCharArray();
180                                assert dbseq.length < Short.MAX_VALUE;
181                                model.Length[j] = (short) dbseq.length;
182                                for (int dResidue = 0; dResidue < dbseq.length; dResidue++) {
183                                        model.dbAA[j][dResidue] = RonnConstraint.INDEX[dbseq[dResidue] - 'A'];
184                                        assert !((model.dbAA[j][dResidue] < 0) || (model.dbAA[j][dResidue] > 19));
185                                }
186                                line = bfr.readLine().trim();
187                                model.W[j] = Float.parseFloat(line);
188                        }
189                        ModelLoader.models.put(model.modelNum, model);
190                        bfr.close();
191                }
192        }
193
194        public static void main(final String[] args) throws NumberFormatException,
195                IOException {
196        final ModelLoader loader = new ModelLoader();
197        loader.loadModels();
198        logger.info("{}", ModelLoader.models.get(0));
199        logger.info("{}", ModelLoader.models.get(9));
200        logger.info("{}", ModelLoader.models.size());
201        }
202}