2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.analysis;
23 import jalview.datamodel.AlignmentAnnotation;
24 import jalview.datamodel.AlignmentI;
25 import jalview.datamodel.SequenceI;
27 import java.util.ArrayList;
28 import java.util.List;
31 * grab bag of useful alignment manipulation operations Expect these to be
32 * refactored elsewhere at some point.
37 public class AlignmentUtils
41 * given an existing alignment, create a new alignment including all, or up to
42 * flankSize additional symbols from each sequence's dataset sequence
48 public static AlignmentI expandContext(AlignmentI core, int flankSize)
50 List<SequenceI> sq = new ArrayList<SequenceI>();
52 for (SequenceI s : core.getSequences())
54 SequenceI newSeq = s.deriveSequence();
55 final int newSeqStart = newSeq.getStart() - 1;
56 if (newSeqStart > maxoffset
57 && newSeq.getDatasetSequence().getStart() < s.getStart())
59 maxoffset = newSeqStart;
65 maxoffset = Math.min(maxoffset, flankSize);
69 * now add offset left and right to create an expanded alignment
71 for (SequenceI s : sq)
74 while (ds.getDatasetSequence() != null)
76 ds = ds.getDatasetSequence();
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;
83 // build new flanked sequence
85 // compute gap padding to start of flanking sequence
86 int offset = maxoffset - ustream_ds;
88 // padding is gapChar x ( maxoffset - min(ustream_ds, flank)
91 if (flankSize < ustream_ds)
93 // take up to flankSize residues
94 offset = maxoffset - flankSize;
95 ustream_ds = flankSize;
97 if (flankSize <= dstream_ds)
99 dstream_ds = flankSize - 1;
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
110 char c = core.getGapCharacter();
113 for (; p < offset; p++)
118 System.arraycopy(upstream, 0, nseq, p, upstream.length);
119 System.arraycopy(coreseq, 0, nseq, p + upstream.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);
127 AlignmentI newAl = new jalview.datamodel.Alignment(
128 sq.toArray(new SequenceI[0]));
129 for (SequenceI s : sq)
131 if (s.getAnnotation() != null)
133 for (AlignmentAnnotation aa : s.getAnnotation())
135 aa.adjustForAlignment(); // JAL-1712 fix
136 newAl.addAnnotation(aa);
140 newAl.setDataset(core.getDataset());
145 * Returns the index (zero-based position) of a sequence in an alignment, or
152 public static int getSequenceIndex(AlignmentI al, SequenceI seq)
156 for (SequenceI alSeq : al.getSequences())