updated to jalview 2.1 and begun ArchiveClient/VamsasClient/VamsasStore updates.
[jalview.git] / src / MCview / PDBfile.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 */
19 package MCview;
20
21 import jalview.datamodel.*;
22
23 import java.io.*;
24
25 import java.util.*;
26 import java.awt.Color;
27
28
29 public class PDBfile extends jalview.io.AlignFile {
30     public Vector chains;
31     public String id;
32
33     public PDBfile(String inFile, String inType) throws IOException
34     {
35         super(inFile, inType);
36     }
37
38     public String print()
39     {
40       return null;
41     }
42
43     public void parse() throws IOException
44     {
45         chains = new Vector();
46
47         PDBChain tmpchain;
48         String line;
49         boolean modelFlag = false;
50         boolean terFlag = false;
51
52
53         int index = 0;
54         while((line = nextLine())!=null)
55         {
56            if (line.indexOf("HEADER") == 0)
57            {
58              id = line.substring(62, 67).trim();
59              continue;
60            }
61
62            if(line.indexOf("MODEL")==0)
63              modelFlag = true;
64
65            if(line.indexOf("TER")==0)
66              terFlag = true;
67
68            if(modelFlag && line.indexOf("ENDMDL")==0)
69              break;
70
71            if (    line.indexOf("ATOM")==0
72                || (line.indexOf("HETATM")==0 && !terFlag)
73              )
74             {
75               terFlag = false;
76
77
78               //Jalview is only interested in CA bonds????
79               if (!line.substring(12, 15).trim().equals("CA"))
80               {
81                 continue;
82               }
83
84               Atom tmpatom = new Atom(line);
85               tmpchain = findChain(tmpatom.chain);
86               if (tmpchain != null)
87               {
88                 tmpchain.atoms.addElement(tmpatom);
89               }
90               else
91               {
92                 tmpchain = new PDBChain(tmpatom.chain);
93                 chains.addElement(tmpchain);
94                 tmpchain.atoms.addElement(tmpatom);
95               }
96           }
97           index ++;
98         }
99
100        makeResidueList();
101        makeCaBondList();
102
103        if(id==null)
104        {
105          id = inFile.getName();
106        }
107        for (int i = 0; i < chains.size(); i++)
108        {
109          SequenceI seq = ( (PDBChain) chains.elementAt(i)).
110              sequence;
111          seq.setName(id + "|" + seq.getName());
112          Sequence dataset = new Sequence(seq.
113              getName(),
114              seq.getSequence().toString(), seq.getStart(), seq.getEnd());
115
116          PDBEntry entry = new PDBEntry();
117          entry.setId(id);
118          if(inFile!=null)
119            entry.setFile(inFile.getAbsolutePath());
120
121          seq.setDatasetSequence(dataset);
122          dataset.addPDBId(entry);
123
124          getSeqs().addElement(seq);
125        }
126     }
127
128     public void makeResidueList() {
129         for (int i = 0; i < chains.size(); i++) {
130             ((PDBChain) chains.elementAt(i)).makeResidueList();
131         }
132     }
133
134     public void makeCaBondList() {
135         for (int i = 0; i < chains.size(); i++) {
136             ((PDBChain) chains.elementAt(i)).makeCaBondList();
137         }
138     }
139
140     public PDBChain findChain(String id) {
141         for (int i = 0; i < chains.size(); i++) {
142             if (((PDBChain) chains.elementAt(i)).id.equals(id)) {
143                 return (PDBChain) chains.elementAt(i);
144             }
145         }
146
147         return null;
148     }
149
150     public void setChargeColours() {
151         for (int i = 0; i < chains.size(); i++) {
152             ((PDBChain) chains.elementAt(i)).setChargeColours();
153         }
154     }
155
156     public void setColours(jalview.schemes.ColourSchemeI cs) {
157         for (int i = 0; i < chains.size(); i++) {
158             ((PDBChain) chains.elementAt(i)).setChainColours(cs);
159         }
160     }
161
162     public void setChainColours()
163     {
164        for (int i = 0; i < chains.size(); i++)
165        {
166             ((PDBChain) chains.elementAt(i)).setChainColours(
167                      Color.getHSBColor(1.0f / (float)i, .4f, 1.0f)
168                 );
169         }
170     }
171 }