User login

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.');
?>

Resolution

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.