/* * Copyright (C) 2008 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. * * @author: Tim Carver */ package uk.ac.sanger.artemis.circular; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.util.Vector; import java.util.Hashtable; import javax.swing.Box; import javax.swing.ButtonGroup; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JSeparator; import uk.ac.sanger.artemis.Entry; import uk.ac.sanger.artemis.EntryGroup; import uk.ac.sanger.artemis.EntryVector; import uk.ac.sanger.artemis.Feature; import uk.ac.sanger.artemis.FeatureVector; import uk.ac.sanger.artemis.Options; import uk.ac.sanger.artemis.SimpleEntryGroup; import uk.ac.sanger.artemis.components.EntryFileDialog; import uk.ac.sanger.artemis.components.MessageDialog; import uk.ac.sanger.artemis.components.StickyFileChooser; import uk.ac.sanger.artemis.components.SwingWorker; import uk.ac.sanger.artemis.io.EntryInformation; import uk.ac.sanger.artemis.io.EntryInformationException; import uk.ac.sanger.artemis.io.Key; import uk.ac.sanger.artemis.io.Location; import uk.ac.sanger.artemis.io.MSPcrunchDocumentEntry; import uk.ac.sanger.artemis.io.Range; import uk.ac.sanger.artemis.io.RangeVector; import uk.ac.sanger.artemis.sequence.Bases; import uk.ac.sanger.artemis.sequence.NoSequenceException; import uk.ac.sanger.artemis.util.Document; import uk.ac.sanger.artemis.util.DocumentFactory; import uk.ac.sanger.artemis.util.OutOfRangeException; import uk.ac.sanger.artemis.util.ReadOnlyException; import uk.ac.sanger.artemis.util.WorkingGZIPInputStream; /** * DNA draw wizard */ public class Wizard { private DNADraw dna = null; public static Track TRACK_1 = new Track(0.95d, "CDS", "pseudo", false, true, false, null); public static Track TRACK_2 = new Track(0.9d, "CDS", "pseudo", false, false, true, null); public static Track TRACK_3 = new Track(0.85d, "CDS", "pseudo", true, true, true, null); public static Track TRACK_4 = new Track(0.8d, "misc_feature", true, true, null); public static Track TRACK_5 = new Track(0.75d, null, true, true, null); private SwingWorker workerGraph; public static Track[] tracks = { TRACK_1, TRACK_2, TRACK_3, TRACK_4, TRACK_5 }; public Wizard(DNADraw dna_current) { int n = getOption(dna_current); // option 0 - read data file // option 1 - edit existing dna // option 2 - read template if(n == 0) dna = getDNADrawFromFile(dna_current); else if(n == 2) { StickyFileChooser chooser = new StickyFileChooser(); chooser.showOpenDialog(null); File fileTemplate = chooser.getSelectedFile(); if(!fileTemplate.exists()) JOptionPane.showMessageDialog(null, fileTemplate.getName()+" cannot be found!", "Missing File", JOptionPane.WARNING_MESSAGE); loadTemplate(chooser.getSelectedFile()); } else if(n == 1) { Vector block = new Vector(); Vector restrictionEnzyme = new Vector(); if(dna_current == null) dna = new DNADraw(); else { dna = dna_current; block = dna_current.getGeneticMarker(); restrictionEnzyme = dna_current.getRestrictionEnzyme(); } LineAttribute la = new LineAttribute(dna); GeneticMarker gm; if(dna_current != null) gm = new GeneticMarker(dna_current,block); else gm = new GeneticMarker(dna,block); RestrictionEnzyme re; if(dna_current != null) re = new RestrictionEnzyme(dna_current,restrictionEnzyme); else re = new RestrictionEnzyme(dna,restrictionEnzyme); Ticks tk = new Ticks(dna_current,false); la.setMinimumSize(la.getPreferredSize()); la.setMaximumSize(la.getPreferredSize()); re.setMinimumSize(re.getPreferredSize()); re.setMaximumSize(re.getPreferredSize()); ScrollPanel pane = new ScrollPanel(new BorderLayout()); Box bdown = Box.createVerticalBox(); bdown.add(new JLabel("Properties")); Box bacross = Box.createHorizontalBox(); bacross.add(la); bacross.add(tk); bacross.add(Box.createHorizontalGlue()); bdown.add(bacross); bdown.add(new JSeparator()); bdown.add(Box.createVerticalStrut(10)); bdown.add(new JLabel("Features")); bacross = Box.createHorizontalBox(); bacross.add(gm); bacross.add(Box.createHorizontalGlue()); bdown.add(bacross); bdown.add(new JSeparator()); bdown.add(Box.createVerticalStrut(10)); bdown.add(new JLabel("Restriction Enzymes")); bacross = Box.createHorizontalBox(); bacross.add(re); bacross.add(Box.createHorizontalGlue()); bdown.add(bacross); pane.add(bdown,BorderLayout.CENTER); JScrollPane createWizScroll = new JScrollPane(pane); Dimension dscreen = createWizScroll.getToolkit().getScreenSize(); int wid = (int)dscreen.getWidth(); if(wid > 700) wid = 700; int hgt = (int)dscreen.getHeight(); if(hgt > 750) hgt = 700; hgt-=50; Dimension d = new Dimension(wid,hgt); createWizScroll.setPreferredSize(d); JOptionPane.showMessageDialog(null, createWizScroll, "DNA Wizard", JOptionPane.PLAIN_MESSAGE); dna.setGeneticMarker(block); dna.setRestrictionEnzyme(restrictionEnzyme); dna.setLineAttributes(la.getLineAttr()); dna.setStartTick(tk.getStartTick()); dna.setMinorTickInterval(tk.getMinorTickInterval()); dna.setTickInterval(tk.getTickInterval()); int s = la.getStart(); dna.setStart(s); s = la.getEnd(); dna.setEnd(s); } } /** * Open a DNA plot based on a template file * @param template */ public Wizard(final String templateName) { loadTemplate(templateName); } /** * Load from a template file * @param template */ private void loadTemplate(final String templateName) { final ProgressFrame progress = new ProgressFrame(); progress.setString("Reading from "+templateName+" "); progress.setValue(2); if(dna == null) dna = new DNADraw(); Options.getOptions(); final BufferedReader inputStream = getReader(templateName); loadTemplate(inputStream, templateName, progress); } /** * Load from a template file * @param template */ private void loadTemplate(final File templateFile) { final ProgressFrame progress = new ProgressFrame(); progress.setString("Reading from "+templateFile.getName()+" "); progress.setValue(2); if(dna == null) dna = new DNADraw(); Options.getOptions(); FileReader reader; try { reader = new FileReader(templateFile); final BufferedReader inputStream = new BufferedReader(reader); loadTemplate(inputStream, templateFile.getName(), progress); } catch(FileNotFoundException e) { e.printStackTrace(); } } private void loadTemplate(final BufferedReader inputStream, final String templateName, final ProgressFrame progress) { try { final EntryGroup entryGroup = new SimpleEntryGroup(); final Hashtable fileEntrys = new Hashtable(); Vector v_tracks = new Vector(); String inLine = null; String lineAttrStr[] = null; String tickMarksStr[] = null; String gcGraphStr[] = null; String gcSkewGraphStr[] = null; String userGraphStr[] = null; String lineAttrStart = "# line attributes:"; String tickMarksStart = "# tick marks:"; String gcGraphStart = "# GC Graph:"; String gcSkewGraphStart = "# GC Skew Graph:"; String userGraphStart = "# User Graph:"; String mergeBlastFeatures = "# merge.blast"; while((inLine = inputStream.readLine()) != null) { if(inLine.startsWith("#") || inLine.trim().equals("")) { if(inLine.startsWith(lineAttrStart)) lineAttrStr = inLine.substring(lineAttrStart.length()).trim().split("[=\\s]"); else if(inLine.startsWith(tickMarksStart)) tickMarksStr = inLine.substring(tickMarksStart.length()).trim().split("[=\\s]"); else if(inLine.startsWith(gcGraphStart)) gcGraphStr = inLine.substring(gcGraphStart.length()).trim().split("[=\\s]"); else if(inLine.startsWith(gcSkewGraphStart)) gcSkewGraphStr = inLine.substring(gcSkewGraphStart.length()).trim().split("[=\\s]"); else if(inLine.startsWith(userGraphStart)) userGraphStr = inLine.substring(userGraphStart.length()).trim().split("[=\\s]"); else if(inLine.startsWith(mergeBlastFeatures)) System.setProperty("merge.blast", "true"); continue; } String properties[] = inLine.split("\t"); final String separator; if (properties[11].indexOf ("://") != -1) separator = "/"; else separator = File.separator; String fileName = properties[11] + separator + properties[10]; Entry entry; if(!fileEntrys.containsKey(fileName)) { progress.setString("Reading "+properties[10]); progress.setValue(4); entry = getEntry(fileName, entryGroup); if(entry == null) continue; fileEntrys.put(fileName, entry); } else { entry = (Entry)fileEntrys.get(fileName); } Track track = new Track(.9,entry); track.setPropertiesFromTemplate(inLine); v_tracks.add(track); } inputStream.close(); progress.setString("Read template "+templateName); progress.setValue(7); Track[] newTracks = new Track[v_tracks.size()]; for(int i=0; i 1) position = tracks[tracks.length-1].getPosition()-0.05; else position = 0.95; Track newTrack = new Track(position, entry); newTracks[tracks.length] = newTrack; Wizard.tracks = newTracks; return newTrack; } protected static void deleteTrack(int trackIndex) { Track[] tracks = getTracks(); Track[] newTracks = new Track[tracks.length-1]; int count = 0; for(int i=0; i