deleteSequenceFeature
[jalview.git] / src / jalview / datamodel / Sequence.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 */
19 package jalview.datamodel;
20
21 import java.awt.*;
22
23 import java.util.*;
24
25
26 /**
27  * DOCUMENT ME!
28  *
29  * @author $author$
30  * @version $Revision$
31  */
32 public class Sequence implements SequenceI
33 {
34   SequenceI datasetSequence;
35   String name;
36   private String sequence;
37   String description;
38   int start;
39   int end;
40   Color color = Color.white;
41   Vector pdbIds;
42   String vamsasId;
43   DBRefEntry[] dbrefs;
44
45   /** This annotation is displayed below the alignment but the
46    * positions are tied to the residues of this sequence */
47   Vector annotation;
48
49   /** DOCUMENT ME!! */
50   public SequenceFeature[] sequenceFeatures;
51
52   /** This array holds hidden sequences
53    * of which this sequence is the representitive member of a group
54    */
55   SequenceGroup hiddenSequences;
56
57   /**
58    * Creates a new Sequence object.
59    *
60    * @param name DOCUMENT ME!
61    * @param sequence DOCUMENT ME!
62    * @param start DOCUMENT ME!
63    * @param end DOCUMENT ME!
64    */
65   public Sequence(String name, String sequence, int start, int end)
66   {
67     this.name = name;
68     this.sequence = sequence;
69     this.start = start;
70     this.end = end;
71
72     parseId();
73
74     checkValidRange();
75   }
76
77   com.stevesoft.pat.Regex limitrx = new com.stevesoft.pat.Regex(
78       "[/][0-9]{1,}[-][0-9]{1,}$");
79   com.stevesoft.pat.Regex endrx = new com.stevesoft.pat.Regex(
80       "[0-9]{1,}$");
81
82   void parseId()
83   {
84     // Does sequence have the /start-end signiature?
85     if (limitrx.search(name))
86     {
87       name = limitrx.left();
88       endrx.search(limitrx.stringMatched());
89       setStart(Integer.parseInt(limitrx.stringMatched().substring(1,
90           endrx.matchedFrom() - 1)));
91       setEnd(Integer.parseInt(endrx.stringMatched()));
92     }
93   }
94
95   void checkValidRange()
96   {
97     if (end < 1)
98     {
99       int endRes = 0;
100       char ch;
101       for (int j = 0; j < sequence.length(); j++)
102       {
103         ch = sequence.charAt(j);
104         if (!jalview.util.Comparison.isGap( (ch)))
105         {
106           endRes++;
107         }
108       }
109       if (endRes > 0)
110       {
111         endRes += start - 1;
112       }
113
114       this.end = endRes;
115     }
116
117   }
118
119   /**
120    * Creates a new Sequence object.
121    *
122    * @param name DOCUMENT ME!
123    * @param sequence DOCUMENT ME!
124    */
125   public Sequence(String name, String sequence)
126   {
127     this(name, sequence, 1, -1);
128   }
129
130   /**
131    * Creates a new Sequence object.
132    *
133    * @param seq DOCUMENT ME!
134    */
135   public Sequence(SequenceI seq)
136   {
137     this(seq.getName(), seq.getSequence(), seq.getStart(), seq.getEnd());
138   }
139
140   /**
141    * DOCUMENT ME!
142    *
143    * @param v DOCUMENT ME!
144    */
145   public void setSequenceFeatures(SequenceFeature[] features)
146   {
147     sequenceFeatures = features;
148   }
149
150   public synchronized void addSequenceFeature(SequenceFeature sf)
151   {
152     if (sequenceFeatures == null)
153     {
154       sequenceFeatures = new SequenceFeature[0];
155     }
156
157     for (int i = 0; i < sequenceFeatures.length; i++)
158     {
159       if (sequenceFeatures[i].equals(sf))
160       {
161         return;
162       }
163     }
164
165     SequenceFeature[] temp = new SequenceFeature[sequenceFeatures.length + 1];
166     System.arraycopy(sequenceFeatures, 0, temp, 0, sequenceFeatures.length);
167     temp[sequenceFeatures.length] = sf;
168
169     sequenceFeatures = temp;
170   }
171
172   public void deleteFeature(SequenceFeature sf)
173   {
174     if(sequenceFeatures==null)
175       return;
176
177     int index=0;
178     for (index = 0; index < sequenceFeatures.length; index++)
179     {
180       if (sequenceFeatures[index].equals(sf))
181       {
182         break;
183       }
184     }
185
186
187     if(index==sequenceFeatures.length)
188       return;
189
190     int sfLength = sequenceFeatures.length;
191     if(sfLength<2)
192     {
193       sequenceFeatures = null;
194     }
195     else
196     {
197       SequenceFeature[] temp = new SequenceFeature[sfLength-1];
198       System.arraycopy(sequenceFeatures, 0, temp, 0, index);
199
200       if(index<sfLength)
201         System.arraycopy(sequenceFeatures,
202                          index + 1,
203                          temp,
204                          index, sequenceFeatures.length - index -1);
205
206       sequenceFeatures = temp;
207     }
208   }
209
210   /**
211    * DOCUMENT ME!
212    *
213    * @return DOCUMENT ME!
214    */
215   public SequenceFeature[] getSequenceFeatures()
216   {
217     return sequenceFeatures;
218   }
219
220   public void addPDBId(PDBEntry entry)
221   {
222     if (pdbIds == null)
223       pdbIds = new Vector();
224
225     pdbIds.addElement(entry);
226   }
227
228   /**
229    * DOCUMENT ME!
230    *
231    * @param id DOCUMENT ME!
232    */
233   public void setPDBId(Vector id)
234   {
235     pdbIds = id;
236   }
237
238   /**
239    * DOCUMENT ME!
240    *
241    * @return DOCUMENT ME!
242    */
243   public Vector getPDBId()
244   {
245     return pdbIds;
246   }
247
248   /**
249    * DOCUMENT ME!
250    *
251    * @return DOCUMENT ME!
252    */
253   public String getDisplayId(boolean jvsuffix)
254   {
255     StringBuffer result = new StringBuffer(name);
256     if (jvsuffix)
257     {
258       result.append("/" + start + "-" + end);
259     }
260
261     return result.toString();
262   }
263
264   /**
265    * DOCUMENT ME!
266    *
267    * @param name DOCUMENT ME!
268    */
269   public void setName(String name)
270   {
271     this.name = name;
272     this.parseId();
273   }
274
275   /**
276    * DOCUMENT ME!
277    *
278    * @return DOCUMENT ME!
279    */
280   public String getName()
281   {
282     return this.name;
283   }
284
285   /**
286    * DOCUMENT ME!
287    *
288    * @param start DOCUMENT ME!
289    */
290   public void setStart(int start)
291   {
292     this.start = start;
293   }
294
295   /**
296    * DOCUMENT ME!
297    *
298    * @return DOCUMENT ME!
299    */
300   public int getStart()
301   {
302     return this.start;
303   }
304
305   /**
306    * DOCUMENT ME!
307    *
308    * @param end DOCUMENT ME!
309    */
310   public void setEnd(int end)
311   {
312     this.end = end;
313   }
314
315   /**
316    * DOCUMENT ME!
317    *
318    * @return DOCUMENT ME!
319    */
320   public int getEnd()
321   {
322     return this.end;
323   }
324
325   /**
326    * DOCUMENT ME!
327    *
328    * @return DOCUMENT ME!
329    */
330   public int getLength()
331   {
332     return this.sequence.length();
333   }
334
335   /**
336    * DOCUMENT ME!
337    *
338    * @param seq DOCUMENT ME!
339    */
340   public void setSequence(String seq)
341   {
342     this.sequence = seq;
343     checkValidRange();
344   }
345
346   /**
347    * DOCUMENT ME!
348    *
349    * @return DOCUMENT ME!
350    */
351   public String getSequence()
352   {
353     return this.sequence;
354   }
355
356   /**
357    * DOCUMENT ME!
358    *
359    * @param start DOCUMENT ME!
360    * @param end DOCUMENT ME!
361    *
362    * @return DOCUMENT ME!
363    */
364   public String getSequence(int start, int end)
365   {
366     // JBPNote - left to user to pad the result here (TODO:Decide on this policy)
367     if (start >= sequence.length())
368     {
369       return "";
370     }
371
372     if (end >= sequence.length())
373     {
374       end = sequence.length();
375     }
376
377     return this.sequence.substring(start, end);
378   }
379
380   /**
381    * make a new Sequence object from start to end (including gaps) over this seqeunce
382    * @param start int
383    * @param end int
384    * @return SequenceI
385    */
386   public SequenceI getSubSequence(int start, int end)
387   {
388     if (start < 0)
389       start = 0;
390     String seq = getSequence(start, end);
391     if (seq == "")
392       return null;
393     int nstart = findPosition(start);
394     int nend = findPosition(end) - 1;
395     // JBPNote - this is an incomplete copy.
396     SequenceI nseq = new Sequence(this.getName(), seq, nstart, nend);
397     nseq.setDatasetSequence(getDatasetSequence());
398     return nseq;
399   }
400
401   /**
402    * DOCUMENT ME!
403    *
404    * @param i DOCUMENT ME!
405    *
406    * @return DOCUMENT ME!
407    */
408   public char getCharAt(int i)
409   {
410     if (i < sequence.length())
411     {
412       return sequence.charAt(i);
413     }
414     else
415     {
416       return ' ';
417     }
418   }
419
420   /**
421    * DOCUMENT ME!
422    *
423    * @param desc DOCUMENT ME!
424    */
425   public void setDescription(String desc)
426   {
427     this.description = desc;
428   }
429
430   /**
431    * DOCUMENT ME!
432    *
433    * @return DOCUMENT ME!
434    */
435   public String getDescription()
436   {
437     return this.description;
438   }
439
440   /**
441    * DOCUMENT ME!
442    *
443    * @param pos DOCUMENT ME!
444    *
445    * @return DOCUMENT ME!
446    */
447   public int findIndex(int pos)
448   {
449     // returns the alignment position for a residue
450     int j = start;
451     int i = 0;
452
453     while ( (i < sequence.length()) && (j <= end) && (j <= pos))
454     {
455       if (!jalview.util.Comparison.isGap(sequence.charAt(i)))
456       {
457         j++;
458       }
459
460       i++;
461     }
462
463     if ( (j == end) && (j < pos))
464     {
465       return end + 1;
466     }
467     else
468     {
469       return i;
470     }
471   }
472
473   /**
474    * Returns the sequence position for an alignment position
475    *
476    * @param i column index in alignment (from 1)
477    *
478    * @return residue number for residue (left of and) nearest ith column
479    */
480   public int findPosition(int i)
481   {
482     int j = 0;
483     int pos = start;
484     int seqlen = sequence.length();
485     while ( (j < i) && (j < seqlen))
486     {
487       if (!jalview.util.Comparison.isGap( (sequence.charAt(j))))
488       {
489         pos++;
490       }
491
492       j++;
493     }
494
495     return pos;
496   }
497
498   /**
499    * Returns an int array where indices correspond to each residue in the sequence and the element value gives its position in the alignment
500    *
501    * @return int[SequenceI.getEnd()-SequenceI.getStart()+1] or null if no residues in SequenceI object
502    */
503   public int[] gapMap()
504   {
505     String seq = jalview.analysis.AlignSeq.extractGaps(jalview.util.Comparison.
506         GapChars, sequence);
507     int[] map = new int[seq.length()];
508     int j = 0;
509     int p = 0;
510
511     while (j < sequence.length())
512     {
513       if (!jalview.util.Comparison.isGap(sequence.charAt(j)))
514       {
515         map[p++] = j;
516       }
517
518       j++;
519     }
520
521     return map;
522   }
523
524   /**
525    * DOCUMENT ME!
526    *
527    * @param i DOCUMENT ME!
528    */
529   public void deleteCharAt(int i)
530   {
531     if (i >= sequence.length())
532     {
533       return;
534     }
535
536     sequence = sequence.substring(0, i) + sequence.substring(i + 1);
537   }
538
539   /**
540    * DOCUMENT ME!
541    *
542    * @param i DOCUMENT ME!
543    * @param j DOCUMENT ME!
544    */
545   public void deleteChars(int i, int j)
546   {
547     if (i >= sequence.length())
548     {
549       return;
550     }
551
552     if (j >= sequence.length())
553     {
554       sequence = sequence.substring(0, i);
555     }
556     else
557     {
558       sequence = sequence.substring(0, i) + sequence.substring(j);
559     }
560   }
561
562   /**
563    * DOCUMENT ME!
564    *
565    * @param i DOCUMENT ME!
566    * @param c DOCUMENT ME!
567    * @param chop DOCUMENT ME!
568    */
569   public void insertCharAt(int i, int length, char c)
570   {
571     StringBuffer tmp;
572
573     if (i >= sequence.length())
574     {
575       tmp = new StringBuffer(sequence);
576     }
577     else
578       tmp = new StringBuffer(sequence.substring(0, i));
579
580     while (length > 0)
581     {
582       tmp.append(c);
583       length--;
584     }
585
586     if (i < sequence.length())
587     {
588       tmp.append(sequence.substring(i));
589     }
590
591     sequence = tmp.toString();
592   }
593
594   public void insertCharAt(int i, char c)
595   {
596     insertCharAt(i, 1, c);
597   }
598
599   /**
600    * DOCUMENT ME!
601    *
602    * @param c DOCUMENT ME!
603    */
604   public void setColor(Color c)
605   {
606     this.color = c;
607   }
608
609   /**
610    * DOCUMENT ME!
611    *
612    * @return DOCUMENT ME!
613    */
614   public Color getColor()
615   {
616     return color;
617   }
618
619   public String getVamsasId()
620   {
621     return vamsasId;
622   }
623
624   public void setVamsasId(String id)
625   {
626     vamsasId = id;
627   }
628
629   public void setDBRef(DBRefEntry[] dbref)
630   {
631     dbrefs = dbref;
632   }
633
634   public DBRefEntry[] getDBRef()
635   {
636     return dbrefs;
637   }
638
639   public void addDBRef(DBRefEntry entry)
640   {
641     if (dbrefs == null)
642       dbrefs = new DBRefEntry[0];
643
644     DBRefEntry[] temp = new DBRefEntry[dbrefs.length + 1];
645     System.arraycopy(dbrefs, 0, temp, 0, dbrefs.length);
646
647     temp[temp.length - 1] = entry;
648
649     dbrefs = temp;
650   }
651
652   public void setDatasetSequence(SequenceI seq)
653   {
654     datasetSequence = seq;
655   }
656
657   public SequenceI getDatasetSequence()
658   {
659     return datasetSequence;
660   }
661
662   public AlignmentAnnotation[] getAnnotation()
663   {
664     if (annotation == null)
665       return null;
666
667     AlignmentAnnotation[] ret = new AlignmentAnnotation[annotation.size()];
668     for (int r = 0; r < ret.length; r++)
669       ret[r] = (AlignmentAnnotation) annotation.elementAt(r);
670
671     return ret;
672   }
673
674   public void addAlignmentAnnotation(AlignmentAnnotation annotation)
675   {
676     if (this.annotation == null)
677       this.annotation = new Vector();
678
679     this.annotation.addElement(annotation);
680   }
681
682   public SequenceGroup getHiddenSequences()
683   {
684     return hiddenSequences;
685   }
686
687   public void addHiddenSequence(SequenceI seq)
688   {
689     if (hiddenSequences == null)
690     {
691       hiddenSequences = new SequenceGroup();
692     }
693     hiddenSequences.addSequence(seq, false);
694   }
695
696   public void showHiddenSequence(SequenceI seq)
697   {
698     hiddenSequences.deleteSequence(seq, false);
699     if (hiddenSequences.getSize(false) < 1)
700     {
701       hiddenSequences = null;
702     }
703   }
704 }
705
706