JAL-4356 release notes
[jalview.git] / src / mc_view / PDBChain.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 mc_view;
22
23 import jalview.analysis.AlignSeq;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.Annotation;
26 import jalview.datamodel.Mapping;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceFeature;
29 import jalview.datamodel.SequenceI;
30 import jalview.schemes.ColourSchemeI;
31 import jalview.schemes.ResidueProperties;
32 import jalview.structure.StructureImportSettings;
33 import jalview.structure.StructureMapping;
34 import jalview.util.Comparison;
35
36 import java.awt.Color;
37 import java.util.List;
38 import java.util.Locale;
39 import java.util.Vector;
40
41 public class PDBChain
42 {
43   public static final String RESNUM_FEATURE = "RESNUM";
44
45   private static final String IEASTATUS = "IEA:jalview";
46
47   public String id;
48
49   public Vector<Bond> bonds = new Vector<>();
50
51   public Vector<Atom> atoms = new Vector<>();
52
53   public Vector<Residue> residues = new Vector<>();
54
55   public int offset;
56
57   /**
58    * sequence is the sequence extracted by the chain parsing code
59    */
60   public SequenceI sequence;
61
62   /**
63    * shadow is the sequence created by any other parsing processes (e.g. Jmol,
64    * RNAview)
65    */
66   public SequenceI shadow = null;
67
68   public boolean isNa = false;
69
70   public boolean isVisible = true;
71
72   public int pdbstart = 0;
73
74   public int pdbend = 0;
75
76   public int seqstart = 0;
77
78   public int seqend = 0;
79
80   public String pdbid = "";
81
82   String tfacName = "Temperature Factor";
83
84   public PDBChain(String thePdbid, String theId,
85           String tempFactorColumnName)
86   {
87     this.pdbid = thePdbid == null ? thePdbid
88             : thePdbid.toLowerCase(Locale.ROOT);
89     this.id = theId;
90     if (tempFactorColumnName != null && tempFactorColumnName.length() > 0)
91     {
92       tfacName = tempFactorColumnName;
93     }
94   }
95
96   /**
97    * import chain data assuming Temperature Factor is in the Temperature Factor
98    * column
99    * 
100    * @param thePdbid
101    * @param theId
102    */
103   public PDBChain(String thePdbid, String theId)
104   {
105     this(thePdbid, theId, null);
106   }
107
108   /**
109    * character used to write newlines
110    */
111   protected String newline = System.getProperty("line.separator");
112
113   public Mapping shadowMap;
114
115   public void setNewlineString(String nl)
116   {
117     newline = nl;
118   }
119
120   public String getNewlineString()
121   {
122     return newline;
123   }
124
125   public String print()
126   {
127     StringBuilder tmp = new StringBuilder(256);
128
129     for (Bond b : bonds)
130     {
131       tmp.append(b.at1.resName).append(" ").append(b.at1.resNumber)
132               .append(" ").append(offset).append(newline);
133     }
134
135     return tmp.toString();
136   }
137
138   /**
139    * Annotate the residues with their corresponding positions in s1 using the
140    * alignment in as NOTE: This clears all atom.alignmentMapping values on the
141    * structure.
142    * 
143    * @param as
144    * @param s1
145    */
146   public void makeExactMapping(AlignSeq as, SequenceI s1)
147   {
148     int pdbpos = as.getSeq2Start() - 2;
149     int alignpos = s1.getStart() + as.getSeq1Start() - 3;
150     // first clear out any old alignmentMapping values:
151     for (Atom atom : atoms)
152     {
153       atom.alignmentMapping = -1;
154     }
155     // and now trace the alignment onto the atom set.
156     for (int i = 0; i < as.astr1.length(); i++)
157     {
158       if (as.astr1.charAt(i) != '-')
159       {
160         alignpos++;
161       }
162
163       if (as.astr2.charAt(i) != '-')
164       {
165         pdbpos++;
166       }
167
168       boolean sameResidue = Comparison.isSameResidue(as.astr1.charAt(i),
169               as.astr2.charAt(i), false);
170       if (sameResidue)
171       {
172         if (pdbpos >= residues.size())
173         {
174           continue;
175         }
176         Residue res = residues.elementAt(pdbpos);
177         for (Atom atom : res.atoms)
178         {
179           atom.alignmentMapping = alignpos;
180         }
181       }
182     }
183   }
184
185   /**
186    * Annotate the residues with their corresponding positions in s1 using the
187    * alignment in as NOTE: This clears all atom.alignmentMapping values on the
188    * structure.
189    * 
190    * @param as
191    * @param s1
192    */
193   public void makeExactMapping(StructureMapping mapping, SequenceI s1)
194   {
195     // first clear out any old alignmentMapping values:
196     for (Atom atom : atoms)
197     {
198       atom.alignmentMapping = -1;
199     }
200     SequenceI ds = s1;
201     while (ds.getDatasetSequence() != null)
202     {
203       ds = ds.getDatasetSequence();
204     }
205     int pdboffset = 0;
206     for (Residue res : residues)
207     {
208       // res.number isn't set correctly for discontinuous/mismapped residues
209       int seqpos = mapping.getSeqPos(res.atoms.get(0).resNumber);
210       char strchar = sequence.getCharAt(pdboffset++);
211       if (seqpos == StructureMapping.UNASSIGNED_VALUE)
212       {
213         continue;
214       }
215       char seqchar = ds.getCharAt(seqpos - ds.getStart());
216
217       boolean sameResidue = Comparison.isSameResidue(seqchar, strchar,
218               false);
219       if (sameResidue)
220       {
221         for (Atom atom : res.atoms)
222         {
223           atom.alignmentMapping = seqpos - 1;
224         }
225       }
226     }
227   }
228
229   /**
230    * Copies over the RESNUM seqfeatures from the internal chain sequence to the
231    * mapped sequence
232    * 
233    * @param seq
234    * @param status
235    *          The Status of the transferred annotation
236    * 
237    * @param altPDBID
238    *          the group id for the features on the destination sequence (e.g.
239    *          the official accession ID)
240    */
241   public void transferRESNUMFeatures(SequenceI seq, String status,
242           String altPDBID)
243   {
244     if (altPDBID == null)
245     {
246       altPDBID = pdbid;
247     }
248     SequenceI sq = seq;
249     while (sq != null && sq.getDatasetSequence() != null)
250     {
251       sq = sq.getDatasetSequence();
252       if (sq == sequence)
253       {
254         return;
255       }
256     }
257
258     /*
259      * Remove any existing features for this chain if they exist ?
260      * SequenceFeature[] seqsfeatures=seq.getSequenceFeatures(); int
261      * totfeat=seqsfeatures.length; // Remove any features for this exact chain
262      * ? for (int i=0; i<seqsfeatures.length; i++) { }
263      */
264     if (status == null)
265     {
266       status = PDBChain.IEASTATUS;
267     }
268
269     List<SequenceFeature> features = sequence.getSequenceFeatures();
270     for (SequenceFeature feature : features)
271     {
272       if (feature.getFeatureGroup() != null
273               && feature.getFeatureGroup().equals(pdbid))
274       {
275         int newBegin = 1
276                 + residues.elementAt(feature.getBegin() - offset).atoms
277                         .elementAt(0).alignmentMapping;
278         int newEnd = 1 + residues.elementAt(feature.getEnd() - offset).atoms
279                 .elementAt(0).alignmentMapping;
280         SequenceFeature tx = new SequenceFeature(feature, newBegin, newEnd,
281                 altPDBID, feature.getScore());
282         tx.setStatus(status
283                 + ((tx.getStatus() == null || tx.getStatus().length() == 0)
284                         ? ""
285                         : ":" + tx.getStatus()));
286         if (tx.begin != 0 && tx.end != 0)
287         {
288           sq.addSequenceFeature(tx);
289         }
290       }
291     }
292   }
293
294   /**
295    * Traverses the list of residues and constructs bonds where CA-to-CA atoms or
296    * P-to-P atoms are found. Also sets the 'isNa' flag if more than 99% of
297    * residues contain a P not a CA.
298    */
299   public void makeCaBondList()
300   {
301     boolean na = false;
302     int numNa = 0;
303     for (int i = 0; i < (residues.size() - 1); i++)
304     {
305       Residue tmpres = residues.elementAt(i);
306       Residue tmpres2 = residues.elementAt(i + 1);
307       Atom at1 = tmpres.findAtom("CA");
308       Atom at2 = tmpres2.findAtom("CA");
309       na = false;
310       if ((at1 == null) && (at2 == null))
311       {
312         na = true;
313         at1 = tmpres.findAtom("P");
314         at2 = tmpres2.findAtom("P");
315       }
316       if ((at1 != null) && (at2 != null))
317       {
318         if (at1.chain.equals(at2.chain))
319         {
320           if (na)
321           {
322             numNa++;
323           }
324           makeBond(at1, at2);
325         }
326       }
327       else
328       {
329         System.out.println("not found " + i);
330       }
331     }
332
333     /*
334      * If > 99% 'P', flag as nucleotide; note the count doesn't include the last
335      * residue
336      */
337     if (residues.size() > 1 && (numNa / (residues.size() - 1) > 0.99))
338     {
339       isNa = true;
340     }
341   }
342
343   /**
344    * Construct a bond from atom1 to atom2 and add it to the list of bonds for
345    * this chain
346    * 
347    * @param at1
348    * @param at2
349    */
350   public void makeBond(Atom at1, Atom at2)
351   {
352     bonds.addElement(new Bond(at1, at2));
353   }
354
355   /**
356    * Traverses the list of atoms and
357    * <ul>
358    * <li>constructs a list of Residues, each containing all the atoms that share
359    * the same residue number</li>
360    * <li>adds a RESNUM sequence feature for each position</li>
361    * <li>creates the sequence string</li>
362    * <li>determines if nucleotide</li>
363    * <li>saves the residue number of the first atom as 'offset'</li>
364    * <li>adds temp factor annotation if the flag is set to do so</li>
365    * </ul>
366    * 
367    * @param visibleChainAnnotation
368    */
369   public void makeResidueList(boolean visibleChainAnnotation)
370   {
371     int count = 0;
372     Object symbol;
373     boolean deoxyn = false;
374     boolean nucleotide = false;
375     StringBuilder seq = new StringBuilder(256);
376     Vector<SequenceFeature> resFeatures = new Vector<>();
377     Vector<Annotation> resAnnotation = new Vector<>();
378     int iSize = atoms.size() - 1;
379     int resNumber = -1;
380     char insCode = ' ';
381
382     for (int i = 0; i <= iSize; i++)
383     {
384       Atom tmp = atoms.elementAt(i);
385       resNumber = tmp.resNumber;
386       insCode = tmp.insCode;
387
388       int res = resNumber;
389       char ins = insCode;
390
391       if (i == 0)
392       {
393         offset = resNumber;
394       }
395
396       Vector<Atom> resAtoms = new Vector<>();
397       // Add atoms to a vector while the residue number
398       // remains the same as the first atom's resNumber (res)
399       while ((resNumber == res) && (ins == insCode) && (i < atoms.size()))
400       {
401         resAtoms.add(atoms.elementAt(i));
402         i++;
403
404         if (i < atoms.size())
405         {
406           resNumber = atoms.elementAt(i).resNumber;
407           insCode = atoms.elementAt(i).insCode;
408         }
409         else
410         {
411           resNumber++;
412         }
413       }
414
415       // We need this to keep in step with the outer for i = loop
416       i--;
417
418       // Add inserted residues as features to the base residue
419       Atom currAtom = resAtoms.get(0);
420       if (currAtom.insCode != ' ' && !residues.isEmpty()
421               && residues.lastElement().atoms
422                       .get(0).resNumber == currAtom.resNumber)
423       {
424         String desc = currAtom.resName + ":" + currAtom.resNumIns + " "
425                 + pdbid + id;
426         SequenceFeature sf = new SequenceFeature("INSERTION", desc,
427                 offset + count - 1, offset + count - 1, "PDB_INS");
428         resFeatures.addElement(sf);
429         residues.lastElement().atoms.addAll(resAtoms);
430       }
431       else
432       {
433         // Make a new Residue object with the new atoms vector
434         residues.addElement(new Residue(resAtoms, resNumber - 1, count));
435
436         Residue tmpres = residues.lastElement();
437         Atom tmpat = tmpres.atoms.get(0);
438         // Make A new SequenceFeature for the current residue numbering
439         String desc = tmpat.resName + ":" + tmpat.resNumIns + " " + pdbid
440                 + id;
441         SequenceFeature sf = new SequenceFeature(RESNUM_FEATURE, desc,
442                 offset + count, offset + count, pdbid);
443         resFeatures.addElement(sf);
444         resAnnotation.addElement(new Annotation(tmpat.tfactor));
445         // Keep totting up the sequence
446
447         if ((symbol = ResidueProperties.getAA3Hash()
448                 .get(tmpat.resName)) == null)
449         {
450           String nucname = tmpat.resName.trim();
451           // use the aaIndex rather than call 'toLower' - which would take a bit
452           // more time.
453           deoxyn = nucname.length() == 2
454                   && ResidueProperties.aaIndex[nucname
455                           .charAt(0)] == ResidueProperties.aaIndex['D'];
456           if (tmpat.name.equalsIgnoreCase("CA")
457                   || ResidueProperties.nucleotideIndex[nucname
458                           .charAt((deoxyn ? 1 : 0))] == -1)
459           {
460             char r = ResidueProperties.getSingleCharacterCode(
461                     ResidueProperties.getCanonicalAminoAcid(tmpat.resName));
462             seq.append(r == '0' ? 'X' : r);
463             // System.err.println("PDBReader:Null aa3Hash for " +
464             // tmpat.resName);
465           }
466           else
467           {
468             // nucleotide flag
469             nucleotide = true;
470             seq.append(nucname.charAt((deoxyn ? 1 : 0)));
471           }
472         }
473         else
474         {
475           if (nucleotide)
476           {
477             System.err.println(
478                     "Warning: mixed nucleotide and amino acid chain.. its gonna do bad things to you!");
479           }
480           seq.append(ResidueProperties.aa[((Integer) symbol).intValue()]);
481         }
482         count++;
483       }
484     }
485
486     if (id.length() < 1)
487     {
488       id = " ";
489     }
490     isNa = nucleotide;
491     sequence = new Sequence(id, seq.toString(), offset, resNumber - 1); // Note:
492     // resNumber-offset
493     // ~=
494     // seq.size()
495     // Add normalised feature scores to RESNUM indicating start/end of sequence
496     // sf.setScore(offset+count);
497
498     // System.out.println("PDB Sequence is :\nSequence = " + seq);
499     // System.out.println("No of residues = " + residues.size());
500
501     if (StructureImportSettings.isShowSeqFeatures())
502     {
503       iSize = resFeatures.size();
504       for (int i = 0; i < iSize; i++)
505       {
506         sequence.addSequenceFeature(resFeatures.elementAt(i));
507         resFeatures.setElementAt(null, i);
508       }
509     }
510     if (visibleChainAnnotation)
511     {
512       Annotation[] annots = new Annotation[resAnnotation.size()];
513       float max = 0f;
514       float min = 0f;
515       iSize = annots.length;
516       for (int i = 0; i < iSize; i++)
517       {
518         annots[i] = resAnnotation.elementAt(i);
519         max = Math.max(max, annots[i].value);
520         min = Math.min(min, annots[i].value);
521         resAnnotation.setElementAt(null, i);
522       }
523       AlignmentAnnotation tfactorann = new AlignmentAnnotation(tfacName,
524               tfacName + " for " + pdbid + id, annots, min, max,
525               AlignmentAnnotation.LINE_GRAPH);
526
527       tfactorann.setCalcId(getClass().getName());
528
529       tfactorann.setSequenceRef(sequence);
530       sequence.addAlignmentAnnotation(tfactorann);
531     }
532   }
533
534   /**
535    * Colour start/end of bonds by charge
536    * <ul>
537    * <li>ASP and GLU red</li>
538    * <li>LYS and ARG blue</li>
539    * <li>CYS yellow</li>
540    * <li>others light gray</li>
541    * </ul>
542    */
543   public void setChargeColours()
544   {
545     for (Bond b : bonds)
546     {
547       if (b.at1 != null && b.at2 != null)
548       {
549         b.startCol = getChargeColour(b.at1.resName);
550         b.endCol = getChargeColour(b.at2.resName);
551       }
552       else
553       {
554         b.startCol = Color.gray;
555         b.endCol = Color.gray;
556       }
557     }
558   }
559
560   public static Color getChargeColour(String resName)
561   {
562     Color result = Color.lightGray;
563     if ("ASP".equals(resName) || "GLU".equals(resName))
564     {
565       result = Color.red;
566     }
567     else if ("LYS".equals(resName) || "ARG".equals(resName))
568     {
569       result = Color.blue;
570     }
571     else if ("CYS".equals(resName))
572     {
573       result = Color.yellow;
574     }
575     return result;
576   }
577
578   /**
579    * Sets the start/end colours of bonds to those of the start/end atoms
580    * according to the specified colour scheme. Note: currently only works for
581    * peptide residues.
582    * 
583    * @param cs
584    */
585   public void setChainColours(ColourSchemeI cs)
586   {
587     int index;
588     for (Bond b : bonds)
589     {
590       try
591       {
592         index = ResidueProperties.aa3Hash.get(b.at1.resName).intValue();
593         b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0,
594                 null, null, 0f);
595
596         index = ResidueProperties.aa3Hash.get(b.at2.resName).intValue();
597         b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0), 0,
598                 null, null, 0f);
599
600       } catch (Exception e)
601       {
602         b.startCol = Color.gray;
603         b.endCol = Color.gray;
604       }
605     }
606   }
607
608   public void setChainColours(Color col)
609   {
610     for (Bond b : bonds)
611     {
612       b.startCol = col;
613       b.endCol = col;
614     }
615   }
616
617   /**
618    * copy any sequence annotation onto the sequence mapped using the provided
619    * StructureMapping
620    * 
621    * @param mapping
622    *          - positional mapping between destination sequence and pdb resnum
623    * @param sqmpping
624    *          - mapping between destination sequence and local chain
625    */
626   public void transferResidueAnnotation(StructureMapping mapping,
627           jalview.datamodel.Mapping sqmpping)
628   {
629     SequenceI sq = mapping.getSequence();
630     SequenceI dsq = sq;
631     if (sqmpping == null)
632     {
633       // SIFTS mappings are recorded in the StructureMapping object...
634
635       sqmpping = mapping.getSeqToPdbMapping();
636     }
637     if (sq != null)
638     {
639       while (dsq.getDatasetSequence() != null)
640       {
641         dsq = dsq.getDatasetSequence();
642       }
643       // any annotation will be transferred onto the dataset sequence
644
645       if (shadow != null && shadow.getAnnotation() != null)
646       {
647
648         for (AlignmentAnnotation ana : shadow.getAnnotation())
649         {
650           // match on calcId, label and description so annotations from
651           // different structures are preserved
652           List<AlignmentAnnotation> transfer = sq.getAlignmentAnnotations(
653                   ana.getCalcId(), ana.label, ana.description);
654           if (transfer == null || transfer.size() == 0)
655           {
656             ana = new AlignmentAnnotation(ana);
657             ana.liftOver(sequence, shadowMap);
658             ana.liftOver(dsq, sqmpping);
659             dsq.addAlignmentAnnotation(ana);
660           }
661           else
662           {
663             continue;
664           }
665         }
666       }
667       else
668       {
669         if (sequence != null && sequence.getAnnotation() != null)
670         {
671           for (AlignmentAnnotation ana : sequence.getAnnotation())
672           {
673             // match on calcId, label and description so annotations from
674             // different structures are preserved
675             List<AlignmentAnnotation> transfer = dsq
676                     .getAlignmentAnnotations(ana.getCalcId(), ana.label,
677                             ana.description);
678             if (transfer == null || transfer.size() == 0)
679             {
680               ana = new AlignmentAnnotation(ana);
681               ana.liftOver(dsq, sqmpping);
682               dsq.addAlignmentAnnotation(ana);
683               // mapping.transfer(ana);
684             }
685             else
686             {
687               continue;
688             }
689           }
690         }
691       }
692       if (false)
693       {
694         // Useful for debugging mappings - adds annotation for mapped position
695         float min = -1, max = 0;
696         Annotation[] an = new Annotation[sq.getEnd() - sq.getStart() + 1];
697         for (int i = sq.getStart(), j = sq
698                 .getEnd(), k = 0; i <= j; i++, k++)
699         {
700           int prn = mapping.getPDBResNum(k + 1);
701
702           an[k] = new Annotation(prn);
703           if (min == -1)
704           {
705             min = k;
706             max = k;
707           }
708           else
709           {
710             if (min > k)
711             {
712               min = k;
713             }
714             else if (max < k)
715             {
716               max = k;
717             }
718           }
719         }
720         sq.addAlignmentAnnotation(new AlignmentAnnotation("PDB.RESNUM",
721                 "PDB Residue Numbering for " + this.pdbid + ":" + this.id,
722                 an, min, max, AlignmentAnnotation.LINE_GRAPH));
723       }
724     }
725   }
726 }