2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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.AlignedCodonFrame;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.SequenceI;
27 import jalview.schemes.ResidueProperties;
28 import jalview.util.MapList;
30 import java.util.ArrayList;
31 import java.util.LinkedHashMap;
32 import java.util.List;
36 * grab bag of useful alignment manipulation operations Expect these to be
37 * refactored elsewhere at some point.
42 public class AlignmentUtils
46 * given an existing alignment, create a new alignment including all, or up to
47 * flankSize additional symbols from each sequence's dataset sequence
53 public static AlignmentI expandContext(AlignmentI core, int flankSize)
55 List<SequenceI> sq = new ArrayList<SequenceI>();
57 for (SequenceI s : core.getSequences())
59 SequenceI newSeq = s.deriveSequence();
60 if (newSeq.getStart() > maxoffset
61 && newSeq.getDatasetSequence().getStart() < s.getStart())
63 maxoffset = newSeq.getStart();
69 maxoffset = flankSize;
71 // now add offset to create a new expanded alignment
72 for (SequenceI s : sq)
75 while (ds.getDatasetSequence() != null)
77 ds = ds.getDatasetSequence();
79 int s_end = s.findPosition(s.getStart() + s.getLength());
80 // find available flanking residues for sequence
81 int ustream_ds = s.getStart() - ds.getStart(), dstream_ds = ds
84 // build new flanked sequence
86 // compute gap padding to start of flanking sequence
87 int offset = maxoffset - ustream_ds;
89 // padding is gapChar x ( maxoffset - min(ustream_ds, flank)
92 if (flankSize < ustream_ds)
94 // take up to flankSize residues
95 offset = maxoffset - flankSize;
96 ustream_ds = flankSize;
98 if (flankSize < dstream_ds)
100 dstream_ds = flankSize;
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 + 1
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();
111 // TODO could lowercase the flanking regions
113 for (; p < offset; p++)
117 // s.setSequence(new String(upstream).toLowerCase()+new String(coreseq) +
118 // new String(downstream).toLowerCase());
119 System.arraycopy(upstream, 0, nseq, p, upstream.length);
120 System.arraycopy(coreseq, 0, nseq, p + upstream.length,
122 System.arraycopy(downstream, 0, nseq, p + coreseq.length
123 + upstream.length, downstream.length);
124 s.setSequence(new String(nseq));
125 s.setStart(s.getStart() - ustream_ds);
126 s.setEnd(s_end + downstream.length);
128 AlignmentI newAl = new jalview.datamodel.Alignment(
129 sq.toArray(new SequenceI[0]));
130 for (SequenceI s : sq)
132 if (s.getAnnotation() != null)
134 for (AlignmentAnnotation aa : s.getAnnotation())
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())
169 * Returns a map of lists of sequences in the alignment, keyed by sequence
170 * name. For use in mapping between different alignment views of the same
173 * @see jalview.datamodel.AlignmentI#getSequencesByName()
175 public static Map<String, List<SequenceI>> getSequencesByName(
178 Map<String, List<SequenceI>> theMap = new LinkedHashMap<String, List<SequenceI>>();
179 for (SequenceI seq : al.getSequences())
181 String name = seq.getName();
184 List<SequenceI> seqs = theMap.get(name);
187 seqs = new ArrayList<SequenceI>();
188 theMap.put(name, seqs);
197 * Build mapping of protein to cDNA alignment. Mappings are made between
198 * sequences which have the same name and compatible lengths. Returns true if
199 * at least one sequence mapping was made, else false.
201 * @param proteinAlignment
202 * @param cdnaAlignment
205 public static boolean mapProteinToCdna(final AlignmentI proteinAlignment,
206 final AlignmentI cdnaAlignment)
208 boolean mapped = false;
209 List<SequenceI> thisSeqs = proteinAlignment.getSequences();
212 * Build a look-up of cDNA sequences by name, for matching purposes.
214 Map<String, List<SequenceI>> cdnaSeqs = cdnaAlignment
215 .getSequencesByName();
217 for (SequenceI aaSeq : thisSeqs)
219 AlignedCodonFrame acf = new AlignedCodonFrame(
220 proteinAlignment.getWidth());
221 List<SequenceI> candidates = cdnaSeqs.get(aaSeq.getName());
222 if (candidates == null)
225 * No cDNA sequence with matching name, so no mapping for this protein
230 for (SequenceI cdnaSeq : candidates)
232 MapList map = mapProteinToCdna(aaSeq, cdnaSeq);
235 acf.addMap(cdnaSeq, aaSeq, map);
239 proteinAlignment.addCodonFrame(acf);
245 * Build a mapping (if possible) of a protein to a cDNA sequence. The cDNA
246 * must be three times the length of the protein, possibly after ignoring
247 * start and/or stop codons. Returns null if no mapping is determined.
253 public static MapList mapProteinToCdna(SequenceI proteinSeq,
256 String aaSeqString = proteinSeq.getDatasetSequence()
257 .getSequenceAsString();
258 String cdnaSeqString = cdnaSeq.getDatasetSequence()
259 .getSequenceAsString();
260 if (aaSeqString == null || cdnaSeqString == null)
265 final int mappedLength = 3 * aaSeqString.length();
266 int cdnaLength = cdnaSeqString.length();
268 int cdnaEnd = cdnaLength;
269 final int proteinStart = 1;
270 final int proteinEnd = aaSeqString.length();
273 * If lengths don't match, try ignoring stop codon.
275 if (cdnaLength != mappedLength)
277 for (Object stop : ResidueProperties.STOP)
279 if (cdnaSeqString.toUpperCase().endsWith((String) stop))
289 * If lengths still don't match, try ignoring start codon.
291 if (cdnaLength != mappedLength
292 && cdnaSeqString.toUpperCase().startsWith(
293 ResidueProperties.START))
299 if (cdnaLength == mappedLength)
301 MapList map = new MapList(new int[]
302 { cdnaStart, cdnaEnd }, new int[]
303 { proteinStart, proteinEnd }, 3, 1);