Skip to content
Snippets Groups Projects
Commit 495890d1 authored by tjc's avatar tjc
Browse files

move drawMax() to AbstractGraphPanel

git-svn-id: svn+ssh://svn.internal.sanger.ac.uk/repos/svn/pathsoft/artemis/trunk@15671 ee4ac58c-ac51-4696-9907-e4b3aa274f04
parent 8285a7b0
No related branches found
No related tags found
No related merge requests found
...@@ -23,12 +23,16 @@ ...@@ -23,12 +23,16 @@
*/ */
package uk.ac.sanger.artemis.components.alignment; package uk.ac.sanger.artemis.components.alignment;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.text.DecimalFormat;
import javax.swing.JCheckBox; import javax.swing.JCheckBox;
import javax.swing.JComponent; import javax.swing.JComponent;
...@@ -46,6 +50,9 @@ public class AbstractGraphPanel extends JPanel ...@@ -46,6 +50,9 @@ public class AbstractGraphPanel extends JPanel
protected int end; protected int end;
protected float pixPerBase; protected float pixPerBase;
protected BamView bamView;
protected int windowSize;
protected int max;
protected boolean autoWinSize = true; protected boolean autoWinSize = true;
protected int userWinSize = 1; protected int userWinSize = 1;
protected JPopupMenu popup = new JPopupMenu(); protected JPopupMenu popup = new JPopupMenu();
...@@ -61,12 +68,13 @@ public class AbstractGraphPanel extends JPanel ...@@ -61,12 +68,13 @@ public class AbstractGraphPanel extends JPanel
GridBagConstraints c = new GridBagConstraints(); GridBagConstraints c = new GridBagConstraints();
JPanel pane = new JPanel(gridbag); JPanel pane = new JPanel(gridbag);
final JTextField newWinSize = new JTextField(Integer.toString(userWinSize), 10); final JTextField newWinSize = new JTextField(Integer.toString(userWinSize), 10);
newWinSize.setEnabled(!autoWinSize);
final JLabel lab = new JLabel("Window size:"); final JLabel lab = new JLabel("Window size:");
c.gridy = 0; c.gridy = 0;
pane.add(lab, c); pane.add(lab, c);
pane.add(newWinSize, c); pane.add(newWinSize, c);
final JCheckBox autoSet = new JCheckBox("Automatically set window size", false); final JCheckBox autoSet = new JCheckBox("Automatically set window size", autoWinSize);
autoSet.addActionListener(new ActionListener() autoSet.addActionListener(new ActionListener()
{ {
public void actionPerformed(ActionEvent e) public void actionPerformed(ActionEvent e)
...@@ -102,6 +110,23 @@ public class AbstractGraphPanel extends JPanel ...@@ -102,6 +110,23 @@ public class AbstractGraphPanel extends JPanel
addMouseListener(new PopupListener()); addMouseListener(new PopupListener());
} }
/**
* Draw maximum average value.
* @param g2
*/
protected void drawMax(Graphics2D g2)
{
DecimalFormat df = new DecimalFormat("0.#");
String maxStr = df.format((float)max/(float)windowSize);
FontMetrics fm = getFontMetrics(getFont());
g2.setColor(Color.black);
int xpos = bamView.getJspView().getVisibleRect().width - fm.stringWidth(maxStr) -
bamView.getJspView().getVerticalScrollBar().getWidth();
g2.drawString(maxStr, xpos, fm.getHeight());
}
protected void setStartAndEnd(int start, int end) protected void setStartAndEnd(int start, int end)
{ {
......
...@@ -26,7 +26,6 @@ package uk.ac.sanger.artemis.components.alignment; ...@@ -26,7 +26,6 @@ package uk.ac.sanger.artemis.components.alignment;
import java.awt.BasicStroke; import java.awt.BasicStroke;
import java.awt.Color; import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
...@@ -49,10 +48,10 @@ import net.sf.samtools.SAMRecord; ...@@ -49,10 +48,10 @@ import net.sf.samtools.SAMRecord;
public class SnpPanel extends AbstractGraphPanel public class SnpPanel extends AbstractGraphPanel
{ {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private BamView bamView;
private Bases bases; private Bases bases;
private float minBaseQualityFilter = 0; private float minBaseQualityFilter = 0;
private int nBins;
private int snpCount[];
public SnpPanel(final BamView bamView, Bases bases) public SnpPanel(final BamView bamView, Bases bases)
{ {
...@@ -96,16 +95,18 @@ import net.sf.samtools.SAMRecord; ...@@ -96,16 +95,18 @@ import net.sf.samtools.SAMRecord;
{ {
super.paintComponent(g); super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g; Graphics2D g2 = (Graphics2D)g;
if(bases == null) if(bases == null || nBins == 0 || bamView.getReadsInView() == null)
return;
List<SAMRecord> readsInView = bamView.getReadsInView();
if(readsInView == null)
return; return;
draw(g2);
int windowSize; drawMax(g2);
}
protected void init(BamView bamView, float pixPerBase, int start, int end)
{
this.bamView = bamView;
setPixPerBase(pixPerBase);
setStartAndEnd(start, end);
if(autoWinSize) if(autoWinSize)
{ {
windowSize = (bamView.getBasesInView()/300); windowSize = (bamView.getBasesInView()/300);
...@@ -116,36 +117,13 @@ import net.sf.samtools.SAMRecord; ...@@ -116,36 +117,13 @@ import net.sf.samtools.SAMRecord;
if(windowSize < 1) if(windowSize < 1)
windowSize = 1; windowSize = 1;
nBins = Math.round((end-start+1.f)/windowSize);
int nBins = Math.round((end-start+1.f)/windowSize); max = 0;
int max = drawPlot(g2, nBins, windowSize); snpCount = null;
String maxStr = Float.toString(max/windowSize);
FontMetrics fm = getFontMetrics(getFont());
g2.setColor(Color.black);
int xpos = getWidth() - fm.stringWidth(maxStr) -
bamView.getJspView().getVerticalScrollBar().getWidth();
g2.drawString(maxStr, xpos, fm.getHeight());
} }
private int drawPlot(Graphics2D g2, int nBins, int windowSize)
{
//lines = CoveragePanel.getLineAttributes(bamView.bamList.size());
List<SAMRecord> readsInView = bamView.getReadsInView();
int snpCount[] = new int[nBins];
for(int i=0; i<snpCount.length; i++)
snpCount[i] = 0;
int max = 0;
for(int i=0; i<readsInView.size(); i++)
{
SAMRecord thisRead = readsInView.get(i);
max = calculateSNPs(thisRead, windowSize, nBins, snpCount, max);
}
protected void draw(Graphics2D g2)
{
g2.setColor(Color.red); g2.setColor(Color.red);
g2.setStroke(new BasicStroke(1.f)); g2.setStroke(new BasicStroke(1.f));
...@@ -182,8 +160,6 @@ import net.sf.samtools.SAMRecord; ...@@ -182,8 +160,6 @@ import net.sf.samtools.SAMRecord;
g2.drawLine(x0, y0, x1, y1); g2.drawLine(x0, y0, x1, y1);
} }
} }
return max;
} }
/** /**
...@@ -193,12 +169,15 @@ import net.sf.samtools.SAMRecord; ...@@ -193,12 +169,15 @@ import net.sf.samtools.SAMRecord;
* @param pixPerBase * @param pixPerBase
* @param ypos * @param ypos
*/ */
private int calculateSNPs(SAMRecord thisRead, protected void addRecord(SAMRecord thisRead)
int windowSize,
int nBins,
int[] snpCount,
int max)
{ {
if(snpCount == null)
{
snpCount = new int[nBins];
for(int i=0; i<snpCount.length; i++)
snpCount[i] = 0;
}
int thisStart = thisRead.getAlignmentStart(); int thisStart = thisRead.getAlignmentStart();
int thisEnd = thisRead.getAlignmentEnd(); int thisEnd = thisRead.getAlignmentEnd();
int offset = bamView.getSequenceOffset(thisRead.getReferenceName()); int offset = bamView.getSequenceOffset(thisRead.getReferenceName());
...@@ -241,7 +220,5 @@ import net.sf.samtools.SAMRecord; ...@@ -241,7 +220,5 @@ import net.sf.samtools.SAMRecord;
{ {
e.printStackTrace(); e.printStackTrace();
} }
return max;
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment