    001    package calhoun.analysis.crf.features.interval13;
    002
    003    import org.apache.commons.logging.Log;
    004    import org.apache.commons.logging.LogFactory;
    005
    006    import calhoun.analysis.crf.ModelManager;
    007    import calhoun.util.Assert;
    008
    009    public class Interval13Tools {
    010            private static final Log log = LogFactory.getLog(Interval13Tools.class);
    011
    012            static protected enum Constraint {NONE, NEVER, PSTART, PDON, PACC, PSTOP, MSTART, MDON, MACC, MSTOP, PCODE, MCODE};
    013            static protected Constraint[] edgeConstraints;
    014            static protected Constraint[] nodeConstraints;
    015            static int numStates;
    016
    017            static {
    018                    log.debug("Setting up constraints in Interval13Tools");
    019
    020                    numStates = 13;
    021
    022                    // Setup the node constraints
    023                    nodeConstraints = new Constraint[numStates];
    024                    for (int j=0; j<numStates; j++) {
    025                            nodeConstraints[j] = Constraint.NONE;
    026                    }
    027                    nodeConstraints[1] = Constraint.PCODE;
    028                    nodeConstraints[2] = Constraint.PCODE;
    029                    nodeConstraints[3] = Constraint.PCODE;
    030                    nodeConstraints[7] = Constraint.MCODE;
    031                    nodeConstraints[8] = Constraint.MCODE;
    032                    nodeConstraints[9] = Constraint.MCODE;
    033                    log.debug("The node constraints are as follows:");
    034                    for (int i=0; i<13; i++) {
    035                            log.debug("  " + i + "  --  " + nodeConstraints[i]);
    036                    }
    037
    038
    039                    // setup the edge constraints
    040                    edgeConstraints = new Constraint[numStates*numStates];
    041
    042                    // The transition is imossible except when explicitly allowed below
    043                    for(int i=0; i<numStates; ++i) {
    044                            for(int j=0; j<numStates; ++j) {
    045                                    edgeConstraints[i*numStates + j] = Constraint.NEVER;
    046                            }
    047                    }
    048
    049                    // By default, self-transitions are allowed
    050                    for(int i=0; i<numStates; i++) {
    051                            edgeConstraints[i*numStates + i] = Constraint.NONE;
    052                    }
    053
    054                    for (int i=0; i<3; i++) {
    055                            // Following six constraints enforce the open reading frame
    056                            edgeConstraints[(0)*numStates + (i+1)] = Constraint.PSTART;
    057                            edgeConstraints[(i+1)*numStates + (0)] = Constraint.PSTOP;
    058                            edgeConstraints[(i+1)*numStates + (i+1)] = Constraint.PCODE;
    059
    060                            edgeConstraints[(0)*numStates + (i+7)] = Constraint.MSTOP;
    061                            edgeConstraints[(i+7)*numStates + (0)] = Constraint.MSTART;
    062                            edgeConstraints[(i+7)*numStates + (i+7)] = Constraint.MCODE;
    063
    064                            for (int j=0; j<3; j++) {
    065                                    // Following four constraints enforce the splice rules
    066                                    edgeConstraints[(i+1)*numStates + (j+4)] = Constraint.PDON;
    067                                    edgeConstraints[(i+4)*numStates + (j+1)] = Constraint.PACC;
    068                                    edgeConstraints[(i+10)*numStates + (j+7)] = Constraint.MDON;
    069                                    edgeConstraints[(i+7)*numStates + (j+10)] = Constraint.MACC;
    070                            }
    071                    }
    072
    073                    log.debug("The transition constraints are as follows:");
    074                    for (int i=0; i<13; i++) {
    075                            String s = "";
    076                            for (int j=0; j<13; j++) {
    077                                    s += edgeConstraints[i*numStates + j] + "\t";
    078                            }
    079                            log.debug(s);
    080                    }
    081            }
    082
    083            static protected int check012(int x) {
    084                    Assert.a(x>=0);
    085                    Assert.a(x<=2);
    086                    return x;
    087            }
    088
    089            static protected void verify(ModelManager modelInfo) {
    090                    Assert.a(modelInfo.getNumStates()==13);
    091
    092                    Assert.a(modelInfo.getStateName(0).equals("intergenic"));
    093                    Assert.a(modelInfo.getStateName(1).equals("exon0"));
    094                    Assert.a(modelInfo.getStateName(2).equals("exon1"));
    095                    Assert.a(modelInfo.getStateName(3).equals("exon2"));
    096                    Assert.a(modelInfo.getStateName(4).equals("intron0"));
    097                    Assert.a(modelInfo.getStateName(5).equals("intron1"));
    098                    Assert.a(modelInfo.getStateName(6).equals("intron2"));
    099                    Assert.a(modelInfo.getStateName(7).equals("exon0m"));
    100                    Assert.a(modelInfo.getStateName(8).equals("exon1m"));
    101                    Assert.a(modelInfo.getStateName(9).equals("exon2m"));
    102                    Assert.a(modelInfo.getStateName(10).equals("intron0m"));
    103                    Assert.a(modelInfo.getStateName(11).equals("intron1m"));
    104                    Assert.a(modelInfo.getStateName(12).equals("intron2m"));
    105            }
    106    }