|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

04-10-2008, 02:18 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 22
|
|
|
Need Help for Dot Plot Graph
I have a little problem at the moment what I am trying to do is plot a graph from two sequences and plot it onto the jPanel but I can't. I have the gui done and the link to a certain file but can't make sense out of it if somebody knows how to do this please help.
The two sequences should look like this
A B C D E F G .....Z
A
B
C
D
E
F
G
.
.
.
.
.
Z
An the inputs are going to be letters in stead of numbers so every time the program encounters the same letter on both axis it will put a line like the photo shows.
File 1
File 2
File 3
and here is my Gui code
/****************************************************************/
/* DotPlot */
/* */
/****************************************************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Summary description for DotPlot
*
*/
public class DotPlot extends JFrame
{
// Variables declaration
private JLabel jLabel1;
private JLabel jLabel2;
private JLabel jLabel3;
private JTextArea jTextArea1;
private JScrollPane jScrollPane1;
private JTextArea jTextArea2;
private JScrollPane jScrollPane2;
private JButton jButton1;
private JPanel contentPane;
//-----
private JPanel jPanel1;
//-----
// End of variables declaration
public DotPlot()
{
super();
initializeComponent();
//
// Add any constructor code after initializeComponent call
//
this.setVisible(true);
}
/**
* This method is called from within the constructor to initialize the form.
*/
private void initializeComponent()
{
jLabel1 = new JLabel();
jLabel2 = new JLabel();
jLabel3 = new JLabel();
jTextArea1 = new JTextArea();
jScrollPane1 = new JScrollPane();
jTextArea2 = new JTextArea();
jScrollPane2 = new JScrollPane();
jButton1 = new JButton();
contentPane = (JPanel)this.getContentPane();
//-----
jPanel1 = new JPanel();
//-----
//
// jLabel1
//
jLabel1.setBackground(new Color(105, 188, 246));
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel1.setOpaque(true);
jLabel1.setText("FASTA Sequences File 1 ");
//
// jLabel2
//
jLabel2.setBackground(new Color(105, 188, 246));
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel2.setOpaque(true);
jLabel2.setText("FASTA Sequences File 2 ");
//
// jLabel3
//
jLabel3.setBackground(new Color(105, 188, 246));
jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
jLabel3.setOpaque(true);
jLabel3.setText("Dot-Plot Graph");
//
// jTextArea1
//
jTextArea1.setWrapStyleWord(true);
//
// jScrollPane1
//
jScrollPane1.setViewportView(jTextArea1);
//
// jTextArea2
//
jTextArea2.setLineWrap(true);
//
// jScrollPane2
//
jScrollPane2.setViewportView(jTextArea2);
//
// jButton1
//
jButton1.setText("Plot the Sequance");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton1_actionPerformed(e);
}
});
//
// contentPane
//
contentPane.setLayout(null);
addComponent(contentPane, jLabel1, 19,10,249,18);
addComponent(contentPane, jLabel2, 18,229,249,18);
addComponent(contentPane, jLabel3, 284,11,500,18);
addComponent(contentPane, jScrollPane1, 18,29,250,160);
addComponent(contentPane, jScrollPane2, 18,247,250,160);
addComponent(contentPane, jButton1, 658,422,127,28);
addComponent(contentPane, jPanel1, 284,30,500,379);
//
// jPanel1
//
jPanel1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
jPanel1.setBorder(BorderFactory.createLineBorder(Color.blue));
//
// DotPlot
//
this.setTitle("DotPlot");
this.setLocation(new Point(0, 0));
this.setSize(new Dimension(800, 486));
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}
//
// Add any appropriate code in the following Event Handling Methods
//
private void jButton1_actionPerformed(ActionEvent e)
{
System.out.println("\njButton1_actionPerformed(ActionEvent e) called.");
// Add any handling code here
}
//
// Add any method code to meet your needs in the following area
//
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception ex)
{
System.out.println("Failed loading L&F: ");
System.out.println(ex);
}
new DotPlot();
}
}
Your help is appreciated
Last edited by BHCluster : 04-10-2008 at 04:48 PM.
|
|

04-11-2008, 06:14 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 644
|
|
From your file list, can you post here separately, lines 65 and 71 of DotPlot.java; and lines 98, 129, 168, 184, 220, and 257 of DotPlotUI.java. It appears the above lines have been cut short by the maximum page width.
Can you elaborate more on your GUI code.. for example, why do you have your GUI code which named after DotPlot.java, and yet you link to a complete DotPlot.java file?
__________________
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
|
|

04-12-2008, 01:16 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 22
|
|
|
Thanks for taking interest in this but I have found this online with the code like it is that's why I don't know how to do it and that's why I am so desperate for this if you know any way around this I would appreciate it.
|
|

