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 Jul 25, 2006
021 *
022 */
023package org.biojava.nbio.structure.align.gui.jmol;
024
025
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029import java.util.regex.Matcher;
030import java.util.regex.Pattern;
031
032public class AtomInfoParser {
033
034        private static final Logger logger = LoggerFactory.getLogger(AtomInfoParser.class);
035
036        static Pattern pattern;
037
038        static {
039
040                String numberPattern = "\\[(.*)\\]([0-9^a-zA-Z]+)(:[a-zA-Z]*)?\\.([a-zA-Z]+)(/[0-9]*)?";
041                pattern = Pattern.compile(numberPattern);
042
043        }
044
045        public AtomInfoParser() {
046                super();
047
048
049
050        }
051
052        public static void main(String[]args){
053                String s1 = "[GLY]371:A.CA #2811";
054                String s2 = "[ASP]1^A:A.CA/2 #2";
055                System.out.println(s1 + " got: " + AtomInfoParser.parse(s1));
056                System.out.println(s2 + " got: " + AtomInfoParser.parse(s2));
057        }
058
059
060        /** parses e.g.
061         *  [MET]361:A.CA/1 #2843
062         *  [GLY]339:A.CA #2573
063         *  [ASN]44.CA #704
064         *
065         * @param jmolAtomInfo
066         * @return an AtomInfo
067         */
068        public static AtomInfo parse(String jmolAtomInfo){
069
070
071                Matcher matcher = pattern.matcher(jmolAtomInfo);
072
073                boolean found = matcher.find();
074                if ( ! found) {
075                        logger.info("Could not parse the atomInfo string {}", jmolAtomInfo);
076                        return new AtomInfo();
077                }
078                String residueName   = matcher.group(1);
079                String residueNumber = matcher.group(2);
080                String chainId       = matcher.group(3);
081                String atomName      = matcher.group(4);
082                String modelNumber   = matcher.group(5);
083
084
085
086                //System.out.println(jmolAtomInfo +" | " +  residueName + " number:" + residueNumber + " chain:" + chainName +
087                //              " atomName:" + atomName + " modelNumber:" + modelNumber );
088
089                AtomInfo info = new AtomInfo();
090
091                info.setAtomName(atomName);
092                info.setResidueName(residueName);
093                info.setResidueNumber(residueNumber.replaceAll("\\^",""));
094
095                String ci = " ";
096                if (chainId != null)
097                        ci = chainId.substring(1,chainId.length());
098                info.setChainId(ci);
099
100                int mn = 1;
101                if ( modelNumber != null)
102                        mn = Integer.parseInt(modelNumber.substring(1,modelNumber.length()));
103                info.setModelNumber(mn);
104
105
106                return info;
107        }
108
109}