Skip to content

Commit

Permalink
Fix AA acide sequence calc -- fixes #923
Browse files Browse the repository at this point in the history
  • Loading branch information
jrobinso committed May 1, 2021
1 parent 5b3f606 commit 48f780e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 63 deletions.
53 changes: 14 additions & 39 deletions src/main/java/org/broad/igv/feature/AminoAcidManager.java
Expand Up @@ -76,10 +76,10 @@ public class AminoAcidManager {

private static final String DEFAULT_CHROMO_KEY = "default";

private LinkedHashMap<CodonTableKey, CodonTable> allCodonTables = new LinkedHashMap<CodonTableKey, CodonTable>(20);
private LinkedHashMap<String, CodonTable> allCodonTables = new LinkedHashMap<>(20);
private CodonTable currentCodonTable;

private static Table<String, String, CodonTableKey> genomeChromoTable = TreeBasedTable.create();
private static Table<String, String, String> genomeChromoTable = TreeBasedTable.create();

private static AminoAcidManager instance;

Expand Down Expand Up @@ -145,7 +145,7 @@ synchronized void clear() {
* @return Whether setting the table was successful
*/
public boolean setCodonTable(String codonTablePath, int id) {
CodonTableKey key = new CodonTableKey(codonTablePath, id);
String key = getCodonTableKey(codonTablePath, id);
return setCodonTable(key);
}

Expand All @@ -155,7 +155,7 @@ public boolean setCodonTable(String codonTablePath, int id) {
* @param key
* @return
*/
public boolean setCodonTable(CodonTableKey key) {
public boolean setCodonTable(String key) {
if (allCodonTables.containsKey(key)) {
currentCodonTable = allCodonTables.get(key);
return true;
Expand Down Expand Up @@ -309,7 +309,7 @@ public static Set<String> getAllSNPs(String sequence) {
* @return
*/
synchronized void loadCodonTables(String codonTablesPath) throws IOException, JsonParseException {
LinkedHashMap<CodonTableKey, CodonTable> newCodonTables = new LinkedHashMap<CodonTableKey, CodonTable>(20);
LinkedHashMap<String, CodonTable> newCodonTables = new LinkedHashMap<>(20);
CodonTable defaultCodonTable = null;

InputStream is = AminoAcidManager.class.getResourceAsStream(codonTablesPath);
Expand Down Expand Up @@ -427,7 +427,7 @@ private static void loadDefaultTranslationTables() throws JsonParseException {
Map.Entry<String, JsonElement> entry = iterator.next();
String chromoName = entry.getKey();
int id = entry.getValue().getAsInt();
CodonTableKey key = new CodonTableKey(codonTablePath, id);
String key = getCodonTableKey(codonTablePath, id);
genomeChromoTable.put(genomeId, chromoName, key);
}

Expand Down Expand Up @@ -456,34 +456,8 @@ private static void loadDefaultTranslationTables() throws JsonParseException {
// }
// }

public static class CodonTableKey {

private final String sourcePath;
private final int id;

private CodonTableKey(String sourcePath, int id) {
this.sourcePath = sourcePath;
this.id = id;
}

@Override
public boolean equals(Object object) {
if (object instanceof CodonTableKey) {
CodonTableKey other = (CodonTableKey) object;
return this.id == other.id &&
Objects.equal(this.sourcePath, other.sourcePath);
}
return false;
}

@Override
public int hashCode() {
return Objects.hashCode(this.sourcePath, this.id);
}

public int getId() {
return id;
}
public static String getCodonTableKey(String sourcePath, int id) {
return "" + id + ":" + (sourcePath == null ? "null" : sourcePath);
}

/**
Expand All @@ -493,7 +467,7 @@ public int getId() {
*/
public static class CodonTable {

private final CodonTableKey key;
private final String key;
private final List<String> names;
private final Set<AminoAcid> starts;
private final Map<String, AminoAcid> codonMap;
Expand All @@ -519,7 +493,7 @@ public AminoAcid getAminoAcid(String codon) {
}

private CodonTable(String path, int id, List<String> names, Set<AminoAcid> starts, Map<String, AminoAcid> codonMap) {
this.key = new CodonTableKey(path, id);
this.key = getCodonTableKey(path, id);
this.names = Collections.unmodifiableList(names);
this.starts = Collections.unmodifiableSet(starts);
this.codonMap = Collections.unmodifiableMap(codonMap);
Expand Down Expand Up @@ -590,7 +564,8 @@ private static void checkLengths(String... values) {
}

public int getId() {
return key.id;
int idx = key.indexOf(":");
return Integer.parseInt(key.substring(0, idx));
}

public String getDisplayName() {
Expand Down Expand Up @@ -623,10 +598,10 @@ public boolean equals(Object object) {

@Override
public int hashCode() {
return Objects.hashCode(this.key.id, this.key.sourcePath, this.names, this.starts, this.codonMap);
return Objects.hashCode(this.key, this.names, this.starts, this.codonMap);
}

public CodonTableKey getKey() {
public String getKey() {
return key;
}
}
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/org/broad/igv/feature/AminoAcidSequence.java
Expand Up @@ -48,11 +48,11 @@ public class AminoAcidSequence {
* If the sequence was computed from a nucleotide sequence,
* we store how the calculation was performed
*/
private final AminoAcidManager.CodonTableKey codonTableKey;
private final String codonTableKey;

public AminoAcidSequence(Strand strand, int startPosition,
List<CodonAA> sequence,
AminoAcidManager.CodonTableKey codonTableKey) {
String codonTableKey) {
this.strand = strand;
this.start = startPosition;
this.sequence = sequence;
Expand Down Expand Up @@ -86,7 +86,19 @@ public boolean hasNonNullSequence() {
return nonNullSequence;
}

public AminoAcidManager.CodonTableKey getCodonTableKey() {
public String getCodonTableKey() {
return codonTableKey;
}

/**
* Return the codon sequence as a string -- primarily for debugging.
* @return
*/
public String getSequenceString() {
String ss = "";
for(CodonAA c : sequence) {
ss += c.getAminoAcid().getSymbol();
}
return ss;
}
}
15 changes: 2 additions & 13 deletions src/main/java/org/broad/igv/feature/Exon.java
Expand Up @@ -57,11 +57,6 @@ public class Exon extends AbstractFeature implements IExon {

private AminoAcidSequence aminoAcidSequence;

//Raw bytes representing nucleotides
//Stored separately from the aminoAcidSequence because the latter changes
//when we change translation tables
private byte[] seqBytes;

private boolean noncoding = false;

// The position of the first base of this exon relative to the start of the mRNA. This will correspond
Expand Down Expand Up @@ -190,8 +185,8 @@ public int getCodingLength() {
}

public AminoAcidSequence getAminoAcidSequence(Genome genome, Exon prevExon, Exon nextExon) {
//If the stored sequence was computed with a different codon table, we reset
if (aminoAcidSequence == null ||
//If the stored sequence was computed with a different codon table, we reset
!(Objects.equal(aminoAcidSequence.getCodonTableKey(), AminoAcidManager.getInstance().getCodonTable().getKey()))) {
computeAminoAcidSequence(genome, prevExon, nextExon);
}
Expand All @@ -211,13 +206,9 @@ private void computeAminoAcidSequence(Genome genome, Exon prevExon, Exon nextExo
int readEnd = Math.min(end, codingEnd);

if (readEnd > readStart + 3) {
if (seqBytes == null) {
seqBytes = genome.getSequence(chr, readStart, readEnd);
}
byte[] seqBytes = genome.getSequence(chr, readStart, readEnd);
if (seqBytes != null) {

if (strand == Strand.POSITIVE) {

if (readingFrame > 0 && prevExon != null) {
int diff = readingFrame;
byte[] d = genome.getSequence(chr, prevExon.getCdEnd() - diff, prevExon.getCdEnd());
Expand All @@ -226,7 +217,6 @@ private void computeAminoAcidSequence(Genome genome, Exon prevExon, Exon nextExo
System.arraycopy(seqBytes, 0, tmp, diff, seqBytes.length);
seqBytes = tmp;
readStart -= readingFrame;

}

// Grab nucleotides from next exon if needed for last codon
Expand Down Expand Up @@ -272,7 +262,6 @@ private void computeAminoAcidSequence(Genome genome, Exon prevExon, Exon nextExo

public Exon copy() {
Exon copy = new Exon(getChr(), getStart(), getEnd(), getStrand());
copy.seqBytes = this.seqBytes;
copy.aminoAcidSequence = this.aminoAcidSequence;
copy.codingEnd = this.codingEnd;
copy.codingStart = this.codingStart;
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/org/broad/igv/track/SequenceTrack.java
Expand Up @@ -373,15 +373,12 @@ private JCheckBoxMenuItem getCodonTableMenuItem(AminoAcidManager.CodonTable codo
item.setToolTipText(fullName);
}
item.setText(shortName);
final AminoAcidManager.CodonTableKey curKey = codonTable.getKey();
final String curKey = codonTable.getKey();
item.setSelected(curKey.equals(AminoAcidManager.getInstance().getCodonTable().getKey()));
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
AminoAcidManager.getInstance().setCodonTable(curKey);
SequenceTrack.this.refreshAminoAcids();
repaint();
}
item.addActionListener(e -> {
AminoAcidManager.getInstance().setCodonTable(curKey);
SequenceTrack.this.refreshAminoAcids();
repaint();
});
return item;
}
Expand Down

0 comments on commit 48f780e

Please sign in to comment.