Skip to content

Commit

Permalink
added build cache example and fixed cache issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mkutmon committed Feb 9, 2015
1 parent 3a10b48 commit 82e2ae8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin
16 changes: 16 additions & 0 deletions examples/src/org/wikipathways/webservice/BuildCacheExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.wikipathways.webservice;

import java.io.File;
import java.io.IOException;

import org.pathvisio.core.model.ConverterException;
import org.wikipathways.client.WikiPathwaysCache;

public class BuildCacheExample {

public static void main(String[] args) throws ConverterException, IOException {
WikiPathwaysCache cache = new WikiPathwaysCache(new File("/home/martina/Downloads/cache/"));
cache.update();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,23 @@ public class WikiPathwaysCache

private File cacheDirectory;
private List<File> files;
private WikiPathwaysClient wpClient;
private URL url;

public WikiPathwaysCache(File cacheDirectory) throws MalformedURLException {
this(new WikiPathwaysClient(new URL("http://webservice.wikipathways.org")), cacheDirectory);
this(new URL("http://webservice.wikipathways.org"), cacheDirectory);
}

public WikiPathwaysCache(WikiPathwaysClient wpClient, File cacheDirectory)
{
this.wpClient = wpClient;
public WikiPathwaysCache(URL url, File cacheDirectory) {
this.url = url;

if (!(cacheDirectory.exists() && cacheDirectory.isDirectory()))
{
if (!(cacheDirectory.exists() && cacheDirectory.isDirectory())) {
throw new IllegalArgumentException ("Illegal cache directory " + cacheDirectory);
}
this.cacheDirectory = cacheDirectory;
files = FileUtils.getFiles(cacheDirectory, PW_EXT, true);
}

public List<File> getFiles()
{
public List<File> getFiles() {
return files;
}

Expand All @@ -93,7 +90,7 @@ public Collection<File> update() throws ConverterException, IOException {
Logger.log.info("Date last modified: " + df.format(d));

Logger.log.info("---[Updating new and removed pathways]---");

WikiPathwaysClient wpClient = new WikiPathwaysClient(url);
List<WSPathwayInfo> pathways = Arrays.asList(wpClient.listPathways());

// remove deleted pathways
Expand All @@ -103,6 +100,7 @@ public Collection<File> update() throws ConverterException, IOException {

Logger.log.info("---[Get Recently Changed pathways]---");

// download recently changed pathways
changedFiles.addAll(processRecentChanges(pathways));

Logger.log.info("---[Ready]---");
Expand All @@ -113,11 +111,14 @@ public Collection<File> update() throws ConverterException, IOException {
return changedFiles;
}

/**
* checks if a pathway is already in the cache or not
* then downloads all new pathways
*/
private List<File> downloadNew(Collection<WSPathwayInfo> pathways) throws ConverterException, IOException {
Set<WSPathwayInfo> newPathways = new HashSet<WSPathwayInfo>();

for(WSPathwayInfo p : pathways)
{
for(WSPathwayInfo p : pathways) {
File f = pathwayToFile(p);
if(!f.exists()) {
newPathways.add(p);
Expand Down Expand Up @@ -156,11 +157,17 @@ private List<File> processRecentChanges(List<WSPathwayInfo> pathways) throws Con
private List<File> downloadFiles (Collection<WSPathwayInfo> pathways) throws ConverterException, IOException {
List<File> files = new ArrayList<File>();

WikiPathwaysClient wpClient = new WikiPathwaysClient(url);
int i = 1;
for(WSPathwayInfo pwi : pathways) {

// to make sure that there are not too many threads in one session
if((i % 30) == 0) {
wpClient = new WikiPathwaysClient(url);
}

Logger.log.info("\tDownloading " + pwi.getName());

File file = pathwayToFile(pwi);
WSPathway wsp = wpClient.getPathway(pwi.getId());
// write out GPML without any tests
Expand Down Expand Up @@ -210,17 +217,7 @@ private void writeInfoFile(WSPathwayInfo pathway) throws IOException {
private File getInfoFile(File pathwayFile) {
return new File(pathwayFile.getAbsolutePath() + "." + INFO_EXT);
}

//Assume that files are in the form: http://host/index.php/Pathway:{Organism}:{PathwayName}
// private String fileToPathwayName(File f) {
// String filename = f.getName(); // gpml file
// String pwyName = filename.substring(0, filename.length() - PW_EXT_DOT.length()); // remove the extension and the first 3 characters i.e. ACE-Inhibitor_pathway_PharmGKB
// //Parse the pathway name
// int slash = pwyName.lastIndexOf('/');
// pwyName = pwyName.substring(slash);
// return pwyName;
// }


/**
* Get the source url (that points to the pathway
* on WikiPathways) for the given cache file.
Expand Down

0 comments on commit 82e2ae8

Please sign in to comment.