com.hfg.bio.taxonomy.ncbi.NCBIRemoteTaxonomyDataSource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.bio.taxonomy.ncbi;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import com.hfg.exception.ProgrammingException;
import com.hfg.util.StringUtil;
//------------------------------------------------------------------------------
/**
A remote data source for use with NCBITaxon. The default URL is the location of
the taxdmp.zip on the NCBI's website. Any specified URL (or File) needs to have
the same format as the taxdmp.zip.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class NCBIRemoteTaxonomyDataSource extends NCBITaxonomyDataSourceImpl
{
private URL mURL;
private static final Logger LOGGER = Logger.getLogger(NCBIRemoteTaxonomyDataSource.class.getPackage().getName());
private static URL sNCBI_URL;
static
{
try
{
sNCBI_URL = new URL("https://ftp.ncbi.nih.gov/pub/taxonomy/taxdmp.zip");
}
catch (MalformedURLException e)
{
throw new ProgrammingException(e);
}
}
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public NCBIRemoteTaxonomyDataSource()
{
setURL(sNCBI_URL);
}
//---------------------------------------------------------------------------
public NCBIRemoteTaxonomyDataSource(URL inURL)
{
setURL(inURL);
}
//---------------------------------------------------------------------------
public NCBIRemoteTaxonomyDataSource(File inFile)
throws MalformedURLException
{
URI uri = inFile.toURI();
setURL(uri.toURL());
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public static Logger getLogger()
{
return LOGGER;
}
//--------------------------------------------------------------------------
public URL getURL()
{
return mURL;
}
//###########################################################################
// PROTECTED METHODS
//###########################################################################
//--------------------------------------------------------------------------
protected synchronized void initialize()
{
if (! mURL.getFile().endsWith(".zip"))
{
throw new RuntimeException("The taxonomy data needs to be in a zipped archive!");
}
try
{
initializeFromStream(mURL.openStream());
}
catch (IOException e)
{
throw new RuntimeException("Problem accessing taxonomy data from " + mURL + " :", e);
}
}
//###########################################################################
// PRIVATE METHODS
//###########################################################################
//--------------------------------------------------------------------------
private void setURL(URL inValue)
{
mURL = inValue;
}
//--------------------------------------------------------------------------
private void initializeFromStream(InputStream inStream)
throws IOException
{
ZipInputStream zipInputStream = new ZipInputStream(inStream);
ZipEntry zipEntry = zipInputStream.getNextEntry();
if (null == zipEntry)
{
throw new RuntimeException("Problem reading zipped taxonomy data from " + mURL + " !");
}
LOGGER.log(Level.FINE, "Initializing from the remote URL " + StringUtil.singleQuote(mURL) + " ...");
while (zipEntry != null)
{
String entryName = zipEntry.getName();
File newFile = new File(entryName);
String directory = newFile.getParent();
LOGGER.log(Level.FINE, "Zip entry:" + StringUtil.singleQuote(entryName));
if (directory == null)
{
if (newFile.isDirectory())
break;
}
if (entryName.equals("names.dmp"))
{
innerParseNamesFile(new BufferedReader(new InputStreamReader(zipInputStream)));
}
else if (entryName.equals("nodes.dmp"))
{
innerParseNodesFile(new BufferedReader(new InputStreamReader(zipInputStream)));
}
zipInputStream.closeEntry();
zipEntry = zipInputStream.getNextEntry();
}
zipInputStream.close();
}
}