Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package licorice.gui; import gatkrunner.gatk.VCFUtils; import licorice.analysis.Analysis; import org.apache.commons.io.FilenameUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import utils.ZipUtil; import utils.reference.GenomeRef; import utils.reference.SimpleGenomeRef; import javax.swing.*; import javax.swing.text.BadLocationException; import javax.swing.text.StyledDocument; import java.io.*; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Map; import java.util.Properties; import static utils.ZipUtil.directoryfy; /** * @author varuzza */ public class MainPanel extends javax.swing.JPanel { private static Logger log = LoggerFactory.getLogger(MainPanel.class); private Analysis analysis; private Properties prop = new Properties(); private StyledDocument doc; private final JFileChooser fc = new JFileChooser(); private Integer minQual = 15; private Double maxNC = 0.95; /** * Creates new form MainPanel */ public MainPanel() { try { String jarPath = MainPanel.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath(); System.out.println(String.format("JAR Path '%s'", jarPath)); File propFile = new File("licorice.properties"); if (propFile.exists()) { prop.load(new FileInputStream("licorice.properties")); minQual = Integer.parseInt(prop.getProperty("minimum.quality")); maxNC = Double.parseDouble(prop.getProperty("maximum.nocall.rate")); } else { prop = new Properties(); prop.setProperty("genome.path", "./genome/GCF_000004515.4_Glycine_max_v2.0_genomic.fna"); prop.setProperty("input.default_dir", "."); } initComponents(); doc = logPane.getStyledDocument(); fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); File defaultDir = new File(prop.getProperty("input.default_dir")); if (!defaultDir.exists()) { defaultDir = new File("."); } fc.setCurrentDirectory(defaultDir); } catch (Exception ex) { log.error(ex.getMessage()); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { inputLabel = new javax.swing.JLabel(); fileNameField = new javax.swing.JTextField(); fileDialogBtn = new javax.swing.JButton(); processBtn = new javax.swing.JButton(); progressBar = new javax.swing.JProgressBar(); jScrollPane1 = new javax.swing.JScrollPane(); logPane = new javax.swing.JTextPane(); titleLabel = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); minQualField = new javax.swing.JTextField(); jLabel2 = new javax.swing.JLabel(); maxNCField = new javax.swing.JTextField(); inputLabel.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N inputLabel.setLabelFor(inputLabel); inputLabel.setText("Input"); fileNameField.setName("fileName"); // NOI18N fileDialogBtn.setText("..."); fileDialogBtn.setName("fileDialogButton"); // NOI18N fileDialogBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { fileDialogBtnActionPerformed(evt); } }); processBtn.setFont(new java.awt.Font("Ubuntu", 1, 24)); // NOI18N processBtn.setText("Process"); processBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { processBtnActionPerformed(evt); } }); logPane.setEditable(false); logPane.setText("Select the ZIP file with the VCF's and then click in Process \nto create the matrix.\n"); jScrollPane1.setViewportView(logPane); titleLabel.setFont(new java.awt.Font("Ubuntu", 0, 24)); // NOI18N titleLabel.setText("Licorice 1.6 (blue sapphire)"); jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N jLabel1.setText("Min. Quality"); minQualField.setText("30"); minQualField.setToolTipText("Minimum Variant Quality"); jLabel2.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N jLabel2.setText("Maximum Nocall Rate"); maxNCField.setText("0.95"); maxNCField.setToolTipText("Minimum Variant Quality"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(32, 32, 32).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1143, Short.MAX_VALUE) .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup().addComponent(inputLabel).addGap(57, 57, 57) .addComponent(fileNameField) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(fileDialogBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(titleLabel) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1).addComponent(jLabel2)).addGap(79, 79, 79) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(maxNCField, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(minQualField, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGap(0, 0, Short.MAX_VALUE)))) .addGroup(layout.createSequentialGroup().addGap(466, 466, 466) .addComponent(processBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(15, 15, 15).addComponent(titleLabel) .addGap(12, 12, 12) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(fileNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(fileDialogBtn).addComponent(inputLabel)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(minQualField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(maxNCField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE) .addComponent(processBtn) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 558, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())); }// </editor-fold>//GEN-END:initComponents public void appendLog(String message) { SwingUtilities.invokeLater(() -> { try { doc.insertString(doc.getLength(), message, null); } catch (BadLocationException e) { e.printStackTrace(); } logPane.setCaretPosition(doc.getLength()); }); } private void processBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_processBtnActionPerformed SwingUtilities.invokeLater(() -> { try { minQual = Integer.parseInt(minQualField.getText()); processBtn.setEnabled(false); //progressBar = new JProgressBar(); progressBar.setMinimum(0); progressBar.setMaximum(100); Path genomePath = Paths.get(prop.getProperty("genome.path")); prop.setProperty("input.default_dir", fc.getCurrentDirectory().getAbsolutePath()); prop.setProperty("minimum.quality", minQual.toString()); prop.setProperty("maximum.nocall.rate", maxNC.toString()); prop.store(new FileOutputStream("licorice.properties"), null); Path inputPath = Paths.get(fileNameField.getText()); if (!inputPath.toFile().exists()) { JOptionPane.showMessageDialog(null, "Input file does not exist"); appendLog("Input file '" + inputPath + "' does not exist"); return; } Path outputPath = inputPath .resolveSibling(FilenameUtils.getBaseName(inputPath.getFileName().toString()) + ".txt"); appendLog("Output in: '" + outputPath.getParent() + "'\n"); appendLog("Analysis Started...\n"); processBtn.setText("Processing..."); SimpleGenomeRef genome = new SimpleGenomeRef(genomePath); GenomeRef.ValidationResult validation = genome.validate(); if (!validation.isValid()) { appendLog("Reference Error: '" + validation.getErrors() + "'\n"); return; } Path effectivePath = ZipUtil.directoryfy(inputPath); Map<String, String> samples = VCFUtils.makeSamplesDictionary(VCFUtils.listVCFFiles(effectivePath)); appendLog("==================================\n"); appendLog("Samples List\n"); appendLog("==================================\n"); appendLog("Sample\tFile"); samples.forEach((k, v) -> appendLog(String.format("%s\t%s\n", k, v))); appendLog("==================================\n"); Path samplesPath = inputPath.resolveSibling( FilenameUtils.getBaseName(inputPath.getFileName().toString()) + ".samples.txt"); appendLog("Samples List in: '" + samplesPath.getParent() + "'\n"); try (PrintStream out = new PrintStream(new FileOutputStream(samplesPath.toFile()))) { out.println("Sample\tFile"); samples.forEach((k, v) -> out.println(String.format("%s\t%s", k, v))); } analysis = new Analysis(genome, minQual, maxNC, false, outputPath, VCFUtils.listVCFFiles(effectivePath)); analysis.progressListener(progress -> progressBar.setValue(progress)); analysis.onFinish(() -> { log.info("Callback called"); appendLog("Analysis Finished.\n"); fileNameField.setText(""); processBtn.setText("Process"); processBtn.setEnabled(true); processBtn.repaint(); this.repaint(); return null; }); analysis.onException((Thread t, Throwable ex) -> { appendLog(ex.getMessage()); appendLog("Analysis Failed!!!"); JOptionPane.showMessageDialog(null, "Analysis Failed!!!"); processBtn.setText("Process"); processBtn.setEnabled(true); processBtn.repaint(); }); } catch (IOException ex) { appendLog(ex.getMessage() + "\n"); log.error(ex.getMessage()); JOptionPane.showMessageDialog(null, "Analysis Failed!!!"); ex.printStackTrace(); } analysis.start(); //progressBar. }); }//GEN-LAST:event_processBtnActionPerformed private void fileDialogBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileDialogBtnActionPerformed int ret = fc.showOpenDialog(this); if (ret == JFileChooser.APPROVE_OPTION) { try { File file = fc.getSelectedFile(); log.info("Opening: " + file.getName() + "."); fileNameField.setText(file.getCanonicalPath()); } catch (IOException ex) { log.error(ex.getMessage()); ex.printStackTrace(); } } }//GEN-LAST:event_fileDialogBtnActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton fileDialogBtn; private javax.swing.JTextField fileNameField; private javax.swing.JLabel inputLabel; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextPane logPane; private javax.swing.JTextField maxNCField; private javax.swing.JTextField minQualField; private javax.swing.JButton processBtn; private javax.swing.JProgressBar progressBar; private javax.swing.JLabel titleLabel; // End of variables declaration//GEN-END:variables }