Relationship processing code from SCF custom Ontology module
<?php
db_query('delete from {ontology_relationships}');
$bio_process_vocab = taxonomy_extra_get_vocab(6);
$cell_components_vocab = taxonomy_extra_get_vocab(7);
$molec_functions_vocab = taxonomy_extra_get_vocab(8);
$term_glossary = array(); // format: term name => taxonomy ID
$term_index = array(); // format: GO ID term => taxonomy ID
foreach ($bio_process_vocab as $id => $term) {
$term_glossary[$term->name] = $id;
}
foreach ($cell_components_vocab as $id => $term) {
$term_glossary[$term->name] = $id;
}
foreach ($molec_functions_vocab as $id => $term) {
$term_glossary[$term->name] = $id;
}
unset($bio_process_vocab);
unset($cell_components_vocab);
unset($molec_functions_vocab);
$term_relations = array(); // format: GO ID of term => [GO ID of related term => relationship]
$file = $form_state['upload_file'];
$handle = fopen($file->filepath, 'r');
// parse the file into term_index and term_relations
$current_id = '';
while ($line = fgets($handle)) {
if (trim($line) == '[Typedef]') break;
if (preg_match('/^id: (.*?)$/', $line, $matches)) {
// set the ID of the current term we're looking at
$current_id = $matches[1];
}
else if (preg_match('/^name: (.*?)$/', $line, $matches)) {
// use the name to look up the taxonomy ID of this term
if (!isset($term_index[$current_id])) {
if (!isset($term_glossary[$matches[1]])) continue;
$term_index[$current_id] = $term_glossary[$matches[1]];
}
}
else if (preg_match('/^is_a: (.*?) !/', $line, $matches)) {
// create a relationship here
$term_relations[$current_id][$matches[1]] = 'is_a';
}
else if (preg_match('/^relationship: ([a-z_]+?) (.+?) !/', $line, $matches)) {
$term_relations[$current_id][$matches[2]] = $matches[1];
}
}
// go through term_index and term_relations and insert into the db
foreach ($term_relations as $subject => $relations) {
foreach ($relations as $object => $predicate) {
if (!isset($term_index[$subject])) continue;
if (!isset($term_index[$object])) continue;
db_query('insert into {ontology_relationships}(subject_id, predicate, object_id) values(%d, \'%s\', %d)', $term_index[$subject], $predicate, $term_index[$object]);
}
}
drupal_set_message('The ontology relationships have been imported.');
?>
Comments
Post new comment