transfer and update of sequenceFeatures for associated alignment sequence
[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       try{
46         chains = new Vector();
47
48         PDBChain tmpchain;
49         String line;
50         boolean modelFlag = false;
51         boolean terFlag = false;
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           // Were we to do anything with SEQRES - we start it here
62           if (line.indexOf("SEQRES") == 0) {              
63           }
64           
65           if (line.indexOf("MODEL") == 0)
66             modelFlag = true;
67
68           if (line.indexOf("TER") == 0)
69             terFlag = true;
70
71           if (modelFlag && line.indexOf("ENDMDL") == 0)
72             break;
73           if (line.indexOf("ATOM") == 0
74               || (line.indexOf("HETATM") == 0 && !terFlag)
75               )
76           {
77             terFlag = false;
78
79             //Jalview is only interested in CA bonds????
80             if (!line.substring(12, 15).trim().equals("CA"))
81             {
82               continue;
83             }
84
85             Atom tmpatom = new Atom(line);
86             tmpchain = findChain(tmpatom.chain);
87             if (tmpchain != null)
88             {
89               tmpchain.atoms.addElement(tmpatom);
90             }
91             else
92             {
93               tmpchain = new PDBChain(tmpatom.chain);
94               chains.addElement(tmpchain);
95               tmpchain.atoms.addElement(tmpatom);
96             }
97           }
98           index++;
99         }
100
101         makeResidueList();
102         makeCaBondList();
103
104         if (id == null)
105         {
106           id = inFile.getName();
107         }
108         for (int i = 0; i < chains.size(); i++)
109         {
110           SequenceI seq = ( (PDBChain) chains.elementAt(i)).
111               sequence;
112           seq.setName(id + "|" + seq.getName());
113           Sequence dataset = new Sequence(seq.
114                                           getName(),
115                                           seq.getSequence().toString(),
116                                           seq.getStart(), seq.getEnd());
117           dataset.setSequenceFeatures(seq.getSequenceFeatures());
118           PDBEntry entry = new PDBEntry();
119           entry.setId(id);
120           if (inFile != null)
121             entry.setFile(inFile.getAbsolutePath());
122
123           seq.setDatasetSequence(dataset);
124           dataset.addPDBId(entry);
125
126           getSeqs().addElement(seq);
127         }
128       }catch(OutOfMemoryError er)
129       {
130         System.out.println("OUT OF MEMORY LOADING PDB FILE");
131         throw new IOException("Out of memory loading PDB File");
132       }
133     }
134
135     public void makeResidueList() {
136         for (int i = 0; i < chains.size(); i++) {
137             ((PDBChain) chains.elementAt(i)).makeResidueList();
138         }
139     }
140
141     public void makeCaBondList() {
142         for (int i = 0; i < chains.size(); i++) {
143             ((PDBChain) chains.elementAt(i)).makeCaBondList();
144         }
145     }
146
147     public PDBChain findChain(String id) {
148         for (int i = 0; i < chains.size(); i++) {
149             if (((PDBChain) chains.elementAt(i)).id.equals(id)) {
150                 return (PDBChain) chains.elementAt(i);
151             }
152         }
153
154         return null;
155     }
156
157     public void setChargeColours() {
158         for (int i = 0; i < chains.size(); i++) {
159             ((PDBChain) chains.elementAt(i)).setChargeColours();
160         }
161     }
162
163     public void setColours(jalview.schemes.ColourSchemeI cs) {
164         for (int i = 0; i < chains.size(); i++) {
165             ((PDBChain) chains.elementAt(i)).setChainColours(cs);
166         }
167     }
168
169     public void setChainColours()
170     {
171        for (int i = 0; i < chains.size(); i++)
172        {
173             ((PDBChain) chains.elementAt(i)).setChainColours(
174                      Color.getHSBColor(1.0f / (float)i, .4f, 1.0f)
175                 );
176         }
177     }
178 }