file format enum wip changes
[jalview.git] / src / MCview / PDBfile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package MCview;
22
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.DBRefSource;
25 import jalview.datamodel.SequenceI;
26 import jalview.io.FileParse;
27 import jalview.io.DataSourceType;
28 import jalview.io.StructureFile;
29 import jalview.structure.StructureImportSettings;
30 import jalview.util.MessageManager;
31
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Hashtable;
35 import java.util.List;
36 import java.util.Vector;
37
38 public class PDBfile extends StructureFile
39 {
40   private static String CALC_ID_PREFIX = "JalviewPDB";
41
42   public PDBfile(boolean addAlignmentAnnotations,
43           boolean predictSecondaryStructure, boolean externalSecStr)
44   {
45     super();
46     addSettings(addAlignmentAnnotations, predictSecondaryStructure,
47             externalSecStr);
48   }
49
50   public PDBfile(boolean addAlignmentAnnotations, boolean predictSecStr,
51           boolean externalSecStr, String dataObject,
52           DataSourceType sourceType)
53           throws IOException
54   {
55     super(false, dataObject, sourceType);
56     addSettings(addAlignmentAnnotations, predictSecStr, externalSecStr);
57     doParse();
58   }
59
60   public PDBfile(boolean addAlignmentAnnotations, boolean predictSecStr,
61           boolean externalSecStr,
62           FileParse source) throws IOException
63   {
64     super(false, source);
65     addSettings(addAlignmentAnnotations, predictSecStr, externalSecStr);
66     doParse();
67   }
68
69   @Override
70   public String print()
71   {
72     return null;
73   }
74
75   @Override
76   public void parse() throws IOException
77   {
78     setDbRefType(DBRefSource.PDB);
79     // TODO set the filename sensibly - try using data source name.
80     setId(safeName(getDataName()));
81
82     setChains(new Vector<PDBChain>());
83     List<SequenceI> rna = new ArrayList<SequenceI>();
84     List<SequenceI> prot = new ArrayList<SequenceI>();
85     PDBChain tmpchain;
86     String line = null;
87     boolean modelFlag = false;
88     boolean terFlag = false;
89     String lastID = "";
90
91     int indexx = 0;
92     String atomnam = null;
93     try
94     {
95       while ((line = nextLine()) != null)
96       {
97         if (line.indexOf("HEADER") == 0)
98         {
99           if (line.length() > 62)
100           {
101             String tid;
102             if (line.length() > 67)
103             {
104               tid = line.substring(62, 67).trim();
105             }
106             else
107             {
108               tid = line.substring(62).trim();
109             }
110             if (tid.length() > 0)
111             {
112               setId(tid);
113             }
114             continue;
115           }
116         }
117         // Were we to do anything with SEQRES - we start it here
118         if (line.indexOf("SEQRES") == 0)
119         {
120         }
121
122         if (line.indexOf("MODEL") == 0)
123         {
124           modelFlag = true;
125         }
126
127         if (line.indexOf("TER") == 0)
128         {
129           terFlag = true;
130         }
131
132         if (modelFlag && line.indexOf("ENDMDL") == 0)
133         {
134           break;
135         }
136         if (line.indexOf("ATOM") == 0
137                 || (StructureImportSettings.isProcessHETATMs()
138                         && line.indexOf("HETATM") == 0 && !terFlag))
139         {
140           terFlag = false;
141
142           // Jalview is only interested in CA bonds????
143           atomnam = line.substring(12, 15).trim();
144           if (!atomnam.equals("CA") && !atomnam.equals("P"))
145           {
146             continue;
147           }
148
149           Atom tmpatom = new Atom(line);
150           try
151           {
152           tmpchain = findChain(tmpatom.chain);
153             if (tmpatom.resNumIns.trim().equals(lastID))
154             {
155               // phosphorylated protein - seen both CA and P..
156               continue;
157             }
158             tmpchain.atoms.addElement(tmpatom);
159           } catch (Exception e)
160           {
161             tmpchain = new PDBChain(getId(), tmpatom.chain);
162             getChains().add(tmpchain);
163             tmpchain.atoms.addElement(tmpatom);
164           }
165           lastID = tmpatom.resNumIns.trim();
166         }
167         index++;
168       }
169
170       makeResidueList();
171       makeCaBondList();
172
173       if (getId() == null)
174       {
175         setId(inFile.getName());
176       }
177       for (PDBChain chain : getChains())
178       {
179         SequenceI chainseq = postProcessChain(chain);
180         if (isRNA(chainseq))
181         {
182           rna.add(chainseq);
183         }
184         else
185         {
186           prot.add(chainseq);
187         }
188       }
189       if (predictSecondaryStructure)
190       {
191         addSecondaryStructure(rna, prot);
192       }
193     } catch (OutOfMemoryError er)
194     {
195       System.out.println("OUT OF MEMORY LOADING PDB FILE");
196       throw new IOException(
197               MessageManager
198                       .getString("exception.outofmemory_loading_pdb_file"));
199     } catch (NumberFormatException ex)
200     {
201       if (line != null)
202       {
203         System.err.println("Couldn't read number from line:");
204         System.err.println(line);
205       }
206     }
207     markCalcIds();
208   }
209
210
211
212   /**
213    * Process a parsed chain to construct and return a Sequence, and add it to
214    * the list of sequences parsed.
215    * 
216    * @param chain
217    * @return
218    */
219
220   public static boolean isCalcIdHandled(String calcId)
221   {
222     return calcId != null && (CALC_ID_PREFIX.equals(calcId));
223   }
224
225   public static boolean isCalcIdForFile(AlignmentAnnotation alan,
226           String pdbFile)
227   {
228     return alan.getCalcId() != null
229             && CALC_ID_PREFIX.equals(alan.getCalcId())
230             && pdbFile.equals(alan.getProperty("PDBID"));
231   }
232
233   public static String relocateCalcId(String calcId,
234           Hashtable<String, String> alreadyLoadedPDB) throws Exception
235   {
236     int s = CALC_ID_PREFIX.length(), end = calcId
237             .indexOf(CALC_ID_PREFIX, s);
238     String between = calcId.substring(s, end - 1);
239     return CALC_ID_PREFIX + alreadyLoadedPDB.get(between) + ":"
240             + calcId.substring(end);
241   }
242
243   private void markCalcIds()
244   {
245     for (SequenceI sq : seqs)
246     {
247       if (sq.getAnnotation() != null)
248       {
249         for (AlignmentAnnotation aa : sq.getAnnotation())
250         {
251           String oldId = aa.getCalcId();
252           if (oldId == null)
253           {
254             oldId = "";
255           }
256           aa.setCalcId(CALC_ID_PREFIX);
257           aa.setProperty("PDBID", getId());
258           aa.setProperty("oldCalcId", oldId);
259         }
260       }
261     }
262   }
263
264 }