+
+ if (!found)
+ {
+ // do a bit more work - search for sequences with references matching
+ // xrefs on this sequence.
+ found = searchDataset(fromDna, dss, xref, rseqs, cf, false);
+ }
+ if (found)
+ {
+ refIterator.remove();
+ }
+ }
+
+ /*
+ * fetch from source database any dbrefs we haven't resolved up to here
+ */
+ if (!sourceRefs.isEmpty())
+ {
+ retrieveCrossRef(sourceRefs, seq, xrfs, fromDna, cf);
+ }
+ }
+
+ Alignment ral = null;
+ if (rseqs.size() > 0)
+ {
+ ral = new Alignment(rseqs.toArray(new SequenceI[rseqs.size()]));
+ if (!cf.isEmpty())
+ {
+ dataset.addCodonFrame(cf);
+ }
+ }
+ return ral;
+ }
+
+ private void retrieveCrossRef(List<DBRefEntry> sourceRefs, SequenceI seq,
+ DBRefEntry[] xrfs, boolean fromDna, AlignedCodonFrame cf)
+ {
+ ASequenceFetcher sftch = SequenceFetcherFactory.getSequenceFetcher();
+ SequenceI[] retrieved = null;
+ SequenceI dss = seq.getDatasetSequence() == null ? seq
+ : seq.getDatasetSequence();
+ // first filter in case we are retrieving crossrefs that have already been
+ // retrieved. this happens for cases where a database record doesn't yield
+ // protein products for CDS
+ removeAlreadyRetrievedSeqs(sourceRefs, fromDna);
+ if (sourceRefs.size() == 0)
+ {
+ // no more work to do! We already had all requested sequence records in
+ // the dataset.
+ return;
+ }
+ try
+ {
+ retrieved = sftch.getSequences(sourceRefs, !fromDna);
+ } catch (Exception e)
+ {
+ System.err.println(
+ "Problem whilst retrieving cross references for Sequence : "
+ + seq.getName());
+ e.printStackTrace();
+ }
+
+ if (retrieved != null)
+ {
+ boolean addedXref = false;
+ List<SequenceI> newDsSeqs = new ArrayList<SequenceI>(),
+ doNotAdd = new ArrayList<SequenceI>();
+
+ for (SequenceI retrievedSequence : retrieved)
+ {
+ // dataset gets contaminated ccwith non-ds sequences. why ??!
+ // try: Ensembl -> Nuc->Ensembl, Nuc->Uniprot-->Protein->EMBL->
+ SequenceI retrievedDss = retrievedSequence
+ .getDatasetSequence() == null ? retrievedSequence
+ : retrievedSequence.getDatasetSequence();
+ addedXref |= importCrossRefSeq(cf, newDsSeqs, doNotAdd, dss,
+ retrievedDss);
+ }
+ if (!addedXref)
+ {
+ // try again, after looking for matching IDs
+ // shouldn't need to do this unless the dbref mechanism has broken.
+ updateDbrefMappings(seq, xrfs, retrieved, cf, fromDna);
+ for (SequenceI retrievedSequence : retrieved)
+ {
+ // dataset gets contaminated ccwith non-ds sequences. why ??!
+ // try: Ensembl -> Nuc->Ensembl, Nuc->Uniprot-->Protein->EMBL->
+ SequenceI retrievedDss = retrievedSequence
+ .getDatasetSequence() == null ? retrievedSequence
+ : retrievedSequence.getDatasetSequence();
+ addedXref |= importCrossRefSeq(cf, newDsSeqs, doNotAdd, dss,
+ retrievedDss);
+ }
+ }
+ for (SequenceI newToSeq : newDsSeqs)
+ {
+ if (!doNotAdd.contains(newToSeq)
+ && dataset.findIndex(newToSeq) == -1)
+ {
+ dataset.addSequence(newToSeq);
+ matcher.add(newToSeq);
+ }