adds annotation for display (with flag to set alignmentAnnotation as hidden initially)
[jalview.git] / src / MCview / PDBfile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 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 java.io.*;
22 import java.util.*;
23
24 import java.awt.*;
25
26 import jalview.datamodel.*;
27
28 public class PDBfile
29     extends jalview.io.AlignFile
30 {
31   public Vector chains;
32   public String id;
33
34   /**
35    * set to true to add chain alignment annotation as visible annotation.
36    */
37   boolean VisibleChainAnnotation=false;
38   public PDBfile(String inFile, String inType)
39       throws IOException
40   {
41     super(inFile, inType);
42   }
43
44   public String print()
45   {
46     return null;
47   }
48
49   public void parse()
50       throws IOException
51   {
52     try
53     {
54       chains = new Vector();
55
56       PDBChain tmpchain;
57       String line;
58       boolean modelFlag = false;
59       boolean terFlag = false;
60
61       int index = 0;
62       while ( (line = nextLine()) != null)
63       {
64         if (line.indexOf("HEADER") == 0)
65         {
66           id = line.substring(62, 67).trim();
67           continue;
68         }
69         // Were we to do anything with SEQRES - we start it here
70         if (line.indexOf("SEQRES") == 0)
71         {
72         }
73
74         if (line.indexOf("MODEL") == 0)
75         {
76           modelFlag = true;
77         }
78
79         if (line.indexOf("TER") == 0)
80         {
81           terFlag = true;
82         }
83
84         if (modelFlag && line.indexOf("ENDMDL") == 0)
85         {
86           break;
87         }
88         if (line.indexOf("ATOM") == 0
89             || (line.indexOf("HETATM") == 0 && !terFlag)
90             )
91         {
92           terFlag = false;
93
94           //Jalview is only interested in CA bonds????
95           if (!line.substring(12, 15).trim().equals("CA"))
96           {
97             continue;
98           }
99
100           Atom tmpatom = new Atom(line);
101           tmpchain = findChain(tmpatom.chain);
102           if (tmpchain != null)
103           {
104             tmpchain.atoms.addElement(tmpatom);
105           }
106           else
107           {
108             tmpchain = new PDBChain(id, tmpatom.chain);
109             chains.addElement(tmpchain);
110             tmpchain.atoms.addElement(tmpatom);
111           }
112         }
113         index++;
114       }
115
116       makeResidueList();
117       makeCaBondList();
118
119       if (id == null)
120       {
121         id = inFile.getName();
122       }
123       for (int i = 0; i < chains.size(); i++)
124       {
125         SequenceI dataset = ( (PDBChain) chains.elementAt(i)).
126             sequence;
127         dataset.setName(id + "|" + dataset.getName());
128         PDBEntry entry = new PDBEntry();
129         entry.setId(id);
130         if (inFile != null)
131         {
132           entry.setFile(inFile.getAbsolutePath());
133         }
134         dataset.addPDBId(entry);
135         SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects maintain reference to dataset
136         seqs.addElement(chainseq);
137         AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
138         if (chainannot!=null)
139         {
140           for (int ai=0; ai<chainannot.length; ai++)
141           {
142             chainannot[ai].visible=VisibleChainAnnotation;
143             annotations.addElement(chainannot[ai]);
144           }
145         }
146       }
147     }
148     catch (OutOfMemoryError er)
149     {
150       System.out.println("OUT OF MEMORY LOADING PDB FILE");
151       throw new IOException("Out of memory loading PDB File");
152     }
153   }
154
155   public void makeResidueList()
156   {
157     for (int i = 0; i < chains.size(); i++)
158     {
159       ( (PDBChain) chains.elementAt(i)).makeResidueList();
160     }
161   }
162
163   public void makeCaBondList()
164   {
165     for (int i = 0; i < chains.size(); i++)
166     {
167       ( (PDBChain) chains.elementAt(i)).makeCaBondList();
168     }
169   }
170
171   public PDBChain findChain(String id)
172   {
173     for (int i = 0; i < chains.size(); i++)
174     {
175       if ( ( (PDBChain) chains.elementAt(i)).id.equals(id))
176       {
177         return (PDBChain) chains.elementAt(i);
178       }
179     }
180
181     return null;
182   }
183
184   public void setChargeColours()
185   {
186     for (int i = 0; i < chains.size(); i++)
187     {
188       ( (PDBChain) chains.elementAt(i)).setChargeColours();
189     }
190   }
191
192   public void setColours(jalview.schemes.ColourSchemeI cs)
193   {
194     for (int i = 0; i < chains.size(); i++)
195     {
196       ( (PDBChain) chains.elementAt(i)).setChainColours(cs);
197     }
198   }
199
200   public void setChainColours()
201   {
202     for (int i = 0; i < chains.size(); i++)
203     {
204       ( (PDBChain) chains.elementAt(i)).setChainColours(
205           Color.getHSBColor(1.0f / (float) i, .4f, 1.0f)
206           );
207     }
208   }
209 }