SequenceI prot = mapping.findAlignedSequence(dnaSeq, protein);
if (prot != null)
{
- Mapping seqMap = mapping.getMappingForSequence(dnaSeq, false);
+ Mapping seqMap = mapping.getMappingForSequence(dnaSeq);
addCodonPositions(dnaSeq, prot, protein.getGapCharacter(), seqMap,
alignedCodons);
unmappedProtein.remove(prot);
/*
* add a mapping from CDS to the (unchanged) mapped to range
*/
- List<int[]> cdsRange = Collections.singletonList(new int[] { 1,
- cdsSeq.getLength() });
+ List<int[]> cdsRange = Collections
+ .singletonList(new int[]
+ { cdsSeq.getStart(),
+ cdsSeq.getLength() + cdsSeq.getStart() - 1 });
MapList cdsToProteinMap = new MapList(cdsRange,
mapList.getToRanges(), mapList.getFromRatio(),
mapList.getToRatio());
static SequenceI makeCdsSequence(SequenceI seq, Mapping mapping,
AlignmentI dataset)
{
- char[] seqChars = seq.getSequence();
- List<int[]> fromRanges = mapping.getMap().getFromRanges();
- int cdsWidth = MappingUtils.getLength(fromRanges);
- char[] newSeqChars = new char[cdsWidth];
+ /*
+ * construct CDS sequence name as "CDS|" with 'from id' held in the mapping
+ * if set (e.g. EMBL protein_id), else sequence name appended
+ */
+ String mapFromId = mapping.getMappedFromId();
+ final String seqId = "CDS|"
+ + (mapFromId != null ? mapFromId : seq.getName());
- int newPos = 0;
- for (int[] range : fromRanges)
+ SequenceI newSeq = null;
+
+ final MapList maplist = mapping.getMap();
+ if (maplist.isContiguous() && maplist.isFromForwardStrand())
{
- if (range[0] <= range[1])
- {
- // forward strand mapping - just copy the range
- int length = range[1] - range[0] + 1;
- System.arraycopy(seqChars, range[0] - 1, newSeqChars, newPos,
- length);
- newPos += length;
- }
- else
+ /*
+ * just a subsequence, keep same dataset sequence
+ */
+ int start = maplist.getFromLowest();
+ int end = maplist.getFromHighest();
+ newSeq = seq.getSubSequence(start - 1, end);
+ newSeq.setName(seqId);
+ }
+ else
+ {
+ /*
+ * construct by splicing mapped from ranges
+ */
+ char[] seqChars = seq.getSequence();
+ List<int[]> fromRanges = maplist.getFromRanges();
+ int cdsWidth = MappingUtils.getLength(fromRanges);
+ char[] newSeqChars = new char[cdsWidth];
+
+ int newPos = 0;
+ for (int[] range : fromRanges)
{
- // reverse strand mapping - copy and complement one by one
- for (int i = range[0]; i >= range[1]; i--)
+ if (range[0] <= range[1])
+ {
+ // forward strand mapping - just copy the range
+ int length = range[1] - range[0] + 1;
+ System.arraycopy(seqChars, range[0] - 1, newSeqChars, newPos,
+ length);
+ newPos += length;
+ }
+ else
{
- newSeqChars[newPos++] = Dna.getComplement(seqChars[i - 1]);
+ // reverse strand mapping - copy and complement one by one
+ for (int i = range[0]; i >= range[1]; i--)
+ {
+ newSeqChars[newPos++] = Dna.getComplement(seqChars[i - 1]);
+ }
}
}
+
+ newSeq = new Sequence(seqId, newSeqChars, 1, newPos);
}
- /*
- * assign 'from id' held in the mapping if set (e.g. EMBL protein_id),
- * else generate a sequence name
- */
- String mapFromId = mapping.getMappedFromId();
- String seqId = "CDS|" + (mapFromId != null ? mapFromId : seq.getName());
- SequenceI newSeq = new Sequence(seqId, newSeqChars, 1, newPos);
if (dataset != null)
{
SequenceI[] matches = dataset.findSequenceMatch(newSeq.getName());
{
copyTo = copyTo.getDatasetSequence();
}
+ if (fromSeq == copyTo || fromSeq.getDatasetSequence() == copyTo)
+ {
+ return 0; // shared dataset sequence
+ }
/*
* get features, optionally restricted by an ontology term
*/
public static int alignAs(AlignmentI unaligned, AlignmentI aligned)
{
- /*
- * easy case - aligning a copy of aligned sequences
- */
- if (alignAsSameSequences(unaligned, aligned))
- {
- return unaligned.getHeight();
- }
-
- /*
- * fancy case - aligning via mappings between sequences
- */
List<SequenceI> unmapped = new ArrayList<>();
Map<Integer, Map<SequenceI, Character>> columnMap = buildMappedColumnsMap(
unaligned, aligned, unmapped);
* true; else returns false
*
* @param unaligned
- * - sequences to be aligned based on aligned
+ * - sequences to be aligned based on aligned
* @param aligned
- * - 'guide' alignment containing sequences derived from same dataset
- * as unaligned
+ * - 'guide' alignment containing sequences derived from same
+ * dataset as unaligned
* @return
+ * @deprecated probably obsolete and incomplete
*/
+ @Deprecated
static boolean alignAsSameSequences(AlignmentI unaligned,
AlignmentI aligned)
{
}
/*
- * first pass - check whether all sequences to be aligned share a dataset
- * sequence with an aligned sequence
+ * first pass - check whether all sequences to be aligned share a
+ * dataset sequence with an aligned sequence; also note the leftmost
+ * ungapped column from which to copy
*/
+ int leftmost = Integer.MAX_VALUE;
for (SequenceI seq : unaligned.getSequences())
{
- if (!alignedDatasets.containsKey(seq.getDatasetSequence()))
+ final SequenceI ds = seq.getDatasetSequence();
+ if (!alignedDatasets.containsKey(ds))
{
return false;
}
+ SequenceI alignedSeq = alignedDatasets.get(ds)
+ .get(0);
+ int startCol = alignedSeq.findIndex(seq.getStart()); // 1..
+ leftmost = Math.min(leftmost, startCol);
}
/*
* heuristic rule: pair off sequences in order for the case where
* more than one shares the same dataset sequence
*/
+ final char gapCharacter = aligned.getGapCharacter();
for (SequenceI seq : unaligned.getSequences())
{
List<SequenceI> alignedSequences = alignedDatasets
.get(seq.getDatasetSequence());
- // TODO: getSequenceAsString() will be deprecated in the future
- // TODO: need to leave to SequenceI implementor to update gaps
- seq.setSequence(alignedSequences.get(0).getSequenceAsString());
+ SequenceI alignedSeq = alignedSequences.get(0);
+
+ /*
+ * gap fill for leading (5') UTR if any
+ */
+ // TODO this copies intron columns - wrong!
+ int startCol = alignedSeq.findIndex(seq.getStart()); // 1..
+ int endCol = alignedSeq.findIndex(seq.getEnd());
+ char[] seqchars = new char[endCol - leftmost + 1];
+ Arrays.fill(seqchars, gapCharacter);
+ char[] toCopy = alignedSeq.getSequence(startCol - 1, endCol);
+ System.arraycopy(toCopy, 0, seqchars, startCol - leftmost,
+ toCopy.length);
+ seq.setSequence(String.valueOf(seqchars));
if (alignedSequences.size() > 0)
{
// pop off aligned sequences (except the last one)