<commit_before>#!/usr/bin/env perl
# Copyright [1999-2014] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#      http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# Modified from "Relational Modeling of
# Biological Data: Trees and Graphs", page 2:
# http://www.oreillynet.com/pub/a/network/2002/11/27/bioconf.html?page=2

use strict;
use warnings;

use DBI;
use Getopt::Long qw( :config no_ignore_case );

#-----------------------------------------------------------------------

sub usage {
  print("Usage:\n");
  printf( "\t%s\t-h dbhost [-P dbport] \\\n"
      . "\t%s\t-u dbuser [-p dbpass] \\\n"
      . "\t%2\$s\t-d dbname\n",
    $0, ' ' x length($0) );
  print("\n");
  printf( "\t%s\t-?\n", $0 );
  print("\n");
  print("Arguments:\n");
  print("\t-h/--host dbhost\tDatabase server host name\n");
  print("\t-P/--port dbport\tDatabase server port (optional)\n");
  print("\t-u/--user dbuser\tDatabase user name\n");
  print("\t-p/--pass dbpass\tUser password (optional)\n");
  print("\t-d/--name dbname\tDatabase name\n");
  print("\t-?/--help\t\tDisplays this information\n");
}

#-----------------------------------------------------------------------

my ( $dbhost, $dbport );
my ( $dbuser, $dbpass );
my $dbname;

$dbport = '3306';

if (
  !GetOptions(
    'dbhost|host|h=s' => \$dbhost,
    'dbport|port|P=i' => \$dbport,
    'dbuser|user|u=s' => \$dbuser,
    'dbpass|pass|p=s' => \$dbpass,
    'dbname|name|d=s' => \$dbname,
    'help|?'          => sub { usage(); exit } )
  || !defined($dbhost)
  || !defined($dbuser)
  || !defined($dbname) )
{
  usage();
  exit;
}

my $dsn = sprintf( "DBI:mysql:database=%s;host=%s;port=%s",
  $dbname, $dbhost, $dbport );

my $dbh =
  DBI->connect( $dsn, $dbuser, $dbpass,
  { 'RaiseError' => 1, 'PrintError' => 1 } );

$dbh->do('TRUNCATE TABLE closure');
$dbh->do('ALTER TABLE closure DISABLE KEYS');

$dbh->do(
  q(
INSERT INTO closure
  (child_term_id, parent_term_id, distance, subparent_term_id, ontology_id)
SELECT  term_id, term_id, 0, NULL, ontology_id
FROM    term
 WHERE  is_obsolete = 0
) );

$dbh->do(
  q(
INSERT IGNORE INTO closure
  (child_term_id, parent_term_id, distance, subparent_term_id, ontology_id)
SELECT term_id, term_id, 0, NULL, r.ontology_id
FROM   term t, relation r
WHERE  term_id = child_term_id
AND    t.ontology_id != r.ontology_id
AND    is_obsolete = 0
) );

$dbh->do(
  q(
INSERT IGNORE INTO closure
  (child_term_id, parent_term_id, distance, subparent_term_id, ontology_id)
SELECT DISTINCT child_term_id, parent_term_id, 1, child_term_id, ontology_id
FROM    relation r,
        relation_type rt
WHERE rt.name IN (
    'is_a', 'part_of' -- in both GO and SO
    -- THE FOLLOWING ARE REMOVED FOR NOW
    -- 'has_part', 'derives_from', 'member_of' -- in SO only
  )
  AND r.relation_type_id = rt.relation_type_id
) );

my $select_sth = $dbh->prepare(
  q(
SELECT DISTINCT
        child.child_term_id,
        parent.parent_term_id,
        child.distance + 1,
        parent.child_term_id,
        child.ontology_id
FROM    closure child
  JOIN  closure parent
    ON  (parent.child_term_id = child.parent_term_id)
WHERE   child.distance  = ?
  AND   parent.distance = 1
  AND   child.ontology_id = parent.ontology_id
) );

my $insert_sth = $dbh->prepare(
  q(
REPLACE INTO closure
  (child_term_id, parent_term_id, distance, subparent_term_id, ontology_id)
VALUES (?, ?, ?, ?, ?)
) );<commit_msg>Extra relationships added to allow closures to get calculated over extra terms in FYPO<commit_after>#!/usr/bin/env perl
# Copyright [1999-2014] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#      http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# Modified from "Relational Modeling of
# Biological Data: Trees and Graphs", page 2:
# http://www.oreillynet.com/pub/a/network/2002/11/27/bioconf.html?page=2

