Search in sources :

Example 1 with HiddenMarkovModel

use of cz1.hmm.model.HiddenMarkovModel in project polyGembler by c-zhou.

the class Haplotyper method run.

@Override
public void run() {
    // TODO Auto-generated method stub
    myLogger.info("Random seed - " + Constants.seed);
    DataEntry[] de = start_pos == null ? DataCollection.readDataEntry(in_zip, scaff) : DataCollection.readDataEntry(in_zip, scaff, start_pos, end_pos);
    // DataEntry[] de = DataCollection.readDataEntry(in_zip, Constants._ploidy_H);
    final HiddenMarkovModel hmm = vbt ? new HiddenMarkovModelVBT(de, seperation, reverse, trainExp, field, founder_hap_coeff) : // new HiddenMarkovModelRST(de, seperation, reverse, trainExp, field, resampling):
    (hmm_file == null ? new HiddenMarkovModelBWT(de, seperation, reverse, trainExp, field, founder_hap_coeff) : new HiddenMarkovModelBWT(de, seperation, reverse, trainExp, field, founder_hap_coeff, hmm_file));
    if (Constants.plot()) {
        Runnable run = new Runnable() {

            public void run() {
                try {
                    hmmf = new HMMFrame();
                    hmmf.clearTabs();
                    if (Constants.showHMM)
                        hmmp = hmmf.addHMMTab(hmm, hmm.de(), new File(out_prefix));
                } catch (Exception e) {
                    Thread t = Thread.currentThread();
                    t.getUncaughtExceptionHandler().uncaughtException(t, e);
                    e.printStackTrace();
                }
            }
        };
        Thread th = new Thread(run);
        th.run();
    }
    if (Constants.plot()) {
        hmmf.pack();
        hmmf.setVisible(true);
    }
    double ll, ll0 = hmm.loglik();
    for (int i = 0; i < max_iter; i++) {
        hmm.train();
        if (Constants.plot())
            hmmp.update();
        ll = hmm.loglik();
        myLogger.info("----------loglik " + ll);
        if (ll < ll0) {
            // throw new RuntimeException("Fatal error!!!");
            myLogger.info("LOGLIK DECREASED!!!");
            // break;
            ll0 = ll;
            continue;
        }
        if (ll0 != Double.NEGATIVE_INFINITY && Math.abs((ll - ll0) / ll0) < Constants.minImprov)
            break;
        ll0 = ll;
    }
    if (Constants.printPlots()) {
        try {
            float width = hmmf.jframe.getSize().width, height = hmmf.jframe.getSize().height;
            Document document = new Document(new Rectangle(width, height));
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(plot_pdf));
            document.open();
            PdfContentByte canvas = writer.getDirectContent();
            PdfTemplate template = canvas.createTemplate(width, height);
            Graphics2D g2d = new PdfGraphics2D(template, width, height);
            hmmf.jframe.paint(g2d);
            g2d.dispose();
            canvas.addTemplate(template, 0, 0);
            document.close();
        } catch (FileNotFoundException | DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    String scaff_str = scaff[0] + (start_pos == null || start_pos[0] == Integer.MIN_VALUE ? "" : "_" + start_pos[0]) + (end_pos == null || end_pos[0] == Integer.MAX_VALUE ? "" : "_" + end_pos[0]);
    for (int i = 1; i < scaff.length; i++) {
        if (scaff_str.length() + scaff[i].length() + 32 <= Constants.MAX_FILE_ID_LENGTH)
            scaff_str += Constants.scaff_collapsed_str + scaff[i] + (start_pos == null || start_pos[i] == Integer.MIN_VALUE ? "" : "_" + start_pos[i]) + (end_pos == null || end_pos[i] == Integer.MAX_VALUE ? "" : "_" + end_pos[i]);
        else {
            scaff_str += Constants.scaff_collapsed_str + "etc" + scaff.length;
            break;
        }
    }
    hmm.write(out_prefix, expr_id, scaff_str, resampling);
}
Also used : Rectangle(com.itextpdf.text.Rectangle) PdfGraphics2D(com.itextpdf.awt.PdfGraphics2D) FileNotFoundException(java.io.FileNotFoundException) Document(com.itextpdf.text.Document) DataEntry(cz1.hmm.data.DataEntry) DocumentException(com.itextpdf.text.DocumentException) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) HiddenMarkovModelBWT(cz1.hmm.model.HiddenMarkovModelBWT) PdfWriter(com.itextpdf.text.pdf.PdfWriter) HiddenMarkovModel(cz1.hmm.model.HiddenMarkovModel) HiddenMarkovModelVBT(cz1.hmm.model.HiddenMarkovModelVBT) HMMFrame(cz1.hmm.swing.HMMFrame) PdfTemplate(com.itextpdf.text.pdf.PdfTemplate) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) DocumentException(com.itextpdf.text.DocumentException) PrinterException(java.awt.print.PrinterException) ParseException(org.apache.commons.cli.ParseException) Graphics2D(java.awt.Graphics2D) PdfGraphics2D(com.itextpdf.awt.PdfGraphics2D) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 2 with HiddenMarkovModel

