Repository navigation
Expand file tree
/
Copy pathAbstractSequence.java
More file actions
736 lines (638 loc) · 18.6 KB
/
Copy pathAbstractSequence.java
File metadata and controls
736 lines (638 loc) · 18.6 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
/*
* 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 01-21-2010
*
* @author Richard Holland
* @author Scooter Willis
* @author Paolo Pavan
*
*/
package org.biojava.nbio.core.sequence.template;
import org.biojava.nbio.core.exceptions.CompoundNotFoundException;
import org.biojava.nbio.core.sequence.AccessionID;
import org.biojava.nbio.core.sequence.DataSource;
import org.biojava.nbio.core.sequence.Strand;
import org.biojava.nbio.core.sequence.TaxonomyID;
import org.biojava.nbio.core.sequence.features.*;
import org.biojava.nbio.core.sequence.loader.UniprotProxySequenceReader;
import org.biojava.nbio.core.sequence.location.SequenceLocation;
import org.biojava.nbio.core.sequence.location.SimpleLocation;
import org.biojava.nbio.core.sequence.location.template.Location;
import org.biojava.nbio.core.sequence.reference.AbstractReference;
import org.biojava.nbio.core.sequence.storage.ArrayListSequenceReader;
import org.biojava.nbio.core.util.Equals;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
/**
*
* The base class for DNA, RNA and Protein sequences.
* @param <C>
*/
public abstract class AbstractSequence<C extends Compound> implements Sequence<C> {
private final static Logger logger = LoggerFactory.getLogger(AbstractSequence.class);
private TaxonomyID taxonomy;
private AccessionID accession;
private SequenceReader<C> sequenceStorage = null;
private CompoundSet<C> compoundSet;
private AnnotationType annotationType = AnnotationType.UNKNOWN;
private String description;
private String originalHeader;
private Collection<Object> userCollection;
private Integer bioBegin = null;
private Integer bioEnd = null;
private AbstractSequence<?> parentSequence = null;
private String source = null;
private ArrayList<String> notesList = new ArrayList<String>();
private Double sequenceScore = null;
private FeaturesKeyWordInterface featuresKeyWord = null;
private DatabaseReferenceInterface databaseReferences = null;
private FeatureRetriever featureRetriever = null;
private ArrayList<FeatureInterface<AbstractSequence<C>, C>> features =
new ArrayList<FeatureInterface<AbstractSequence<C>, C>>();
private LinkedHashMap<String, ArrayList<FeatureInterface<AbstractSequence<C>, C>>> groupedFeatures =
new LinkedHashMap<String, ArrayList<FeatureInterface<AbstractSequence<C>, C>>>();
private List<String> comments = new ArrayList<>();
private List<AbstractReference> references;
public AbstractSequence() {
}
/**
* Create a Sequence from a simple string where the values should be found in compoundSet
* @param seqString
* @param compoundSet
* @throws CompoundNotFoundException
*/
public AbstractSequence(String seqString, CompoundSet<C> compoundSet) throws CompoundNotFoundException {
setCompoundSet(compoundSet);
sequenceStorage = new ArrayListSequenceReader<C>();
sequenceStorage.setCompoundSet(this.getCompoundSet());
sequenceStorage.setContents(seqString);
}
/**
* A ProxySequenceReader allows abstraction of both the storage of the sequence data and the location
* of the sequence data. A variety of use cases are possible. A ProxySequenceReader that knows the offset and of the sequence in
* a large fasta file. A ProxySequenceReader that can pull Sequence data from UniProt, NCBI or a custom database.
* If the ProxySequenceReader implements various interfaces then the sequence will set those interfaces so that calls to
* various methods will be valid.
*
* @param proxyLoader
* @param compoundSet
*/
public AbstractSequence(SequenceReader<C> proxyLoader, CompoundSet<C> compoundSet) {
setCompoundSet(compoundSet);
setProxySequenceReader(proxyLoader);
}
/**
* Very important method that allows external mappings of sequence data and features. This method
* will gain additional interface inspection that allows external data sources with knowledge
* of features for a sequence to be supported.
*
* @param proxyLoader
*/
public void setProxySequenceReader(SequenceReader<C> proxyLoader) {
this.sequenceStorage = proxyLoader;
if (proxyLoader instanceof FeaturesKeyWordInterface) {
this.setFeaturesKeyWord((FeaturesKeyWordInterface) sequenceStorage);
}
if (proxyLoader instanceof DatabaseReferenceInterface) {
this.setDatabaseReferences((DatabaseReferenceInterface) sequenceStorage);
}
if (proxyLoader instanceof FeatureRetriever) {
this.setFeatureRetriever((FeatureRetriever) sequenceStorage);
HashMap<String, ArrayList<AbstractFeature>> ff = getFeatureRetriever().getFeatures();
for (String k: ff.keySet()){
for (AbstractFeature f: ff.get(k)){
this.addFeature(f);
}
}
// success of next statement guaranteed because source is a compulsory field
//DBReferenceInfo dbQualifier = (DBReferenceInfo)ff.get("source").get(0).getQualifiers().get("db_xref");
ArrayList<DBReferenceInfo> dbQualifiers = (ArrayList)ff.get("source").get(0).getQualifiers().get("db_xref");
DBReferenceInfo dbQualifier = dbQualifiers.get(0);
if (dbQualifier != null) this.setTaxonomy(new TaxonomyID(dbQualifier.getDatabase()+":"+dbQualifier.getId(), DataSource.UNKNOWN));
}
if(getAccession() == null && proxyLoader instanceof UniprotProxySequenceReader){ // we have lots of unsupported operations for this call so quick fix to allow this tow rork
this.setAccession(proxyLoader.getAccession());
}
}
public SequenceReader<C> getProxySequenceReader() {
return sequenceStorage;
}
/**
* @return the bioBegin
*/
public Integer getBioBegin() {
if (bioBegin == null) {
return 1;
} else {
return bioBegin;
}
}
/**
* @param bioBegin the bioBegin to set
*/
public void setBioBegin(Integer bioBegin) {
this.bioBegin = bioBegin;
}
/**
* @return the bioEnd
*/
public Integer getBioEnd() {
if (bioEnd == null) {
return this.getLength();
} else {
return bioEnd;
}
}
/**
* @param bioEnd the bioEnd to set
*/
public void setBioEnd(Integer bioEnd) {
this.bioEnd = bioEnd;
}
/**
* Provided for convince if the developer needs to associate data with a sequence
*
* @return
*/
public Collection<Object> getUserCollection() {
return userCollection;
}
/**
*
* @param userCollection
*/
public void setUserCollection(Collection<Object> userCollection) {
this.userCollection = userCollection;
}
/**
* @return the annotation
*/
public AnnotationType getAnnotationType() {
return annotationType;
}
/**
* @param annotationType the annotation to set
*/
public void setAnnotationType(AnnotationType annotationType) {
this.annotationType = annotationType;
}
/**
* @return the description
*/
public String getDescription() {
return description;
}
/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @return the originalHeader
*/
public String getOriginalHeader() {
return originalHeader;
}
/**
* @param originalHeader the originalHeader to set
*/
public void setOriginalHeader(String originalHeader) {
this.originalHeader = originalHeader;
}
/**
* @return the parentSequence
*/
public AbstractSequence<?> getParentSequence() {
return parentSequence;
}
/**
* @param parentSequence the parentSequence to set
*/
public void setParentSequence(AbstractSequence<?> parentSequence) {
this.parentSequence = parentSequence;
}
/**
* Added support for the source of this sequence for GFF3 export
* If a sub sequence doesn't have source then check for parent source
* @return the source
*/
public String getSource() {
if (source != null) {
return source;
}
if (parentSequence != null) {
return parentSequence.getSource();
}
return null;
}
/**
* Added support for the source of this sequence for GFF3 export
* @param source the source to set
*/
public void setSource(String source) {
this.source = source;
}
/**
* Add notes about this sequence that will get exported for GFF3
* @param note
*/
public void addNote(String note) {
notesList.add(note);
}
public void removeNote(String note) {
notesList.remove(note);
}
/**
* @return the notesList
*/
public ArrayList<String> getNotesList() {
return notesList;
}
/**
* @param notesList the notesList to set
*/
public void setNotesList(ArrayList<String> notesList) {
this.notesList = notesList;
}
/**
* Provide place holder for a metric that indicate a score associated with the sequence
* @return the sequenceScore
*/
public Double getSequenceScore() {
return sequenceScore;
}
/**
* @param sequenceScore the sequenceScore to set
*/
public void setSequenceScore(Double sequenceScore) {
this.sequenceScore = sequenceScore;
}
/**
* @since 5.0.0
* @return the list of {@link AbstractReference}
*/
public List<AbstractReference> getReferences() {
return references;
}
/**
* Set the list of {@link AbstractReference}
* @since 5.0.0
* @param references
*/
public void setReferences(List<AbstractReference> references) {
this.references = references;
}
/**
* Return features at a sequence position by type
* @param featureType
* @param bioSequencePosition
* @return
*/
public List<FeatureInterface<AbstractSequence<C>, C>> getFeatures(String featureType, int bioSequencePosition) {
ArrayList<FeatureInterface<AbstractSequence<C>, C>> featureHits =
new ArrayList<FeatureInterface<AbstractSequence<C>, C>>();
List<FeatureInterface<AbstractSequence<C>, C>> features = getFeaturesByType(featureType);
if (features != null) {
for (FeatureInterface<AbstractSequence<C>, C> feature : features) {
if (bioSequencePosition >= feature.getLocations().getStart().getPosition() && bioSequencePosition <= feature.getLocations().getEnd().getPosition()) {
featureHits.add(feature);
}
}
}
return featureHits;
}
/**
* Return features at a sequence position
* @param bioSequencePosition
* @return
*/
public List<FeatureInterface<AbstractSequence<C>, C>> getFeatures(int bioSequencePosition) {
ArrayList<FeatureInterface<AbstractSequence<C>, C>> featureHits =
new ArrayList<FeatureInterface<AbstractSequence<C>, C>>();
if (features != null) {
for (FeatureInterface<AbstractSequence<C>, C> feature : features) {
if (bioSequencePosition >= feature.getLocations().getStart().getPosition() && bioSequencePosition <= feature.getLocations().getEnd().getPosition()) {
featureHits.add(feature);
}
}
}
return featureHits;
}
/**
*
* @return
*/
public List<FeatureInterface<AbstractSequence<C>, C>> getFeatures() {
return features;
}
/**
* Method to help set the proper details for a feature as it relates to a sequence
* where the feature needs to have a location on the sequence
* @param bioStart
* @param bioEnd
* @param feature
*/
public void addFeature(int bioStart, int bioEnd, FeatureInterface<AbstractSequence<C>, C> feature) {
SequenceLocation<AbstractSequence<C>, C> sequenceLocation =
new SequenceLocation<AbstractSequence<C>, C>(bioStart, bioEnd, this);
feature.setLocation(sequenceLocation);
addFeature(feature);
}
/**
* Add a feature to this sequence. The feature will be added to the collection where the order is start position and if more than
* one feature at the same start position then longest is added first. This helps on doing feature layout for displaying features
* in SequenceFeaturePanel
* @param feature
*/
public void addFeature(FeatureInterface<AbstractSequence<C>, C> feature) {
features.add(feature);
ArrayList<FeatureInterface<AbstractSequence<C>, C>> featureList = groupedFeatures.get(feature.getType());
if (featureList == null) {
featureList = new ArrayList<FeatureInterface<AbstractSequence<C>, C>>();
groupedFeatures.put(feature.getType(), featureList);
}
featureList.add(feature);
Collections.sort(features, AbstractFeature.LOCATION_LENGTH);
Collections.sort(featureList, AbstractFeature.LOCATION_LENGTH);
}
/**
* Remove a feature from the sequence
* @param feature
*/
public void removeFeature(FeatureInterface<AbstractSequence<C>, C> feature) {
features.remove(feature);
ArrayList<FeatureInterface<AbstractSequence<C>, C>> featureList = groupedFeatures.get(feature.getType());
if (featureList != null) {
featureList.remove(feature);
if (featureList.isEmpty()) {
groupedFeatures.remove(feature.getType());
}
}
}
/**
*
* @param type
* @return
*/
public List<FeatureInterface<AbstractSequence<C>, C>> getFeaturesByType(String type) {
List<FeatureInterface<AbstractSequence<C>, C>> features = groupedFeatures.get(type);
if (features == null) {
features = new ArrayList<FeatureInterface<AbstractSequence<C>, C>>();
}
return features;
}
/**
*
* @return comments
*/
public List<String> getComments() {
return comments;
}
/**
* Set comments.
* @param comments
*/
public void setComments(List<String> comments) {
this.comments = comments;
}
/**
* @return the featuresKeyWord
*/
public FeaturesKeyWordInterface getFeaturesKeyWord() {
return featuresKeyWord;
}
/**
* @param featuresKeyWord the featuresKeyWord to set
*/
public void setFeaturesKeyWord(FeaturesKeyWordInterface featuresKeyWord) {
this.featuresKeyWord = featuresKeyWord;
}
/**
* @return the databaseReferences
*/
public DatabaseReferenceInterface getDatabaseReferences() {
return databaseReferences;
}
/**
* @param databaseReferences the databaseReferences to set
*/
public void setDatabaseReferences(DatabaseReferenceInterface databaseReferences) {
this.databaseReferences = databaseReferences;
}
public FeatureRetriever getFeatureRetriever() {
return featureRetriever;
}
public void setFeatureRetriever(FeatureRetriever featureRetriever) {
this.featureRetriever = featureRetriever;
}
public enum AnnotationType {
CURATED, PREDICTED, UNKNOWN;
}
/**
* @return the accession
*/
@Override
public AccessionID getAccession() {
return accession;
}
/**
* @param accession the accession to set
*/
public void setAccession(AccessionID accession) {
this.accession = accession;
}
/**
* @return the species
*/
public TaxonomyID getTaxonomy() {
return taxonomy;
}
/**
* @param taxonomy the species to set
*/
public void setTaxonomy(TaxonomyID taxonomy) {
this.taxonomy = taxonomy;
}
@Override
public CompoundSet<C> getCompoundSet() {
if (compoundSet != null) {
return compoundSet;
}
// This is invalid since the parentSequence isn't guaranteed to have the same compound set as this sequence,
// e.g., the case where the parent sequence for a protein is a CDS.
/*
if (parentSequence != null) {
return parentSequence.getCompoundSet();
}
*/
return null;
}
public void setCompoundSet(CompoundSet<C> compoundSet) {
this.compoundSet = compoundSet;
}
@Override
public boolean equals(Object o){
if(! Equals.classEqual(this, o)) {
return false;
}
Sequence<C> other = (Sequence<C>)o;
if ( other.getCompoundSet() != getCompoundSet())
return false;
List<C> rawCompounds = getAsList();
List<C> otherCompounds = other.getAsList();
if ( rawCompounds.size() != otherCompounds.size())
return false;
for (int i = 0 ; i < rawCompounds.size() ; i++){
Compound myCompound = rawCompounds.get(i);
Compound otherCompound = otherCompounds.get(i);
if ( ! myCompound.equalsIgnoreCase(otherCompound))
return false;
}
return true;
}
@Override
public int hashCode(){
String s = getSequenceAsString();
return s.hashCode();
}
@Override
public String toString() {
return getSequenceAsString();
}
private SequenceReader<C> getSequenceStorage() {
if (sequenceStorage != null) {
return sequenceStorage;
}
if (parentSequence != null) {
//return parentSequence.getSequenceStorage();
if ( this.compoundSet.equals(parentSequence.getCompoundSet())){
sequenceStorage = new ArrayListSequenceReader<C>();
sequenceStorage.setCompoundSet(this.getCompoundSet());
try {
sequenceStorage.setContents(parentSequence.getSequenceAsString());
} catch (CompoundNotFoundException e) {
// TODO is there a better way to handle this exception?
logger.error("Problem setting contents from parent sequence, some unrecognised compound: {}",e.getMessage());
}
return sequenceStorage;
}
}
return null;
}
/**
*
* @param bioStart
* @param bioEnd
* @param strand
* @return
*/
public String getSequenceAsString(Integer bioStart, Integer bioEnd, Strand strand) {
Location loc = new SimpleLocation(bioStart, bioEnd, strand);
return loc.getSubSequence(this).getSequenceAsString();
}
/**
* Default case is to assume strand is positive because only CDSSequence can be either positive or negative Strand.
* @return
*/
@Override
public String getSequenceAsString() {
return SequenceMixin.toString(this);
}
/**
*
* @return
*/
@Override
public List<C> getAsList() {
return sequenceStorage.getAsList();
}
/**
*
* @param position The 1-indexed position of the amino acid
* @return
*/
@Override
public C getCompoundAt(int position) {
return getSequenceStorage().getCompoundAt(position);
}
/**
*
* @param compound
* @return The first index of compound in this sequence (1-based)
*/
@Override
public int getIndexOf(C compound) {
return getSequenceStorage().getIndexOf(compound);
}
/**
*
* @param compound
* @return The last index of compound in this sequence (1-based)
*/
@Override
public int getLastIndexOf(C compound) {
return getSequenceStorage().getLastIndexOf(compound);
}
/**
*
* @return
*/
@Override
public int getLength() {
return getSequenceStorage().getLength();
}
/**
*
* @param bioStart
* @param bioEnd
* @return
*/
@Override
public SequenceView<C> getSubSequence(final Integer bioStart, final Integer bioEnd) {
return new SequenceProxyView<C>(this, bioStart, bioEnd);
}
/**
*
* @return
*/
@Override
public Iterator<C> iterator() {
return getSequenceStorage().iterator();
}
/**
*
* @param compounds
* @return
*/
@Override
public int countCompounds(C... compounds) {
return SequenceMixin.countCompounds(this, compounds);
}
/**
*
* @return
*/
@Override
public SequenceView<C> getInverse() {
return SequenceMixin.inverse(this);
}
}