Skip to content
Commits on Source (2)
......@@ -2,7 +2,7 @@
<parent>
<artifactId>biojava</artifactId>
<groupId>org.biojava</groupId>
<version>4.2.11</version>
<version>4.2.12</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>biojava-aa-prop</artifactId>
......@@ -70,12 +70,12 @@
<dependency>
<groupId>org.biojava</groupId>
<artifactId>biojava-core</artifactId>
<version>4.2.11</version>
<version>4.2.12</version>
</dependency>
<dependency>
<groupId>org.biojava</groupId>
<artifactId>biojava-structure</artifactId>
<version>4.2.11</version>
<version>4.2.12</version>
</dependency>
<!-- logging dependencies (managed by parent pom, don't set versions or scopes here) -->
......
......@@ -4,7 +4,7 @@
<parent>
<artifactId>biojava</artifactId>
<groupId>org.biojava</groupId>
<version>4.2.11</version>
<version>4.2.12</version>
</parent>
<artifactId>biojava-alignment</artifactId>
<name>biojava-alignment</name>
......@@ -46,7 +46,7 @@
<dependency>
<groupId>org.biojava</groupId>
<artifactId>biojava-core</artifactId>
<version>4.2.11</version>
<version>4.2.12</version>
<scope>compile</scope>
</dependency>
<dependency>
......@@ -74,7 +74,7 @@
<dependency>
<groupId>org.biojava</groupId>
<artifactId>biojava-phylo</artifactId>
<version>4.2.11</version>
<version>4.2.12</version>
</dependency>
</dependencies>
</project>
......@@ -3,7 +3,7 @@
<parent>
<artifactId>biojava</artifactId>
<groupId>org.biojava</groupId>
<version>4.2.11</version>
<version>4.2.12</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>biojava-core</artifactId>
......
......@@ -88,7 +88,7 @@ public class UniprotProxySequenceReader<C extends Compound> implements ProxySequ
private static final String TREMBLID_PATTERN = "[A-NR-Z][0-9]([A-Z][A-Z0-9]{2}[0-9]){1,2}";
public static final Pattern UP_AC_PATTERN = Pattern.compile("(" + SPID_PATTERN + "|" + TREMBLID_PATTERN + ")");
private static String uniprotbaseURL = "http://www.uniprot.org"; //"http://pir.uniprot.org";
private static String uniprotbaseURL = "https://www.uniprot.org"; //"http://pir.uniprot.org";
private static String uniprotDirectoryCache = null;
private String sequence;
private CompoundSet<C> compoundSet;
......@@ -415,6 +415,61 @@ public class UniprotProxySequenceReader<C extends Compound> implements ProxySequ
fw.close();
}
/**
* Open a URL connection.
*
* Follows redirects.
* @param url
* @throws IOException
*/
private static HttpURLConnection openURLConnection(URL url) throws IOException {
// This method should be moved to a utility class in BioJava 5.0
final int timeout = 5000;
final String useragent = "BioJava";
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("User-Agent", useragent);
conn.setInstanceFollowRedirects(true);
conn.setConnectTimeout(timeout);
conn.setReadTimeout(timeout);
int status = conn.getResponseCode();
while (status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER) {
// Redirect!
String newUrl = conn.getHeaderField("Location");
if(newUrl.equals(url.toString())) {
throw new IOException("Cyclic redirect detected at "+newUrl);
}
// Preserve cookies
String cookies = conn.getHeaderField("Set-Cookie");
// open the new connection again
url = new URL(newUrl);
conn.disconnect();
conn = (HttpURLConnection) url.openConnection();
if(cookies != null) {
conn.setRequestProperty("Cookie", cookies);
}
conn.addRequestProperty("User-Agent", useragent);
conn.setInstanceFollowRedirects(true);
conn.setConnectTimeout(timeout);
conn.setReadTimeout(timeout);
conn.connect();
status = conn.getResponseCode();
logger.info("Redirecting from {} to {}", url, newUrl);
}
conn.connect();
return conn;
}
private StringBuilder fetchUniprotXML(String uniprotURL)
throws IOException, CompoundNotFoundException {
......@@ -423,11 +478,9 @@ public class UniprotProxySequenceReader<C extends Compound> implements ProxySequ
int attempt = 5;
List<String> errorCodes = new ArrayList<String>();
while(attempt > 0) {
HttpURLConnection uniprotConnection = (HttpURLConnection) uniprot.openConnection();
uniprotConnection.setRequestProperty("User-Agent", "BioJava");
uniprotConnection.connect();
HttpURLConnection uniprotConnection = openURLConnection(uniprot);
int statusCode = uniprotConnection.getResponseCode();
if (statusCode == 200) {
if (statusCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(
new InputStreamReader(
uniprotConnection.getInputStream()));
......
......@@ -3,7 +3,7 @@
<parent>
<artifactId>biojava</artifactId>
<groupId>org.biojava</groupId>
<version>4.2.11</version>
<version>4.2.12</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>biojava-genome</artifactId>
......@@ -85,13 +85,13 @@
<dependency>
<groupId>org.biojava</groupId>
<artifactId>biojava-core</artifactId>
<version>4.2.11</version>
<version>4.2.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.biojava</groupId>
<artifactId>biojava-alignment</artifactId>
<version>4.2.11</version>
<version>4.2.12</version>
<scope>compile</scope>
</dependency>
<dependency>
......
......@@ -4,7 +4,7 @@
<parent>
<artifactId>biojava</artifactId>
<groupId>org.biojava</groupId>
<version>4.2.11</version>
<version>4.2.12</version>
</parent>
<artifactId>biojava-integrationtest</artifactId>
<packaging>jar</packaging>
......@@ -32,7 +32,7 @@
<dependency>
<groupId>org.biojava</groupId>
<artifactId>biojava-structure</artifactId>
<version>4.2.11</version>
<version>4.2.12</version>
</dependency>
<!-- logging dependencies (managed by parent pom, don't set versions or scopes here) -->
<dependency>
......
......@@ -4,7 +4,7 @@
<parent>
<artifactId>biojava</artifactId>
<groupId>org.biojava</groupId>
<version>4.2.11</version>
<version>4.2.12</version>
</parent>
<artifactId>biojava-modfinder</artifactId>
<name>biojava-modfinder</name>
......@@ -31,7 +31,7 @@
<dependency>
<groupId>org.biojava</groupId>
<artifactId>biojava-structure</artifactId>
<version>4.2.11</version>
<version>4.2.12</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
......
......@@ -4,7 +4,7 @@
<parent>
<groupId>org.biojava</groupId>
<artifactId>biojava</artifactId>
<version>4.2.11</version>
<version>4.2.12</version>
</parent>
<artifactId>biojava-ontology</artifactId>
......
......@@ -3,7 +3,7 @@
<parent>
<artifactId>biojava</artifactId>
<groupId>org.biojava</groupId>
<version>4.2.11</version>
<version>4.2.12</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>biojava-phylo</artifactId>
......@@ -44,7 +44,7 @@
<dependency>
<groupId>org.biojava</groupId>
<artifactId>biojava-core</artifactId>
<version>4.2.11</version>
<version>4.2.12</version>
<scope>compile</scope>
</dependency>
<dependency>
......
......@@ -3,7 +3,7 @@
<parent>
<artifactId>biojava</artifactId>
<groupId>org.biojava</groupId>
<version>4.2.11</version>
<version>4.2.12</version>
</parent>
<artifactId>biojava-protein-disorder</artifactId>
<packaging>jar</packaging>
......@@ -63,7 +63,7 @@
<dependency>
<groupId>org.biojava</groupId>
<artifactId>biojava-core</artifactId>
<version>4.2.11</version>
<version>4.2.12</version>
</dependency>
<!-- logging dependencies (managed by parent pom, don't set versions or
scopes here) -->
......
......@@ -3,7 +3,7 @@
<parent>
<artifactId>biojava</artifactId>
<groupId>org.biojava</groupId>
<version>4.2.11</version>
<version>4.2.12</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>biojava-sequencing</artifactId>
......@@ -47,7 +47,7 @@
<dependency>
<groupId>org.biojava</groupId>
<artifactId>biojava-core</artifactId>
<version>4.2.11</version>
<version>4.2.12</version>
<scope>compile</scope>
</dependency>
<!-- logging dependencies (managed by parent pom, don't set versions or scopes here) -->
......
......@@ -3,7 +3,7 @@
<parent>
<artifactId>biojava</artifactId>
<groupId>org.biojava</groupId>
<version>4.2.11</version>
<version>4.2.12</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>biojava-structure-gui</artifactId>
......@@ -25,13 +25,13 @@
<dependency>
<groupId>org.biojava</groupId>
<artifactId>biojava-structure</artifactId>
<version>4.2.11</version>
<version>4.2.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.biojava</groupId>
<artifactId>biojava-core</artifactId>
<version>4.2.11</version>
<version>4.2.12</version>
<scope>compile</scope>
</dependency>
......
......@@ -4,7 +4,7 @@
<parent>
<artifactId>biojava</artifactId>
<groupId>org.biojava</groupId>
<version>4.2.11</version>
<version>4.2.12</version>
</parent>
<artifactId>biojava-structure</artifactId>
<name>biojava-structure</name>
......@@ -22,13 +22,13 @@
<dependency>
<groupId>org.biojava</groupId>
<artifactId>biojava-alignment</artifactId>
<version>4.2.11</version>
<version>4.2.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.biojava</groupId>
<artifactId>biojava-core</artifactId>
<version>4.2.11</version>
<version>4.2.12</version>
<scope>compile</scope>
</dependency>
......
......@@ -120,6 +120,11 @@ public class StructureIO {
cache = c;
}
public static AtomCache getAtomCache() {
checkInitAtomCache();
return cache;
}
/**
* Returns the first biologicalAssembly that is available for a protein structure. For more documentation on quaternary structures see:
* {@link http://www.pdb.org/pdb/101/static101.do?p=education_discussion/Looking-at-Structures/bioassembly_tutorial.html}
......
......@@ -636,6 +636,7 @@ public class CathInstallation implements CathDatabase{
protected void downloadFileFromRemote(URL remoteURL, File localFile) throws IOException{
// System.out.println("downloading " + remoteURL + " to: " + localFile);
LOGGER.info("Downloading file {} to local file {}", remoteURL, localFile);
long timeS = System.currentTimeMillis();
File tempFile = File.createTempFile(FileDownloadUtils.getFilePrefix(localFile), "."+ FileDownloadUtils.getFileExtension(localFile));
......@@ -665,7 +666,7 @@ public class CathInstallation implements CathDatabase{
disp = disp / 1024.0;
}
long timeE = System.currentTimeMillis();
LOGGER.info("Downloaded file {} ({}) to local file {} in {} sec.", remoteURL, String.format("%.1f",disp) + unit, localFile, (timeE - timeS)/1000);
LOGGER.info("Downloaded {} in {} sec. to {}", String.format("%.1f",disp) + unit, (timeE - timeS)/1000, localFile);
}
private boolean domainDescriptionFileAvailable(){
......
......@@ -35,6 +35,7 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
......@@ -127,6 +128,9 @@ public abstract class LocalPDBDirectory implements StructureIOFile {
protected static final String lineSplit = System.getProperty("file.separator");
/** Minimum size for a valid structure file (CIF or PDB), in bytes */
public static final long MIN_PDB_FILE_SIZE = 40; // Empty gzip files are 20bytes. Add a few more for buffer.
private File path;
private List<String> extensions;
......@@ -402,8 +406,9 @@ public abstract class LocalPDBDirectory implements StructureIOFile {
* Attempts to delete all versions of a structure from the local directory.
* @param pdbId
* @return True if one or more files were deleted
* @throws IOException if the file cannot be deleted
*/
public boolean deleteStructure(String pdbId){
public boolean deleteStructure(String pdbId) throws IOException{
boolean deleted = false;
// Force getLocalFile to check in obsolete locations
ObsoleteBehavior obsolete = getObsoleteBehavior();
......@@ -421,7 +426,7 @@ public abstract class LocalPDBDirectory implements StructureIOFile {
// delete file
boolean success = existing.delete();
if(success) {
logger.info("Deleting "+existing.getAbsolutePath());
logger.debug("Deleting "+existing.getAbsolutePath());
}
deleted = deleted || success;
......@@ -430,7 +435,7 @@ public abstract class LocalPDBDirectory implements StructureIOFile {
if(parent != null) {
success = parent.delete();
if(success) {
logger.info("Deleting "+parent.getAbsolutePath());
logger.debug("Deleting "+parent.getAbsolutePath());
}
}
......@@ -660,8 +665,9 @@ public abstract class LocalPDBDirectory implements StructureIOFile {
* Searches for previously downloaded files
* @param pdbId
* @return A file pointing to the existing file, or null if not found
* @throws IOException If the file exists but is empty and can't be deleted
*/
public File getLocalFile(String pdbId) {
public File getLocalFile(String pdbId) throws IOException {
// Search for existing files
......@@ -687,6 +693,11 @@ public abstract class LocalPDBDirectory implements StructureIOFile {
for(String ex : getExtensions() ){
File f = new File(searchdir,prefix + pdbId.toLowerCase() + ex) ;
if ( f.exists()) {
// delete files that are too short to have contents
if( f.length() < MIN_PDB_FILE_SIZE ) {
Files.delete(f.toPath());
return null;
}
return f;
}
}
......@@ -697,9 +708,11 @@ public abstract class LocalPDBDirectory implements StructureIOFile {
}
protected boolean checkFileExists(String pdbId){
try {
File path = getLocalFile(pdbId);
if ( path != null)
return true;
} catch(IOException e) {}
return false;
}
......
......@@ -68,9 +68,8 @@ public class ChemCompGroupFactory {
* again. Note that this change can have unexpected behavior of
* code executed afterwards.
* <p>
* Changing the provider does not reset the cache, so Chemical
* Component definitions already downloaded from previous providers
* will be used. To reset the cache see {@link #getCache()).
* Changing the provider also resets the cache, so any groups
* previously accessed will be reread or re-downloaded.
*
* @param provider
*/
......@@ -85,6 +84,15 @@ public class ChemCompGroupFactory {
return chemCompProvider;
}
/**
* Force the in-memory cache to be reset.
*
* Note that the ChemCompProvider may have additional memory or disk caches that need to be cleared too.
*/
public static void clearCache() {
cache.clear();
}
public static Group getGroupFromChemCompDictionary(String recordName) {
// make sure we work with upper case records
......
......@@ -42,6 +42,7 @@ import java.util.zip.GZIPOutputStream;
import org.biojava.nbio.core.util.InputStreamProvider;
import org.biojava.nbio.structure.align.util.HTTPConnectionTools;
import org.biojava.nbio.structure.align.util.UserConfiguration;
import org.biojava.nbio.structure.io.LocalPDBDirectory;
import org.biojava.nbio.structure.io.mmcif.model.ChemComp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -86,31 +87,36 @@ public class DownloadChemCompProvider implements ChemCompProvider {
protectedIDs.add("NUL");
}
private static ChemCompProvider fallback = null; // Fallback provider if the download fails
/** by default we will download only some of the files. User has to request that all files should be downloaded...
*
*/
boolean downloadAll = false;
public DownloadChemCompProvider(){
logger.debug("Initialising DownloadChemCompProvider");
// note that path is static, so this is just to make sure that all non-static methods will have path initialised
initPath();
this(null);
}
public DownloadChemCompProvider(String cacheFilePath){
logger.debug("Initialising DownloadChemCompProvider");
// note that path is static, so this is just to make sure that all non-static methods will have path initialised
if(cacheFilePath != null) {
path = new File(cacheFilePath);
}
}
private static void initPath(){
/**
* Get this provider's cache path
* @return
*/
public static File getPath(){
if (path==null) {
UserConfiguration config = new UserConfiguration();
path = new File(config.getCacheFilePath());
}
return path;
}
/**
......@@ -127,7 +133,7 @@ public class DownloadChemCompProvider implements ChemCompProvider {
// this makes sure there is a file separator between every component,
// if path has a trailing file separator or not, it will work for both cases
File dir = new File(path, CHEM_COMP_CACHE_DIRECTORY);
File dir = new File(getPath(), CHEM_COMP_CACHE_DIRECTORY);
File f = new File(dir, "components.cif.gz");
if ( ! f.exists()) {
......@@ -161,7 +167,7 @@ public class DownloadChemCompProvider implements ChemCompProvider {
logger.info("Installing individual chem comp files ...");
File dir = new File(path, CHEM_COMP_CACHE_DIRECTORY);
File dir = new File(getPath(), CHEM_COMP_CACHE_DIRECTORY);
File f = new File(dir, "components.cif.gz");
......@@ -212,7 +218,7 @@ public class DownloadChemCompProvider implements ChemCompProvider {
*/
private void writeID(String contents, String currentID) throws IOException{
String localName = DownloadChemCompProvider.getLocalFileName(currentID);
String localName = getLocalFileName(currentID);
try ( PrintWriter pw = new PrintWriter(new GZIPOutputStream(new FileOutputStream(localName))) ) {
......@@ -272,7 +278,10 @@ public class DownloadChemCompProvider implements ChemCompProvider {
ChemComp chemComp = dict.getChemComp(recordName);
// May be null if the file was corrupt. Fall back on ReducedChemCompProvider in that case
if(chemComp != null) {
return chemComp;
}
} catch (IOException e) {
......@@ -296,9 +305,12 @@ public class DownloadChemCompProvider implements ChemCompProvider {
// see https://github.com/biojava/biojava/issues/315
// probably a network error happened. Try to use the ReducedChemCOmpProvider
ReducedChemCompProvider reduced = new ReducedChemCompProvider();
if( fallback == null) {
fallback = new ReducedChemCompProvider();
}
return reduced.getChemComp(recordName);
logger.warn("Falling back to ReducedChemCompProvider for {}. This could indicate a network error.", recordName);
return fallback.getChemComp(recordName);
}
......@@ -313,16 +325,15 @@ public class DownloadChemCompProvider implements ChemCompProvider {
recordName = "_" + recordName;
}
initPath();
File f = new File(path, CHEM_COMP_CACHE_DIRECTORY);
File f = new File(getPath(), CHEM_COMP_CACHE_DIRECTORY);
if (! f.exists()){
logger.info("Creating directory " + f);
boolean success = f.mkdir();
// we've checked in initPath that path is writable, so there's no need to check if it succeeds
// in the unlikely case that in the meantime it isn't writable at least we log an error
if (!success) logger.error("Directory {} could not be created",f);
if (!success)
logger.error("Directory {} could not be created",f);
}
......@@ -337,6 +348,14 @@ public class DownloadChemCompProvider implements ChemCompProvider {
File f = new File(fileName);
// delete files that are too short to have contents
if( f.length() < LocalPDBDirectory.MIN_PDB_FILE_SIZE ) {
// Delete defensively.
// Note that if delete is unsuccessful, we re-download the file anyways
f.delete();
return false;
}
return f.exists();
}
......
......@@ -212,6 +212,9 @@ public class SimpleMMcifParser implements MMcifParser {
// the first line is a data_PDBCODE line, test if this looks like a mmcif file
line = buf.readLine();
while( line != null && (line.isEmpty() || line.startsWith(COMMENT_CHAR))) {
line = buf.readLine();
}
if (line == null || !line.startsWith(MMCIF_TOP_HEADER)){
logger.error("This does not look like a valid mmCIF file! The first line should start with 'data_', but is: '" + line+"'");
triggerDocumentEnd();
......
......@@ -21,9 +21,6 @@
*/
package org.biojava.nbio.structure.io.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
......@@ -36,6 +33,15 @@ import java.net.URLConnection;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FileDownloadUtils {
......@@ -241,6 +247,41 @@ public class FileDownloadUtils {
return connection;
}
/**
* Recursively delete a folder & contents
*
* @param dir directory to delete
*/
public static void deleteDirectory(Path dir) throws IOException {
if(dir == null || !Files.exists(dir))
return;
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
if (e != null) {
throw e;
}
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}
/**
* Recursively delete a folder & contents
*
* @param dir directory to delete
*/
public static void deleteDirectory(String dir) throws IOException {
deleteDirectory(Paths.get(dir));
}
public static void main(String[] args) {
String url;
url = "http://scop.mrc-lmb.cam.ac.uk/scop/parse/";
......