package org.petapico.npop.topic;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nanopub.Nanopub;
import org.openrdf.model.Resource;
import org.openrdf.model.Statement;
import org.openrdf.model.vocabulary.RDF;
import org.petapico.npop.Topic.TopicHandler;

public class WikipathwaysTopics implements TopicHandler {

	@Override
	public String getTopic(Nanopub np) {
		Map<Resource,Integer> resourceCount = new HashMap<>();
		List<String> organismNames = new ArrayList<>();
		for (Statement st : np.getAssertion()) {
			Resource subj = st.getSubject();
			if (subj.equals(np.getUri())) continue;
			String ps = st.getPredicate().stringValue();
			if (ps.equals(RDF.TYPE.stringValue())) continue;
			if (ps.equals("http://vocabularies.wikipathways.org/wp#pathwayOntologyTag")) continue;
			if (ps.equals("http://vocabularies.wikipathways.org/wp#organismName")) {
				organismNames.add(st.getObject().stringValue().replace(" ", "_"));
			}
			if (!resourceCount.containsKey(subj)) resourceCount.put(subj, 0);
			resourceCount.put(subj, resourceCount.get(subj) + 1);
		}
		int max = 0;
		Resource topic = null;
		for (Resource r : resourceCount.keySet()) {
			int c = resourceCount.get(r);
			if (c > max) {
				topic = r;
				max = c;
			} else if (c == max) {
				topic = null;
			}
		}
		String organisms = "";
		if (!organismNames.isEmpty()) {
			Collections.sort(organismNames);
			for (String s : organismNames) {
				organisms += "|" + s;
			}
		}
		return topic + organisms;
	}

}

@@ -558,16 +558,63 @@
 st%3C%3E();%0A
+%09%09List%3CString%3E pathwayIds = new ArrayList%3C%3E();%0A
 %09%09for (S
@@ -648,16 +648,16 @@
 on()) %7B%0A
-
 %09%09%09Resou
@@ -909,32 +909,196 @@
 ag%22)) continue;%0A
+%09%09%09if (ps.equals(%22http://purl.org/dc/terms/isPartOf%22)) %7B%0A%09%09%09%09pathwayIds.add(st.getObject().stringValue().replace(%22http://identifiers.org/wikipathways/%22, %22%22));%0A%09%09%09%7D%0A
 %09%09%09if (ps.equals
@@ -1581,16 +1581,171 @@
 %09%09%7D%0A%09%09%7D%0A
+%09%09String pathways = %22%22;%0A%09%09if (!pathwayIds.isEmpty()) %7B%0A%09%09%09Collections.sort(pathwayIds);%0A%09%09%09for (String s : pathwayIds) %7B%0A%09%09%09%09pathways += %22%7C%22 + s;%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%09String
@@ -1902,16 +1902,16 @@
 %09%09%7D%0A%09%09%7D%0A
-
 %09%09return
@@ -1918,16 +1918,39 @@
  topic +
+ %22:%22 + pathways + %22:%22 +
  organis

Improve WikiPathways topics