Repository navigation
Expand file tree
/
Copy pathChainImpl.java
More file actions
802 lines (646 loc) · 18.7 KB
/
Copy pathChainImpl.java
File metadata and controls
802 lines (646 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
/*
* BioJava development code
*
* This code may be freely distributed and modified under the
* terms of the GNU Lesser General Public Licence. This should
* be distributed with the code. If you do not have a copy,
* see:
*
* http://www.gnu.org/copyleft/lesser.html
*
* Copyright for this code is held jointly by the individual
* authors. These should be listed in @author doc comments.
*
* For more information on the BioJava project and its aims,
* or to join the biojava-l mailing list, visit the home page
* at:
*
* http://www.biojava.org/
*
* Created on 12.03.2004
* @author Andreas Prlic
*
*/
package org.biojava.nbio.structure;
import org.biojava.nbio.structure.io.FileConvert;
import org.biojava.nbio.structure.io.mmcif.ChemCompGroupFactory;
import org.biojava.nbio.structure.io.mmcif.chem.PolymerType;
import org.biojava.nbio.structure.io.mmcif.model.ChemComp;
import org.biojava.nbio.core.exceptions.CompoundNotFoundException;
import org.biojava.nbio.core.sequence.ProteinSequence;
import org.biojava.nbio.core.sequence.compound.AminoAcidCompound;
import org.biojava.nbio.core.sequence.template.Sequence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
/**
* A Chain in a PDB file. It contains several groups which can be of
* one of the types defined in the {@link GroupType} constants.
*
* @author Andreas Prlic
* @author Jules Jacobsen
* @since 1.4
*/
public class ChainImpl implements Chain {
private final static Logger logger = LoggerFactory.getLogger(ChainImpl.class);
private static final long serialVersionUID = 1990171805277911840L;
/**
* The default chain identifier used to be an empty space
*/
private static final String DEFAULT_CHAIN_ID = "A";
private String swissprot_id ;
private String authId; // the 'public' chain identifier as assigned by authors in PDB files
private List <Group> groups;
private List<Group> seqResGroups;
private EntityInfo entity;
private Structure parent;
private Map<String, Integer> pdbResnumMap;
private String asymId; // the 'internal' chain identifier as used in mmCIF files
private List<SeqMisMatch> seqMisMatches = null;
/**
* Constructs a ChainImpl object.
*/
public ChainImpl() {
super();
authId = DEFAULT_CHAIN_ID;
groups = new ArrayList<>() ;
seqResGroups = new ArrayList<>();
pdbResnumMap = new HashMap<>();
asymId = null;
}
/** {@inheritDoc}
*
*/
@Override
public String getId() {
return asymId;
}
/** {@inheritDoc}
*
*/
@Override
public void setId(String asymId) {
this.asymId = asymId;
}
/** {@inheritDoc}
*
*/
@Override
public String getName() { return authId; }
/** {@inheritDoc}
*
*/
@Override
public void setName(String authId) { this.authId = authId; }
/** {@inheritDoc}
*
*/
@Override
@Deprecated
public void setParent(Structure parent) {
setStructure(parent);
}
/** {@inheritDoc}
*
*/
@Override
public void setStructure(Structure parent){
this.parent = parent;
}
/** Returns the parent Structure of this chain.
*
* @return the parent Structure object
*/
@Override
public Structure getStructure() {
return parent;
}
/** Returns the parent Structure of this chain.
*
* @return the parent Structure object
* @deprecated use getStructure instead.
*/
@Override
@Deprecated
public Structure getParent() {
return getStructure();
}
/** Returns an identical copy of this Chain .
* @return an identical copy of this Chain
*/
@Override
public Object clone() {
// go through all groups and add to new Chain.
ChainImpl n = new ChainImpl();
// copy chain data:
n.setId(getId());
n.setName(getName());
n.setSwissprotId ( getSwissprotId());
// NOTE the EntityInfo will be reset at the parent level (Structure) if cloning is happening from parent level
// here we don't deep-copy it and just keep the same reference, in case the cloning is happening at the Chain level only
n.setEntityInfo(this.entity);
for (Group group : groups) {
Group g = (Group) group.clone();
n.addGroup(g);
g.setChain(n);
}
if (seqResGroups!=null){
List<Group> tmpSeqRes = new ArrayList<>();
// cloning seqres and atom groups is ugly, due to their
// nested relationship (some of the atoms can be in the seqres, but not all)
for (Group seqResGroup : seqResGroups) {
if (seqResGroup==null) {
tmpSeqRes.add(null);
continue;
}
int i = groups.indexOf(seqResGroup);
Group g ;
if (i!=-1) {
// group found in atom groups, we get the equivalent reference from the newly cloned atom groups
g = n.getAtomGroup(i);
} else {
// group not found in atom groups, we clone the seqres group
g = (Group) seqResGroup.clone();
}
g.setChain(n);
tmpSeqRes.add(g);
}
n.setSeqResGroups(tmpSeqRes);
}
return n ;
}
/** {@inheritDoc}
*
*/
@Override
public void setEntityInfo(EntityInfo mol) {
this.entity = mol;
}
/** {@inheritDoc}
*
*/
@Override
public EntityInfo getEntityInfo() {
return this.entity;
}
/** set the Swissprot id of this chains .
* @param sp_id a String specifying the swissprot id value
* @see #getSwissprotId
*/
@Override
public void setSwissprotId(String sp_id){
swissprot_id = sp_id ;
}
/** get the Swissprot id of this chains .
* @return a String representing the swissprot id value
* @see #setSwissprotId
*/
@Override
public String getSwissprotId() {
return swissprot_id ;
}
/** {@inheritDoc}
*
*/
@Override
public void addGroup(Group group) {
group.setChain(this);
// Set the altlocs chain as well
for(Group g : group.getAltLocs()) {
g.setChain(this);
}
groups.add(group);
// store the position internally for quick access of this group
String pdbResnum = null ;
ResidueNumber resNum = group.getResidueNumber();
if ( resNum != null)
pdbResnum = resNum.toString();
if ( pdbResnum != null) {
Integer pos = groups.size() - 1;
// ARGH sometimes numbering in PDB files is confusing.
// e.g. PDB: 1sfe
/*
* ATOM 620 N GLY 93 -24.320 -6.591 4.210 1.00 46.82 N
* ATOM 621 CA GLY 93 -24.960 -6.849 5.497 1.00 47.35 C
* ATOM 622 C GLY 93 -26.076 -5.873 5.804 1.00 47.24 C
* ATOM 623 O GLY 93 -26.382 -4.986 5.006 1.00 47.56 O
* and ...
* HETATM 1348 O HOH 92 -21.853 -16.886 19.138 1.00 66.92 O
* HETATM 1349 O HOH 93 -26.126 1.226 29.069 1.00 71.69 O
* HETATM 1350 O HOH 94 -22.250 -18.060 -6.401 1.00 61.97 O
*/
// this check is to give in this case the entry priority that is an AminoAcid / comes first...
// a good example of same residue number for 2 residues is 3th3, chain T, residue 201 (a LYS and a sugar BGC covalently attached to it) - JD 2016-03-09
if ( pdbResnumMap.containsKey(pdbResnum)) {
logger.warn("Adding residue {}({}) to chain {} but a residue with same residue number is already present: {}({}). Will add only the aminoacid residue (if any) to the lookup, lookups for that residue number won't work properly.",
pdbResnum, group.getPDBName(), getChainID(), groups.get(pdbResnumMap.get(pdbResnum)).getResidueNumber(), groups.get(pdbResnumMap.get(pdbResnum)).getPDBName());
if ( group instanceof AminoAcid)
pdbResnumMap.put(pdbResnum,pos);
} else
pdbResnumMap.put(pdbResnum,pos);
}
}
/**
* {@inheritDoc}
*/
@Override
public Group getAtomGroup(int position) {
return groups.get(position);
}
/**
* {@inheritDoc}
*/
@Override
public List<Group> getAtomGroups(GroupType type){
List<Group> tmp = new ArrayList<>() ;
for (Group g : groups) {
if (g.getType().equals(type)) {
tmp.add(g);
}
}
return tmp ;
}
/** {@inheritDoc}
*
*/
@Override
public List<Group> getAtomGroups(){
return groups ;
}
/** {@inheritDoc}
*
*/
@Override
public void setAtomGroups(List<Group> groups){
for (Group g:groups){
g.setChain(this);
}
this.groups = groups;
}
@Override
public Group[] getGroupsByPDB(ResidueNumber start, ResidueNumber end, boolean ignoreMissing)
throws StructureException {
// Short-circut for include all groups
if(start == null && end == null) {
return groups.toArray(new Group[groups.size()]);
}
List<Group> retlst = new ArrayList<>();
boolean adding, foundStart;
if( start == null ) {
// start with first group
adding = true;
foundStart = true;
} else {
adding = false;
foundStart = false;
}
for (Group g: groups){
// Check for start
if (!adding && start.equalsPositional(g.getResidueNumber())) {
adding = true;
foundStart = true;
}
// Check if past start
if ( ignoreMissing && ! (foundStart && adding) ) {
ResidueNumber pos = g.getResidueNumber();
if ( start != null && start.compareToPositional(pos) <= 0) {
foundStart = true;
adding = true;
}
}
if ( adding)
retlst.add(g);
// check for end
if ( end != null && end.equalsPositional(g.getResidueNumber())) {
if ( ! adding)
throw new StructureException("did not find start PDB residue number " + start + " in chain " + authId);
adding = false;
break;
}
// check if past end
if ( ignoreMissing && adding && end != null){
ResidueNumber pos = g.getResidueNumber();
if ( end.compareToPositional(pos) <= 0) {
adding = false;
break;
}
}
}
if ( ! foundStart){
throw new StructureException("did not find start PDB residue number " + start + " in chain " + authId);
}
if ( end != null && adding && !ignoreMissing) {
throw new StructureException("did not find end PDB residue number " + end + " in chain " + authId);
}
//not checking if the end has been found in this case...
return retlst.toArray(new Group[retlst.size()] );
}
/**
* {@inheritDoc}
*
*/
@Override
public Group getGroupByPDB(ResidueNumber resNum) throws StructureException {
String pdbresnum = resNum.toString();
if ( pdbResnumMap.containsKey(pdbresnum)) {
Integer pos = pdbResnumMap.get(pdbresnum);
return groups.get(pos);
} else {
throw new StructureException("unknown PDB residue number " + pdbresnum + " in chain " + authId);
}
}
/**
* {@inheritDoc}
*
*/
@Override
public Group[] getGroupsByPDB(ResidueNumber start, ResidueNumber end)
throws StructureException {
return getGroupsByPDB(start, end, false);
}
/**
* {@inheritDoc}
*/
@Override
public int getSeqResLength() {
//new method returns the length of the sequence defined in the SEQRES records
return seqResGroups.size();
}
/**
* {@inheritDoc}
*/
@Override
public void setChainID(String asymId) { this.asymId = asymId; }
/**
* {@inheritDoc}
*/
@Override
public String getChainID() { return this.asymId; }
/** String representation.
* @return String representation of the Chain
*/
@Override
public String toString(){
String newline = System.getProperty("line.separator");
StringBuilder str = new StringBuilder();
str.append("Chain asymId:").append(getChainID()).append(" authId:").append(getName()).append(newline);
if ( entity != null ){
if ( entity.getDescription() != null){
str.append(entity.getDescription()).append(newline);
}
}
str.append("total SEQRES length: ").append(getSeqResGroups().size()).append(" total ATOM length:")
.append(getAtomLength()).append(" residues ").append(newline);
return str.toString() ;
}
/**
* {@inheritDoc}
*/
@Override
public Sequence<?> getBJSequence() {
String seq = getSeqResSequence();
Sequence<AminoAcidCompound> s = null;
try {
s = new ProteinSequence(seq);
} catch (CompoundNotFoundException e) {
logger.error("Could not create sequence object from seqres sequence. Some unknown compound: {}",e.getMessage());
}
//TODO: return a DNA sequence if the content is DNA...
return s;
}
/**
* {@inheritDoc}
*/
@Override
public String getAtomSequence(){
List<Group> groups = getAtomGroups();
StringBuilder sequence = new StringBuilder() ;
for ( Group g: groups){
ChemComp cc = g.getChemComp();
if ( PolymerType.PROTEIN_ONLY.contains(cc.getPolymerType()) ||
PolymerType.POLYNUCLEOTIDE_ONLY.contains(cc.getPolymerType())){
// an amino acid residue.. use for alignment
String oneLetter= ChemCompGroupFactory.getOneLetterCode(cc);
if ( oneLetter == null)
oneLetter = Character.toString(StructureTools.UNKNOWN_GROUP_LABEL);
sequence.append(oneLetter);
}
}
return sequence.toString();
}
/**
* {@inheritDoc}
*/
@Override
public String getSeqResSequence(){
StringBuilder str = new StringBuilder();
for (Group g : seqResGroups) {
ChemComp cc = g.getChemComp();
if ( cc == null) {
logger.warn("Could not load ChemComp for group: ", g);
str.append(StructureTools.UNKNOWN_GROUP_LABEL);
} else if ( PolymerType.PROTEIN_ONLY.contains(cc.getPolymerType()) ||
PolymerType.POLYNUCLEOTIDE_ONLY.contains(cc.getPolymerType())){
// an amino acid residue.. use for alignment
String oneLetter= ChemCompGroupFactory.getOneLetterCode(cc);
// AB oneLetter.length() should be one. e.g. in 1EMA it is 3 and this makes mapping residue to sequence impossible.
if ( oneLetter == null || oneLetter.isEmpty() || oneLetter.equals("?")) {
oneLetter = Character.toString(StructureTools.UNKNOWN_GROUP_LABEL);
}
str.append(oneLetter);
} else {
str.append(StructureTools.UNKNOWN_GROUP_LABEL);
}
}
return str.toString();
}
/**
* Get the one letter sequence so that Sequence is guaranteed to
* be the same length as seqResGroups.
* Method related to https://github.com/biojava/biojava/issues/457
* @return a string of the sequence guaranteed to be the same length
* as seqResGroups.
*/
public String getSeqResOneLetterSeq(){
StringBuilder str = new StringBuilder();
for (Group g : seqResGroups) {
ChemComp cc = g.getChemComp();
if ( cc == null) {
logger.warn("Could not load ChemComp for group: ", g);
str.append(StructureTools.UNKNOWN_GROUP_LABEL);
} else if ( PolymerType.PROTEIN_ONLY.contains(cc.getPolymerType()) ||
PolymerType.POLYNUCLEOTIDE_ONLY.contains(cc.getPolymerType())){
// an amino acid residue.. use for alignment
String oneLetter= ChemCompGroupFactory.getOneLetterCode(cc);
// AB oneLetter.length() should be one. e.g. in 1EMA it is 3 and this makes mapping residue to sequence impossible.
if ( oneLetter == null || oneLetter.isEmpty() || oneLetter.equals("?") || oneLetter.length()!=1) {
oneLetter = Character.toString(StructureTools.UNKNOWN_GROUP_LABEL);
}
str.append(oneLetter);
} else {
str.append(StructureTools.UNKNOWN_GROUP_LABEL);
}
}
return str.toString();
}
/**
* {@inheritDoc}
*/
@Override
public Group getSeqResGroup(int position) {
return seqResGroups.get(position);
}
/**
* {@inheritDoc}
*/
@Override
public List<Group> getSeqResGroups(GroupType type) {
List<Group> tmp = new ArrayList<>() ;
for (Group g : seqResGroups) {
if (g.getType().equals(type)) {
tmp.add(g);
}
}
return tmp ;
}
/** {@inheritDoc}
*
*/
@Override
public List<Group> getSeqResGroups() {
return seqResGroups;
}
/** {@inheritDoc}
*
*/
@Override
public void setSeqResGroups(List<Group> groups){
for (Group g: groups){
if (g != null) {
g.setChain(this);
}
}
this.seqResGroups = groups;
}
/** {@inheritDoc}
*
*/
@Override
public int getAtomLength() {
return groups.size();
}
/** {@inheritDoc}
*
*/
@Override
public List<Group> getAtomLigands(){
List<Group> ligands = new ArrayList<>();
for (Group g : groups)
if (!seqResGroups.contains(g) && !g.isWater())
ligands.add(g);
return ligands;
}
@Override
public String getInternalChainID() {
return asymId;
}
@Override
public void setInternalChainID(String internalChainID) {
this.asymId = internalChainID;
}
@Override
public String toPDB() {
return FileConvert.toPDB(this);
}
@Override
public String toMMCIF() {
return FileConvert.toMMCIF(this, true);
}
@Override
public void setSeqMisMatches(List<SeqMisMatch> seqMisMatches) {
this.seqMisMatches = seqMisMatches;
}
@Override
public List<SeqMisMatch> getSeqMisMatches() {
return seqMisMatches;
}
@Override
public EntityType getEntityType() {
if (getEntityInfo()==null) return null;
return getEntityInfo().getType();
}
@Override
public boolean isWaterOnly() {
for (Group g : getAtomGroups()) {
if (!g.isWater())
return false;
}
return true;
}
@Override
public boolean isPureNonPolymer() {
for (Group g : getAtomGroups()) {
//ChemComp cc = g.getChemComp();
if ( g.isPolymeric() &&
!g.isHetAtomInFile() ) {
// important: the aminoacid or nucleotide residue can be in Atom records
return false;
}
}
return true;
}
@Override
public GroupType getPredominantGroupType(){
double ratioResiduesToTotal = StructureTools.RATIO_RESIDUES_TO_TOTAL;
int sizeAminos = getAtomGroups(GroupType.AMINOACID).size();
int sizeNucleotides = getAtomGroups(GroupType.NUCLEOTIDE).size();
List<Group> hetAtoms = getAtomGroups(GroupType.HETATM);
int sizeHetatoms = hetAtoms.size();
int sizeWaters = 0;
for (Group g : hetAtoms) {
if (g.isWater())
sizeWaters++;
}
int sizeHetatomsWithoutWater = sizeHetatoms - sizeWaters;
int fullSize = sizeAminos + sizeNucleotides + sizeHetatomsWithoutWater;
if ((double) sizeAminos / (double) fullSize > ratioResiduesToTotal)
return GroupType.AMINOACID;
if ((double) sizeNucleotides / (double) fullSize > ratioResiduesToTotal)
return GroupType.NUCLEOTIDE;
if ((double) (sizeHetatomsWithoutWater) / (double) fullSize > ratioResiduesToTotal)
return GroupType.HETATM;
// finally if neither condition works, we try based on majority, but log
// it
GroupType max;
if (sizeNucleotides > sizeAminos) {
if (sizeNucleotides > sizeHetatomsWithoutWater) {
max = GroupType.NUCLEOTIDE;
} else {
max = GroupType.HETATM;
}
} else {
if (sizeAminos > sizeHetatomsWithoutWater) {
max = GroupType.AMINOACID;
} else {
max = GroupType.HETATM;
}
}
logger.debug(
"Ratio of residues to total for chain with asym_id {} is below {}. Assuming it is a {} chain. "
+ "Counts: # aa residues: {}, # nuc residues: {}, # non-water het residues: {}, # waters: {}, "
+ "ratio aa/total: {}, ratio nuc/total: {}",
getId(), ratioResiduesToTotal, max, sizeAminos,
sizeNucleotides, sizeHetatomsWithoutWater, sizeWaters,
(double) sizeAminos / (double) fullSize,
(double) sizeNucleotides / (double) fullSize);
return max;
}
@Override
public boolean isProtein() {
return getPredominantGroupType() == GroupType.AMINOACID;
}
@Override
public boolean isNucleicAcid() {
return getPredominantGroupType() == GroupType.NUCLEOTIDE;
}
}