JAL-1712 fixes/tests for Castor binding and 'show flanking regions'
[jalview.git] / src / jalview / analysis / AlignmentUtils.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3  * Copyright (C) 2014 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 jalview.analysis;
22
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.SequenceI;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 /**
31  * grab bag of useful alignment manipulation operations Expect these to be
32  * refactored elsewhere at some point.
33  * 
34  * @author jimp
35  * 
36  */
37 public class AlignmentUtils
38 {
39
40   /**
41    * given an existing alignment, create a new alignment including all, or up to
42    * flankSize additional symbols from each sequence's dataset sequence
43    * 
44    * @param core
45    * @param flankSize
46    * @return AlignmentI
47    */
48   public static AlignmentI expandContext(AlignmentI core, int flankSize)
49   {
50     List<SequenceI> sq = new ArrayList<SequenceI>();
51     int maxoffset = 0;
52     for (SequenceI s : core.getSequences())
53     {
54       SequenceI newSeq = s.deriveSequence();
55       final int newSeqStart = newSeq.getStart() - 1;
56       if (newSeqStart > maxoffset
57               && newSeq.getDatasetSequence().getStart() < s.getStart())
58       {
59         maxoffset = newSeqStart;
60       }
61       sq.add(newSeq);
62     }
63     if (flankSize > -1)
64     {
65       maxoffset = Math.min(maxoffset, flankSize);
66     }
67
68     /*
69      * now add offset left and right to create an expanded alignment
70      */
71     for (SequenceI s : sq)
72     {
73       SequenceI ds = s;
74       while (ds.getDatasetSequence() != null)
75       {
76         ds = ds.getDatasetSequence();
77       }
78       int s_end = s.findPosition(s.getStart() + s.getLength());
79       // find available flanking residues for sequence
80       int ustream_ds = s.getStart() - ds.getStart();
81       int dstream_ds = ds.getEnd() - s_end;
82
83       // build new flanked sequence
84
85       // compute gap padding to start of flanking sequence
86       int offset = maxoffset - ustream_ds;
87
88       // padding is gapChar x ( maxoffset - min(ustream_ds, flank)
89       if (flankSize >= 0)
90       {
91         if (flankSize < ustream_ds)
92         {
93           // take up to flankSize residues
94           offset = maxoffset - flankSize;
95           ustream_ds = flankSize;
96         }
97         if (flankSize <= dstream_ds)
98         {
99           dstream_ds = flankSize - 1;
100         }
101       }
102       // TODO use Character.toLowerCase to avoid creating String objects?
103       char[] upstream = new String(ds.getSequence(s.getStart() - 1
104               - ustream_ds, s.getStart() - 1)).toLowerCase().toCharArray();
105       char[] downstream = new String(ds.getSequence(s_end - 1, s_end
106               + dstream_ds)).toLowerCase().toCharArray();
107       char[] coreseq = s.getSequence();
108       char[] nseq = new char[offset + upstream.length + downstream.length
109               + coreseq.length];
110       char c = core.getGapCharacter();
111
112       int p = 0;
113       for (; p < offset; p++)
114       {
115         nseq[p] = c;
116       }
117
118       System.arraycopy(upstream, 0, nseq, p, upstream.length);
119       System.arraycopy(coreseq, 0, nseq, p + upstream.length,
120               coreseq.length);
121       System.arraycopy(downstream, 0, nseq, p + coreseq.length
122               + upstream.length, downstream.length);
123       s.setSequence(new String(nseq));
124       s.setStart(s.getStart() - ustream_ds);
125       s.setEnd(s_end + downstream.length);
126     }
127     AlignmentI newAl = new jalview.datamodel.Alignment(
128             sq.toArray(new SequenceI[0]));
129     for (SequenceI s : sq)
130     {
131       if (s.getAnnotation() != null)
132       {
133         for (AlignmentAnnotation aa : s.getAnnotation())
134         {
135           aa.adjustForAlignment(); // JAL-1712 fix
136           newAl.addAnnotation(aa);
137         }
138       }
139     }
140     newAl.setDataset(core.getDataset());
141     return newAl;
142   }
143
144   /**
145    * Returns the index (zero-based position) of a sequence in an alignment, or
146    * -1 if not found.
147    * 
148    * @param al
149    * @param seq
150    * @return
151    */
152   public static int getSequenceIndex(AlignmentI al, SequenceI seq)
153   {
154     int result = -1;
155     int pos = 0;
156     for (SequenceI alSeq : al.getSequences())
157     {
158       if (alSeq == seq)
159       {
160         result = pos;
161         break;
162       }
163       pos++;
164     }
165     return result;
166   }
167 }