use of cz1.hmm.model.HiddenMarkovModel in project polyGembler by c-zhou.

the class Resampler method run.

@Override
public void run() {
    // TODO Auto-generated method stub
    myLogger.info("Random seed - " + Constants.seed);
    DataEntry[] de = start_pos == null ? DataCollection.readDataEntry(in_zip, scaff) : DataCollection.readDataEntry(in_zip, scaff, start_pos, end_pos);
    // DataEntry[] de = DataCollection.readDataEntry(in_zip, Constants._ploidy_H);
    final HiddenMarkovModel hmm = vbt ? new HiddenMarkovModelVBT(de, seperation, reverse, trainExp, field) : (hmm_file == null ? new HiddenMarkovModelBWT(de, seperation, reverse, trainExp, field) : new HiddenMarkovModelBWT(de, seperation, reverse, trainExp, field, hmm_file));
    double ll, ll0 = hmm.loglik();
    String scaff_str = scaff[0] + (start_pos == null || start_pos[0] == Integer.MIN_VALUE ? "" : "_" + start_pos[0]) + (end_pos == null || end_pos[0] == Integer.MAX_VALUE ? "" : "_" + end_pos[0]);
    for (int i = 1; i < scaff.length; i++) {
        if (scaff_str.length() + scaff[i].length() + 32 <= Constants.MAX_FILE_ID_LENGTH)
            scaff_str += Constants.scaff_collapsed_str + scaff[i] + (start_pos == null || start_pos[i] == Integer.MIN_VALUE ? "" : "_" + start_pos[i]) + (end_pos == null || end_pos[i] == Integer.MAX_VALUE ? "" : "_" + end_pos[i]);
        else {
            scaff_str += Constants.scaff_collapsed_str + "etc" + scaff.length;
            break;
        }
    }
    hmm.write(out_prefix, expr_id, scaff_str, resampling);
}
Also used : DataEntry(cz1.hmm.data.DataEntry) HiddenMarkovModelBWT(cz1.hmm.model.HiddenMarkovModelBWT) HiddenMarkovModel(cz1.hmm.model.HiddenMarkovModel) HiddenMarkovModelVBT(cz1.hmm.model.HiddenMarkovModelVBT)

Aggregations

DataEntry (cz1.hmm.data.DataEntry)2 HiddenMarkovModel (cz1.hmm.model.HiddenMarkovModel)2 HiddenMarkovModelBWT (cz1.hmm.model.HiddenMarkovModelBWT)2 HiddenMarkovModelVBT (cz1.hmm.model.HiddenMarkovModelVBT)2 PdfGraphics2D (com.itextpdf.awt.PdfGraphics2D)1 Document (com.itextpdf.text.Document)1 DocumentException (com.itextpdf.text.DocumentException)1 Rectangle (com.itextpdf.text.Rectangle)1 PdfContentByte (com.itextpdf.text.pdf.PdfContentByte)1 PdfTemplate (com.itextpdf.text.pdf.PdfTemplate)1 PdfWriter (com.itextpdf.text.pdf.PdfWriter)1 HMMFrame (cz1.hmm.swing.HMMFrame)1 Graphics2D (java.awt.Graphics2D)1 PrinterException (java.awt.print.PrinterException)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ParseException (org.apache.commons.cli.ParseException)1