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