/*
* Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
* Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
*
* This file is part of Jalview.
*
* Jalview is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* Jalview is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Jalview. If not, see .
*/
package jalview.ws;
import jalview.bin.Cache;
import jalview.datamodel.DBRefEntry;
import jalview.datamodel.SequenceFeature;
import jalview.datamodel.SequenceI;
import jalview.gui.AlignFrame;
import jalview.gui.Desktop;
import jalview.gui.FeatureSettings;
import jalview.util.UrlLink;
import jalview.ws.dbsources.das.api.DasSourceRegistryI;
import jalview.ws.dbsources.das.api.jalviewSourceI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.JOptionPane;
import org.biodas.jdas.client.FeaturesClient;
import org.biodas.jdas.client.adapters.features.DasGFFAdapter;
import org.biodas.jdas.client.adapters.features.DasGFFAdapter.GFFAdapter;
import org.biodas.jdas.client.threads.FeaturesClientMultipleSources;
import org.biodas.jdas.schema.features.ERRORSEGMENT;
import org.biodas.jdas.schema.features.FEATURE;
import org.biodas.jdas.schema.features.LINK;
import org.biodas.jdas.schema.features.SEGMENT;
import org.biodas.jdas.schema.features.TYPE;
import org.biodas.jdas.schema.features.UNKNOWNFEATURE;
import org.biodas.jdas.schema.features.UNKNOWNSEGMENT;
import org.biodas.jdas.schema.sources.COORDINATES;
/**
* DOCUMENT ME!
*
* @author $author$
* @version $Revision$
*/
public class DasSequenceFeatureFetcher
{
SequenceI[] sequences;
AlignFrame af;
FeatureSettings fsettings;
StringBuffer sbuffer = new StringBuffer();
List selectedSources;
boolean cancelled = false;
private void debug(String mesg)
{
debug(mesg, null);
}
private void debug(String mesg, Exception e)
{
if (Cache.log != null)
{
Cache.log.debug(mesg, e);
}
else
{
System.err.println(mesg);
if (e != null)
{
e.printStackTrace();
}
}
}
long startTime;
private DasSourceRegistryI sourceRegistry;
private boolean useJDASMultiThread = true;
/**
* Creates a new SequenceFeatureFetcher object. Uses default
*
* @param align
* DOCUMENT ME!
* @param ap
* DOCUMENT ME!
*/
public DasSequenceFeatureFetcher(SequenceI[] sequences,
FeatureSettings fsettings, Vector selectedSources)
{
this(sequences, fsettings, selectedSources, true, true, true);
}
public DasSequenceFeatureFetcher(SequenceI[] oursequences,
FeatureSettings fsettings, List selectedSources2,
boolean checkDbrefs, boolean promptFetchDbrefs)
{
this(oursequences, fsettings, selectedSources2, checkDbrefs,
promptFetchDbrefs, true);
}
public DasSequenceFeatureFetcher(SequenceI[] oursequences,
FeatureSettings fsettings, List selectedSources2,
boolean checkDbrefs, boolean promptFetchDbrefs,
boolean useJDasMultiThread)
{
this.useJDASMultiThread = useJDasMultiThread;
this.selectedSources = new ArrayList();
// filter both sequences and sources to eliminate duplicates
for (jalviewSourceI src : selectedSources2)
{
if (!selectedSources.contains(src))
{
selectedSources.add(src);
}
;
}
Vector sqs = new Vector();
for (int i = 0; i < oursequences.length; i++)
{
if (!sqs.contains(oursequences[i]))
{
sqs.addElement(oursequences[i]);
}
}
sequences = new SequenceI[sqs.size()];
for (int i = 0; i < sequences.length; i++)
{
sequences[i] = (SequenceI) sqs.elementAt(i);
}
if (fsettings != null)
{
this.fsettings = fsettings;
this.af = fsettings.af;
af.setShowSeqFeatures(true);
}
int uniprotCount = 0;
for (jalviewSourceI source : selectedSources)
{
for (COORDINATES coords : source.getVersion().getCOORDINATES())
{
// TODO: match UniProt coord system canonically (?) - does
// UniProt==uniprot==UNIPROT ?
if (coords.getAuthority().toLowerCase().equals("uniprot"))
{
uniprotCount++;
break;
}
}
}
int refCount = 0;
for (int i = 0; i < sequences.length; i++)
{
DBRefEntry[] dbref = sequences[i].getDBRef();
if (dbref != null)
{
for (int j = 0; j < dbref.length; j++)
{
if (dbref[j].getSource().equals(
jalview.datamodel.DBRefSource.UNIPROT))
{
refCount++;
break;
}
}
}
}
if (checkDbrefs && refCount < sequences.length && uniprotCount > 0)
{
int reply = JOptionPane.YES_OPTION;
if (promptFetchDbrefs)
{
reply = JOptionPane
.showInternalConfirmDialog(
Desktop.desktop,
"Do you want Jalview to find\n"
+ "Uniprot Accession ids for given sequence names?",
"Find Uniprot Accession Ids",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
}
if (reply == JOptionPane.YES_OPTION)
{
Thread thread = new Thread(new FetchDBRefs());
thread.start();
}
else
{
_startFetching();
}
}
else
{
_startFetching();
}
}
private void _startFetching()
{
running = true;
new Thread(new FetchSeqFeatures()).start();
}
class FetchSeqFeatures implements Runnable
{
public void run()
{
startFetching();
setGuiFetchComplete();
}
}
class FetchDBRefs implements Runnable
{
public void run()
{
running = true;
new DBRefFetcher(sequences, af).fetchDBRefs(true);
startFetching();
setGuiFetchComplete();
}
}
/**
* Spawns Fetcher threads to add features to sequences in the dataset
*/
void startFetching()
{
running = true;
cancelled = false;
startTime = System.currentTimeMillis();
if (af != null)
{
af.setProgressBar("Fetching DAS Sequence Features", startTime);
}
if (sourceRegistry == null)
{
sourceRegistry = Cache.getDasSourceRegistry();
}
if (selectedSources == null || selectedSources.size() == 0)
{
try
{
jalviewSourceI[] sources = sourceRegistry.getSources().toArray(
new jalviewSourceI[0]);
String active = jalview.bin.Cache.getDefault("DAS_ACTIVE_SOURCE",
"uniprot");
StringTokenizer st = new StringTokenizer(active, "\t");
selectedSources = new Vector();
String token;
while (st.hasMoreTokens())
{
token = st.nextToken();
for (int i = 0; i < sources.length; i++)
{
if (sources[i].getTitle().equals(token))
{
selectedSources.add(sources[i]);
break;
}
}
}
} catch (Exception ex)
{
debug("Exception whilst setting default feature sources from registry and local preferences.",
ex);
}
}
if (selectedSources == null || selectedSources.size() == 0)
{
System.out.println("No DAS Sources active");
cancelled = true;
setGuiNoDassourceActive();
return;
}
sourcesRemaining = selectedSources.size();
FeaturesClientMultipleSources fc = new FeaturesClientMultipleSources();
fc.setConnProps(sourceRegistry.getSessionHandler());
// Now sending requests one at a time to each server
ArrayList srcobj = new ArrayList();
ArrayList src = new ArrayList();
List> ids = new ArrayList>();
List> idobj = new ArrayList>();
List