Example #1
0
def query(yaml: str,
          output_dir: str,
          query_key: str = 'query',
          endpoint_key: str = 'endpoint',
          outfile_ext: str = ".tsv") -> None:
    """Perform a query of knowledge graph using a class contained in query_utils

    Args:
        yaml: A YAML file containing a SPARQL query (see queries/sparql/ for examples)
        output_dir: Directory to output results of query
        query_key: the key in the yaml file containing the query string
        endpoint_key: the key in the yaml file containing the sparql endpoint URL
        outfile_ext: file extension for output file [.tsv]
    Returns:
        None.

    """
    query = parse_query_yaml(yaml)
    result_dict = run_query(query=query[query_key],
                            endpoint=query[endpoint_key])
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    outfile = os.path.join(
        output_dir,
        os.path.splitext(os.path.basename(yaml))[0] + outfile_ext)
    result_dict_to_tsv(result_dict, outfile)
Example #2
0
    def test_result_dict_to_tsv_makes_correct_file(self):
        result_dict = load_obj(self.test_result_dict_file)
        result_dict_to_tsv(result_dict, self.tempfile)

        df = pd.read_csv(self.tempfile, sep="\t")
        self.assertEqual((18, 2), df.shape)
        self.assertEqual(['v1', 'v0'], list(df.columns))
        self.assertEqual([10384, 'human_phenotype'], list(df.iloc[1]))
Example #3
0
def query(yaml: str, output_dir: str,
          query_key: str='query', endpoint_key: str='endpoint',
          outfile_ext: str=".tsv") -> None:
    """Perform a query of knowledge graph using a class contained in query_utils

    Args:
        yaml: A rq file containing a SPARQL query in grlc format:
        https://github.com/CLARIAH/grlc/blob/master/README.md
        output_dir: Directory to output results of query
        query_key: the key in the yaml file containing the query string
        endpoint_key: the key in the yaml file containing the sparql endpoint URL
        outfile_ext: file extension for output file [.tsv]
    Returns:
        None.

    """
    query = parse_query_rq(yaml)
    result_dict = run_query(query=query[query_key], endpoint=query[endpoint_key])
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    outfile = os.path.join(output_dir, os.path.splitext(os.path.basename(yaml))[0] +
                           outfile_ext)
    result_dict_to_tsv(result_dict, outfile)
Example #4
0
 def test_result_dict_to_tsv_makes_file(self):
     result_dict = load_obj(self.test_result_dict_file)
     result_dict_to_tsv(result_dict, self.tempfile)
     self.assertTrue(os.path.isfile(self.tempfile))