// Implementation of some algorithms for pairwise alignment from
// Durbin et al: Biological Sequence Analysis, CUP 1998, chapter 2.
// A primitive graphical user interface
// Peter Sestoft, sestoft@itu.dk 1999-09-27, 2000-04-05 version 1.1 dina
// Reference:  http://www.itu.dk/people/sestoft/bsa.html

// License: Anybody can use this code for any purpose, including
// teaching, research, and commercial purposes, provided proper
// reference is made to its origin.  Neither the author nor the Royal
// Veterinary and Agricultural University, Copenhagen, Denmark, can
// take any responsibility for the consequences of using this code.

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class MatchAppletDinaWS extends Applet {

  TextField seq1in = new TextField("HEAGAWGHEE");
  TextField seq2in = new TextField("PAWHEAE");
  Button alignButton = new Button("Compute alignment");
  TextArea outa = new TextArea(15, 80);

  public void init() {
    // Set font to Courier
    Font courier = new Font("Courier", Font.PLAIN, 12);
    seq1in.setFont(courier); seq2in.setFont(courier); outa.setFont(courier); 
    // Lay out the input fields above the button above the output field
    setLayout(new BorderLayout());
    Panel input = new Panel();
    input.setLayout(new BorderLayout());
    input.add(seq1in, "North"); 
    input.add(seq2in, "Center"); 
    Panel butpanel = new Panel();
    final Checkbox printf = new Checkbox("Display F matrix");
    butpanel.add(printf);
    butpanel.add(alignButton);
    input.add(butpanel, "South");
    add(input, "North");
    add(outa, "Center");

    final Substitution sub = new Blosum50();
    alignButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
	String seq1 = seq1in.getText(), seq2 = seq2in.getText();
	Output out = new Output () {
	  public void print(String s)
	  { outa.append(s); }
	  
	  public void println(String s)
	  { outa.append(s); outa.append("\n"); }
	  
	  public void println()
	  { outa.append("\n"); }
	};

	outa.setText("");
      (new NW      (sub, 8,     seq1, seq2)).
	domatch(out, "GLOBAL ALIGNMENT", printf.getState());
      (new SW      (sub, 8,     seq1, seq2)).
	domatch(out, "LOCAL ALIGNMENT", printf.getState());
      (new NWAffine(sub, 12, 2,  seq1, seq2)).
	domatch(out, "AFFINE GLOBAL", printf.getState());
      (new NWSmart (sub, 8,     seq1, seq2)).
	domatch(out, "SMART GLOBAL", printf.getState());
      (new SWSmartAffine (sub, 12, 2, seq1, seq2)).
	domatch(out, "SMART AFFINE LOCAL", printf.getState());
      }
    });
  }
}
