package jalview.gui; import java.awt.*; import java.awt.event.*; import jalview.datamodel.*; import javax.swing.*; import jalview.schemes.*; import jalview.analysis.*; public class SeqPanel extends JPanel { public SeqCanvas seqCanvas; public AlignmentPanel parent; protected int startres; protected int lastres; protected int endres; protected int startseq; protected int padseq; public boolean editFlag; protected AlignViewport av; public SeqPanel(AlignViewport av, AlignmentPanel p) { this.av = av; seqCanvas = new SeqCanvas(av); setLayout(new BorderLayout()); add(seqCanvas, BorderLayout.CENTER); parent = p; addMouseMotionListener( new MouseMotionAdapter() { public void mouseMoved(MouseEvent evt) { doMouseMoved(evt); } public void mouseDragged(MouseEvent evt) { doMouseDragged(evt); } }); addMouseListener( new MouseAdapter() { public void mouseReleased(MouseEvent evt) { doMouseReleased(evt); } public void mousePressed(MouseEvent evt) { doMousePressed(evt); } }); repaint(); } public void doMouseReleased(MouseEvent evt) { int x = evt.getX(); int res = (int)(x/av.getCharWidth()) + av.getStartRes(); endres = res; // This is to detect edits - we're at the end of an edit if mouse is up editFlag = false; startseq = -1; startres = -1; lastres = -1; parent.RefreshPanels(); repaint(); } public void doMousePressed(MouseEvent evt) { parent.alignFrame.addHistoryItem("sequence edit"); int seq; int res; int x = evt.getX(); int y = evt.getY(); res = (int)(x/av.getCharWidth()) + av.getStartRes(); seq = (y)/av.getCharHeight() + av.getStartSeq(); if (seq < av.getAlignment().getHeight() && res < av.getAlignment().getSequenceAt(seq).getLength()) { //char resstr = align.getSequenceAt(seq).getSequence().charAt(res); // Find the residue's position in the sequence (res is the position // in the alignment startseq = seq; if (startseq == (av.getAlignment().getHeight()-1)) { padseq = 1; } else { padseq = 1; } startres = res; lastres = res; } else { startseq = -1; startres = -1; lastres = -1; } return; } public void doMouseMoved(MouseEvent evt) { int res=0, seq=0; int x = evt.getX(); int y = evt.getY(); if(av.wrapAlignment) { y -= 2*av.charHeight; int chunkHeight = (av.getAlignment().getHeight()+2)*av.charHeight; res = (int)((y/chunkHeight)*(getWidth()/av.charWidth)) + (int)(x/av.getCharWidth()) + av.getStartRes(); System.out.println( ((y/chunkHeight)*(getWidth()/av.charWidth)) +" "+((x / av.getCharWidth()) + av.getStartRes()) ); y %= chunkHeight; seq = y / av.getCharHeight() + av.getStartSeq(); // chunkHeight = (da.getHeight() + 2)*charHeight; // startx += chunkWidth; } else { res = (int) (x / av.getCharWidth()) + av.getStartRes(); seq = y / av.getCharHeight() + av.getStartSeq(); } if(seq>=av.getAlignment().getHeight()) return; Object obj = ResidueProperties.aa2Triplet.get( av.getAlignment().getSequenceAt(seq).getCharAt(res)+"" ) ; String aa = ""; if(obj!=null) aa = obj.toString(); StringBuffer text = new StringBuffer("Sequence " +(seq+1)+" ID: "+av.getAlignment().getSequenceAt(seq).getName()); if(aa!="") text.append(" Residue: "+aa+" ("+ av.getAlignment().getSequenceAt(seq).findPosition(res)+")"); parent.alignFrame.statusBar.setText(text.toString()); } public void doMouseDragged(MouseEvent evt) { // If we're dragging we're editing editFlag = true; int x = evt.getX(); int res = (int)(x/av.getCharWidth()) + av.getStartRes(); if (res < 0) {res = 0;} if (res != lastres) { if (startseq != -1) { // Group editing if (av.getGroupEdit()) { SequenceGroup sg = av.getAlignment().findGroup(startseq); if(sg!=null) if (res < av.getAlignment().getWidth() && res < lastres) { boolean flag = false; for (int i= 0 ; i < sg.getSize(); i++) { SequenceI s = (SequenceI)sg.getSequenceAt(i); for (int j=lastres-1; j >= res; j--) { if (!flag) { if (!s.getSequence().substring(j,j+1).equals(av.getAlignment().getGapCharacter()) && !s.getSequence().substring(j,j+1).equals(" ")) { res = j+1; flag = true; } } } } } if(sg!=null) for (int i= 0 ; i < sg.getSize(); i++) { SequenceI s = (SequenceI)sg.getSequenceAt(i); boolean found = false; int sno = -1; for (int k = 0; k < av.getAlignment().getHeight(); k++) { if (av.getAlignment().getSequenceAt(k) == s) { found = true; sno = k; break; } } if (found && sno != -1) { if (res < av.getAlignment().getWidth() && res > lastres) { for (int j = lastres; j < res; j++) insertChar(j,sno); int index = av.getAlignment().findIndex(s); if (index != -1) drawChars(index,index+1,lastres); } else if (res < av.getAlignment().getWidth() && res < lastres) { for (int j = res; j < lastres; j++) { deleteChar(j,res,sno); startres = res; } int index = av.getAlignment().findIndex(s); if (index != -1) drawChars(index,index+1,res); } } } lastres = res; } else { if (res < av.getAlignment().getWidth() && res > lastres) { // dragging to the right for (int j = lastres; j < res; j++) insertChar(j,startseq); drawChars(startseq,startseq+1,lastres); } else if (res < av.getAlignment().getWidth() && res < lastres) { // dragging to the left for (int j = res; j < lastres; j++) { deleteChar(j,res,startseq); startres = res; } drawChars(startseq,startseq+1,res); } } } lastres = res; } repaint(); return; } public void drawChars(int seqstart, int seqend, int start) { seqCanvas.drawPanel(seqCanvas.gg, start,av.getEndRes(),seqstart,seqend,av.getStartRes(),av.getStartSeq(),0); repaint(); } public void insertChar(int j, int seq) { av.getAlignment().getSequenceAt(seq).insertCharAt(j, av.getGapCharacter().charAt(0)); updateConservation(seq); } public void deleteChar(int j, int res, int sno) { if (av.getAlignment().getSequenceAt(sno).getSequence().substring(j,j+1).equals(".") || av.getAlignment().getSequenceAt(sno).getSequence().substring(j,j+1).equals("-") || av.getAlignment().getSequenceAt(sno).getSequence().substring(j,j+1).equals(" ") ) { av.getAlignment().getSequenceAt(sno).deleteCharAt(j); } updateConservation(sno); av.getAlignment().getWidth(); repaint(); } void updateConservation(int i) { Alignment al = (Alignment) av.getAlignment(); SequenceGroup sg = av.alignment.findGroup( al.getSequenceAt(i)); if(sg==null || !(sg.cs instanceof ConservationColourScheme)) return; Conservation c = sg.getConservation(); c = new Conservation("All", al.cons, ResidueProperties.propHash, 3, sg.sequences, 0, al.getWidth()); c.calculate(); c.verdict(false, 100); sg.setConservation(c); ConservationColourScheme ccs = (ConservationColourScheme)sg.cs; ccs.conserve = c; // sg.cs = new ConservationColourScheme(sg); } public void setColourScheme(ColourSchemeI cs, boolean showConservation) { if (av.getSelection().size() == 0) { seqCanvas.cs = cs; for (int i = 0; i < av.alignment.getGroups().size();i++) { SequenceGroup sg = (SequenceGroup)av.alignment.getGroups().elementAt(i); sg.cs = cs; if(!showConservation && !(sg.cs instanceof Blosum62ColourScheme) && sg.cs instanceof ConservationColourScheme) { // remove ConservationColouring from existing Conservation group ConservationColourScheme ccs = (ConservationColourScheme)sg.cs; sg.cs = ccs.cs; } else if(showConservation && !(sg.cs instanceof ConservationColourScheme)) { // add ConservationColouring to new group Conservation c = sg.getConservation(); Alignment al = (Alignment) av.getAlignment(); c = new Conservation("All", al.cons, ResidueProperties.propHash, 3, sg.sequences, 0, al.getWidth() ); c.calculate(); c.verdict(false, 100); sg.setConservation(c); sg.cs = new ConservationColourScheme(sg); } } } else { SequenceGroup sg = av.alignment.addGroup(); sg.cs = cs; for (int i=0; i < av.getSelection().size(); i++) { av.alignment.removeFromGroup(av.alignment.findGroup((Sequence)av.sel.sequenceAt(i)), (Sequence)av.sel.sequenceAt(i)); av.alignment.addToGroup(sg,(Sequence)av.sel.sequenceAt(i)); } // Selection is made, we only want to change the conservationColour for selected group if( showConservation && !(sg.cs instanceof ConservationColourScheme) && !(sg.cs instanceof Blosum62ColourScheme)) { Conservation c = sg.getConservation(); Alignment al = (Alignment) av.getAlignment(); c = new Conservation("All", al.cons, ResidueProperties.propHash, 3, sg.sequences, 0, al.getWidth() ); c.calculate(); c.verdict(false, 100); sg.setConservation(c); sg.cs = new ConservationColourScheme(sg); } else if( !showConservation && sg.cs instanceof ConservationColourScheme) { ConservationColourScheme ccs = (ConservationColourScheme)sg.cs; sg.cs = ccs.cs; } } seqCanvas.paintFlag = true; repaint(); } public ColourSchemeI getColourScheme() { if(av.getSelection().size()>0) return av.alignment.findGroup((Sequence)av.sel.sequenceAt(0)).cs; else return seqCanvas.cs; } }