Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import org.biojava.nbio.core.sequence.AccessionID;
import org.biojava.nbio.core.sequence.DataSource;
import org.biojava.nbio.core.sequence.TaxonomyID;
import org.biojava.nbio.core.sequence.features.AbstractFeature;
import org.biojava.nbio.core.sequence.features.DBReferenceInfo;
import org.biojava.nbio.core.sequence.features.Qualifier;
import org.biojava.nbio.core.sequence.io.template.SequenceCreatorInterface;
import org.biojava.nbio.core.sequence.io.template.SequenceHeaderParserInterface;
import org.biojava.nbio.core.sequence.template.AbstractSequence;
Expand All @@ -46,6 +48,7 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -181,10 +184,9 @@ public Map<String, S> process(final int max) throws IOException, CompoundNotFoun
.forEach(sequence::addFeature);

// add taxonomy ID to new sequence
List<DBReferenceInfo> dbQualifier = genbankParser.getDatabaseReferences().get("db_xref");
if (dbQualifier != null){
DBReferenceInfo q = dbQualifier.get(0);
sequence.setTaxonomy(new TaxonomyID(q.getDatabase()+":"+q.getId(), DataSource.GENBANK));
TaxonomyID taxonomy = getTaxonomy(genbankParser.getFeatures().getOrDefault("source", Collections.emptyList()));
if (taxonomy != null) {
sequence.setTaxonomy(taxonomy);
}

sequences.put(sequence.getAccession().getID(), sequence);
Expand Down Expand Up @@ -357,6 +359,23 @@ public void close() throws IOException {
}
}

/**
* Gets the taxonomy ID from the "taxon" db_xref of the source features. A
* source can have other db_xrefs, such as a culture collection, listed
* before it (#617).
*/
private static TaxonomyID getTaxonomy(List<? extends AbstractFeature<?, ?>> sources) {
for (AbstractFeature<?, ?> source : sources) {
for (Qualifier q : source.getQualifiers().getOrDefault("db_xref", Collections.emptyList())) {
if (q instanceof DBReferenceInfo && "taxon".equalsIgnoreCase(((DBReferenceInfo) q).getDatabase())) {
DBReferenceInfo xref = (DBReferenceInfo) q;
return new TaxonomyID(xref.getDatabase() + ":" + xref.getId(), DataSource.GENBANK);
}
}
}
return null;
}

public void close() {
try {
bufferedReader.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,16 @@ public void setProxySequenceReader(SequenceReader<C> proxyLoader) {
}
// success of next statement guaranteed because source is a compulsory field
//DBReferenceInfo dbQualifier = (DBReferenceInfo)ff.get("source").get(0).getQualifiers().get("db_xref");
// the source can have several db_xrefs (e.g. a culture collection): use the "taxon" one (#617)
List<DBReferenceInfo> dbQualifiers = (ArrayList)ff.get("source").get(0).getQualifiers().get("db_xref");
DBReferenceInfo dbQualifier = dbQualifiers.get(0);

if (dbQualifier != null) this.setTaxonomy(new TaxonomyID(dbQualifier.getDatabase()+":"+dbQualifier.getId(), DataSource.UNKNOWN));
if (dbQualifiers != null) {
for (DBReferenceInfo dbQualifier : dbQualifiers) {
if ("taxon".equalsIgnoreCase(dbQualifier.getDatabase())) {
this.setTaxonomy(new TaxonomyID(dbQualifier.getDatabase()+":"+dbQualifier.getId(), DataSource.UNKNOWN));
break;
}
}
}
}

if(getAccession() == null && proxyLoader instanceof UniprotProxySequenceReader){ // we have lots of unsupported operations for this call so quick fix to allow this tow rork
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,23 @@ public void testLegacyLocusCompatable() throws IOException, CompoundNotFoundExce

}

/**
* The taxonomy ID must come from the source's "taxon" db_xref, even when
* another db_xref (here ATCC:700721) is listed first.
* https://github.com/biojava/biojava/issues/617
*/
@Test
public void testTaxonomyFromTaxonDbXref() throws Exception {
InputStream is = getClass().getResourceAsStream("/org/biojava/nbio/core/sequence/loader/152970917.gb");
GenbankReader<ProteinSequence, AminoAcidCompound> reader = new GenbankReader<>(
is, new GenericGenbankHeaderParser<>(),
new ProteinSequenceCreator(AminoAcidCompoundSet.getAminoAcidCompoundSet()));
ProteinSequence seq = reader.process().values().iterator().next();
reader.close();

assertEquals("taxon:272620", seq.getTaxonomy().getID());
}

@Test
public void readSequenceWithZeroSpanFeature() throws IOException, CompoundNotFoundException {
logger.info("make or read genbank file error when feature spans zero point of circular sequence (issue #855)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ so it should be done here (manualy).
// test taxonomy id
logger.info("taxonomy id: {}", seq.getTaxonomy().getID());
Assert.assertNotNull(seq.getTaxonomy().getID());
// must come from the "taxon" db_xref even if another one is listed first (#617)
Assert.assertTrue(seq.getTaxonomy().getID(), seq.getTaxonomy().getID().startsWith("taxon:"));
Assert.assertNotNull(Integer.decode(seq.getTaxonomy().getID().split(":")[1]));

// test taxonomy name
Expand Down
Loading