/** Transeq0.java ** ** This is the code for the base starting point in ** a sequence of exercises leading to the ultimate ** re-creation of Transequence.java. ** ** I am grateful to Jim Knight of CuraGen Corporation ** for permitting me to use and modify his original ** fmtseq.java applet, which can convert biological ** database sequences from one file format to another. ** ** I have now extensively changed, rewritten, and ** extended Jim's primarily databank conversion program ** to a pedagogical tool, Transequence.java, for ** extracting some interesting DNA sequence properties, ** but which does not do databank conversion. ** ** Author fmtseq.java: ** ** James Knight at CuraGen Corporation ** 322 E. Main St. ** Branford, CT 06405 ** E-mail: jknight@curagen.com ** Copyright (c) 1996-2001 James Knight at CuraGen Corp. ** ** HISTORY ** ** 9 Nov 1996 Original Implementation of fmtseq.java ** by James Knight ** ** 25 Mar 2001 Modified to FormatSeq.java for Java course ** by John Pais ** ** Author Transequence.java: ** ** John Pais at Interactive Mathvision ** E-mail: pais@kinetigram.com ** Copyright (c) 2001 John Pais ** ** HISTORY ** ** 30 May 2001 Implementation of Transequence.java ** by John Pais **/ import java.awt.*; import java.applet.*; import java.lang.*; import java.net.*; import java.util.*; public class Transeq0 extends Applet { /** Global Variables that the Applet's Methods Use to Communicate with ** the GridBagLayout GUI, Most of which are Initialized in init(). **/ TextArea input_text; Checkbox reverse_box, complement_box, tu_box, ut_box; TextArea output_text; boolean error_flag = false; // Global variable not currently being used by any method, // but set to true if error_message is called at the beginning // or at the end of the do_Transformations method. Color lightblue = new Color(76,155,255); // Background color. // Border colors used to paint borders, // and horizontal lines using fmtseqHRule method. Color blueline = new Color(76,140,255); // Light border color. Color blackline = new Color(0,0,0); // Dark border color. Color GBLColor = lightblue; // Initialize background color. Color light_border = blueline; // Initialize light border color. Color dark_border = blackline; // Initialize dark border color. public void init() { /** GridBagLayout Variables Used Only to Construct GUI. ** ** Note that these variables are named and introducted ** below in such a way that the row-by-row creation of ** the GridBagLayout (GBL) can by easily understood. **/ GridBagLayout GBL; GridBagConstraints GBC; Panel GBL_Row01_Panel, GBL_Row01_Col01_Panel, GBL_Row01_Col02_Panel; Label GBL_Row01_Col01_Label; Button GBL_Row01_Col02_Button; TextArea GBL_Row02_TextArea; fmtseqHRule GBL_Row03_HRule; // Instance of special class (below) to draw a horizontal rule. Panel GBL_Row04_Panel, GBL_Row04_Col01_Panel; Label GBL_Row04_Col01_Label; Panel GBL_Row05_Panel, GBL_Row05_Col01_Panel, GBL_Row05_Col01_Panel01, GBL_Row05_Col01_Panel02, GBL_Row05_Col01_Panel03; Checkbox GBL_Row05_Col01_Checkbox01, GBL_Row05_Col01_Checkbox02, GBL_Row05_Col01_Checkbox03a, GBL_Row05_Col01_Checkbox03b; fmtseqHRule GBL_Row06_HRule; // Instance of special class (below) to draw a horizontal rule. Panel GBL_Row10_Panel, GBL_Row10_Col01_Panel, GBL_Row10_Col02_Panel; Label GBL_Row10_Col01_Label; Button GBL_Row10_Col02_Button; TextArea GBL_Row11_TextArea; GridBagConstraints GBCRow11; Font TextAreafont; TextAreafont = new Font("Courier", Font.PLAIN, 14); // Create the font used by the text areas. /** Construct the Row-by-Row GridBagLayout GUI. **/ /** Set the layout for the GUI so that it flows row-by-row down the page, ** using GridBagLayout (GBL) (Java doesn't have a column-by-column flow ** layout). Note that with the GBL layout, each one of several rows of ** the GUI must be encapsulated in a panel of its own. ** ** Also, set the constraints so that none of the rows except the output ** text area can resize itself (so that the output text area grows to ** fill up the available space). **/ GBL = new GridBagLayout(); setLayout(GBL); GBC = new GridBagConstraints(); GBC.fill = GridBagConstraints.HORIZONTAL; GBC.gridwidth = GridBagConstraints.REMAINDER; /** Build the Input Section of the Row-by-Row GridBagLayout GUI. **/ /** Create Row 1 of the GridBagLayout **/ GBL_Row01_Panel = new Panel(); GBL_Row01_Panel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 3)); /** Create Row 1 Column 1 of the GridBagLayout **/ GBL_Row01_Col01_Panel = new Panel(); GBL_Row01_Col01_Panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); GBL_Row01_Col01_Label = new Label("Enter Input Sequence Below:"); GBL_Row01_Col01_Panel.add(GBL_Row01_Col01_Label); GBL_Row01_Panel.add(GBL_Row01_Col01_Panel); /** Create Row 1 Column 2 of the GridBagLayout **/ GBL_Row01_Col02_Panel = new Panel(); GBL_Row01_Col02_Panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); GBL_Row01_Col02_Button = new Button("Clear Input"); GBL_Row01_Col02_Panel.add(GBL_Row01_Col02_Button); GBL_Row01_Panel.add(GBL_Row01_Col02_Panel); GBL_Row01_Panel.setBackground(GBLColor); GBL.setConstraints(GBL_Row01_Panel, GBC); add(GBL_Row01_Panel); /** Create Row 2 of the GridBagLayout **/ GBL_Row02_TextArea = new TextArea(3, 60); input_text = GBL_Row02_TextArea; // Initialize global variable. GBL_Row02_TextArea.setFont(TextAreafont); GBL.setConstraints(GBL_Row02_TextArea, GBC); GBL_Row02_TextArea.setBackground(Color.white); add(GBL_Row02_TextArea); /** Create Row 3 of the GridBagLayout **/ GBL_Row03_HRule = new fmtseqHRule(); GBL_Row03_HRule.resize(20, 30); GBL_Row03_HRule.setBackground(GBLColor); GBL.setConstraints(GBL_Row03_HRule, GBC); add(GBL_Row03_HRule); /** Build the Transformation Section of the Row-by-Row GridBagLayout GUI. **/ /** Note: To work around the AWT's annoying habit of centering ** the labels of checkboxes that appear inside a grid ** layout, each checkbox is enclosed in its own Panel. **/ /** Create Row 4 of the GridBagLayout **/ GBL_Row04_Panel = new Panel(); GBL_Row04_Panel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 0)); /** Create Row 4 Column 1 of the GridBagLayout **/ GBL_Row04_Col01_Panel = new Panel(); GBL_Row04_Col01_Panel.setLayout(new FlowLayout(FlowLayout.LEFT, 12, 0)); GBL_Row04_Col01_Label = new Label("Basic Transformations:"); GBL_Row04_Col01_Panel.add(GBL_Row04_Col01_Label); GBL_Row04_Panel.add(GBL_Row04_Col01_Panel); GBL_Row04_Panel.setBackground(GBLColor); GBL.setConstraints(GBL_Row04_Panel, GBC); add(GBL_Row04_Panel); /** Create Row 5 of the GridBagLayout **/ GBL_Row05_Panel = new Panel(); GBL_Row05_Panel.setLayout(new FlowLayout(FlowLayout.LEFT, 35, 8)); /** Create Row 5 Column 1 of the GridBagLayout **/ GBL_Row05_Col01_Panel = new Panel(); GBL_Row05_Col01_Panel.setLayout(new GridLayout(3, 1)); GBL_Row05_Col01_Panel01 = new Panel(); GBL_Row05_Col01_Panel01.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); GBL_Row05_Col01_Checkbox01 = new Checkbox("Reverse"); reverse_box = GBL_Row05_Col01_Checkbox01; // Initialize global variable. GBL_Row05_Col01_Panel01.add(GBL_Row05_Col01_Checkbox01); GBL_Row05_Col01_Panel.add(GBL_Row05_Col01_Panel01); GBL_Row05_Col01_Panel02 = new Panel(); GBL_Row05_Col01_Panel02.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); GBL_Row05_Col01_Checkbox02 = new Checkbox("Complement"); complement_box = GBL_Row05_Col01_Checkbox02; // Initialize global variable. GBL_Row05_Col01_Panel02.add(GBL_Row05_Col01_Checkbox02); GBL_Row05_Col01_Panel.add(GBL_Row05_Col01_Panel02); GBL_Row05_Col01_Panel03 = new Panel(); GBL_Row05_Col01_Panel03.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); GBL_Row05_Col01_Checkbox03a = new Checkbox("T -> U"); tu_box = GBL_Row05_Col01_Checkbox03a; // Initialize global variable. GBL_Row05_Col01_Panel03.add(GBL_Row05_Col01_Checkbox03a); GBL_Row05_Col01_Checkbox03b = new Checkbox("U -> T"); ut_box = GBL_Row05_Col01_Checkbox03b; // Initialize global variable. GBL_Row05_Col01_Panel03.add(GBL_Row05_Col01_Checkbox03b); GBL_Row05_Col01_Panel.add(GBL_Row05_Col01_Panel03); GBL_Row05_Panel.add(GBL_Row05_Col01_Panel); GBL_Row05_Panel.setBackground(GBLColor); GBL.setConstraints(GBL_Row05_Panel, GBC); add(GBL_Row05_Panel); /** Create Row 6 of the GridBagLayout **/ GBL_Row06_HRule = new fmtseqHRule(); GBL_Row06_HRule.resize(20, 24); GBL_Row06_HRule.setBackground(GBLColor); GBL.setConstraints(GBL_Row06_HRule, GBC); add(GBL_Row06_HRule); /** Build the Output Section of the Row-by-Row GridBagLayout GUI. **/ /** Create Row 10 of the GridBagLayout **/ GBL_Row10_Panel = new Panel(); GBL_Row10_Panel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 3)); /** Create Row 10 Column 1 of the GridBagLayout **/ GBL_Row10_Col01_Panel = new Panel(); GBL_Row10_Col01_Panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); GBL_Row10_Col01_Label = new Label("Output Window Below:"); GBL_Row10_Col01_Panel.add(GBL_Row10_Col01_Label); GBL_Row10_Panel.add(GBL_Row10_Col01_Panel); /** Create Row 10 Column 2 of the GridBagLayout **/ GBL_Row10_Col02_Panel = new Panel(); GBL_Row10_Col02_Panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); GBL_Row10_Col02_Button = new Button("Process Input Sequence"); GBL_Row10_Col02_Panel.add(GBL_Row10_Col02_Button); GBL_Row10_Panel.add(GBL_Row10_Col02_Panel); GBL_Row10_Panel.setBackground(GBLColor); GBL.setConstraints(GBL_Row10_Panel, GBC); add(GBL_Row10_Panel); /** Create Row 11 of the GridBagLayout **/ GBL_Row11_TextArea = new TextArea(); output_text = GBL_Row11_TextArea; // Initialize global variable. GBL_Row11_TextArea.setFont(TextAreafont); GBCRow11 = new GridBagConstraints(); GBCRow11.fill = GridBagConstraints.BOTH; GBCRow11.weightx = 1.0; GBCRow11.weighty = 1.0; GBL.setConstraints(GBL_Row11_TextArea, GBCRow11); GBL_Row11_TextArea.setBackground(Color.white); add(GBL_Row11_TextArea); do_Assignment(); // Print assigment in output text area. } /** Paint the 3-D border around the edge of the applet ** using two fillPolygon calls. **/ public void paint(Graphics g) { int x[] = new int[6]; int y[] = new int[6]; //Color light_border = new Color(224, 224, 224); //Color dark_border = new Color(128, 128, 128); Dimension d = size(); setBackground(GBLColor); x[0] = 0; y[0] = 0; x[1] = d.width; y[1] = 0; x[2] = d.width - 3; y[2] = 3; x[3] = 3; y[3] = 3; x[4] = 3; y[4] = d.height - 3; x[5] = 0; y[5] = d.height; g.setColor(light_border); g.fillPolygon(x, y, 6); x[0] = d.width; y[0] = d.height; x[1] = 0; y[1] = d.height; x[2] = 3; y[2] = d.height - 3; x[3] = d.width - 3; y[3] = d.height - 3; x[4] = d.width - 3; y[4] = 3; x[5] = d.width; y[5] = 0; g.setColor(dark_border); g.fillPolygon(x, y, 6); super.paint(g); } /** Set GridBagLayout Insets for Border of GUI **/ public Insets insets() { return new Insets(8, 8, 8, 8); } /** fmtseqHRule ** ** This class is essentially a programming trick to get a 3D horizontal ** line to appear across a canvas (not the one being extended here, but ** the one where this class is used). ** ** The class itself just creates a canvas where a 3D horizontal line ** is drawn through the middle of it. The trick occurs when it is used. ** When creating an instance of the class, use the resize method to fix ** an initial height for the instance and then set the layout manager ** so that it freezes that height (while letting the width be resizable, ** so that it fills out to the sides of the frame or panel). When drawn, ** redrawn or resized, what you get is a horizontal line across the frame ** or panel. Cool, eh? (JK) Indeed! (JP) **/ class fmtseqHRule extends Canvas { public void paint(Graphics g) { int base; Dimension d = size(); base = (d.height - 1) / 2; g.setColor(light_border); g.drawLine(0, base, d.width, base); g.setColor(dark_border); g.drawLine(0, base + 1, d.width, base + 1); } } private void do_Assignment() { String output_text_Help = "\n ASSIGNMENT FOR EXERCISE 1\n" + "\n Beginning with the code for Transeq0(click on link above)" + "\n create your own version of Transeq1, so that it has the" + "\n same functionality as the one at right, as follows:\n" + "\n 1. Write a do_Help method that prints the help message" + "\n in the input area when the applet is initialized. " + "\n 2. Write methods to perform the appropriate sequence" + "\n operation when one of the four checkboxes is checked." + "\n (What does Transeq0 currently do?)\n"; output_text.setText(output_text_Help); output_text.select(0,0); } /** End of Construction of the Row-by-Row GridBagLayout GUI. **/ /** Detect GUI Action Event and Return Appropriate Response **/ public boolean action(Event evt, Object arg) { if (arg.equals("Clear Input")) // Button event: clear input text area. { input_text.setText(""); return true; } else if (arg.equals("Process Input Sequence")) // Button event: apply Transformations. { do_Transformations(); return true; } return false; } private void do_Transformations() { String instring; instring = input_text.getText(); // Initialize string version of input text. if (instring.length() == 0) { error_message("There is no input."); return; } char[] inbuffer; inbuffer = instring.toCharArray(); // Create character array version of input text. int inbuflen; inbuflen = instring.length(); char[] seq; // Create seq char array of appropriate seq = new char[inbuflen]; // dimension to store and modify input sequence. int seqlen = 0; for (int pos = 0; pos < inbuflen; pos++) { if (!isDigit(inbuffer[pos]) && !isSpace(inbuffer[pos])) { seq[seqlen] = inbuffer[pos]; // Remove digits and spaces from seq seqlen++; // and adjust seqlen appropriately. } // This is the version of seq that } // is used throughout this method. output_text.setText(""); // Initialize string version of output text. String output_text_String = ""; /** Determine the Transformation Choices. **/ boolean reverse_mode, complement_mode, tu_mode, ut_mode; /** Basic Transformations Choices **/ reverse_mode = reverse_box.getState(); complement_mode = complement_box.getState(); tu_mode = tu_box.getState(); ut_mode = ut_box.getState(); /** Define Booleans For Basic Transformations Row and Advanced Transformations Row **/ boolean BasicTransRow = true; /** Begin Basic Transformations Row **/ if (BasicTransRow) { /** Begin Basic Transformations Row, Column 1 **/ if (reverse_mode) { reverse_box.setState(false); } if (complement_mode) { complement_box.setState(false); } if (tu_mode && ut_mode) { tu_box.setState(false); // Disallow simultaneous choice of both ut_box.setState(false); // T->U and U->T transformations. } else if (tu_mode && !ut_mode) { tu_box.setState(false); } else if (!tu_mode && ut_mode) { ut_box.setState(false); } /** Begin Print for Basic Transformations Row **/ output_text_String = output_text_String + "\n" + String.copyValueOf(seq, 0, seqlen); } if (seqlen == 0) { error_message("No sequence to output."); // Returned when input comprised } // of digits and/or spaces. else { output_text.setText(output_text_String); // Print output. output_text.select(0, 0); // Position cursor at upper left } // hand corner of output window. } /** Several Helpful Utility Methods **/ private boolean isAlpha(char ch) { return Character.isLetter(ch); } private boolean isAlNum(char ch) { return Character.isLetterOrDigit(ch); } private boolean isLowerCase(char ch) { return Character.isLowerCase(ch); } private char toLowerCase(char ch) { return (isUpperCase(ch) ? Character.toLowerCase(ch) : ch); } private boolean isUpperCase(char ch) { return Character.isUpperCase(ch); } private char toUpperCase(char ch) { return (isLowerCase(ch) ? Character.toUpperCase(ch) : ch); } private boolean isSpace(char ch) { return Character.isSpace(ch); } private boolean isDigit(char ch) { return Character.isDigit(ch); } private void error_message(String s) { output_text.setText("\n\n Error: " + s); error_flag = true; } }