Search in sources :

Example 1 with RdfTypeInfo

use of org.nextprot.api.rdf.domain.RdfTypeInfo in project nextprot-api by calipho-sib.

the class RdfHelpServiceImpl method getRdfTypeFullInfoList.

@Cacheable("rdfhelp")
@Override
public synchronized List<RdfTypeInfo> getRdfTypeFullInfoList() {
    long t0 = System.currentTimeMillis();
    Set<String> rdfTypesNames = getRdfTypesNames();
    List<Future<RdfTypeInfo>> rdfFutureTypes = new ArrayList<Future<RdfTypeInfo>>();
    List<RdfTypeInfo> rdfTypes = Collections.synchronizedList(new ArrayList<RdfTypeInfo>());
    ExecutorService executor = Executors.newFixedThreadPool(NUMBER_THREADS);
    for (String rdfTypeName : rdfTypesNames) {
        // LOGGER.info("step1 - found rdf:type name " + rdfTypeName);
        Future<RdfTypeInfo> futureRdfTypeInfo = executor.submit(new FillRdfTypeInfoTask(this, rdfTypeName));
        rdfFutureTypes.add(futureRdfTypeInfo);
    }
    executor.shutdown();
    try {
        executor.awaitTermination(1, TimeUnit.DAYS);
    } catch (InterruptedException e) {
        e.printStackTrace();
        throw new NextProtException(e.getLocalizedMessage());
    }
    for (Future<RdfTypeInfo> futureRdfTypeInfo : rdfFutureTypes) {
        try {
            rdfTypes.add(futureRdfTypeInfo.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
    // now populate parent and parent triples of each type
    for (RdfTypeInfo rti : rdfTypes) {
        // LOGGER.info("step2 - updating rdf:type " + rti.getTypeName());
        for (RdfTypeInfo parent : rdfTypes) {
            List<TripleInfo> triples = parent.findTriplesWithObjectType(rti.getTypeName());
            if (triples.size() > 0) {
                // LOGGER.info("step3 - linking parent rdf:type " + parent.getTypeName()  + " to rdf:type " + rti.getTypeName() + " , triple size: " + triples.size());
                rti.addParent(parent.getTypeName());
                for (TripleInfo triple : triples) rti.addParentTriple(triple);
            }
        }
    }
    Map<String, RdfTypeInfo> fullMap = new HashMap<String, RdfTypeInfo>();
    for (RdfTypeInfo rti : rdfTypes) {
        fullMap.put(rti.getTypeName(), rti);
    }
    if (fullMap.containsKey(":Entry"))
        buildPathToOrigin(fullMap, fullMap.get(":Entry"), "?entry ", 0);
    long seconds = (System.currentTimeMillis() - t0) / 1000;
    String duration = String.format("%d:%02d:%02d", seconds / 3600, (seconds % 3600) / 60, (seconds % 60)) + " [H:MM:SS]";
    LOGGER.info("errors: " + errorCount);
    LOGGER.info("duration: " + duration);
    return rdfTypes;
}
Also used : HashMap(java.util.HashMap) RdfTypeInfo(org.nextprot.api.rdf.domain.RdfTypeInfo) ArrayList(java.util.ArrayList) TripleInfo(org.nextprot.api.rdf.domain.TripleInfo) NextProtException(org.nextprot.api.commons.exception.NextProtException) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 2 with RdfTypeInfo

use of org.nextprot.api.rdf.domain.RdfTypeInfo in project nextprot-api by calipho-sib.

the class RdfHelpServiceImpl method buildPathToOrigin.

private static void buildPathToOrigin(final Map<String, RdfTypeInfo> fullMap, RdfTypeInfo currentEntry, String currentPath, int currenDepth) {
    if (currenDepth > 20)
        return;
    for (TripleInfo childTripleInfo : currentEntry.getTriples()) {
        RdfTypeInfo childTypeInfo = fullMap.get(childTripleInfo.getObjectType());
        if (childTypeInfo != null && !currentPath.contains(childTripleInfo.getPredicate()) && !childTripleInfo.getPredicate().equals(":interaction")) {
            String nextPath = currentPath + childTripleInfo.getPredicate();
            childTypeInfo.addPathToOrigin(nextPath);
            buildPathToOrigin(fullMap, childTypeInfo, nextPath + "/", currenDepth + 1);
        }
    }
}
Also used : RdfTypeInfo(org.nextprot.api.rdf.domain.RdfTypeInfo) TripleInfo(org.nextprot.api.rdf.domain.TripleInfo)

Example 3 with RdfTypeInfo

use of org.nextprot.api.rdf.domain.RdfTypeInfo in project nextprot-api by calipho-sib.

the class RdfHelpController method helpFullInfo.

/*

	@RequestMapping("/rdf/help/type/{rdfType}/values")
	@ResponseBody
	public List<String> helpFullInfoValues(@PathVariable("rdfType") String rdfType, Model model) {
		try {
			return this.service.getRdfTypeValues(rdfType);
		} catch (Exception e) {
			throw new NextProtException(e.getMessage());
		}
	}

	@RequestMapping("/rdf/help/visgraphp")
	public String visgraphp(Model model) {

		Map<String, RdfTypeInfo> map = new HashMap<String, RdfTypeInfo>();
		List<String> names = new ArrayList<String>();

		for (RdfTypeInfo rti : this.service.getRdfTypeFullInfoList()) {
			map.put(rti.getTypeName(), rti);
			names.add(rti.getTypeName());
		}

		model.addAttribute("visutils", new VisUtils());
		model.addAttribute("map", map);
		model.addAttribute("names", names);

		return "visgraphp";
	}

	@RequestMapping("/rdf/help/visgraph")
	public String visgraph(Model model) {

		Map<String, RdfTypeInfo> map = new HashMap<String, RdfTypeInfo>();
		List<String> names = new ArrayList<String>();

		for (RdfTypeInfo rti : this.service.getRdfTypeFullInfoList()) {
			map.put(rti.getTypeName(), rti);
			if (!rti.getTypeName().startsWith(":Isoform") && !rti.getTypeName().startsWith(":Evidence")) {
				names.add(rti.getTypeName());
			} else
				System.out.println("Skipped for" + rti);
		}

		model.addAttribute("visutils", new VisUtils());
		model.addAttribute("map", map);
		model.addAttribute("names", names);

		return "visgraph";
	}

	// Example http://localhost:8080/nextprot-api/rdf/help/visgraph/:Entry,:Gene
	@RequestMapping("/rdf/help/visgraph/{rdfType}")
	public String visgraph(@PathVariable("rdfType") String rdfType, Model model) {

		Map<String, RdfTypeInfo> map = new HashMap<String, RdfTypeInfo>();
		for (RdfTypeInfo rti : this.service.getRdfTypeFullInfoList()) {
			map.put(rti.getTypeName(), rti);
		}

		model.addAttribute("visutils", new VisUtils());
		model.addAttribute("map", map);
		model.addAttribute("names", Arrays.asList(rdfType.split(",")));

		return "visgraph";
	}

	// Example http://localhost:8080/nextprot-api/rdf/help/visgraph/:Entry,:Gene
	@RequestMapping("/rdf/help/visgraphp/{rdfType}")
	public String visgraphp(@PathVariable("rdfType") String rdfType, Model model) {

		Map<String, RdfTypeInfo> map = new HashMap<String, RdfTypeInfo>();
		for (RdfTypeInfo rti : this.service.getRdfTypeFullInfoList()) {
			map.put(rti.getTypeName(), rti);
		}

		model.addAttribute("visutils", new VisUtils());
		model.addAttribute("map", map);
		model.addAttribute("names", Arrays.asList(rdfType.split(",")));

		return "visgraphp";
	}

	public static class VisUtils {
		private int n = 6666;
		Set<Integer> ids = new TreeSet<Integer>();
		Map<String, Integer> mapIds = new HashMap<String, Integer>();
		
		public void addId(int id) {
			ids.add(id);
		}

		public Integer getMaxDepth(RdfTypeInfo rti) {
			int max = 0;
			for (String p : rti.getPathToOrigin()) {
				max = Math.max(max, getDepth(p));
			}
			return max;
		}

		public Integer getTripleId(TripleInfo t) {
			String key = t.getSubjectType() + "" + t.getPredicate();
			if (!mapIds.containsKey(key)) {
				mapIds.put(key, n++);
			}
			return mapIds.get(key);
		}

		public Integer getNodeId(String currentNode, String relativePath, String _path) {
			System.out.println("CN:" + currentNode + " RP:" + relativePath + " P:" + _path);

			String path = _path.replace("/", "");

			if (currentNode.equals("Entry"))
				return getPathId(currentNode);

			if (currentNode.equals("isoform")) {
				String afterNode = path.replace(relativePath, "").replace(currentNode, "");
				int ni = afterNode.indexOf(":");
				int nextIndex = afterNode.indexOf(":", ni+1);
				if(nextIndex == -1) {
					nextIndex = ni;
				}
				if(ni == -1) {
					nextIndex = 0;
				}

				
				System.out.println("AN" + afterNode + " ANS" + afterNode.substring(nextIndex));
				return getPathId(currentNode + afterNode.substring(nextIndex));
			}
			
			String currentAndAfterNode = path.replace(relativePath, "");
			System.out.println(currentAndAfterNode + getPathId(currentAndAfterNode));
			System.out.println();
			return getPathId(currentAndAfterNode);
		}

		public Integer getPathId(String path) {
			if (!mapIds.containsKey(path)) {
				mapIds.put(path, n++);
			}
			return mapIds.get(path);
		}

		public boolean isLast(String path, String absolutePath) {
			return path.replace("/", "").equals(absolutePath);
		}

		public Integer getEdgeId(Integer id1, Integer id2) {
			String s = id1.toString() + id2.toString();
			return Integer.valueOf(s);
		}

		public Integer getDepth(String p) {
			return p.split(":").length;
		}

		public String getSimplePath(String path) {
			int n = StringUtils.reverse(path).indexOf(":");
			if (n == -1)
				return "Entry";
			return path.substring(path.length() - n);
		}

		public List<String> getPathArray(String path) {
			String s = path.replace("?entry ", "");
			String ss[] = s.split("/");
			List<String> r = new ArrayList<String>();
			String current = "?entry";
			current += " ";
			r.add(current);
			for (String p : ss) {
				current += p;
				r.add(current);
			}
			return r;
		}

		public boolean contains(int id) {
			return ids.contains(id);
		}
		
		private Set<String> edges = new TreeSet<String>();
		public String getEdge (Integer from, Integer to, String label){
			String key = null;
			if(from < to){
				key = from + "" + to;
			}else key = to + "" + from;
			
			if(edges.contains(key)){
				return "";
			}else {
				edges.add(key);
				return "edges.push({from: " + from + ", to: " + to + ", label: '" + label + "'});";
			}
		
		}
		
	}
	
	
	/**
	 * Full info of a single rdf:type
	 * 
	 * @param rdfType
	 * @param model
	 * @return
	 */
// we want this undocumented   @ApiMethod(path = "/rdf/help/type/{rdfType}", verb = ApiVerb.GET, description = "Gets a description and properties of a rdf type", produces = { MediaType.APPLICATION_JSON_VALUE })
@RequestMapping("/rdf/help/type/{rdfType}")
@ResponseBody
public RdfTypeInfo helpFullInfo(@PathVariable("rdfType") String rdfType, Model model) {
    try {
        RdfTypeInfo typ;
        typ = this.service.getRdfTypeFullInfo(rdfType);
        model.addAttribute("result", typ);
        return typ;
    } catch (Exception e) {
        e.printStackTrace();
        throw new NextProtException(e.getMessage());
    }
}
Also used : NextProtException(org.nextprot.api.commons.exception.NextProtException) RdfTypeInfo(org.nextprot.api.rdf.domain.RdfTypeInfo) NextProtException(org.nextprot.api.commons.exception.NextProtException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with RdfTypeInfo

use of org.nextprot.api.rdf.domain.RdfTypeInfo in project nextprot-api by calipho-sib.

the class RdfHelpServiceImpl method getRdfTypeFullInfo.

@Override
public RdfTypeInfo getRdfTypeFullInfo(String rdfTypeName) {
    RdfTypeInfo rdfTypeInfo = new RdfTypeInfo();
    rdfTypeInfo.setTypeName(rdfTypeName);
    Map<String, String> properties = getRdfTypeProperties(rdfTypeName);
    if (!properties.isEmpty()) {
        rdfTypeInfo.setTypeName(properties.get("rdfType"));
        rdfTypeInfo.setRdfsLabel(properties.get("label"));
        rdfTypeInfo.setRdfsComment(properties.get("comment"));
        rdfTypeInfo.setInstanceCount(Integer.valueOf(properties.get("instanceCount")));
        rdfTypeInfo.setInstanceSample(properties.get("instanceSample"));
    }
    List<TripleInfo> triples = getTripleInfoList(rdfTypeInfo.getTypeName());
    Set<String> values = null;
    if (completeSetOfValuesForTypes.contains(rdfTypeInfo.getTypeName())) {
        values = getRdfTypeValues(rdfTypeName, Integer.MAX_VALUE);
    } else {
        values = getRdfTypeValues(rdfTypeName, 20);
    }
    rdfTypeInfo.setValues(values);
    for (TripleInfo triple : triples) {
        rdfTypeInfo.addTriple(triple);
        if (triple.isLiteralType() && (!triple.getObjectType().equals(RdfConstants.BLANK_OBJECT_TYPE))) {
            String typeLiteral = rdfTypeName + "/" + triple.getPredicate();
            Set<String> exampleValues = null;
            if (completeSetOfValuesForLiteral.contains(typeLiteral)) {
                exampleValues = getValuesForTriple(rdfTypeName, triple.getPredicate(), Integer.MAX_VALUE);
            } else {
                exampleValues = getValuesForTriple(rdfTypeName, triple.getPredicate(), 50);
            }
            triple.setValues(exampleValues);
        }
    }
    return rdfTypeInfo;
}
Also used : RdfTypeInfo(org.nextprot.api.rdf.domain.RdfTypeInfo) TripleInfo(org.nextprot.api.rdf.domain.TripleInfo)

Aggregations

RdfTypeInfo (org.nextprot.api.rdf.domain.RdfTypeInfo)4 TripleInfo (org.nextprot.api.rdf.domain.TripleInfo)3 NextProtException (org.nextprot.api.commons.exception.NextProtException)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 Cacheable (org.springframework.cache.annotation.Cacheable)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1