Skip to content
Snippets Groups Projects
DataViewInternalFrame.java 11.3 KiB
Newer Older
  • Learn to ignore specific revisions
  • tjc's avatar
    tjc committed
    /*
     *
     * created: Wed Aug 3 2004
     *
     * This file is part of Artemis
     *
     * Copyright(C) 2000  Genome Research Limited
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * as published by the Free Software Foundation; either version 2
     * of the License, or(at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program; if not, write to the Free Software
     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     *
     */
    
    package uk.ac.sanger.artemis.editor;
    
    import javax.swing.*;
    
    tjc's avatar
    tjc committed
    import java.awt.*;
    import javax.swing.event.ChangeListener;
    import javax.swing.event.ChangeEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    
    tjc's avatar
    tjc committed
    import java.util.Vector;
    
    tjc's avatar
    tjc committed
    import java.util.StringTokenizer;
    import java.util.Enumeration;
    
    tjc's avatar
    tjc committed
    import java.io.File;
    
    tjc's avatar
    tjc committed
    import java.io.BufferedReader;
    import java.io.StringReader;
    import java.io.IOException;
    
    tjc's avatar
    tjc committed
    
    public class DataViewInternalFrame extends JInternalFrame
    {
      private JTabbedPane tabPane = new JTabbedPane();
    
    tjc's avatar
    tjc committed
      private Annotation ann;
    
    tjc's avatar
    tjc committed
      private Box evidenceBox;
    
    tjc's avatar
    tjc committed
      private Vector fastaCollection = new Vector();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
      public DataViewInternalFrame(Object dataFile[], JDesktopPane desktop,
    
    tjc's avatar
    tjc committed
                                   final JScrollPane scrollEvidence,
                                   int wid, int hgt, String qualifier_txt)
    
    tjc's avatar
    tjc committed
      {
        super("Document " + dataFile[0], 
                  true, //resizable
                  true, //closable
                  true, //maximizable
                  true);//iconifiable
    
    
    tjc's avatar
    tjc committed
    // graphical evidence display
        JInternalFrame evidence = new JInternalFrame("Evidence", true,
                                                     true, true, true);
        JPanel evidencePanel = (JPanel)evidence.getContentPane();
        evidenceBox = Box.createVerticalBox();
    
    //
    
    tjc's avatar
    tjc committed
        ann = new Annotation(desktop);
    
    tjc's avatar
    tjc committed
    
        StringBuffer annFormat = new StringBuffer();
    
    tjc's avatar
    tjc committed
        annFormat.append(htmlBreaks(qualifier_txt.trim()));
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        for(int i=0; i<dataFile.length; i++)
        {
    
    tjc's avatar
    tjc committed
          //ensure results file exists
          File fdata = new File((String)dataFile[i]);
          if(!fdata.exists())
          {
            fdata = new File((String)dataFile[i]+".gz");
            
            if(!fdata.exists())
            {
              JOptionPane.showMessageDialog(desktop, "Results file: \n"+
                                          dataFile[i] + "\ndoes not exist!",
                                          "File Not Found",
                                          JOptionPane.WARNING_MESSAGE);
              continue;
            }
          }
      
    
    tjc's avatar
    tjc committed
          String tabName = (String)dataFile[i];
          int ind = tabName.lastIndexOf("/");
          if(ind > -1)
          {
            String go = "";
    
            if(tabName.indexOf("blastp+go") > -1)
              go = ":: GO :: ";
            tabName = go + tabName.substring(ind+1);
          }
    
    
    tjc's avatar
    tjc committed
          // add fasta results internal frame
    
    tjc's avatar
    tjc committed
          FastaTextPane fastaPane = new FastaTextPane((String)dataFile[i]);
    
    tjc's avatar
    tjc committed
          fastaCollection.add(fastaPane);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          if(qualifier_txt.indexOf("/"+fastaPane.getFormat()+"_file=\"") == -1)
          {
            if(i > 0)
              annFormat.append("\n<br>");
            annFormat.append("/"+fastaPane.getFormat()+"_file=\""+
                                         dataFile[i]+"\"");
          }
    
    tjc's avatar
    tjc committed
    
          // graphical view
    
    tjc's avatar
    tjc committed
          final JScrollPane dbviewScroll = new JScrollPane();
          final DBViewer dbview = new DBViewer(fastaPane,dbviewScroll);
    
    tjc's avatar
    tjc committed
          dbviewScroll.setViewportView(dbview);
    
    tjc's avatar
    tjc committed
    
          final Dimension d = new Dimension((int)dbviewScroll.getPreferredSize().getWidth(), 
                                           hgt/3);
    
    tjc's avatar
    tjc committed
    
          final Box yBox = Box.createVerticalBox();
    
    tjc's avatar
    tjc committed
          final Box xBox = Box.createHorizontalBox();
          final MouseOverButton hide = new MouseOverButton("X");
          hide.setForeground(Color.blue);
          hide.setBackground(Color.white);
          hide.setFont(BigPane.font);
          hide.setMargin(new Insets(0,0,0,0));
          hide.setBorderPainted(false);
          hide.setActionCommand("HIDE");
    
          final Box bacross = Box.createHorizontalBox();
          bacross.add(dbviewScroll);
    
          hide.addActionListener(new ActionListener()
          {
            public void actionPerformed(ActionEvent event)
            {
              if(hide.getActionCommand().equals("HIDE"))
              {
                bacross.remove(dbviewScroll);
    
    tjc's avatar
    tjc committed
                bacross.add(yBox);
    
    tjc's avatar
    tjc committed
                hide.setText("+");
                scrollEvidence.setViewportView(evidenceBox);
                hide.setActionCommand("SHOW");
              }
              else
              {
    
    tjc's avatar
    tjc committed
                bacross.remove(yBox);
                dbviewScroll.setColumnHeaderView(yBox);
    
    tjc's avatar
    tjc committed
                bacross.add(dbviewScroll);
                hide.setText("X");
                scrollEvidence.setViewportView(evidenceBox);
                hide.setActionCommand("HIDE");
              }
            }
          });
    
          xBox.add(hide);
          JLabel tabLabel = new JLabel(fastaPane.getFormat()+" "+tabName);
          tabLabel.setFont(BigPane.font);
    
          tabLabel.setOpaque(true);
          xBox.add(tabLabel);
          xBox.add(Box.createHorizontalGlue());
    
    
    tjc's avatar
    tjc committed
          yBox.add(xBox);
          yBox.add(dbview.getRuler());
    
    
    tjc's avatar
    tjc committed
          dbviewScroll.setPreferredSize(d);
    
    tjc's avatar
    tjc committed
          dbviewScroll.setColumnHeaderView(yBox);
    
    tjc's avatar
    tjc committed
          fastaPane.addFastaListener(dbview);
    
    tjc's avatar
    tjc committed
          evidenceBox.add(bacross);
        
    
    tjc's avatar
    tjc committed
          // add data pane
          DataCollectionPane dataPane =
    
    tjc's avatar
    tjc committed
             new DataCollectionPane(fastaPane,ann,desktop,this);
    
    tjc's avatar
    tjc committed
          fastaPane.addFastaListener(dataPane);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          ActiveJSplitPane split = new ActiveJSplitPane(JSplitPane.VERTICAL_SPLIT,
                                                        fastaPane,dataPane);
          split.setLabel(tabLabel);
    
    tjc's avatar
    tjc committed
          split.setDividerLocation(250);
    
    tjc's avatar
    tjc committed
          split.setOneTouchExpandable(true);
    
    tjc's avatar
    tjc committed
          if(i == 0)
            split.setActive(true);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          tabPane.add(fastaPane.getFormat()+" "+tabName,split);
    
    tjc's avatar
    tjc committed
        }
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
    //  evidenceBox.add(Box.createVerticalGlue());
    
    tjc's avatar
    tjc committed
      
    
    tjc's avatar
    tjc committed
        // add tab pane listener
        tabPane.addChangeListener(new TabChangeListener());
    
    
    tjc's avatar
    tjc committed
        // add setActive annotator text pane
    
    tjc's avatar
    tjc committed
        ann.setAnnotation(annFormat.toString().trim());
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        JScrollPane annotationScroll = new JScrollPane(ann);   
    
    tjc's avatar
    tjc committed
        annotationScroll.setPreferredSize(new Dimension(500,150));
    
    tjc's avatar
    tjc committed
    
        JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                                          annotationScroll,tabPane);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        split.setDividerLocation(150);
    
    tjc's avatar
    -R  
    tjc committed
        getContentPane().add(split);
    
    tjc's avatar
    tjc committed
         
        setVisible(true);
    
    tjc's avatar
    tjc committed
        evidence.setVisible(true);
        desktop.add(evidence);
      }
    
    
      protected Box getEvidenceBox()
      {
        return evidenceBox;
    
    tjc's avatar
    tjc committed
      }
    
    
    tjc's avatar
    tjc committed
      protected String getFeatureText()
      {
        return ann.getFeatureText();
      }
    
    
    tjc's avatar
    tjc committed
      protected void reReadSelectedResults()
      {
    
    tjc's avatar
    tjc committed
        ActiveJSplitPane split = (ActiveJSplitPane)tabPane.getSelectedComponent();
    
    tjc's avatar
    tjc committed
        Component comps[] = split.getComponents();
        for(int i=0; i<comps.length;i++)
        {
    
    tjc's avatar
    tjc committed
          if(comps[i] instanceof FastaTextPane)
    
    tjc's avatar
    tjc committed
          {
    
    tjc's avatar
    tjc committed
            ((FastaTextPane)comps[i]).reRead();
            return;
    
    tjc's avatar
    tjc committed
          }
        }
      }
    
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
      protected void stopGetz()
      {
        Enumeration fastaEnum = fastaCollection.elements();
    
        while(fastaEnum.hasMoreElements())
          ((FastaTextPane)fastaEnum.nextElement()).stopGetz();
      }
    
    
    
    tjc's avatar
    tjc committed
      /**
      *
      * Delete note field in for all similar hits.
      *
      */
      protected void deleteNote()
      {
        ann.deleteNote();
      }
    
    
      /**
      * 
      * Add a note field in for all similar hits.
      *
      */
      protected void updateNote()
      {
        StringReader in     = new StringReader(getFeatureText());
        BufferedReader buff = new BufferedReader(in);
        String line       = null;
        StringBuffer note = null;
    
        try
        {
          while((line = buff.readLine()) != null)
          {
            if(line.startsWith("/similarity="))
            {
              if(note == null)
    
    tjc's avatar
    tjc committed
                note = new StringBuffer("\n/note=\"Similar to ");
    
    tjc's avatar
    tjc committed
              else
    
    tjc's avatar
    tjc committed
                note.append(", and to ");
    
    tjc's avatar
    tjc committed
    
              StringTokenizer tok = new StringTokenizer(line,";");
              String type = tok.nextToken();
              int ind1 = type.indexOf("\"");
              type = type.substring(ind1+1);
    
              String id   = tok.nextToken();
    
    tjc's avatar
    tjc committed
              ind1 = id.indexOf("with=")+4;
              if(ind1 == 3)
    
    tjc's avatar
    tjc committed
                ind1 = 0;         
                
    //        ind1 = id.indexOf(":");
    
    tjc's avatar
    tjc committed
              id = id.substring(ind1+1).trim();
    
    
    tjc's avatar
    tjc committed
              String next = tok.nextToken().trim();
              if(next.endsWith("."))
                next = next.substring(0,next.length()-1);
    
              note.append(next);
    
    tjc's avatar
    tjc committed
              note.append(tok.nextToken().toLowerCase());
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
              String length = tok.nextToken().trim();
    
    tjc's avatar
    tjc committed
              if(!length.startsWith("length"))
                note.append(" "+length.toLowerCase());
              note.append(" "+id);
    
    
    tjc's avatar
    tjc committed
              while(!length.startsWith("length"))
                length = tok.nextToken().trim();
    
    tjc's avatar
    tjc committed
    
              ind1 = length.indexOf("=");
    
    tjc's avatar
    tjc committed
              if(ind1 == -1)
                ind1 = length.indexOf(" ");
    
    
    tjc's avatar
    tjc committed
              length = length.substring(ind1+1);
    
    
    tjc's avatar
    tjc committed
              if(!length.endsWith(" aa"))
                length = length + " aa";
    
    
    tjc's avatar
    tjc committed
              note.append(" ("+length+") ");
              note.append(type+" scores: ");
              
              String pid  = tok.nextToken().trim();  // percent id
    
    
    tjc's avatar
    tjc committed
              if(pid.endsWith("%"))  // fasta
              {
                ind1 = pid.indexOf(" ");
                pid  = pid.substring(ind1+1);
    
                tok.nextToken();                       // ungapped id
                String eval = tok.nextToken().trim();
                String len  = tok.nextToken().trim();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
                while(!len.endsWith("aa overlap"))
                  len  = tok.nextToken().trim();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
                len = len.substring(0,len.length()-8);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
                note.append(eval);
                note.append(", "+pid+" id in ");
                note.append(len);
              }
              else                  // blastp
                note.append(pid);
    
    tjc's avatar
    tjc committed
            }
          }
        }
        catch(IOException ioe){}
    
        if(note != null)
          note.append("\"");
        else
          return;
    
        ann.insert(note.toString(),true);
    //  System.out.println(note.toString());
    //  Enumeration enumHits = hits.elements();
    //  while(enumHits.hasMoreElements())
    //  {
    //    String id = (String)enumHits.nextElement();
    //    System.out.println(id);
    //    findHitInfo(String
    //  }
        return;
      }
    
    
    tjc's avatar
    tjc committed
      private String htmlBreaks(String t)
      {
        int ind = 0;
        while((ind = t.indexOf("\n",ind+5)) > -1)
          t = t.substring(0,ind) + "<br>" +
              t.substring(ind);
    
        return t;
      }
    
    
    tjc's avatar
    tjc committed
      public class TabChangeListener implements ChangeListener
      {
        ActiveJSplitPane lastSelected = (ActiveJSplitPane)tabPane.getSelectedComponent();
        public void stateChanged(ChangeEvent e)
        {
          ActiveJSplitPane split = (ActiveJSplitPane)tabPane.getSelectedComponent();
          lastSelected.setActive(false);
          split.setActive(true);
          lastSelected = split;
        }
      }
    
    
      public class ActiveJSplitPane extends JSplitPane
      {
        private JLabel tabLabel;
        private Color bg;
    
        public ActiveJSplitPane(int newOrientation,
                      Component newLeftComponent,
                      Component newRightComponent)
        {
          super(newOrientation,newLeftComponent,newRightComponent);
        }
        
        public void setLabel(JLabel tabLabel)
        {
          this.tabLabel = tabLabel;
          this.bg = tabLabel.getBackground();
        }
    
        public void setActive(boolean active)
        {
          if(active)
            tabLabel.setBackground(Color.yellow);
          else
            tabLabel.setBackground(bg);
        }
      }
    
    tjc's avatar
    tjc committed
    }