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 */
021package org.biojava.nbio.structure.align.gui.jmol;
022
023import org.biojava.nbio.structure.*;
024
025import java.util.regex.Matcher;
026import java.util.regex.Pattern;
027
028public class JmolTools {
029
030        /** get jmol style info:
031         *  jmol style: [MET]508:A.CA/1 #3918
032         *  insertion code: [ASP]1^A:A.CA/1 #2
033         * @param a .. the Atom
034         *
035         * @return a String representation in Jmol style of the PDB information of this atom
036         */
037        public static final String getPdbInfo(Atom a){
038                return getPdbInfo(a,true);
039        }
040
041        private static Pattern inscodePatter ;
042        static {
043                inscodePatter = Pattern.compile("([0-9]+)([a-zA-Z]*)?");
044        }
045        public static void main(String[] args){
046
047                Chain c = new ChainImpl();
048                c.setChainID("X");
049
050                Group g = new AminoAcidImpl();
051                g.setResidueNumber(ResidueNumber.fromString("1A"));
052                try {
053                        g.setPDBName("ALA");
054                } catch (Exception e){}
055                Atom a = new AtomImpl();
056                a.setName("CA");
057                g.addAtom(a);
058                c.addGroup(g);
059
060                System.out.println(getPdbInfo(a));
061        }
062
063
064        // TODO: move this to AtomInfo class
065
066        public static final String getPdbInfo(Atom a, boolean printResName){
067                String aa3 = "";
068
069                String chain1 ="";
070                String res1 = "";
071
072                if ( a != null){
073                        Group g1 = a.getGroup();
074                        if ( g1 != null){
075                                aa3 = g1.getPDBName();
076                                res1 = g1.getResidueNumber().toString();
077                                Chain ch1 = g1.getChain();
078                                if (ch1 != null)
079                                        chain1 = ch1.getChainID();
080                        }
081                }
082
083                StringBuffer buf = new StringBuffer();
084                if ( printResName) {
085                        if ( !aa3.equals("")){
086                                buf.append("[");
087                                buf.append(aa3);
088                                buf.append("]");
089                        }
090                }
091                if ( ! res1.equals("")) {
092
093                        // let's check if there is an insertion code...
094                        Matcher matcher = inscodePatter.matcher(res1);
095
096                        boolean found = matcher.find();
097                        if ( ! found) {
098                                System.err.println("JmolTools: could not parse the residue number string " + res1);
099                                buf.append(res1);
100                        } else {
101                                String residueNumber = matcher.group(1);
102                                String insCode = matcher.group(2);
103                                buf.append(residueNumber);
104                                if ( insCode != null && ! ( insCode.equals(""))) {
105                                        buf.append("^");
106                                        buf.append(insCode);
107                                }
108                        }
109
110                }
111
112
113
114
115                if ( ! chain1.equals("")){
116                        buf.append(":");
117                        buf.append(chain1);
118                }
119                return buf.toString();
120        }
121}