6385fa75076c0ef2d32d715ae67781bcb8126f39
[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.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       if (newSeq.getStart() > maxoffset
56               && newSeq.getDatasetSequence().getStart() < s.getStart())
57       {
58         maxoffset = newSeq.getStart();
59       }
60       sq.add(newSeq);
61     }
62     if (flankSize > -1)
63     {
64       maxoffset = flankSize;
65     }
66     // now add offset to create a new expanded alignment
67     for (SequenceI s : sq)
68     {
69       SequenceI ds = s;
70       while (ds.getDatasetSequence() != null)
71       {
72         ds = ds.getDatasetSequence();
73       }
74       int s_end = s.findPosition(s.getStart() + s.getLength());
75       // find available flanking residues for sequence
76       int ustream_ds = s.getStart() - ds.getStart(), dstream_ds = ds
77               .getEnd() - s_end;
78
79       // build new flanked sequence
80
81       // compute gap padding to start of flanking sequence
82       int offset = maxoffset - ustream_ds;
83
84       // padding is gapChar x ( maxoffset - min(ustream_ds, flank)
85       if (flankSize >= 0)
86       {
87         if (flankSize < ustream_ds)
88         {
89           // take up to flankSize residues
90           offset = maxoffset - flankSize;
91           ustream_ds = flankSize;
92         }
93         if (flankSize < dstream_ds)
94         {
95           dstream_ds = flankSize;
96         }
97       }
98       char[] upstream = new String(ds.getSequence(s.getStart() - 1
99               - ustream_ds, s.getStart() - 1)).toLowerCase().toCharArray();
100       char[] downstream = new String(ds.getSequence(s_end - 1, s_end + 1
101               + dstream_ds)).toLowerCase().toCharArray();
102       char[] coreseq = s.getSequence();
103       char[] nseq = new char[offset + upstream.length + downstream.length
104               + coreseq.length];
105       char c = core.getGapCharacter();
106       // TODO could lowercase the flanking regions
107       int p = 0;
108       for (; p < offset; p++)
109       {
110         nseq[p] = c;
111       }
112       // s.setSequence(new String(upstream).toLowerCase()+new String(coreseq) +
113       // new String(downstream).toLowerCase());
114       System.arraycopy(upstream, 0, nseq, p, upstream.length);
115       System.arraycopy(coreseq, 0, nseq, p + upstream.length,
116               coreseq.length);
117       System.arraycopy(downstream, 0, nseq, p + coreseq.length
118               + upstream.length, downstream.length);
119       s.setSequence(new String(nseq));
120       s.setStart(s.getStart() - ustream_ds);
121       s.setEnd(s_end + downstream.length);
122     }
123     AlignmentI newAl = new jalview.datamodel.Alignment(
124             sq.toArray(new SequenceI[0]));
125     for (SequenceI s : sq)
126     {
127       if (s.getAnnotation() != null)
128       {
129         for (AlignmentAnnotation aa : s.getAnnotation())
130         {
131           newAl.addAnnotation(aa);
132         }
133       }
134     }
135     newAl.setDataset(core.getDataset());
136     return newAl;
137   }
138
139   /**
140    * Returns the index (zero-based position) of a sequence in an alignment, or
141    * -1 if not found.
142    * 
143    * @param al
144    * @param seq
145    * @return
146    */
147   public static int getSequenceIndex(AlignmentI al, SequenceI seq)
148   {
149     int result = -1;
150     int pos = 0;
151     for (SequenceI alSeq : al.getSequences())
152     {
153       if (alSeq == seq)
154       {
155         result = pos;
156         break;
157       }
158       pos++;
159     }
160     return result;
161   }
162 }