View Single Post
  #5 (permalink)  
Old 04-15-2008, 01:37 PM
CaptainMorgan's Avatar
CaptainMorgan CaptainMorgan is offline
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 737
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
As requested:

GraphPane

Code:
/** * 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
Code:
/** * 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
Code:
/** * 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); } }
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on July 13, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Reply With Quote