use strict;
use warnings;

use DBI;
use Getopt::Long qw( :config no_ignore_case );

#-----------------------------------------------------------------------

sub usage {
  print("Usage:\n");
  printf( "\t%s\t-h dbhost [-P dbport] \\\n"
      . "\t%s\t-u dbuser [-p dbpass] \\\n"
      . "\t%2\$s\t-d dbname\n",
    $0, ' ' x length($0) );
  print("\n");
  printf( "\t%s\t-?\n", $0 );
  print("\n");
  print("Arguments:\n");
  print("\t-h/--host dbhost\tDatabase server host name\n");
  print("\t-P/--port dbport\tDatabase server port (optional)\n");
  print("\t-u/--user dbuser\tDatabase user name\n");
  print("\t-p/--pass dbpass\tUser password (optional)\n");
  print("\t-d/--name dbname\tDatabase name\n");
  print("\t-?/--help\t\tDisplays this information\n");
}

#-----------------------------------------------------------------------

my ( $dbhost, $dbport );
my ( $dbuser, $dbpass );
my $dbname;

$dbport = '3306';

if (
  !GetOptions(
    'dbhost|host|h=s' => \$dbhost,
    'dbport|port|P=i' => \$dbport,
    'dbuser|user|u=s' => \$dbuser,
    'dbpass|pass|p=s' => \$dbpass,
    'dbname|name|d=s' => \$dbname,
    'help|?'          => sub { usage(); exit } )
  || !defined($dbhost)
  || !defined($dbuser)
  || !defined($dbname) )
{
  usage();
  exit;
}

my $dsn = sprintf( "DBI:mysql:database=%s;host=%s;port=%s",
  $dbname, $dbhost, $dbport );

my $dbh =
  DBI->connect( $dsn, $dbuser, $dbpass,
  { 'RaiseError' => 1, 'PrintError' => 1 } );

$dbh->do('TRUNCATE TABLE closure');
$dbh->do('ALTER TABLE closure DISABLE KEYS');

$dbh->do(
  q(
INSERT INTO closure
  (child_term_id, parent_term_id, distance, subparent_term_id, ontology_id)
SELECT  term_id, term_id, 0, NULL, ontology_id
FROM    term
 WHERE  is_obsolete = 0
) );

$dbh->do(
  q(
INSERT IGNORE INTO closure
  (child_term_id, parent_term_id, distance, subparent_term_id, ontology_id)
SELECT term_id, term_id, 0, NULL, r.ontology_id
FROM   term t, relation r
WHERE  term_id = child_term_id
AND    t.ontology_id != r.ontology_id
AND    is_obsolete = 0
) );

$dbh->do(
  q(
INSERT IGNORE INTO closure
  (child_term_id, parent_term_id, distance, subparent_term_id, ontology_id)
SELECT DISTINCT child_term_id, parent_term_id, 1, child_term_id, ontology_id
FROM    relation r,
        relation_type rt
WHERE rt.name IN (
    'is_a', 'part_of', -- in both GO and SO
    'output_of', 'has_output', 'results_in_formation_of' -- Used in FYPO as a relationship
    -- THE FOLLOWING ARE REMOVED FOR NOW
    -- 'has_part', 'derives_from', 'member_of' -- in SO only
  )
  AND r.relation_type_id = rt.relation_type_id
) );

my $select_sth = $dbh->prepare(
  q(
SELECT DISTINCT
        child.child_term_id,
        parent.parent_term_id,
        child.distance + 1,
        parent.child_term_id,
        child.ontology_id
FROM    closure child
  JOIN  closure parent
    ON  (parent.child_term_id = child.parent_term_id)
WHERE   child.distance  = ?
  AND   parent.distance = 1
  AND   child.ontology_id = parent.ontology_id
) );

my $insert_sth = $dbh->prepare(
  q(
REPLACE INTO closure
  (child_term_id, parent_term_id, distance, subparent_term_id, ontology_id)
VALUES (?, ?, ?, ?, ?)
) );
{'fraction_non_alphanumeric': 0.17973733583489682, 'fraction_numerical': 0.00975609756097561, 'mean_word_length': 3.054766734279919, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6877915', 'n_tokens_mistral': 2975, 'n_tokens_neox': 2855, 'n_words': 746}