JAL-2120 JAL-2360 remove obsolete PDBViewer
[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.DataSourceType;
27 import jalview.io.FileParse;
28 import jalview.io.StructureFile;
29 import jalview.util.MessageManager;
30
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.Hashtable;
34 import java.util.List;
35 import java.util.Vector;
36
37 public class PDBfile extends StructureFile
38 {
39   private static String CALC_ID_PREFIX = "JalviewPDB";
40
41   public PDBfile(boolean addAlignmentAnnotations,
42           boolean predictSecondaryStructure, boolean externalSecStr)
43   {
44     super();
45     addSettings(addAlignmentAnnotations, predictSecondaryStructure,
46             externalSecStr);
47   }
48
49   public PDBfile(boolean addAlignmentAnnotations, boolean predictSecStr,
50           boolean externalSecStr, String dataObject,
51           DataSourceType sourceType)
52           throws IOException
53   {
54     super(false, dataObject, sourceType);
55     addSettings(addAlignmentAnnotations, predictSecStr, externalSecStr);
56     doParse();
57   }
58
59   public PDBfile(boolean addAlignmentAnnotations, boolean predictSecStr,
60           boolean externalSecStr, FileParse source) throws IOException
61   {
62     super(false, source);
63     addSettings(addAlignmentAnnotations, predictSecStr, externalSecStr);
64     doParse();
65   }
66
67   @Override
68   public String print(SequenceI[] seqs, boolean jvSuffix)
69   {
70     return null;
71   }
72
73   @Override
74   public void parse() throws IOException
75   {
76     setDbRefType(DBRefSource.PDB);
77     // TODO set the filename sensibly - try using data source name.
78     setId(safeName(getDataName()));
79
80     setChains(new Vector<PDBChain>());
81     List<SequenceI> rna = new ArrayList<SequenceI>();
82     List<SequenceI> prot = new ArrayList<SequenceI>();
83     PDBChain tmpchain;
84     String line = null;
85     boolean modelFlag = false;
86     boolean terFlag = false;
87     String lastID = "";
88
89     int indexx = 0;
90     String atomnam = null;
91     try
92     {
93       while ((line = nextLine()) != null)
94       {
95         if (line.indexOf("HEADER") == 0)
96         {
97           if (line.length() > 62)
98           {
99             String tid;
100             if (line.length() > 67)
101             {
102               tid = line.substring(62, 67).trim();
103             }
104             else
105             {
106               tid = line.substring(62).trim();
107             }
108             if (tid.length() > 0)
109             {
110               setId(tid);
111             }
112             continue;
113           }
114         }
115         // Were we to do anything with SEQRES - we start it here
116         if (line.indexOf("SEQRES") == 0)
117         {
118         }
119
120         if (line.indexOf("MODEL") == 0)
121         {
122           modelFlag = true;
123         }
124
125         if (line.indexOf("TER") == 0)
126         {
127           terFlag = true;
128         }
129
130         if (modelFlag && line.indexOf("ENDMDL") == 0)
131         {
132           break;
133         }
134         if (line.indexOf("ATOM") == 0
135                 || (line.indexOf("HETATM") == 0 && !terFlag))
136         {
137           terFlag = false;
138
139           // Jalview is only interested in CA bonds????
140           atomnam = line.substring(12, 15).trim();
141           if (!atomnam.equals("CA") && !atomnam.equals("P"))
142           {
143             continue;
144           }
145
146           Atom tmpatom = new Atom(line);
147           try
148           {
149             tmpchain = findChain(tmpatom.chain);
150             if (tmpatom.resNumIns.trim().equals(lastID))
151             {
152               // phosphorylated protein - seen both CA and P..
153               continue;
154             }
155             tmpchain.atoms.addElement(tmpatom);
156           } catch (Exception e)
157           {
158             tmpchain = new PDBChain(getId(), tmpatom.chain);
159             getChains().add(tmpchain);
160             tmpchain.atoms.addElement(tmpatom);
161           }
162           lastID = tmpatom.resNumIns.trim();
163         }
164         index++;
165       }
166
167       makeResidueList();
168       makeCaBondList();
169
170       if (getId() == null)
171       {
172         setId(inFile.getName());
173       }
174       for (PDBChain chain : getChains())
175       {
176         SequenceI chainseq = postProcessChain(chain);
177         if (isRNA(chainseq))
178         {
179           rna.add(chainseq);
180         }
181         else
182         {
183           prot.add(chainseq);
184         }
185       }
186       if (predictSecondaryStructure)
187       {
188         addSecondaryStructure(rna, prot);
189       }
190     } catch (OutOfMemoryError er)
191     {
192       System.out.println("OUT OF MEMORY LOADING PDB FILE");
193       throw new IOException(
194               MessageManager
195                       .getString("exception.outofmemory_loading_pdb_file"));
196     } catch (NumberFormatException ex)
197     {
198       if (line != null)
199       {
200         System.err.println("Couldn't read number from line:");
201         System.err.println(line);
202       }
203     }
204     markCalcIds();
205   }
206
207   /**
208    * Process a parsed chain to construct and return a Sequence, and add it to
209    * the list of sequences parsed.
210    * 
211    * @param chain
212    * @return
213    */
214
215   public static boolean isCalcIdHandled(String calcId)
216   {
217     return calcId != null && (CALC_ID_PREFIX.equals(calcId));
218   }
219
220   public static boolean isCalcIdForFile(AlignmentAnnotation alan,
221           String pdbFile)
222   {
223     return alan.getCalcId() != null
224             && CALC_ID_PREFIX.equals(alan.getCalcId())
225             && pdbFile.equals(alan.getProperty("PDBID"));
226   }
227
228   public static String relocateCalcId(String calcId,
229           Hashtable<String, String> alreadyLoadedPDB) throws Exception
230   {
231     int s = CALC_ID_PREFIX.length(), end = calcId
232             .indexOf(CALC_ID_PREFIX, s);
233     String between = calcId.substring(s, end - 1);
234     return CALC_ID_PREFIX + alreadyLoadedPDB.get(between) + ":"
235             + calcId.substring(end);
236   }
237
238   private void markCalcIds()
239   {
240     for (SequenceI sq : seqs)
241     {
242       if (sq.getAnnotation() != null)
243       {
244         for (AlignmentAnnotation aa : sq.getAnnotation())
245         {
246           String oldId = aa.getCalcId();
247           if (oldId == null)
248           {
249             oldId = "";
250           }
251           aa.setCalcId(CALC_ID_PREFIX);
252           aa.setProperty("PDBID", getId());
253           aa.setProperty("oldCalcId", oldId);
254         }
255       }
256     }
257   }
258
259 }