001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * Created on August 13, 2010
021 * Author: Mark Chapman
022 */
023
024package org.biojava.nbio.alignment.io;
025
026import org.biojava.nbio.alignment.io.StockholmStructure.DatabaseReference;
027
028import java.util.*;
029
030/**
031 * Stores all the content parsed from the #=GF lines
032 *
033 *
034 * @since 3.0.5
035 * @author Amr ALHOSSARY
036 * @author Marko Vaz
037 *
038 */
039public class StockholmFileAnnotation {
040        public static class StockholmFileAnnotationReference {
041                private String refMedline;//TODO 8 digits
042                private CharSequence refTitle;//on several lines
043                private CharSequence refAuthor;//TODO comma-separated, semicolon terminated list;
044                /**<b>TODO to be formatted later on.</b><br>
045                 * RL  Journal abbreviation year;volume:page-page.<br>
046                 * RL   Virus Genes 1997;14:163-165.<br>
047                 * RL   J Mol Biol 1994;242:309-320.
048                 */
049                private String refLocation;
050
051
052                public String getRefMedline() {
053                        return refMedline;
054                }
055                public void setRefMedline(String refMedline) {
056                        this.refMedline = refMedline;
057                }
058                public String getRefTitle() {
059                        return refTitle.toString();
060                }
061                public void setRefTitle(String refTitle) {
062                        this.refTitle = refTitle;
063                }
064                public void addToRefTitle(String refTitle) {
065                        if (this.refTitle== null) {
066                                this.refTitle = new StringBuffer(refTitle);
067                        } else if (this.refTitle instanceof StringBuffer){
068                                ((StringBuffer) this.refTitle).append(' ').append(refTitle);
069                        }else {
070                                this.refTitle = new StringBuffer(this.refTitle).append(' ').append(refTitle);
071                        }
072                }
073
074                public String getRefAuthor() {
075                        return refAuthor.toString();
076                }
077                public void setRefAuthor(StringBuffer refAuthor) {
078                        this.refAuthor = refAuthor;
079                }
080                public void addToRefAuthor(String refAuthor) {
081                        if (this.refAuthor== null) {
082                                this.refAuthor = new StringBuffer(refAuthor);
083                        } else if (this.refAuthor instanceof StringBuffer){
084                                ((StringBuffer) this.refAuthor).append(' ').append(refAuthor);
085                        }else {
086                                this.refAuthor = new StringBuffer(this.refAuthor).append(' ').append(refAuthor);
087                        }
088                }
089
090                public String getRefLocation() {
091                        return refLocation;
092                }
093                public void setRefLocation(String refLocation) {
094                        this.refLocation = refLocation;
095                }
096
097
098
099        }
100
101        //TODO revise these 4 fields usage
102        private final static String TREE_DEFAULT_ID = "DEFAULT_ID";
103        private static final String PB_PFAM_STRING = "PB";
104        private static final String PF_PFAM_STRING = "PF";
105        private static final String RF_RFAM_STRING = "RF";
106
107        private StringBuffer format;
108        private StringBuffer version;
109        private String accessionNumber;
110        private StringBuffer identification;
111        private StringBuffer definition;
112        private String[] authors;
113        private String alignmentMethod;
114        private CharSequence buildMethod;
115        private StringBuffer searchMethod;
116        private StringBuffer sourceSeed;
117        private StringBuffer sourceStructure;
118        private float[] gatheringThreshs;
119        private float[] noiseCutoffs;
120        private float[] trustedCutoffs;
121        private String typeField;
122        private String[] previousIDs;
123        private int numSequences;
124        private StringBuffer dbComment;
125        private Set<DatabaseReference> dbReferences;
126        private StringBuffer refComment;
127        /**TODO When implementing toString(), the function should loop on the vector */
128        private Vector<StockholmFileAnnotationReference> references = new Vector<StockholmFileAnnotation.StockholmFileAnnotationReference>();
129        private StringBuffer keywords;
130        private CharSequence comment;
131        private StringBuffer pfamAccession;
132        private StringBuffer location;
133        private StringBuffer wikipediaLink;
134        private StringBuffer clan;
135        private StringBuffer membership;
136        private final Map<String, List<String>> embTrees;
137        private float falseDiscoveryRate;
138
139        public StockholmFileAnnotation() {
140                embTrees = new HashMap<String, List<String>>();
141        }
142
143        public StringBuffer getDbComment() {
144                return dbComment;
145        }
146
147        public void setDbComment(String dbComment) {
148                if (this.dbComment != null) {
149                        this.dbComment.append(dbComment);
150                } else {
151                        this.dbComment = new StringBuffer(dbComment);
152                }
153        }
154
155        public Set<DatabaseReference> getDbReferences() {
156                return dbReferences;
157        }
158
159        public void setDbReferences(Set<DatabaseReference> dbReferences) {
160                this.dbReferences = dbReferences;
161        }
162        /**
163         * @param dbReference the string without the initial annotation identifier ( #=GS DR )
164         */
165        public void addDBReference(String dbReferenceRepresentingString) {
166                if (this.dbReferences == null) {
167                        this.dbReferences = new HashSet<DatabaseReference>();
168                }
169                dbReferences.add(new DatabaseReference(dbReferenceRepresentingString));
170        }
171
172        public float getFalseDiscoveryRate() {
173                return falseDiscoveryRate;
174        }
175
176        public void setFalseDiscoveryRate(float falseDiscoveryRate) {
177                this.falseDiscoveryRate = falseDiscoveryRate;
178        }
179
180        public StringBuffer getRefComment() {
181                return refComment;
182        }
183
184        public StringBuffer getKeywords() {
185                return keywords;
186        }
187
188        public String getComment() {
189                return comment.toString();
190        }
191
192        public StringBuffer getPfamAccession() {
193                return pfamAccession;
194        }
195
196        public StringBuffer getLocation() {
197                return location;
198        }
199
200        public StringBuffer getWikipediaLink() {
201                return wikipediaLink;
202        }
203
204        public StringBuffer getClan() {
205                return clan;
206        }
207
208        public StringBuffer getMembership() {
209                return membership;
210        }
211
212        public Map<String, List<String>> getEmbTrees() {
213                return embTrees;
214        }
215
216        public void setNumSequences(int numSequences) {
217                this.numSequences = numSequences;
218        }
219
220        public StringBuffer getIdentification() {
221                return identification;
222        }
223
224        public void setGFIdentification(String identification) {
225                if (this.identification != null) {
226                        this.identification.append(identification);
227                } else {
228                        this.identification = new StringBuffer(identification);
229                }
230        }
231
232        public StringBuffer getDefinition() {
233                return definition;
234        }
235
236        public void setGFDefinition(String definition) {
237                if (this.definition != null) {
238                        this.definition.append(definition);
239                } else {
240                        this.definition = new StringBuffer(definition);
241                }
242        }
243
244        public String[] getAuthors() {
245                return authors;
246        }
247
248        public void setGFAuthors(String authors) {
249                this.authors = authors.split(",");
250        }
251
252        public String getBuildMethod() {
253                return buildMethod.toString();
254        }
255
256        public void addGFBuildMethod(String buildMethod) {
257                if (this.buildMethod == null) {
258                        this.buildMethod = new StringBuffer(buildMethod);
259                } else if (this.buildMethod instanceof StringBuffer){
260                        ((StringBuffer) this.buildMethod).append(System.getProperty("line.seperator")).append(buildMethod);
261                }else {
262                        this.buildMethod = new StringBuffer(this.buildMethod).append(System.getProperty("line.seperator")).append(buildMethod);
263                }
264        }
265
266        public StringBuffer getSearchMethod() {
267                return searchMethod;
268        }
269
270        public void setGFSearchMethod(String searchMethod) {
271                if (this.searchMethod != null) {
272                        this.searchMethod.append(searchMethod);
273                } else {
274                        this.searchMethod = new StringBuffer(searchMethod);
275                }
276        }
277
278        public StringBuffer getSourceSeed() {
279                return sourceSeed;
280        }
281
282        public void setGFSourceSeed(String sourceSeed) {
283                if (this.sourceSeed != null) {
284                        this.sourceSeed.append(sourceSeed);
285                } else {
286                        this.sourceSeed = new StringBuffer(sourceSeed);
287                }
288        }
289
290        public StringBuffer getSourceStructure() {
291                return sourceStructure;
292        }
293
294        public void setGFSourceStructure(String sourceStructure) {
295                if (this.sourceStructure != null) {
296                        this.sourceStructure.append(sourceStructure);
297                } else {
298                        this.sourceStructure = new StringBuffer(sourceStructure);
299                }
300        }
301
302        /**Not always 2.<br>
303         * It may undergo further change.
304         * @return
305         */
306        public float[] getGatheringThreshs() {
307                return gatheringThreshs;
308        }
309
310        public void setGFGatheringThreshs(String gatheringThresh) {
311                this.gatheringThreshs = stringToFloats(gatheringThresh);
312        }
313
314        /**Not always 2.<br>
315         * It may undergo further change.
316         * @return
317         */
318        public float[] getNoiseCutoffs() {
319                return noiseCutoffs;
320        }
321
322        public void setGFNoiseCutoffs(String noiseCutoff) {
323                this.noiseCutoffs=stringToFloats(noiseCutoff);
324        }
325
326
327        /**Not always 2.<br>
328         * It may undergo further change.
329         * @return
330         */
331        public float[] getTrustedCutoffs() {
332                return trustedCutoffs;
333        }
334
335        public void setGFTrustedCutoffs(String trustedCutoff) {
336                this.trustedCutoffs = stringToFloats(trustedCutoff);
337        }
338
339        public float[] stringToFloats(String string) {
340                String[] coublets= string.split(";");
341                float[] floats = new float[coublets.length*2];
342                int counter=0;
343                for (int i = 0; i < coublets.length; i++) {
344                        String[] subStrings = coublets[i].trim().split("\\s");
345                        float f = Float.parseFloat(subStrings[i]);
346                        floats[counter++]=f;
347                }
348                return floats;
349        }
350
351        public String getTypeField() {
352                return typeField;
353        }
354
355        public void setGFTypeField(String typeField) {
356                this.typeField = typeField;
357        }
358
359        public String[] getPreviousIDs() {
360                return previousIDs;
361        }
362
363        public void setGFPreviousIDs(String previousIDs) {
364                this.previousIDs = previousIDs.split(";");
365        }
366
367        public StringBuffer getFormat() {
368                return format;
369        }
370
371        public void setFormat(String format) {
372                if (this.format != null) {
373                        this.format.append(format);
374                } else {
375                        this.format = new StringBuffer(format);
376                }
377        }
378
379        public StringBuffer getVersion() {
380                return version;
381        }
382
383        public void setVersion(String version) {
384                if (this.version != null) {
385                        this.version.append(version);
386                } else {
387                        this.version = new StringBuffer(version);
388                }
389        }
390
391        public String getAccessionNumber() {
392                return accessionNumber;
393        }
394
395        public void setGFAccessionNumber(String accessionNumber) {
396                this.accessionNumber = accessionNumber;
397        }
398
399        public boolean isPFam() {
400                return accessionNumber != null
401                                && (accessionNumber.startsWith(PF_PFAM_STRING) || accessionNumber.startsWith(PB_PFAM_STRING));
402        }
403
404        public boolean isRFam() {
405                return accessionNumber == null
406                                || accessionNumber.startsWith(RF_RFAM_STRING);
407        }
408
409        public int getNumSequences() {
410                return numSequences;
411        }
412
413        public void setGFNumSequences(String numSequences) {
414                this.numSequences = Integer.parseInt(numSequences);
415        }
416
417        public void setGFDBComment(String dbComment) {
418                if (this.dbComment != null) {
419                        this.dbComment.append(dbComment);
420                } else {
421                        this.dbComment = new StringBuffer(dbComment);
422                }
423        }
424
425
426        public void setGFRefComment(String refComment) {
427                if (this.refComment != null) {
428                        this.refComment.append(refComment);
429                } else {
430                        this.refComment = new StringBuffer(refComment);
431                }
432        }
433
434        public void setGFKeywords(String keywords) {
435                if (this.keywords != null) {
436                        this.keywords.append(keywords);
437                } else {
438                        this.keywords = new StringBuffer(keywords);
439                }
440        }
441
442        public void addToGFComment(String comment) {
443                if (this.comment == null) {
444                        this.comment = new StringBuffer(comment);
445                } else if (this.comment instanceof StringBuffer){
446                        ((StringBuffer) this.comment).append(' ').append(comment);
447                }else {
448                        this.comment = new StringBuffer(this.comment).append(' ').append(comment);
449                }
450        }
451
452        public void setGFPfamAccession(String pfamAccession) {
453                if (this.pfamAccession != null) {
454                        this.pfamAccession.append(pfamAccession);
455                } else {
456                        this.pfamAccession = new StringBuffer(pfamAccession);
457                }
458        }
459
460        public void setGFLocation(String location) {
461                if (this.location != null) {
462                        this.location.append(location);
463                } else {
464                        this.location = new StringBuffer(location);
465                }
466        }
467
468        public void setGFWikipediaLink(String wikipediaLink) {
469                if (this.wikipediaLink != null) {
470                        this.wikipediaLink.append(wikipediaLink);
471                } else {
472                        this.wikipediaLink = new StringBuffer(wikipediaLink);
473                }
474        }
475
476        public void setGFClan(String clan) {
477                if (this.clan != null) {
478                        this.clan.append(clan);
479                } else {
480                        this.clan = new StringBuffer(clan);
481                }
482        }
483
484        public void setGFMembership(String membership) {
485                if (this.membership != null) {
486                        this.membership.append(membership);
487                } else {
488                        this.membership = new StringBuffer(membership);
489                }
490        }
491
492        public void addGFNewHampshire(String newHampshire) {
493                List<String> hampshireTree = embTrees.get(TREE_DEFAULT_ID);
494                if (hampshireTree == null) {
495                        hampshireTree = new ArrayList<String>();
496                }
497                hampshireTree.add(newHampshire);
498                embTrees.put(TREE_DEFAULT_ID, hampshireTree);
499        }
500
501        public void addGFTreeID(String treeID) {
502                List<String> hampshireTree = embTrees.remove(TREE_DEFAULT_ID);
503                embTrees.put(treeID, hampshireTree);
504        }
505
506        public void addGFFalseDiscoveryRate(String falseDiscoveryRate) {
507                this.falseDiscoveryRate = Float.parseFloat(falseDiscoveryRate);
508        }
509
510        public String getAlignmentMethod() {
511                return alignmentMethod;
512        }
513
514        public void setAlignmentMethod(String alignmentMethod) {
515                this.alignmentMethod = alignmentMethod;
516        }
517
518        public Vector<StockholmFileAnnotationReference> getReferences() {
519                return references;
520        }
521
522        public void setReferences(Vector<StockholmFileAnnotationReference> references) {
523                this.references = references;
524        }
525
526}