04-12-2008, 01:23 AM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 644
|
|
BH, actually.... what I did was complete those lines that were cut off saying (floa with: (float)10;
Those lines that were cut off with strings I just closed the string and then it all compiled and ran. The GUI runs but it requires a "Fasta" file type which I don't have and I assume you'll need to open a graph.
Best of luck. 
__________________
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
|
|

04-15-2008, 01:37 PM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 644
|
|
As requested:
GraphPane
/**
* File: GraphPane.java
* Created: Apr 10, 2008, at 10:54:12 PM
*
* Description: See the comments before the class below.
*/
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
/* This class extends teh JPanel class and is
* used primarily to contain the Graphical dot-plot
*/
class GraphPane extends JPanel {
/* These are the attributes required for this class.
* Their primary purpose is to fit the graph to the
* panel.
*/
int scaleWidth = 600;
int scaleHeight = 600;
final int PAD = 20;
Image scaledImage;
DotPlot dp;
String sequence_1 = "";
String sequence_2 = "";
int window;
int threshold;
/* This is the sole constructor for GraphPane. The
* class is populted from these initial parameters.
*
*/
public GraphPane(String sequence_1, String sequence_2, int window, int threshold)
{
this.window = window;
this.threshold = threshold;
this.sequence_1 = sequence_1;
this.sequence_2 = sequence_2;
}
// Paints the graph to a panel
public void paint(Graphics g)
{
DotPlot dp = new DotPlot();
dp = new DotPlot();
dp.readInGenome(this.sequence_1,this.sequence_2,this.window,this.threshold);
dp.setDimensions();
Panel panel = new Panel();
panel.setBackground(new Color(235,235,235));
add(panel);
g.setColor(Color.red);
dp.draw(panel);
scaledImage = dp.img.getScaledInstance(scaleWidth,scaleHeight,0);
g.drawImage(scaledImage,0,0,panel);
}
}
DotPlotUI
/**
* File: DotPlotUI.java
* Created: Apr 10, 2008, at 10:52:47 PM
*
* Description: See the comments before the class below.
*/
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;
/* This is the main driver for the Dot-Plot program.
* Compilation: javac DotPlotUI.java
* Usage: java DotPlotUI
*
*/
public class DotPlotUI implements ActionListener
{
// the following are the components used to make
// up the application UI
GraphPane graphPane;
JTabbedPane tabbedPane;
TextField tfDirectory = new TextField();
TextField tfFile = new TextField();
TextField tfFilter = new TextField();
FileDialog fd;
JFrame f;
final JButton button1;
final JButton button2;
final JButton button3;
TextArea textArea1;
TextArea textArea2;
JLabel window_label;
JTextField window;
JLabel threshold_label;
JTextField threshold;
TextArea status;
JLabel status_label;
JPanel pre_plot;
/* The is the sole constructionfor the DotPlotUI
* In this constructor all the components are added
* sequentially/
*/
public DotPlotUI()
{
f = new JFrame("Dot-plot Comparative Sequence Analysis");
f.getContentPane().setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
Rectangle bounds = env.getMaximumWindowBounds();
button1 = new JButton("Fasta File 1");
button2 = new JButton("Fasta File 2");
button3 = new JButton("Create Dot-plot");
textArea1 = new TextArea("Fasta File 1", 10,10,TextArea.SCROLLBARS_NONE);
textArea2 = new TextArea("Fasta File 2", 10,10,TextArea.SCROLLBARS_NONE);
status = new TextArea("", 10,bounds.width,TextArea.SCROLLBARS_VERTICAL_ONLY);
status.setEditable(false);
window_label = new JLabel("Windows Size");
window = new JTextField(10);
threshold_label = new JLabel("Threshold Size");
threshold = new JTextField(10);
status_label = new JLabel("Process Status/Error Info");
pre_plot = new JPanel();
pre_plot.setBackground(new Color(235,235,235));
f.getContentPane().add(textArea1);
f.getContentPane().add(button1);
f.getContentPane().add(textArea2);
f.getContentPane().add(button2);
f.getContentPane().add(window_label);
f.getContentPane().add(window);
f.getContentPane().add(threshold_label);
f.getContentPane().add(threshold);
f.getContentPane().add(button3);
f.getContentPane().add(status);
f.getContentPane().add(status_label);
f.getContentPane().add(pre_plot);
button1.addActionListener(this);
button2.addActionListener(this);
window.addActionListener(this);
threshold.addActionListener(this);
button3.addActionListener(this);
textArea1.setBounds(10,10,182,150);
Dimension size = button1.getPreferredSize();
button1.setBounds(10,170,size.width,size.height);
textArea2.setBounds(10,180+size.height,182,150);
size = button1.getPreferredSize();
button2.setBounds(10,340+size.height,size.width,size.height);
size = threshold_label.getPreferredSize();
window_label.setBounds(10,420+size.height,size.width,size.height );
window.setBounds(10+size.width+10,420+size.height,size.width,size.height );
threshold_label.setBounds(10,445+size.height,size.width,size.height );
threshold.setBounds(10+size.width+10,445+size.height,size.width,size.height );
size = button3.getPreferredSize();
button3.setBounds(10,485+size.height,size.width+5,size.height+5);
button3.setBackground(new Color(153,153,153));
size = status_label.getPreferredSize();
//status_label.setBounds((int)((bounds.width/2)-(size.width/2)),bounds.height-115,size);
status.setBounds(10,bounds.height-100,bounds.width-30,65);
status.setBackground(new Color(255,255,204));
status.setForeground(Color.red);
pre_plot.setBounds(300,10, 600, 600);
f.setSize((int)bounds.getWidth(),(int)bounds.getHeight());
f.setLocation(0,0);
f.setVisible(true);
}
/* This handles all action events to the Application UI.
* It poulates the Fasta File TextFields, and ultimatley
* produces the dot-plot.
*/
public void actionPerformed(ActionEvent evt)
{
// load the first fasta file
if ((evt.getActionCommand()).equals("Fasta File 1")) {
fd = new FileDialog(f, null, FileDialog.LOAD);
fd.setDirectory(".\\");
}
// load the second fasta file
else if ((evt.getActionCommand()).equals("Fasta File 2"))
{
fd = new FileDialog(f, null, FileDialog.LOAD);
fd.setDirectory(".\\");
}
// display the dot-plot of the previously loaded data
else if ((evt.getActionCommand()).equals("Create Dot-plot"))
{
graphPane = new GraphPane(textArea1.getText(),textArea2.getText(),getWindow(),getWindow());
f.getContentPane().add(graphPane);
graphPane.setBounds(300,10, 600, 600);
// display program status
status.append("Info: Dot-plot sequence analysis diagram created!");
}
// Populate textArea_1 with sequences
if ((evt.getActionCommand()).equals("Fasta File 1"))
{
fd.show();
String sequence = this.readFile(fd.getDirectory()+fd.getFile());
textArea1.replaceRange(sequence,0,textArea1.getText().length());
// display program status
status.append("Info: Fast File 1 sequence data loaded, " + sequence.length()
+ " characters in length.\n");
}
// Populate textArea_2 with sequences
else if((evt.getActionCommand()).equals("Fasta File 2"))
{
fd.show();
String sequence = this.readFile(fd.getDirectory()+fd.getFile());
textArea2.replaceRange(sequence,0,textArea2.getText().length());
// display program status
status.append("Info: Fast File 2 sequence data loaded, " + sequence.length()
+" characters in length.\n");
}
}
/* This method is used to collect the window size in the
* text-fields. If they are empty or erroneous a default value
* of 100 is chosen, and a warning message outputted.
*/
public int getWindow()
{
String win_val = window.getText();
if(win_val.equals("") || win_val == null)
{
status.append("Warning: No window size selected, using default window size of 10");
return 100;
}
win_val.trim();
boolean is_number = true;
for(int i=0; i<win_val.length(); i++)
{
if(Character.isDigit(win_val.charAt(i)) == false)
{
is_number = false;
break;
}
}
if(is_number == false)
{
status.append("Error: The chosen window size is not a number, using default windoow");
return 100;
}
else
{
status.append("Info: Window size set at: " + win_val + ".\n");
return Integer.parseInt(win_val);
}
}
/* This method is used to collect the threshold size in the
* text-fields. If they are empty or erroneous a default value
* of 100 is chosen, and a warning message outputted.
*/
public int getThreshold()
{
String thres_val = window.getText();
if(thres_val.equals("") || thres_val == null)
{
status.append("Warning: No threshold size selected, using default threshold size");
return 100;
}
thres_val.trim();
boolean is_number = true;
for(int i=0; i<thres_val.length(); i++)
{
if(Character.isDigit(thres_val.charAt(i)) == false)
{
is_number = false;
break;
}
}
if(is_number == false)
{
status.append("Error: The chosen threshold size is not a number, using default th?");
return 100;
}
else
{
status.append("Info: Threshold size set at: " + thres_val + ".\n");
return Integer.parseInt(thres_val);
}
}
/* This method reads the genomic sequences from the file
* passed in the argument. The file must be of FASTA formatting.
*/
public String readFile(String file)
{
BufferedReader inFile = null;
String sequence = "";
try{
inFile = new BufferedReader(new FileReader(file));
String str = null;
while((str = inFile.readLine()) != null)
{
if(!str.equals(""))
{
if(str.substring(0,1).equals(">"))
{
while((str = inFile.readLine()) != null && !str.equals(""))
sequence = sequence.concat(str);
}
}
}
}
catch (IOException e)
{
System.out.println("INPUT ERROR: Input file not recognized at DotPlotUI.readFile");
System.exit(1);
}
return sequence;
}
public static void main(String[] args)
{
new DotPlotUI();
}
}
DotPlot
/**
* File: DotPlot.java
* Created: Apr 10, 2008, at 10:51:43 PM
*
* Description: See the comments before the class below.
*/
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
/* Class DotPlot works out the technical aspects of the graph
* by computing the thresholds, window size and where sequences match.
* It is also responsible for drawing the points of the Dot-plot.
*/
class DotPlot
{
// Required attibutes including the Image to be displayed,seq_1, seq_2
// window and threshold vlaues
Image img;
String seq1 = null;
String seq2 = null;
int windowSize = 0;
int threshold = 0;
Vector vecPoints = new Vector();
int index = 0;
int noOfCoors = 0;
int imageWidth = 1000;
int imageHeight = 1000;
int scale = 1000;
// Initialise the classes attributes
public void readInGenome(String seq1, String seq2, int windowSize, int threshold)
{
this.windowSize = windowSize;
this.threshold = threshold;
this.seq1 = seq1;
this.seq2 = seq2;
}
/* Sets up the dimensions for the to be
* displayed image depending on whether the sequence
* is longer, shoter or equla to the default image dimensions
*/
public void setDimensions()
{
if(seq1.length() == seq2.length())//Both seqs same length
{
if(seq1.length() > scale)//Both greater than scale
{
imageWidth = scale;
imageHeight = scale;
}
else//Both less than scale
{
imageWidth = seq1.length();
imageHeight = seq2.length();
}
}
else//Different sized seqs
{
if(seq1.length() < scale && seq2.length() < scale)//seq_1 = seq_2
{
imageWidth = seq1.length();
imageHeight = seq2.length();
}
else if(seq1.length() > seq2.length())//seq_1 > seq_2
{
imageWidth = scale;
float fHeight = (float) ( (float)seq2.length()/(float)seq1.length() ) * (float)10;
imageHeight = (int) fHeight;
}
else if(seq2.length() > seq1.length())//seq_2 > seq_1
{
imageHeight = scale;
float fWidth = (float) ( (float)seq1.length()/(float)seq2.length() ) * (float)10;
imageWidth = (int) fWidth;
}
}
}
/* Calculates if the sequential seegment starting
* at 'a' and ending at 'b' meeets the current
* threshold
*/
public boolean satisfiesThreshold(int a, int b)
{
int outCount = 0;
int matchCount = 0;
for(int d=1; d<=windowSize; d++)
{
if(inBounds(a-d,b-d))
{
if(isMatch(a-d,b-d))
matchCount++;
}
else
outCount++;
if(inBounds(a+d,b+d))
{
if(isMatch(a+d,b+d))
matchCount++;
}
else
outCount++;
}
if(matchCount >= threshold)
return true;
else
return false;
}
/* Check if the base indexes match
* True or False is returned.
*/
public boolean isMatch(int a, int b)
{
String base1 = seq1.substring(a,a+1);
String base2 = seq2.substring(b,b+1);
if(base1.equals(base2))
return true;
else return false;
}
/* This makes sure we don't run over bounds with our
* windowing.
* True or False is returned.
*/
public boolean inBounds(int a, int b)
{
if(a < 0 || b < 0 || a > seq1.length()-1 || b > seq2.length()-1)
return false;
else return true;
}
/* Uses java.Grphics to draw the points
* tothe screen using the fore-mentioned
* integers a and b
*/
public void drawPoint(int a, int b, Graphics g)
{
int xcoor = 0;
int ycoor = 0;
if(seq1.length() > imageWidth)
{
float xx = (float) ( (float)a/(float)seq1.length() * (float)imageWidth );
xcoor = (int) xx;
}
else
xcoor = a;
if(seq2.length() > imageHeight)
{
float yy = (float) ( (float)b/(float)seq2.length() * (float)imageHeight );
ycoor = (int)yy;
}
else
ycoor = b;
g.drawLine(xcoor,ycoor,xcoor,ycoor);
}
/* Uses java.Graphics to set the points
* for the Gragh. this is somewhat of a culmination
* of methods.
*/
public void setUpPoints(Graphics g)
{
String base1 = null;
String base2 = null;
for(int i=0; i<seq1.length(); i++)
{
for(int j=0; j<seq2.length(); j++)
{
base1 = seq1.substring(i,i+1);
base2 = seq2.substring(j,j+1);
if(base1.equals(base2))
{
if(satisfiesThreshold(i,j))
drawPoint(i,j,g);
}
}
}
}
public void draw(Component c)
{
img = c.createImage(imageWidth,imageHeight);
Graphics g = img.getGraphics();
g.setColor(new Color(38,0,133));
setUpPoints(g);
}
}
__________________
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
|
|

04-15-2008, 03:54 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 22
|
|
|
Thanks alot man.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|