[Biojava-l] Re: Sequence Color Coding

Michael L. Heuer heuermh@acm.org
Tue, 6 Aug 2002 15:50:43 -0400 (EDT)


Hello Satya,

Attached is a very simple example using SequencePoster.  You can use more
interesting SequenceRenderer(s) to change what is drawn in the sequence
table cells.

   michael

---

//:  SequenceTableExample.java

import java.awt.Component;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

import org.biojava.bio.seq.*;
import org.biojava.bio.seq.impl.*;
import org.biojava.bio.symbol.*;
import org.biojava.bio.gui.sequence.*;

import org.biojava.utils.*;

public class SequenceTableExample {
    public static void main(String args[]) {
	SequenceTableExample ste = new SequenceTableExample();
    }

    public SequenceTableExample() {
	try {

	    SequenceFactory sf = new SimpleSequenceFactory();

	    Sequence s1 = sf.createSequence(DNATools.createDNA("actgtgatcgactg"),
					    "seq1",
					    "seq1",
					    null);

	    Sequence s2 = sf.createSequence(DNATools.createDNA("tgtgctatgtactg"),
					    "seq2",
					    "seq2",
					    null);

	    SequenceTable st = new SequenceTable(new Sequence[] { s1, s2 });

	    JFrame f = new JFrame("Sequence table example");
	    f.getContentPane().add(new JScrollPane(st));

	    f.setBounds(40,40,400,300);
	    f.setVisible(true);

	} catch (IllegalSymbolException e) {}
    }

    private class SequenceTable
	extends JTable {

	public SequenceTable(List sequences) {
	    super();
	    setModel(new SequenceTableModel(sequences));
	    setDefaultRenderer(Sequence.class, new SequencePanelCellRenderer());
	    setRowHeight(24);
	}
	public SequenceTable(Sequence[] sequences) {
	    super();
	    setModel(new SequenceTableModel(Arrays.asList(sequences)));
	    setDefaultRenderer(Sequence.class, new SequencePanelCellRenderer());
	    setRowHeight(24);
	}
    }

    private class SequencePanelCellRenderer
	implements TableCellRenderer {

	private SequencePoster sp;

	public SequencePanelCellRenderer() {
	    try {
		sp = new SequencePoster();
		sp.setDirection(SequencePoster.HORIZONTAL);
		sp.setRenderer(new SymbolSequenceRenderer());
	    } catch (ChangeVetoException e) {}
	}
	public SequencePoster getSequencePoster() {
	    return sp;
	}

	public Component getTableCellRendererComponent(JTable t,
						       Object value,
						       boolean isSelected,
						       boolean hasFocus,
						       int row,
						       int column)
	{
	    Sequence s = (Sequence) value;
	    sp.setSequence(s);
	    return sp;
	}
    }

    private class SequenceTableModel
	extends AbstractTableModel {

	private List sequences;

	public SequenceTableModel(List sequences) {
	    super();
	    this.sequences = sequences;
	}
	public void addSequence(Sequence seq) {
	    sequences.add(seq);
	    fireTableDataChanged();
	}
	public void removeSequence(Sequence seq) {
	    sequences.remove(seq);
	    fireTableDataChanged();
	}

	public int getRowCount() {
	    return sequences.size();
	}
	public int getColumnCount() {
	    return 3;
	}
	public String getColumnName(int index) {
	    switch (index) {
	    case 0:
		return "Name";
	    case 1:
		return "URN";
	    case 2:
		return "Sequence";
	    default:
		return "";
	    }
	}
	public Class getColumnClass(int index) {
	    switch (index) {
	    case 0:
		return String.class;
	    case 1:
		return String.class;
	    case 2:
		return Sequence.class;
	    default:
		return null;
	    }
	}
	public Object getValueAt(int row, int col) {
	    Sequence s = (Sequence) sequences.get(row);

	    switch (col) {
	    case 0:
		return s.getName();
	    case 1:
		return s.getURN();
	    case 2:
		return s;
	    default:
		return null;
	    }
	}
    }
}

On Mon, 5 Aug 2002, Matthew Pocock wrote:

> satyanarayana pasupuleti wrote:
> >
> > Hello!!
> >
> > Thanks a lot for your help, Jim and Mathew. It appears to me that the
> > sequences are rendered on to a SequencePanel object. Is there any way
> > color coded sequences can be rendered in a JTable?
> >
> > Thanks a lot
> > -satya
> >
> > _________________________________________________________________
> > MSN Photos is the easiest way to share and print your photos:
> > http://photos.msn.com/support/worldwide.aspx
> >
>
> The JList and JTable (and other swing things) delegate rendering to cell
> renderers. These are just normal swing components, so you can delegate
> to a SequencePannel or whatever. Take a look at the API javadocs for
> JList for a code example called MyCellRenderer. I have never used
> biojava renderers in this way, but I have written my own renderers for
> other data types before and it seems to work.
>
> Good luck.
>
> Matthew
>
> __________________________________________________
> Do You Yahoo!?
> Everything you'll ever need on one web page
> from News and Sport to Email and Music Charts
> http://uk.my.yahoo.com
>
> _______________________________________________
> Biojava-l mailing list  -  Biojava-l@biojava.org
> http://biojava.org/mailman/listinfo/biojava-l
>