// $Id: PickUtils.java,v 1.1 2006/05/20 17:02:03 Sasha Buzko Exp $ // // Copyright 2000-2004 The Regents of the University of California. // All Rights Reserved. // // Permission to use, copy, modify and distribute any part of this // Molecular Biology Toolkit (MBT) // for educational, research and non-profit purposes, without fee, and without // a written agreement is hereby granted, provided that the above copyright // notice, this paragraph and the following three paragraphs appear in all // copies. // // Those desiring to incorporate this MBT into commercial products // or use for commercial purposes should contact the Technology Transfer & // Intellectual Property Services, University of California, San Diego, 9500 // Gilman Drive, Mail Code 0910, La Jolla, CA 92093-0910, Ph: (858) 534-5815, // FAX: (858) 534-7345, E-MAIL:invent@ucsd.edu. // // IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING // LOST PROFITS, ARISING OUT OF THE USE OF THIS MBT, EVEN IF THE // UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.// // THE MBT PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND THE // UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. THE UNIVERSITY OF CALIFORNIA MAKES // NO REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER IMPLIED OR // EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF // MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR THAT THE USE OF THE // MBT WILL NOT INFRINGE ANY PATENT, TRADEMARK OR OTHER RIGHTS. // // For further information, please see: http://mbt.sdsc.edu // // History: // $Log: PickUtils.java,v $ // Revision 1.1 2006/05/20 17:02:03 Sasha Buzko // Updated version // // Revision 1.1 2006/04/30 20:13:59 Sasha Buzko // New version of the app // // Revision 1.1 2006/04/15 19:42:26 Sasha Buzko // Initial commit // // Revision 1.1 2006/01/06 00:40:25 Administrator // *** empty log message *** // // Revision 1.2 2005/11/08 20:58:33 moreland // Switched style code to new StructureStyles API. // // Revision 1.1 2004/10/22 23:42:31 moreland // First version. // // Revision 1.0 2004/09/08 18:06:54 moreland // First version. // package edu.sdsc.mbt.util; import edu.sdsc.mbt.*; /** * This class provides a number of utility methods for handling picking * and subsequent selection. Provides pick-level behavior mapping * (eg: Atoms->Residues->Fragments->Chains). Provides closestAtom method * to determine the closest atom relative to a specified StructureComponent * and point in 3D space. *
* @author John L. Moreland
* @see edu.sdsc.mbt.StructureComponent
*/
public class PickUtils
{
public static final int PICK_AUTO = 0;
public static final int PICK_ATOMS = 1;
public static final int PICK_RESIDUES = 2;
public static final int PICK_FRAGMENTS = 3;
public static final int PICK_CHAINS = 4;
public static final int PICK_MOLECULES = 5;
private static final int PICK_MIN = PICK_AUTO;
private static final int PICK_MAX = PICK_MOLECULES;
public static int pickLevel = PICK_AUTO;
/**
* Set the structure component pick level.
*/
public static void setPickLevel( int level )
{
if ( (level < PICK_MIN) || (level > PICK_MAX) )
throw new IllegalArgumentException( "invalid pick level" );
pickLevel = level;
}
/**
* Get the structure component pick level.
*/
public static int getPickLevel( )
{
return pickLevel;
}
/**
* Walk the data hierarchy (atom/bond-residue-fragment-chain-molecule)
* from the given structure component level toward the current pick level,
* and return the current pick level component.
*/
public static StructureComponent pickLevelComponent( StructureComponent sc,
double[] coord )
{
if ( sc == null )
throw new NullPointerException( "null StructureComponent" );
if ( coord == null )
throw new NullPointerException( "null coord" );
StructureComponent result = sc; // Default result is the source.
if ( pickLevel != PICK_AUTO )
{
String scType = sc.getStructureComponentType( );
int scLevel = PICK_AUTO;
if ( scType == StructureComponentRegistry.TYPE_ATOM )
scLevel = PICK_ATOMS;
else if ( scType == StructureComponentRegistry.TYPE_BOND )
scLevel = PICK_ATOMS;
else if ( scType == StructureComponentRegistry.TYPE_RESIDUE )
scLevel = PICK_RESIDUES;
else if ( scType == StructureComponentRegistry.TYPE_FRAGMENT )
scLevel = PICK_FRAGMENTS;
else if ( scType == StructureComponentRegistry.TYPE_CHAIN )
scLevel = PICK_CHAINS;
if ( pickLevel != scLevel )
{
StructureMap structureMap = sc.getStructure().getStructureMap( );
Atom atom = closestAtom( sc, coord );
if ( atom == null ) return result;
if ( pickLevel == PICK_ATOMS )
{
if ( scType != StructureComponentRegistry.TYPE_BOND )
result = atom;
return result;
}
Residue residue = structureMap.getResidue( atom );
if ( residue == null ) return result;
if ( pickLevel == PICK_RESIDUES )
{
result = residue;
return result;
}
Fragment fragment = residue.getFragment( );
if ( fragment == null ) return result;
if ( pickLevel == PICK_FRAGMENTS )
{
result = fragment;
return result;
}
Chain chain = fragment.getChain( );
if ( chain == null ) return result;
if ( pickLevel == PICK_CHAINS )
{
result = chain;
return result;
}
}
}
return result;
}
/**
* Find the atom object within the specified structure component which
* is closest to the given coordinate.
*/
public static Atom closestAtom( StructureComponent sc, double[] coord )
{
Atom result = null;
String scType = sc.getStructureComponentType( );
if ( scType == StructureComponentRegistry.TYPE_ATOM )
{
result = (Atom) sc;
}
else if ( scType == StructureComponentRegistry.TYPE_BOND )
{
Bond bond = (Bond) sc;
Atom atom0 = bond.getAtom( 0 );
Atom atom1 = bond.getAtom( 1 );
double dist0 = Algebra.distance( coord, atom0.coordinate );
double dist1 = Algebra.distance( coord, atom1.coordinate );
if ( dist0 <= dist1 )
result = atom0;
else
result = atom1;
}
else if ( scType == StructureComponentRegistry.TYPE_RESIDUE )
{
Residue residue = (Residue) sc;
double minDist = 10000.0f;
int atomCount = residue.getAtomCount( );
for ( int i=0; i