From 09841c61c520b7144485131c1020ae5a4fd0f352 Mon Sep 17 00:00:00 2001
From: tjc <tjc@ee4ac58c-ac51-4696-9907-e4b3aa274f04>
Date: Tue, 7 Dec 2010 16:04:08 +0000
Subject: [PATCH] VCF record filtering

git-svn-id: svn+ssh://svn.internal.sanger.ac.uk/repos/svn/pathsoft/artemis/trunk@15197 ee4ac58c-ac51-4696-9907-e4b3aa274f04
---
 .../artemis/components/variant/VCFFilter.java | 154 ++++++++++++++++++
 1 file changed, 154 insertions(+)
 create mode 100644 uk/ac/sanger/artemis/components/variant/VCFFilter.java

diff --git a/uk/ac/sanger/artemis/components/variant/VCFFilter.java b/uk/ac/sanger/artemis/components/variant/VCFFilter.java
new file mode 100644
index 000000000..3333c5c4d
--- /dev/null
+++ b/uk/ac/sanger/artemis/components/variant/VCFFilter.java
@@ -0,0 +1,154 @@
+package uk.ac.sanger.artemis.components.variant;
+
+import java.awt.GridBagConstraints;
+import java.awt.GridBagLayout;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+import javax.swing.JButton;
+import javax.swing.JFrame;
+import javax.swing.JLabel;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.JTextField;
+
+import uk.ac.sanger.artemis.components.Utilities;
+
+public class VCFFilter extends JFrame
+{
+  private static final long serialVersionUID = 1L;
+  private static float MIN_QUALITY = 0;
+  private static int MIN_DP = 0;
+  private static float MIN_MQ = 0;
+  private static float MIN_AF1 = 0;
+  
+  /**
+   * Filter VCF records by different values in the record, QUAL, DP, MQ and AF1.
+   * @param vcfView
+   */
+  public VCFFilter(final VCFview vcfView)
+  {
+    super("Filter");
+    GridBagConstraints c = new GridBagConstraints();
+    
+    JPanel panel = (JPanel)getContentPane();
+    panel.setLayout(new GridBagLayout());
+
+    // min quality
+    c.gridy = 0;
+    c.gridx = 0;
+    c.anchor = GridBagConstraints.WEST;
+    panel.add(new JLabel("Minimum quality score (QUAL):"), c);
+    final JTextField minQuality = new JTextField(Float.toString(MIN_QUALITY), 15);
+    c.gridx = 1;
+    panel.add(minQuality, c);
+    
+    // min DP
+    c.gridy = c.gridy+1;
+    c.gridx = 0;
+    panel.add(new JLabel("Minimum combined depth across samples (DP):"), c);
+    final JTextField minDP = new JTextField(Integer.toString(MIN_DP), 15);
+    c.gridx = 1;
+    panel.add(minDP, c);
+    
+    // min MQ
+    c.gridy = c.gridy+1;
+    c.gridx = 0;
+    panel.add(new JLabel("Minimum RMS mapping quality (MQ):"), c);
+    final JTextField minMQ = new JTextField(Float.toString(MIN_MQ),15);
+    c.gridx = 1;
+    panel.add(minMQ, c);
+    
+    // min AF1
+    c.gridy = c.gridy+1;
+    c.gridx = 0;
+    panel.add(new JLabel("Minimum site frequency of strongest non-reference allele (AF1):"), c);
+    final JTextField minAF1 = new JTextField(Float.toString(MIN_AF1),15);
+    c.gridx = 1;
+    panel.add(minAF1, c);
+    
+    //
+    c.gridy = c.gridy+1;
+    c.gridx = 0;
+    c.anchor = GridBagConstraints.EAST;
+    JButton apply = new JButton("Apply");
+    apply.addActionListener(new ActionListener()
+    {
+      public void actionPerformed(ActionEvent e)
+      {
+        try
+        {
+          MIN_QUALITY = Float.parseFloat(minQuality.getText());
+          MIN_DP = Integer.parseInt(minDP.getText());
+          MIN_MQ = Float.parseFloat(minMQ.getText());
+          MIN_AF1 = Float.parseFloat(minAF1.getText());
+          vcfView.repaint();
+        }
+        catch(NumberFormatException ex)
+        {
+          JOptionPane.showMessageDialog(null, 
+              ex.getMessage(), 
+              "Format Error", JOptionPane.ERROR_MESSAGE);
+        }
+      }
+    });
+    panel.add(apply, c);
+
+    c.gridx = 1;
+    c.anchor = GridBagConstraints.WEST;
+    JButton close = new JButton("Close");
+    close.addActionListener(new ActionListener()
+    {
+      public void actionPerformed(ActionEvent e)
+      {
+        dispose();
+      }
+    });
+    panel.add(close, c);
+    
+    pack();
+    Utilities.centreFrame(this);
+    setVisible(true);
+  }
+  
+  /**
+   * Test for a given VCF record to see if it passes the filters.
+   * @param record
+   * @return
+   */
+  protected static boolean passFilter(VCFRecord record)
+  {
+    try
+    {
+      if(record.getQuality() < VCFFilter.MIN_QUALITY)
+        return false;
+      
+      try
+      {
+        if(VCFFilter.MIN_DP > 0 && Integer.parseInt(record.getInfoValue("DP")) < VCFFilter.MIN_DP)
+          return false;
+      }
+      catch(NullPointerException npe){}
+    
+      try
+      {
+        if(VCFFilter.MIN_MQ > 0 && Float.parseFloat(record.getInfoValue("MQ")) < VCFFilter.MIN_MQ)
+          return false;
+      }
+      catch(NullPointerException npe){}
+
+      try
+      {
+        if(VCFFilter.MIN_AF1 > 0 && Float.parseFloat(record.getInfoValue("AF1")) < VCFFilter.MIN_AF1)
+          return false;
+      }
+      catch(NullPointerException npe){}
+    }
+    catch(NumberFormatException e)
+    {
+      System.err.println(e.getMessage()); 
+    }
+
+    return true;
+  }
+}
\ No newline at end of file
-- 
GitLab