*/
public class AlignmentUtils
{
-
private static final int CODON_LENGTH = 3;
private static final String SEQUENCE_VARIANT = "sequence_variant:";
- private static final String ID = "ID";
+ /*
+ * the 'id' attribute is provided for variant features fetched from
+ * Ensembl using its REST service with JSON format
+ */
+ public static final String VARIANT_ID = "id";
/**
* A data model to hold the 'normal' base value at a position, and an optional
*/
public static AlignmentI expandContext(AlignmentI core, int flankSize)
{
- List<SequenceI> sq = new ArrayList<SequenceI>();
+ List<SequenceI> sq = new ArrayList<>();
int maxoffset = 0;
for (SequenceI s : core.getSequences())
{
}
/**
- * Returns the index (zero-based position) of a sequence in an alignment, or
- * -1 if not found.
- *
- * @param al
- * @param seq
- * @return
- */
- public static int getSequenceIndex(AlignmentI al, SequenceI seq)
- {
- int result = -1;
- int pos = 0;
- for (SequenceI alSeq : al.getSequences())
- {
- if (alSeq == seq)
- {
- result = pos;
- break;
- }
- pos++;
- }
- return result;
- }
-
- /**
* Returns a map of lists of sequences in the alignment, keyed by sequence
* name. For use in mapping between different alignment views of the same
* sequences.
public static Map<String, List<SequenceI>> getSequencesByName(
AlignmentI al)
{
- Map<String, List<SequenceI>> theMap = new LinkedHashMap<String, List<SequenceI>>();
+ Map<String, List<SequenceI>> theMap = new LinkedHashMap<>();
for (SequenceI seq : al.getSequences())
{
String name = seq.getName();
List<SequenceI> seqs = theMap.get(name);
if (seqs == null)
{
- seqs = new ArrayList<SequenceI>();
+ seqs = new ArrayList<>();
theMap.put(name, seqs);
}
seqs.add(seq);
return false;
}
- Set<SequenceI> mappedDna = new HashSet<SequenceI>();
- Set<SequenceI> mappedProtein = new HashSet<SequenceI>();
+ Set<SequenceI> mappedDna = new HashSet<>();
+ Set<SequenceI> mappedProtein = new HashSet<>();
/*
* First pass - map sequences where cross-references exist. This include
{
String lastCodon = String.valueOf(cdnaSeqChars,
cdnaLength - CODON_LENGTH, CODON_LENGTH).toUpperCase();
- for (String stop : ResidueProperties.STOP)
+ for (String stop : ResidueProperties.STOP_CODONS)
{
if (lastCodon.equals(stop))
{
* allow * in protein to match untranslatable in dna
*/
final char aaRes = aaSeqChars[aaPos];
- if ((translated == null || "STOP".equals(translated)) && aaRes == '*')
+ if ((translated == null || ResidueProperties.STOP.equals(translated))
+ && aaRes == '*')
{
continue;
}
if (dnaPos == cdnaSeqChars.length - CODON_LENGTH)
{
String codon = String.valueOf(cdnaSeqChars, dnaPos, CODON_LENGTH);
- if ("STOP".equals(ResidueProperties.codonTranslate(codon)))
+ if (ResidueProperties.STOP
+ .equals(ResidueProperties.codonTranslate(codon)))
{
return true;
}
System.err.println("Wrong alignment type in alignProteinAsDna");
return 0;
}
- List<SequenceI> unmappedProtein = new ArrayList<SequenceI>();
+ List<SequenceI> unmappedProtein = new ArrayList<>();
Map<AlignedCodon, Map<SequenceI, AlignedCodon>> alignedCodons = buildCodonColumnsMap(
protein, dna, unmappedProtein);
return alignProteinAs(protein, alignedCodons, unmappedProtein);
* {dnaSequence, {proteinSequence, codonProduct}} at that position. The
* comparator keeps the codon positions ordered.
*/
- Map<AlignedCodon, Map<SequenceI, AlignedCodon>> alignedCodons = new TreeMap<AlignedCodon, Map<SequenceI, AlignedCodon>>(
+ Map<AlignedCodon, Map<SequenceI, AlignedCodon>> alignedCodons = new TreeMap<>(
new CodonComparator());
for (SequenceI dnaSeq : dna.getSequences())
// TODO delete this ugly hack once JAL-2022 is resolved
// i.e. we can model startPhase > 0 (incomplete start codon)
- List<SequenceI> sequencesChecked = new ArrayList<SequenceI>();
+ List<SequenceI> sequencesChecked = new ArrayList<>();
AlignedCodon lastCodon = null;
- Map<SequenceI, AlignedCodon> toAdd = new HashMap<SequenceI, AlignedCodon>();
+ Map<SequenceI, AlignedCodon> toAdd = new HashMap<>();
for (Entry<AlignedCodon, Map<SequenceI, AlignedCodon>> entry : alignedCodons
.entrySet())
Map<SequenceI, AlignedCodon> seqProduct = alignedCodons.get(codon);
if (seqProduct == null)
{
- seqProduct = new HashMap<SequenceI, AlignedCodon>();
+ seqProduct = new HashMap<>();
alignedCodons.put(codon, seqProduct);
}
seqProduct.put(protein, codon);
{
continue;
}
- final List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
+ final List<AlignmentAnnotation> result = new ArrayList<>();
for (AlignmentAnnotation dsann : datasetAnnotations)
{
/*
throw new IllegalArgumentException(
"IMPLEMENTATION ERROR: dataset.getDataset() must be null!");
}
- List<SequenceI> foundSeqs = new ArrayList<SequenceI>();
- List<SequenceI> cdsSeqs = new ArrayList<SequenceI>();
+ List<SequenceI> foundSeqs = new ArrayList<>();
+ List<SequenceI> cdsSeqs = new ArrayList<>();
List<AlignedCodonFrame> mappings = dataset.getCodonFrames();
HashSet<SequenceI> productSeqs = null;
if (products != null)
{
- productSeqs = new HashSet<SequenceI>();
+ productSeqs = new HashSet<>();
for (SequenceI seq : products)
{
productSeqs.add(seq.getDatasetSequence() == null ? seq : seq
* @param seqMappings
* the set of mappings involving dnaSeq
* @param aMapping
- * an initial candidate from seqMappings
+ * a transcript-to-peptide mapping
* @return
*/
static SequenceI findCdsForProtein(List<AlignedCodonFrame> mappings,
if (mappedFromLength == dnaLength
|| mappedFromLength == dnaLength - CODON_LENGTH)
{
- return seqDss;
+ /*
+ * if sequence has CDS features, this is a transcript with no UTR
+ * - do not take this as the CDS sequence! (JAL-2789)
+ */
+ if (seqDss.getFeatures().getFeaturesByOntology(SequenceOntologyI.CDS)
+ .isEmpty())
+ {
+ return seqDss;
+ }
}
/*
{
/*
* found a 3:1 mapping to the protein product which covers
- * the whole dna sequence i.e. is from CDS; finally check it
- * is from the dna start sequence
+ * the whole dna sequence i.e. is from CDS; finally check the CDS
+ * is mapped from the given dna start sequence
*/
SequenceI cdsSeq = map.getFromSeq();
+ // todo this test is weak if seqMappings contains multiple mappings;
+ // we get away with it if transcript:cds relationship is 1:1
List<AlignedCodonFrame> dnaToCdsMaps = MappingUtils
.findMappingsForSequence(cdsSeq, seqMappings);
if (!dnaToCdsMaps.isEmpty())
protected static List<DBRefEntry> propagateDBRefsToCDS(SequenceI cdsSeq,
SequenceI contig, SequenceI proteinProduct, Mapping mapping)
{
+
// gather direct refs from contig congruent with mapping
- List<DBRefEntry> direct = new ArrayList<DBRefEntry>();
- HashSet<String> directSources = new HashSet<String>();
+ List<DBRefEntry> direct = new ArrayList<>();
+ HashSet<String> directSources = new HashSet<>();
+
if (contig.getDBRefs() != null)
{
for (DBRefEntry dbr : contig.getDBRefs())
DBRefEntry[] onSource = DBRefUtils.selectRefs(
proteinProduct.getDBRefs(),
directSources.toArray(new String[0]));
- List<DBRefEntry> propagated = new ArrayList<DBRefEntry>();
+ List<DBRefEntry> propagated = new ArrayList<>();
// and generate appropriate mappings
for (DBRefEntry cdsref : direct)
int mappedDnaLength = MappingUtils.getLength(ranges);
/*
- * if not a whole number of codons, something is wrong,
- * abort mapping
+ * if not a whole number of codons, truncate mapping
*/
- if (mappedDnaLength % CODON_LENGTH > 0)
+ int codonRemainder = mappedDnaLength % CODON_LENGTH;
+ if (codonRemainder > 0)
{
- return null;
+ mappedDnaLength -= codonRemainder;
+ MappingUtils.removeEndPositions(codonRemainder, ranges);
}
int proteinLength = proteinSeq.getLength();
proteinStart++;
proteinLength--;
}
- List<int[]> proteinRange = new ArrayList<int[]>();
+ List<int[]> proteinRange = new ArrayList<>();
/*
* dna length should map to protein (or protein plus stop codon)
*/
protected static List<int[]> findCdsPositions(SequenceI dnaSeq)
{
- List<int[]> result = new ArrayList<int[]>();
+ List<int[]> result = new ArrayList<>();
List<SequenceFeature> sfs = dnaSeq.getFeatures().getFeaturesByOntology(
SequenceOntologyI.CDS);
{
for (String base : alleles.split(","))
{
- String codon = base + base2 + base3;
- if (addPeptideVariant(peptide, peptidePos, residue, var, codon))
+ if (!base1.equalsIgnoreCase(base))
{
- count++;
+ String codon = base.toUpperCase() + base2.toLowerCase()
+ + base3.toLowerCase();
+ String canonical = base1.toUpperCase() + base2.toLowerCase()
+ + base3.toLowerCase();
+ if (addPeptideVariant(peptide, peptidePos, residue, var,
+ codon, canonical))
+ {
+ count++;
+ }
}
}
}
{
for (String base : alleles.split(","))
{
- String codon = base1 + base + base3;
- if (addPeptideVariant(peptide, peptidePos, residue, var, codon))
+ if (!base2.equalsIgnoreCase(base))
{
- count++;
+ String codon = base1.toLowerCase() + base.toUpperCase()
+ + base3.toLowerCase();
+ String canonical = base1.toLowerCase() + base2.toUpperCase()
+ + base3.toLowerCase();
+ if (addPeptideVariant(peptide, peptidePos, residue, var,
+ codon, canonical))
+ {
+ count++;
+ }
}
}
}
{
for (String base : alleles.split(","))
{
- String codon = base1 + base2 + base;
- if (addPeptideVariant(peptide, peptidePos, residue, var, codon))
+ if (!base3.equalsIgnoreCase(base))
{
- count++;
+ String codon = base1.toLowerCase() + base2.toLowerCase()
+ + base.toUpperCase();
+ String canonical = base1.toLowerCase() + base2.toLowerCase()
+ + base3.toUpperCase();
+ if (addPeptideVariant(peptide, peptidePos, residue, var,
+ codon, canonical))
+ {
+ count++;
+ }
}
}
}
}
/**
- * Helper method that adds a peptide variant feature, provided the given codon
- * translates to a value different to the current residue (is a non-synonymous
- * variant). ID and clinical_significance attributes of the dna variant (if
- * present) are copied to the new feature.
+ * Helper method that adds a peptide variant feature. ID and
+ * clinical_significance attributes of the dna variant (if present) are copied
+ * to the new feature.
*
* @param peptide
* @param peptidePos
* @param residue
* @param var
* @param codon
+ * the variant codon e.g. aCg
+ * @param canonical
+ * the 'normal' codon e.g. aTg
* @return true if a feature was added, else false
*/
static boolean addPeptideVariant(SequenceI peptide, int peptidePos,
- String residue, DnaVariant var, String codon)
+ String residue, DnaVariant var, String codon, String canonical)
{
/*
* get peptide translation of codon e.g. GAT -> D
* e.g. multibase variants or HGMD_MUTATION etc
* are currently ignored here
*/
- String trans = codon.contains("-") ? "-"
+ String trans = codon.contains("-") ? null
: (codon.length() > CODON_LENGTH ? null
: ResidueProperties.codonTranslate(codon));
- if (trans != null && !trans.equals(residue))
+ if (trans == null)
+ {
+ return false;
+ }
+ String desc = canonical + "/" + codon;
+ String featureType = "";
+ if (trans.equals(residue))
+ {
+ featureType = SequenceOntologyI.SYNONYMOUS_VARIANT;
+ }
+ else if (ResidueProperties.STOP.equals(trans))
+ {
+ featureType = SequenceOntologyI.STOP_GAINED;
+ }
+ else
{
String residue3Char = StringUtils
.toSentenceCase(ResidueProperties.aa2Triplet.get(residue));
String trans3Char = StringUtils
.toSentenceCase(ResidueProperties.aa2Triplet.get(trans));
- String desc = "p." + residue3Char + peptidePos + trans3Char;
- SequenceFeature sf = new SequenceFeature(
- SequenceOntologyI.SEQUENCE_VARIANT, desc, peptidePos,
- peptidePos, var.getSource());
- StringBuilder attributes = new StringBuilder(32);
- String id = (String) var.variant.getValue(ID);
- if (id != null)
- {
- if (id.startsWith(SEQUENCE_VARIANT))
- {
- id = id.substring(SEQUENCE_VARIANT.length());
- }
- sf.setValue(ID, id);
- attributes.append(ID).append("=").append(id);
- // TODO handle other species variants JAL-2064
- StringBuilder link = new StringBuilder(32);
- try
- {
- link.append(desc).append(" ").append(id).append(
- "|http://www.ensembl.org/Homo_sapiens/Variation/Summary?v=")
- .append(URLEncoder.encode(id, "UTF-8"));
- sf.addLink(link.toString());
- } catch (UnsupportedEncodingException e)
- {
- // as if
- }
- }
- String clinSig = (String) var.variant.getValue(CLINICAL_SIGNIFICANCE);
- if (clinSig != null)
+ desc = "p." + residue3Char + peptidePos + trans3Char;
+ featureType = SequenceOntologyI.NONSYNONYMOUS_VARIANT;
+ }
+ SequenceFeature sf = new SequenceFeature(featureType, desc, peptidePos,
+ peptidePos, var.getSource());
+
+ StringBuilder attributes = new StringBuilder(32);
+ String id = (String) var.variant.getValue(VARIANT_ID);
+ if (id != null)
+ {
+ if (id.startsWith(SEQUENCE_VARIANT))
{
- sf.setValue(CLINICAL_SIGNIFICANCE, clinSig);
- attributes.append(";").append(CLINICAL_SIGNIFICANCE).append("=")
- .append(clinSig);
+ id = id.substring(SEQUENCE_VARIANT.length());
}
- peptide.addSequenceFeature(sf);
- if (attributes.length() > 0)
+ sf.setValue(VARIANT_ID, id);
+ attributes.append(VARIANT_ID).append("=").append(id);
+ // TODO handle other species variants JAL-2064
+ StringBuilder link = new StringBuilder(32);
+ try
+ {
+ link.append(desc).append(" ").append(id).append(
+ "|http://www.ensembl.org/Homo_sapiens/Variation/Summary?v=")
+ .append(URLEncoder.encode(id, "UTF-8"));
+ sf.addLink(link.toString());
+ } catch (UnsupportedEncodingException e)
{
- sf.setAttributes(attributes.toString());
+ // as if
}
- return true;
}
- return false;
+ String clinSig = (String) var.variant.getValue(CLINICAL_SIGNIFICANCE);
+ if (clinSig != null)
+ {
+ sf.setValue(CLINICAL_SIGNIFICANCE, clinSig);
+ attributes.append(";").append(CLINICAL_SIGNIFICANCE).append("=")
+ .append(clinSig);
+ }
+ peptide.addSequenceFeature(sf);
+ if (attributes.length() > 0)
+ {
+ sf.setAttributes(attributes.toString());
+ }
+ return true;
}
/**
* map from peptide position to all variants of the codon which codes for it
* LinkedHashMap ensures we keep the peptide features in sequence order
*/
- LinkedHashMap<Integer, List<DnaVariant>[]> variants = new LinkedHashMap<Integer, List<DnaVariant>[]>();
+ LinkedHashMap<Integer, List<DnaVariant>[]> variants = new LinkedHashMap<>();
List<SequenceFeature> dnaFeatures = dnaSeq.getFeatures()
.getFeaturesByOntology(SequenceOntologyI.SEQUENCE_VARIANT);
if (codonVariants == null)
{
codonVariants = new ArrayList[CODON_LENGTH];
- codonVariants[0] = new ArrayList<DnaVariant>();
- codonVariants[1] = new ArrayList<DnaVariant>();
- codonVariants[2] = new ArrayList<DnaVariant>();
+ codonVariants[0] = new ArrayList<>();
+ codonVariants[1] = new ArrayList<>();
+ codonVariants[2] = new ArrayList<>();
variants.put(peptidePosition, codonVariants);
}
/*
* fancy case - aligning via mappings between sequences
*/
- List<SequenceI> unmapped = new ArrayList<SequenceI>();
+ List<SequenceI> unmapped = new ArrayList<>();
Map<Integer, Map<SequenceI, Character>> columnMap = buildMappedColumnsMap(
unaligned, aligned, unmapped);
int width = columnMap.size();
}
// map from dataset sequence to alignment sequence(s)
- Map<SequenceI, List<SequenceI>> alignedDatasets = new HashMap<SequenceI, List<SequenceI>>();
+ Map<SequenceI, List<SequenceI>> alignedDatasets = new HashMap<>();
for (SequenceI seq : aligned.getSequences())
{
SequenceI ds = seq.getDatasetSequence();
* {unalignedSequence, characterPerSequence} at that position.
* TreeMap keeps the entries in ascending column order.
*/
- SortedMap<Integer, Map<SequenceI, Character>> map = new TreeMap<Integer, Map<SequenceI, Character>>();
+ SortedMap<Integer, Map<SequenceI, Character>> map = new TreeMap<>();
/*
* record any sequences that have no mapping so can't be realigned
Map<SequenceI, Character> seqsMap = map.get(fromCol);
if (seqsMap == null)
{
- seqsMap = new HashMap<SequenceI, Character>();
+ seqsMap = new HashMap<>();
map.put(fromCol, seqsMap);
}
seqsMap.put(seq, seq.getCharAt(mappedCharPos - toStart));