JAL-1517 fix copyright for 2.8.2
[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 java.util.ArrayList;
24 import java.util.List;
25
26 import jalview.datamodel.SequenceI;
27 import jalview.datamodel.AlignmentI;
28
29 /**
30  * grab bag of useful alignment manipulation operations
31  * Expect these to be refactored elsewhere at some point.
32  * @author jimp
33  *
34  */
35 public class AlignmentUtils
36 {
37
38   /**
39    * given an existing alignment, create a new alignment including all, or up to flankSize additional symbols from each sequence's dataset sequence
40    * @param core
41    * @param flankSize
42    * @return AlignmentI
43    */
44   public static AlignmentI expandContext(AlignmentI core, int flankSize)
45   {
46     List<SequenceI> sq = new ArrayList<SequenceI>();
47     int maxoffset = 0;
48     for (SequenceI s:core.getSequences())
49     {
50       SequenceI newSeq = s.deriveSequence();
51       if (newSeq.getStart()>maxoffset && newSeq.getDatasetSequence().getStart()<s.getStart())
52       {
53         maxoffset = newSeq.getStart();
54       }
55       sq.add(newSeq);
56     }
57     if (flankSize>-1) {
58       maxoffset = flankSize;
59     }
60     // now add offset to create a new expanded alignment
61     for (SequenceI s:sq)
62     {
63       SequenceI ds = s;
64       while (ds.getDatasetSequence()!=null) {
65         ds=ds.getDatasetSequence();
66       }
67       int s_end = s.findPosition(s.getStart()+s.getLength());
68       // find available flanking residues for sequence
69       int ustream_ds=s.getStart()-ds.getStart(),dstream_ds=ds.getEnd()-s_end;
70       
71       // build new flanked sequence
72       
73       // compute gap padding to start of flanking sequence
74       int offset=maxoffset - ustream_ds;
75       
76       // padding is gapChar x ( maxoffset - min(ustream_ds, flank)
77       if (flankSize>=0) {
78         if (flankSize<ustream_ds)
79         {
80         // take up to flankSize residues
81         offset = maxoffset-flankSize;
82         ustream_ds = flankSize;
83       }
84         if (flankSize<dstream_ds)
85         {
86           dstream_ds=flankSize;
87         }
88       }
89       char[] upstream = new String(ds.getSequence(s.getStart()-1-ustream_ds, s.getStart()-1)).toLowerCase().toCharArray();
90       char[] downstream = new String(ds.getSequence(s_end-1,s_end+1+dstream_ds)).toLowerCase().toCharArray();
91       char[] coreseq=s.getSequence();
92       char[] nseq = new char[offset+upstream.length+downstream.length+coreseq.length]; 
93       char c = core.getGapCharacter();
94       // TODO could lowercase the flanking regions
95       int p=0;
96       for (; p<offset;p++)
97       {
98         nseq[p] = c;
99       }
100 //      s.setSequence(new String(upstream).toLowerCase()+new String(coreseq) + new String(downstream).toLowerCase());
101       System.arraycopy(upstream, 0, nseq, p, upstream.length);
102       System.arraycopy(coreseq, 0, nseq, p+upstream.length, coreseq.length);
103       System.arraycopy(downstream, 0, nseq, p+coreseq.length+upstream.length, downstream.length);
104       s.setSequence(new String(nseq));
105       s.setStart(s.getStart()-ustream_ds);
106       s.setEnd(s_end+downstream.length);
107     }
108     AlignmentI newAl = new jalview.datamodel.Alignment(sq.toArray(new SequenceI[0]));
109     newAl.setDataset(core.getDataset());
110     return newAl;
111   }
112 }