001/* 002 * BioJava development code 003 * 004 * This code may be freely distributed and modified under the 005 * terms of the GNU Lesser General Public Licence. This should 006 * be distributed with the code. If you do not have a copy, 007 * see: 008 * 009 * http://www.gnu.org/copyleft/lesser.html 010 * 011 * Copyright for this code is held jointly by the individual 012 * authors. These should be listed in @author doc comments. 013 * 014 * For more information on the BioJava project and its aims, 015 * or to join the biojava-l mailing list, visit the home page 016 * at: 017 * 018 * http://www.biojava.org/ 019 * 020 * Created on Nov 6, 2009 021 * Author: Andreas Prlic 022 * 023 */ 024 025package org.biojava.nbio.structure.align.gui; 026 027import org.biojava.nbio.structure.Atom; 028import org.biojava.nbio.structure.Structure; 029import org.biojava.nbio.structure.StructureException; 030import org.biojava.nbio.structure.StructureTools; 031import org.biojava.nbio.structure.align.StructureAlignment; 032import org.biojava.nbio.structure.align.StructureAlignmentFactory; 033import org.biojava.nbio.structure.align.ce.CeMain; 034import org.biojava.nbio.structure.align.ce.CeParameters; 035import org.biojava.nbio.structure.align.ce.CeParameters.ScoringStrategy; 036import org.biojava.nbio.structure.align.ce.ConfigStrucAligParams; 037import org.biojava.nbio.structure.align.gui.jmol.StructureAlignmentJmol; 038import org.biojava.nbio.structure.align.model.AFPChain; 039import org.biojava.nbio.structure.align.util.AtomCache; 040import org.biojava.nbio.structure.align.util.UserConfiguration; 041import org.biojava.nbio.structure.align.webstart.WebStartMain; 042import org.biojava.nbio.structure.io.PDBFileReader; 043import org.biojava.nbio.structure.io.StructureIOFile; 044 045import javax.swing.*; 046import javax.swing.event.ListSelectionEvent; 047import javax.swing.event.ListSelectionListener; 048import javax.swing.table.TableModel; 049import javax.swing.table.TableRowSorter; 050import java.awt.event.ActionEvent; 051import java.awt.event.ActionListener; 052import java.io.*; 053import java.net.URL; 054import java.util.ArrayList; 055import java.util.List; 056 057 058public class DBResultTable implements ActionListener{ 059 060 public static final String[] ceColumnNames = {"name1","tname2","score","z-score" ,"rmsd","len1","len2","cov1","cov2","%ID","Description",""}; 061 public static final String[] fatColumnNames = {"name1","tname2","score","probability","rmsd","len1","len2","cov1","cov2","%ID","Description",""}; 062 063 Object[][] data; 064 JTable table; 065 066 String oldName1; 067 String oldName2; 068 069 String algorithmName; 070 StructureAlignment algorithm; 071 072 boolean isCE = true; 073 UserConfiguration config; 074 AtomCache cache ; 075 076 String userPath ; 077 String userChain; 078 079 080 081 public static void main(String[] args){ 082 083 String file = "/tmp/results_4hhb.A.out"; 084 085 DBResultTable table = new DBResultTable(); 086 UserConfiguration config = WebStartMain.getDefaultConfig(); 087 table.show(new File(file),config); 088 } 089 090 public DBResultTable(){ 091 oldName1 = ""; 092 oldName2 = ""; 093 userPath = null; 094 userChain = null; 095 } 096 097 public void show(BufferedReader in, UserConfiguration config) throws IOException{ 098 String str; 099 List<String[]> tmpdat = new ArrayList<String[]>(); 100 while ((str = in.readLine()) != null) { 101 if ( str.startsWith("#")) { 102 if ( str.startsWith("# algorithm:")) { 103 String[] spl = str.split(":"); 104 if ( spl.length == 2) { 105 algorithmName = spl[1]; 106 if (algorithmName.startsWith("jCE")) 107 isCE = true; 108 else 109 isCE = false; 110 } 111 initAlgorithm(algorithmName); 112 113 } 114 115 else if ( str.startsWith("#param:file1=")){ 116 String path = str.substring(13); 117 userPath = path.trim(); 118 } 119 120 else if ( str.startsWith("#param:chain1=")){ 121 String chain = str.substring(14); 122 userChain = chain.trim(); 123 } 124 125 else if ( str.startsWith("#param:scoring=")){ 126 try { 127 String[] spl = str.split("="); 128 ScoringStrategy scoreS; 129 try { 130 // try to convert from integer score 131 int stratNum = Integer.parseInt(spl[1]); 132 ScoringStrategy[] vals = ScoringStrategy.values(); 133 scoreS = vals[stratNum];//throws OutOfBounds if invalid; caught below 134 } catch(NumberFormatException e) { 135 scoreS = ScoringStrategy.valueOf(spl[1]); // 136 } 137 if (algorithm != null){ 138 // scoring is a parameter of CE... 139 ConfigStrucAligParams params = algorithm.getParameters(); 140 if ( params instanceof CeParameters){ 141 CeParameters ceParams = (CeParameters) params; 142 ceParams.setScoringStrategy(scoreS); 143 } 144 } 145 } catch (IndexOutOfBoundsException e){ 146 System.err.println("Unknown scoring strategy from line: " + str); 147 } catch (IllegalArgumentException e) { 148 System.err.println("Unknown scoring strategy from line: " + str); 149 } catch (Exception e) { 150 System.err.println("Unknown parameter can't read parameters from line: " + str); 151 e.printStackTrace(); 152 } 153 154 } 155 continue; 156 } 157 String[] spl = str.split("\t"); 158 if ( spl.length != ceColumnNames.length -1) { 159 System.err.println("wrong table width! " + spl.length + " should be: " + (ceColumnNames.length -1 )); 160 System.err.println(str); 161 continue; 162 } 163 tmpdat.add(spl); 164 165 } 166 in.close(); 167 168 Object[][] d = new Object[tmpdat.size()][ceColumnNames.length + 1]; 169 170 int i = -1; 171 for (String[] spl : tmpdat){ 172 173 i++; 174 Object[] o = new Object[spl.length + 1]; 175 for ( int j=0; j< spl.length;j++){ 176 177 if (( j >= 2 && j <= 4)|| (j==9)) { 178 o[j] = Double.parseDouble(spl[j]); 179 } else if ( j >4 && j< 10) { 180 181 o[j] = Integer.parseInt(spl[j]); 182 } else { 183 o[j] = spl[j]; 184 } 185 } 186 187 o[spl.length ] = "Align"; 188 189 d[i] = o; 190 191 } 192 data = d; 193 String[] columnNames = ceColumnNames; 194 if ( ! isCE) 195 columnNames = fatColumnNames; 196 table = new JTable(data, columnNames); 197 198 TableRowSorter<TableModel> sorter = new MyTableRowSorter(table.getModel()); 199 table.setRowSorter(sorter); 200 //table.setAutoCreateRowSorter(true); 201 202 JScrollPane scrollPane = new JScrollPane(table); 203 table.setFillsViewportHeight(true); 204 205 // take care of selections: 206 table.setSelectionMode( ListSelectionModel.SINGLE_INTERVAL_SELECTION); 207 table.getSelectionModel().addListSelectionListener(new RowListener()); 208 209 210 JFrame f = new JFrame(); 211 f.getContentPane().add(scrollPane); 212 f.pack(); 213 f.setVisible(true); 214 215 } 216 217 public void show(File file, UserConfiguration config){ 218 this.config = config; 219 220 cache = new AtomCache(config); 221 try { 222 BufferedReader in = new BufferedReader(new FileReader(file)); 223 show(in, config); 224 225 } catch (IOException e) { 226 e.printStackTrace(); 227 } 228 229 } 230 231 public void show(URL url, UserConfiguration config){ 232 this.config = config; 233 234 cache = new AtomCache(config); 235 try { 236 BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 237 show(in, config); 238 239 } catch (IOException e) { 240 e.printStackTrace(); 241 } 242 243 } 244 245 246 private void initAlgorithm(String algorithmName) { 247 try { 248 algorithm = StructureAlignmentFactory.getAlgorithm(algorithmName); 249 } catch (Exception e){ 250 e.printStackTrace(); 251 System.err.println("Can't guess algorithm from output. Using jCE as default..."); 252 try { 253 algorithm = StructureAlignmentFactory.getAlgorithm(CeMain.algorithmName); 254 } catch (Exception ex){ 255 ex.printStackTrace(); 256 return; 257 } 258 } 259 260 } 261 262 private void outputSelection() { 263 StringBuffer output = new StringBuffer(); 264 output.append(String.format("Lead: %d, %d. ", 265 table.getSelectionModel().getLeadSelectionIndex(), 266 table.getColumnModel().getSelectionModel(). 267 getLeadSelectionIndex())); 268 output.append("Rows:"); 269 for (int c : table.getSelectedRows()) { 270 output.append(String.format(" %d", c)); 271 } 272 273 output.append(". Columns:"); 274 for (int c : table.getSelectedColumns()) { 275 output.append(String.format(" %d", c)); 276 } 277 278 System.out.println(output.toString()); 279 } 280 281 private class RowListener implements ListSelectionListener { 282 @Override 283 public void valueChanged(ListSelectionEvent event) { 284 if (event.getValueIsAdjusting()) { 285 return; 286 } 287 int row = table.getSelectionModel().getLeadSelectionIndex(); 288 String name1 = (String)table.getValueAt(row, 0); 289 String name2 = (String)table.getValueAt(row, 1); 290 291 if ( name1.equals(oldName1) && oldName2.equals(name2)){ 292 return; 293 } 294 System.out.println("recreating alignment of: " + name1 + " " + name2 + " using " + algorithmName); 295 outputSelection(); 296 showAlignment(name1,name2); 297 oldName1 = name1; 298 oldName2 = name2; 299 300 301 } 302 } 303 304 private void showAlignment( String name1, String name2){ 305 306 307 if ( algorithm == null) { 308 initAlgorithm(null); 309 } 310 311 try { 312 Structure structure1 = null; 313 if ( name1.equals("CUSTOM")) { 314 // user uploaded a custom PDB file... 315 structure1 = loadCustomStructure(userPath,userChain); 316 } else { 317 structure1 = cache.getStructure(name1); 318 } 319 Structure structure2 = cache.getStructure(name2); 320 321 Atom[] ca1; 322 Atom[] ca2; 323 324 ca1 = StructureTools.getRepresentativeAtomArray(structure1); 325 ca2 = StructureTools.getRepresentativeAtomArray(structure2); 326 327 AFPChain afpChain; 328 329 afpChain = algorithm.align(ca1, ca2); 330 afpChain.setName1(name1); 331 afpChain.setName2(name2); 332 333 334 335 StructureAlignmentJmol jmol = StructureAlignmentDisplay.display(afpChain,ca1,ca2); 336 337 //String result = afpChain.toFatcat(ca1, ca2); 338 339 //String rot = afpChain.toRotMat(); 340 341 DisplayAFP.showAlignmentPanel(afpChain, ca1,ca2,jmol); 342 343 344 } catch (Exception e){ 345 e.printStackTrace(); 346 } 347 } 348 349 private Structure loadCustomStructure(String userPath2, String userChain2) throws StructureException{ 350 StructureIOFile reader = new PDBFileReader(); 351 Structure s = null; 352 try { 353 s = reader.getStructure(userPath2); 354 } catch (IOException e){ 355 356 //e.printStackTrace(); 357 throw new StructureException(e); 358 } 359 360 361 return StructureTools.getReducedStructure(s, userChain2); 362 } 363 364 @Override 365 public void actionPerformed(ActionEvent e) { 366 367 368 369 } 370 371 372}