import jalview.io.gff.SequenceOntologyI;
import jalview.util.MapList;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
*/
public class EnsemblGene extends EnsemblSeqProxy
{
+ private static final String GENE_PREFIX = "gene:";
+
// TODO modify to accept other species e.g. ENSMUSGnnn
private static final Regex ACCESSION_REGEX = new Regex(
- "((ENSG)[0-9]{11})");
+ "(ENSG|ENST)[0-9]{11}$");
private static final EnsemblFeatureType[] FEATURES_TO_FETCH = {
EnsemblFeatureType.gene, EnsemblFeatureType.transcript,
@Override
public AlignmentI getSequenceRecords(String query) throws Exception
{
- // TODO ? if an ENST identifier is supplied, convert to ENSG?
+ List<String> transcriptsWanted = null;
+
+ if (isTranscriptIdentifier(query))
+ {
+ transcriptsWanted = Arrays.asList(query
+ .split(getAccessionSeparator()));
+ query = getGeneForTranscript(query);
+ if (query == null)
+ {
+ return null;
+ }
+ }
+
AlignmentI al = super.getSequenceRecords(query);
if (al.getHeight() > 0)
{
- getTranscripts(al, query);
+ getTranscripts(al, query, transcriptsWanted);
}
return al;
}
/**
+ * Gets the parent gene identifier for a given transcript identifier, by
+ * retrieving 'transcript' features overlapping the transcript, and finding
+ * the Parent property of the feature whose id is the given identifier.
+ *
+ * @param query
+ * @return
+ */
+ protected String getGeneForTranscript(String transcriptId)
+ {
+ String geneId = null;
+
+ /*
+ * reduce multiple transcripts (e.g. from Uniprot x-ref) to the first
+ * one only as representative (they should all have the same gene)
+ */
+ transcriptId = transcriptId.split(getAccessionSeparator())[0];
+
+ try
+ {
+ EnsemblFeatureType[] geneFeature = new EnsemblFeatureType[] { EnsemblFeatureType.transcript };
+ AlignmentI al = new EnsemblFeatures().getSequenceRecords(
+ transcriptId, geneFeature);
+ if (al != null && al.getHeight() > 0)
+ {
+ SequenceFeature[] sfs = al.getSequenceAt(0).getSequenceFeatures();
+ if (sfs != null)
+ {
+ for (SequenceFeature sf : sfs)
+ {
+ if (transcriptId.equals(getTranscriptId(sf)))
+ {
+ String parent = (String) sf.getValue(PARENT);
+ if (parent != null && parent.startsWith(GENE_PREFIX))
+ {
+ geneId = parent.substring(5);
+ }
+ break;
+ }
+ }
+ }
+ }
+ return geneId;
+ } catch (IOException e)
+ {
+ System.err.println("Error retrieving gene id for " + transcriptId
+ + ": " + e.getMessage());
+ return null;
+ }
+ }
+
+ /**
* Constructs all transcripts for the gene, as identified by "transcript"
* features whose Parent is the requested gene. The coding transcript
* sequences (i.e. with introns omitted) are added to the alignment.
*
* @param al
* @param accId
+ * @param transcriptsWanted
+ * optional list of transcript ids to filter by
* @throws Exception
*/
- protected void getTranscripts(AlignmentI al, String accId)
+ protected void getTranscripts(AlignmentI al, String accId,
+ List<String> transcriptsWanted)
throws Exception
{
SequenceI gene = al.getSequenceAt(0);
List<SequenceFeature> transcriptFeatures = getTranscriptFeatures(accId,
- gene);
+ gene, transcriptsWanted);
for (SequenceFeature transcriptFeature : transcriptFeatures)
{
SequenceI makeTranscript(SequenceFeature transcriptFeature,
AlignmentI al, SequenceI gene)
{
- String accId = (String) transcriptFeature.getValue("transcript_id");
+ String accId = getTranscriptId(transcriptFeature);
if (accId == null)
{
return null;
}
/**
+ * Returns the 'transcript_id' property of the sequence feature (or null)
+ *
+ * @param feature
+ * @return
+ */
+ protected String getTranscriptId(SequenceFeature feature)
+ {
+ return (String) feature.getValue("transcript_id");
+ }
+
+ /**
* Returns a list of the transcript features on the sequence whose Parent is
* the gene for the accession id.
*
* @param accId
* @param geneSequence
+ * @param transcriptsWanted
+ * optional list of ids to filter on
* @return
*/
protected List<SequenceFeature> getTranscriptFeatures(String accId,
- SequenceI geneSequence)
+ SequenceI geneSequence, List<String> transcriptsWanted)
{
List<SequenceFeature> transcriptFeatures = new ArrayList<SequenceFeature>();
- String parentIdentifier = "gene:" + accId;
+ String parentIdentifier = GENE_PREFIX + accId;
SequenceFeature[] sfs = geneSequence.getSequenceFeatures();
if (sfs != null)
{
if (isTranscript(sf.getType()))
{
+ if (transcriptsWanted != null)
+ {
+ String transcriptId = (String) sf.getValue("transcript_id");
+ if (!transcriptsWanted.contains(transcriptId))
+ {
+ // continue;
+ }
+ }
String parent = (String) sf.getValue(PARENT);
if (parentIdentifier.equals(parent))
{
@Override
public String getDescription()
{
- return "Fetches all transcripts and variant features for a gene";
+ return "Fetches all transcripts and variant features for a gene or transcript";
}
/**
- * Default test query is a transcript
+ * Default test query is a gene id (can also enter a transcript id)
*/
@Override
public String getTestQuery()
SequenceOntologyI.GENE))
{
String id = (String) sf.getValue(ID);
- if (("gene:" + accId).equals(id))
+ if ((GENE_PREFIX + accId).equals(id))
{
return true;
}
if (isTranscript(sf.getType()))
{
String parent = (String) sf.getValue(PARENT);
- if (!("gene:" + accessionId).equals(parent))
+ if (!(GENE_PREFIX + accessionId).equals(parent))
{
return false;
}