@Test
  public void testSelectToWurcsSparql() throws SparqlException, UnsupportedEncodingException {
    GlycoSequenceToWurcsSelectSparql s = new GlycoSequenceToWurcsSelectSparql("glycoct");
    SparqlEntity se = new SparqlEntity();
    se.setValue(
        GlycoSequenceToWurcsSelectSparql.FromSequence,
        "RES\n1b:a-dgal-HEX-1:5\n2s:n-acetyl\n3b:b-dgal-HEX-1:5\n4b:b-dglc-HEX-1:5\n5s:n-acetyl\n6b:b-dgal-HEX-1:5\n7b:a-lgal-HEX-1:5|6:d\n8b:b-dglc-HEX-1:5\n9s:n-acetyl\n10b:b-dglc-HEX-1:5\n11s:n-acetyl\n12b:b-dgal-HEX-1:5\n13b:a-lgal-HEX-1:5|6:d\nLIN\n1:1d(2+1)2n\n2:1o(3+1)3d\n3:3o(3+1)4d\n4:4d(2+1)5n\n5:4o(4+1)6d\n6:6o(2+1)7d\n7:3o(6+1)8d\n8:8d(2+1)9n\n9:1o(6+1)10d\n10:10d(2+1)11n\n11:10o(4+1)12d\n12:12o(2+1)13d"
            .replaceAll("\n", "\\\\n"));
    s.setSparqlEntity(se);
    logger.debug(s.getSparql());
    Query query =
        QueryFactory.create(s.getSparql().replaceAll("null", "").replace("?Sequence", ""));
    //        QueryExecution qe =
    // QueryExecutionFactory.sparqlService("http://localhost:3030/glycobase/query",query);
    QueryExecution qe =
        QueryExecutionFactory.sparqlService("http://test.ts.glytoucan.org/sparql", query);
    ResultSet rs = qe.execSelect();

    List<SparqlEntity> results = new ArrayList<SparqlEntity>();

    while (rs.hasNext()) {
      QuerySolution row = rs.next();
      Iterator<String> columns = row.varNames();
      SparqlEntity se2 = new SparqlEntity();
      while (columns.hasNext()) {
        String column = columns.next();
        RDFNode cell = row.get(column);

        if (cell.isResource()) {
          Resource resource = cell.asResource();
          // do something maybe with the OntModel???
          if (resource.isLiteral()) se.setValue(column, resource.asLiteral().getString());
          else se.setValue(column, resource.toString());
        } else if (cell.isLiteral()) {
          se.setValue(column, cell.asLiteral().getString());
        } else if (cell.isAnon()) {
          se.setValue(column, "anon");
        } else {
          se.setValue(column, cell.toString());
        }
      }
      results.add(se);
    }

    for (SparqlEntity entity : results) {
      System.out.println("results: " + entity.getValue("PrimaryId"));
    }
  }
  public BedStats findHospitalAvailableBedsAllClinics(String hospital) {
    ApplicationContext appContext = new ClassPathXmlApplicationContext();
    org.springframework.core.io.Resource resource =
        appContext.getResource("classpath:sparqlQueries/BedsAvailabilityQuery");
    try {
      InputStream is = resource.getInputStream();
      BufferedReader br = new BufferedReader(new InputStreamReader(is));

      String line;
      queryTemplate = "";
      while ((line = br.readLine()) != null) {
        queryTemplate += line + "\n";
      }
      br.close();
    } catch (IOException e1) {
      e1.printStackTrace();
      return null;
    }

    List<String> params = new ArrayList<String>();
    params.add(hospital);
    params.add(hospital);
    String sparqlQuery = prepareQuery(queryTemplate, params);
    System.out.println(sparqlQuery);
    Query query = QueryFactory.create(sparqlQuery);
    QueryExecution qexec = QueryExecutionFactory.sparqlService(endpoint, query);
    ResultSet results = qexec.execSelect();
    if (results.hasNext()) {
      QuerySolution soln = results.next();

      Literal available = soln.getLiteral("available"); // Get a result variable - must be a literal
      Literal deployed = soln.getLiteral("deployed");
      Literal supplementary = soln.getLiteral("supplementary");
      // Literal lastDate = soln.getLiteral("maxDate");
      BedStats bedStats = new BedStats();
      bedStats.setHospitalName(params.get(0));
      bedStats.setAvailabeBeds(available.getInt());
      bedStats.setDeployedBeds(deployed.getInt());
      bedStats.setSupplementaryBeds(supplementary.getInt());
      // bedStats.setLastDate(lastDate.getShort());
      return bedStats;
    } else {
      return null;
    }
  }
  public static List<String> listThingDescriptions(String query) {
    List<String> tds = new ArrayList<>();

    Dataset dataset = Repository.get().dataset;
    dataset.begin(ReadWrite.READ);

    try {
      String q = "SELECT DISTINCT ?g WHERE { GRAPH ?g { " + query + " }}";
      try (QueryExecution qexec = QueryExecutionFactory.create(q, dataset)) {
        ResultSet result = qexec.execSelect();
        while (result.hasNext()) {
          tds.add(result.next().get("g").asResource().getURI());
        }
      }
    } finally {
      dataset.end();
    }

    return tds;
  }
  /** {@inheritDoc} */
  @Override
  protected Collection<TestCaseResult> executeSingleTest(TestSource testSource, TestCase testCase)
      throws TestCaseExecutionException {

    Collection<TestCaseResult> testCaseResults = new ArrayList<>();
    PropertyValuePairSet.PropertyValuePairSetBuilder annotationSetBuilder =
        PropertyValuePairSet.builder();

    try (QueryExecution qe =
        testSource
            .getExecutionFactory()
            .createQueryExecution(queryGenerationFactory.getSparqlQuery(testCase))) {

      ResultSet results = qe.execSelect();

      ExtendedTestCaseResultImpl.Builder resultBuilder = null;
      String prevResource = "";

      while (results.hasNext()) {

        QuerySolution qs = results.next();

        String resource = qs.get("this").toString();
        if (qs.get("this").isLiteral()) {
          resource = StringUtils.getHashFromString(resource);
        }
        String message = testCase.getResultMessage();
        if (qs.contains("message")) {
          message = qs.get("message").toString();
        }
        RLOGLevel logLevel = testCase.getLogLevel();

        // If resource != before
        // we add the previous result in the list
        if (!prevResource.equals(resource)) {
          // The very first time we enter, result = null and we don't add any result
          if (resultBuilder != null) {
            testCaseResults.add(
                resultBuilder
                    .setResultAnnotations(annotationSetBuilder.build().getAnnotations())
                    .build());
          }

          resultBuilder =
              new ExtendedTestCaseResultImpl.Builder(
                  testCase.getTestURI(), logLevel, message, resource);

          annotationSetBuilder = PropertyValuePairSet.builder(); // reset

          // get static annotations for new test
          for (ResultAnnotation resultAnnotation : testCase.getResultAnnotations()) {
            // Get values
            if (resultAnnotation.getAnnotationValue().isPresent()) {
              annotationSetBuilder.annotation(
                  PropertyValuePair.create(
                      resultAnnotation.getAnnotationProperty(),
                      resultAnnotation.getAnnotationValue().get()));
            }
          }
        }

        // result must be initialized by now
        checkNotNull(resultBuilder);

        // get annotations from the SPARQL query
        for (ResultAnnotation resultAnnotation : testCase.getVariableAnnotations()) {
          // Get the variable name
          if (resultAnnotation.getAnnotationVarName().isPresent()) {
            String variable = resultAnnotation.getAnnotationVarName().get().trim();
            // If it exists, add it in the Set
            if (qs.contains(variable)) {
              annotationSetBuilder.annotation(
                  PropertyValuePair.create(
                      resultAnnotation.getAnnotationProperty(), qs.get(variable)));
            }
          }
        }
      }
      // Add last result (if query return any)
      if (resultBuilder != null) {
        testCaseResults.add(
            resultBuilder
                .setResultAnnotations(annotationSetBuilder.build().getAnnotations())
                .build());
      }
    } catch (QueryExceptionHTTP e) {
      checkQueryResultStatus(e);
    }

    return testCaseResults;
  }
  @Test
  public void testKBtoWurcsSparqlTranslation() throws SparqlException {

    List<Translation> translations = Ebean.find(Translation.class).findList();
    HashSet<String> resultList = new HashSet<>();

    String ct = "";

    for (Translation translation : translations) {
      System.out.println("id check " + translation.id + " ct " + translation.ct);
      if (translation.ct == null) continue;

      if (translation.structure.id > 0) {

        ct = translation.ct;

        GlycoSequenceToWurcsSelectSparql s = new GlycoSequenceToWurcsSelectSparql("glycoct");
        SparqlEntity se = new SparqlEntity();
        ct = StringUtils.chomp(ct);
        System.out.println("ct on top: " + ct);
        if (ct != null) {
          se.setValue(
              GlycoSequenceToWurcsSelectSparql.FromSequence,
              ct.replaceAll("\n", "\\\\n")
                  .replaceAll("x\\(", "u\\(")
                  .replaceAll("\\)x", "\\)u")
                  .trim());
          s.setSparqlEntity(se);
          logger.debug(s.getSparql());

          Query query =
              QueryFactory.create(s.getSparql().replaceAll("null", "").replace("?Sequence", ""));
          System.out.println(
              "Id "
                  + translation.structure.id
                  + " Query: "
                  + s.getSparql().replaceAll("null", "").replace("?Sequence", ""));
          QueryExecution qe =
              QueryExecutionFactory.sparqlService("http://test.ts.glytoucan.org/sparql", query);
          ResultSet rs = qe.execSelect();

          List<SparqlEntity> results = new ArrayList<>();
          HashSet<String> resultsList = new HashSet<>();

          while (rs.hasNext()) {
            QuerySolution row = rs.next();
            Iterator<String> columns = row.varNames();
            SparqlEntity se2 = new SparqlEntity();
            while (columns.hasNext()) {
              String column = columns.next();
              RDFNode cell = row.get(column);

              if (cell.isResource()) {
                Resource resource = cell.asResource();
                // do something maybe with the OntModel???
                if (resource.isLiteral()) se.setValue(column, resource.asLiteral().getString());
                else se.setValue(column, resource.toString());
              } else if (cell.isLiteral()) {
                se.setValue(column, cell.asLiteral().getString());
              } else if (cell.isAnon()) {
                se.setValue(column, "anon");
              } else {
                se.setValue(column, cell.toString());
              }
            }
            results.add(se);
          }

          for (SparqlEntity entity : results) {
            // System.out.println("results: " + entity.getValue("PrimaryId"));
            resultList.add(
                translation.structure.id + "\t" + entity.getValue("PrimaryId").toString());
          }
        }
      }
    }

    for (String c : resultList) {
      System.out.println(c);
    }
  }
  @Test
  public void testKBtoWurcsSparql() throws SparqlException {

    List<Structure> structures = Ebean.find(Structure.class).findList();
    HashSet<String> resultList = new HashSet<>();

    String ct = "";

    for (Structure structure : structures) {
      if (structure.id >= 7400) {

        if (structure.glycanst.startsWith("v--")) {
          structure.glycanst = structure.glycanst.replace("v--", "FreeEnd--");
        }

        if (structure.glycanst.startsWith("FreenEnd")) {
          structure.glycanst = structure.glycanst.replace("FreenEnd", "FreeEnd");
        }

        if (structure.glycanst.startsWith("FreeEnd?")) {
          structure.glycanst = structure.glycanst.replace("FreeEnd?", "FreeEnd--?");
        }

        if (structure.glycanst.startsWith("<Gly") || structure.glycanst.contains("0.0000u")) {
          continue;
        }

        System.out.println(structure.getGlycanst());

        BuilderWorkspace workspace = new BuilderWorkspace(new GlycanRendererAWT());
        workspace.setNotation("cfg"); // cfgbw | uoxf | uoxfcol | text
        GlycanRenderer renderer = workspace.getGlycanRenderer();
        org.eurocarbdb.application.glycanbuilder.Glycan glycan =
            org.eurocarbdb.application.glycanbuilder.Glycan.fromString(structure.glycanst.trim());
        if (glycan != null) {
          ct = glycan.toGlycoCTCondensed();
          System.out.println("this was the ct: " + ct);
          GlycoSequenceToWurcsSelectSparql s = new GlycoSequenceToWurcsSelectSparql("glycoct");
          SparqlEntity se = new SparqlEntity();
          ct = StringUtils.chomp(ct);
          se.setValue(
              GlycoSequenceToWurcsSelectSparql.FromSequence,
              ct.replaceAll("\n", "\\\\n")
                  .replaceAll("x\\(", "u\\(")
                  .replaceAll("\\)x", "\\)u")
                  .trim());
          s.setSparqlEntity(se);
          logger.debug(s.getSparql());

          Query query =
              QueryFactory.create(s.getSparql().replaceAll("null", "").replace("?Sequence", ""));
          System.out.println(
              "Id "
                  + structure.id
                  + " Query: "
                  + s.getSparql().replaceAll("null", "").replace("?Sequence", ""));
          QueryExecution qe =
              QueryExecutionFactory.sparqlService("http://test.ts.glytoucan.org/sparql", query);
          ResultSet rs = qe.execSelect();

          List<SparqlEntity> results = new ArrayList<>();
          HashSet<String> resultsList = new HashSet<>();

          while (rs.hasNext()) {
            QuerySolution row = rs.next();
            Iterator<String> columns = row.varNames();
            SparqlEntity se2 = new SparqlEntity();
            while (columns.hasNext()) {
              String column = columns.next();
              RDFNode cell = row.get(column);

              if (cell.isResource()) {
                Resource resource = cell.asResource();
                // do something maybe with the OntModel???
                if (resource.isLiteral()) se.setValue(column, resource.asLiteral().getString());
                else se.setValue(column, resource.toString());
              } else if (cell.isLiteral()) {
                se.setValue(column, cell.asLiteral().getString());
              } else if (cell.isAnon()) {
                se.setValue(column, "anon");
              } else {
                se.setValue(column, cell.toString());
              }
            }
            results.add(se);
          }

          for (SparqlEntity entity : results) {
            // System.out.println("results: " + entity.getValue("PrimaryId"));
            resultList.add(structure.id + "\t" + entity.getValue("PrimaryId").toString());
          }
        }
      }
    }
    PrintWriter writer = null;
    try {
      writer =
          new PrintWriter(
              new OutputStreamWriter(new FileOutputStream("/tmp/HashSet.txt"), "UTF-8"));
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    for (String c : resultList) {
      System.out.println(c);
      writer.println(c);
    }
  }