/src/tripod/tools/kinome/StepDataImport.java

https://bitbucket.org/caodac/kinome-viewer · Java · 288 lines · 239 code · 46 blank · 3 comment · 35 complexity · 87fe5e4ef33e19acab90276fdc004f7d MD5 · raw file

  1. package tripod.tools.kinome;
  2. import java.util.*;
  3. import java.util.logging.Logger;
  4. import java.util.logging.Level;
  5. import java.io.*;
  6. import java.awt.*;
  7. import java.awt.event.*;
  8. import javax.swing.*;
  9. import javax.swing.event.*;
  10. import javax.swing.table.*;
  11. import org.pietschy.wizard.*;
  12. import org.pietschy.wizard.models.*;
  13. import org.jdesktop.swingx.JXErrorPane;
  14. import com.jgoodies.forms.layout.*;
  15. import com.jgoodies.forms.builder.PanelBuilder;
  16. import tripod.ui.kinome.*;
  17. public class StepDataImport extends StepCommon
  18. implements ActionListener, ListSelectionListener {
  19. private static final Logger logger = Logger.getLogger
  20. (StepDataImport.class.getName());
  21. public static final String NAME = "Data Import";
  22. public static final String DESC =
  23. "Please setup your data file for import";
  24. static class DataValueModel extends AbstractTableModel
  25. implements Comparator {
  26. String[] columns = new String[] {
  27. "Value",
  28. "Count"
  29. };
  30. Class[] classes = new Class[] {
  31. Object.class,
  32. Integer.class
  33. };
  34. Object[] rows;
  35. Map values;
  36. boolean isKinase;
  37. DataValueModel () {
  38. }
  39. void setValues (String name, Map values) {
  40. setValues (name, values, false);
  41. }
  42. void setValues (String name, Map values, boolean isKinase) {
  43. this.values = values;
  44. this.isKinase = isKinase;
  45. columns[0] = name;
  46. rows = values.keySet().toArray(new Object[0]);
  47. Arrays.sort(rows, this);
  48. fireTableStructureChanged ();
  49. }
  50. void clear () {
  51. values = null;
  52. rows = null;
  53. columns[0] = "Value";
  54. fireTableStructureChanged ();
  55. }
  56. public int compare (Object key1, Object key2) {
  57. Integer c1 = (Integer)values.get(key1);
  58. Integer c2 = (Integer)values.get(key2);
  59. if (c1 == null && c2 == null) return 0;
  60. if (c1 == null && c2 != null) return -1;
  61. if (c1 != null && c2 == null) return 1;
  62. return c2 - c1;
  63. }
  64. public int getColumnCount () {
  65. return isKinase ? (columns.length+1) : columns.length;
  66. }
  67. public int getRowCount () {
  68. return values != null ? values.size() : 0;
  69. }
  70. public String getColumnName (int col) {
  71. return col < columns.length ? columns[col] : "Kinase Gene?";
  72. }
  73. public Class getColumnClass (int col) {
  74. return col < classes.length ? classes[col] : Boolean.class;
  75. }
  76. public Object getValueAt (int row, int col) {
  77. if (col == 0) {
  78. return rows[row];
  79. }
  80. if (col < columns.length)
  81. return values.get(rows[row]);
  82. // is kinase?
  83. return KinomeDataset.isValidKinase((String)rows[row]);
  84. }
  85. }
  86. JCheckBox headerCb;
  87. JTable table, viewtab;
  88. String delimiter = "CSV";
  89. public StepDataImport () {
  90. super (NAME, DESC);
  91. }
  92. @Override
  93. protected void initUI () {
  94. JPanel pane = new JPanel (new BorderLayout (0, 2));
  95. pane.add(createOptionPane (), BorderLayout.NORTH);
  96. JSplitPane split = new JSplitPane (JSplitPane.VERTICAL_SPLIT);
  97. split.setTopComponent(createTablePane ());
  98. split.setBottomComponent(createViewPane ());
  99. split.setDividerSize(5);
  100. split.setResizeWeight(.5);
  101. pane.add(split);
  102. setLayout (new BorderLayout ());
  103. add (pane);
  104. }
  105. Component createOptionPane () {
  106. JPanel pane = new JPanel (new BorderLayout ());
  107. pane.setBorder(BorderFactory.createCompoundBorder
  108. (BorderFactory.createTitledBorder(""),
  109. BorderFactory.createEmptyBorder(1,1,1,1)));
  110. Box box = Box.createHorizontalBox();
  111. box.add(headerCb = new JCheckBox ("Header"));
  112. headerCb.setSelected(true);
  113. headerCb.setToolTipText("Is the first line a header?");
  114. box.add(Box.createHorizontalStrut(10));
  115. Object[][] delimiters = new Object[][] {
  116. {new JRadioButton ("CSV"), "Comma delimited file"},
  117. {new JRadioButton ("Tab"), "Tab delimited file"},
  118. {new JRadioButton ("Space"), "Space delimited file"}
  119. };
  120. ButtonGroup bg = new ButtonGroup ();
  121. for (int i = 0; i < delimiters.length; ++i) {
  122. box.add(Box.createHorizontalStrut(2));
  123. AbstractButton ab = (AbstractButton)delimiters[i][0];
  124. ab.addActionListener(this);
  125. String text = (String)delimiters[i][1];
  126. ab.setToolTipText(text);
  127. box.add(ab);
  128. bg.add(ab);
  129. ab.setSelected(i == 0);
  130. }
  131. pane.add(box);
  132. JButton btn = new JButton ("Load");
  133. btn.setToolTipText("Load data file");
  134. btn.addActionListener(this);
  135. pane.add(btn, BorderLayout.EAST);
  136. return pane;
  137. }
  138. Component createTablePane () {
  139. JPanel pane = new JPanel (new BorderLayout ());
  140. pane.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
  141. pane.add(new JScrollPane (table = new JTable
  142. (new DataImportModel ())));
  143. table.getSelectionModel()
  144. .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  145. table.getSelectionModel().addListSelectionListener(this);
  146. //table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  147. table.setDefaultEditor
  148. (DataLabel.class, new DefaultCellEditor
  149. (new JComboBox (EnumSet.allOf(DataLabel.class)
  150. .toArray(new DataLabel[0]))));
  151. table.setDefaultEditor
  152. (DataType.class, new DefaultCellEditor
  153. (new JComboBox (EnumSet.allOf(DataType.class)
  154. .toArray(new DataType[0]))));
  155. return pane;
  156. }
  157. Component createViewPane () {
  158. JPanel pane = new JPanel (new BorderLayout ());
  159. pane.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
  160. pane.add(new JScrollPane (viewtab = new JTable
  161. (new DataValueModel ())));
  162. return pane;
  163. }
  164. public void valueChanged (ListSelectionEvent e) {
  165. if (e.getValueIsAdjusting()) return;
  166. int row = table.getSelectedRow();
  167. if (row >= 0) {
  168. row = table.convertRowIndexToModel(row);
  169. DataImportModel model = (DataImportModel)table.getModel();
  170. DataLabel label = (DataLabel)model.getValueAt(row, 2);
  171. Map values = model.getValues(row);
  172. ((DataValueModel)viewtab.getModel()).setValues
  173. ((String)model.getValueAt(row, 0), values,
  174. label != null && label == DataLabel.Kinase);
  175. }
  176. }
  177. public void actionPerformed (ActionEvent e) {
  178. String cmd = e.getActionCommand();
  179. if ("load".equalsIgnoreCase(cmd)) {
  180. doLoad ();
  181. }
  182. else if (e.getSource() instanceof JRadioButton) {
  183. delimiter = cmd;
  184. }
  185. }
  186. protected void doLoad () {
  187. JFileChooser chooser = getFileChooser ();
  188. chooser.setDialogTitle("Please select input data file");
  189. int op = chooser.showOpenDialog(this);
  190. if (JFileChooser.APPROVE_OPTION == op) {
  191. try {
  192. File file = chooser.getSelectedFile();
  193. loadData (file);
  194. ((DataValueModel)viewtab.getModel()).clear();
  195. }
  196. catch (Exception ex) {
  197. JXErrorPane.showDialog(ex);
  198. }
  199. }
  200. }
  201. protected void loadData (File file) throws IOException {
  202. DataImportModel model = (DataImportModel)table.getModel();
  203. String sep = "";
  204. if ("csv".equalsIgnoreCase(delimiter))
  205. sep = ",";
  206. else if ("tab".equalsIgnoreCase(delimiter))
  207. sep = "[\\t]";
  208. else if ("space".equalsIgnoreCase(delimiter))
  209. sep = "[\\s]";
  210. else
  211. throw new IllegalStateException
  212. ("Unknown delimiter option: "+delimiter);
  213. model.load(file, headerCb.isSelected(), sep);
  214. }
  215. @Override
  216. public void applyState () throws InvalidStateException {
  217. // make sure we have at least Kinase and Activity labels
  218. DataImportModel model = (DataImportModel)table.getModel();
  219. EnumSet<DataLabel> labels = model.getLabels();
  220. if (!labels.contains(DataLabel.Kinase))
  221. throw new InvalidStateException
  222. ("At least one (and only one) column must be labeled Kinase!");
  223. if (!labels.contains(DataLabel.Activity))
  224. throw new InvalidStateException
  225. ("At least one (and only one) column must "
  226. +"be labeled Activity!");
  227. getModel().put("DataImportModel", model);
  228. }
  229. public static void main (String[] argv) throws Exception {
  230. SwingUtilities.invokeLater(new Runnable () {
  231. public void run () {
  232. JFrame f = new JFrame ();
  233. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  234. StepDataImport ss = new StepDataImport ();
  235. f.getContentPane().add(ss);
  236. f.setSize(new Dimension (400, 400));
  237. f.pack();
  238. f.setVisible(true);
  239. }
  240. });
  241. }
  242. }