123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233 |
- <?php
- function tripal_cv_obo_form($form, &$form_state) {
-
- $sql = "SELECT * FROM {tripal_cv_obo} ORDER BY name";
- $results = db_query($sql);
- $obos = array();
- $obos[] = '';
- foreach ($results as $obo) {
- $obos[$obo->obo_id] = $obo->name;
- }
- $form['obo_existing'] = array(
- '#type' => 'fieldset',
- '#title' => t('Use a Saved Ontology OBO Reference')
- );
- $form['obo_new'] = array(
- '#type' => 'fieldset',
- '#title' => t('Use a New Ontology OBO Reference')
- );
- $form['obo_existing']['existing_instructions']= array(
- '#value' => t('The Ontology OBO files listed in the drop down below have been automatically added upon
- installation of the Tripal CV module or were added from a previous upload. Select
- an OBO, then click the submit button to load the vocabulary into the database. If the
- vocabularies already exist then the ontology will be updated.'),
- );
- $form['obo_existing']['obo_id'] = array(
- '#title' => t('Ontology OBO File Reference'),
- '#type' => 'select',
- '#options' => $obos,
- );
- $form['obo_new']['path_instructions']= array(
- '#value' => t('Provide the name and path for the OBO file. If the vocabulary OBO file
- is stored local to the server provide a file name. If the vocabulry is stored remotely,
- provide a URL. Only provide a URL or a local file, not both.'),
- );
- $form['obo_new']['obo_name']= array(
- '#type' => 'textfield',
- '#title' => t('New Vocabulary Name'),
- '#description' => t('Please provide a name for this vocabulary. After upload, this name will appear in the drop down
- list above for use again later.'),
- );
- $form['obo_new']['obo_url']= array(
- '#type' => 'textfield',
- '#title' => t('Remote URL'),
- '#description' => t('Please enter a URL for the online OBO file. The file will be downloaded and parsed.
- (e.g. http://www.obofoundry.org/ro/ro.obo'),
- );
- $form['obo_new']['obo_file']= array(
- '#type' => 'textfield',
- '#title' => t('Local File'),
- '#description' => t('Please enter the full system path for an OBO definition file, or a path within the Drupal
- installation (e.g. /sites/default/files/xyz.obo). The path must be accessible to the
- server on which this Drupal instance is running.'),
- );
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Submit'),
- '#executes_submit_callback' => TRUE,
- );
- $form['#redirect'] = 'admin/tripal/tripal_cv/obo_loader';
- return $form;
- }
- function tripal_cv_obo_form_submit($form, &$form_state) {
- $obo_id = $form_state['values']['obo_id'];
- $obo_name = trim($form_state['values']['obo_name']);
- $obo_url = trim($form_state['values']['obo_url']);
- $obo_file = trim($form_state['values']['obo_file']);
- tripal_submit_obo_job(array(
- 'obo_id' => $obo_id,
- 'name' => $obo_name,
- 'url' => $obo_url,
- 'file' => $obo_file
- ));
- }
- function tripal_cv_load_obo_v1_2_id($obo_id, $jobid = NULL) {
-
- $sql = "SELECT * FROM {tripal_cv_obo} WHERE obo_id = :obo_id";
- $obo = db_query($sql, array(':obo_id' => $obo_id))->fetchObject();
-
- if (preg_match("/\{(.*?)\}/", $obo->path, $matches)) {
- $module = $matches[1];
- $path = drupal_realpath(drupal_get_path('module', $module));
- $obo->path = preg_replace("/\{.*?\}/", $path, $obo->path);
- }
-
- if (preg_match("/^http:\/\//", $obo->path) or preg_match("/^ftp:\/\//", $obo->path)) {
- tripal_cv_load_obo_v1_2_url($obo->name, $obo->path, $jobid, 0);
- }
-
- else {
-
- $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $obo->path;
- if (file_exists($dfile)) {
- tripal_cv_load_obo_v1_2_file($obo->name, $dfile , $jobid, 0);
- }
-
-
- else {
- if (file_exists($obo->path)) {
- tripal_cv_load_obo_v1_2_file($obo->name, $obo->path, $jobid, 0);
- }
- else {
- print "ERROR: counld not find OBO file: '$obo->path'\n";
- }
- }
- }
- }
- function tripal_cv_load_obo_v1_2_file($obo_name, $file, $jobid = NULL, $is_new = TRUE) {
- $newcvs = array();
- if ($is_new) {
- tripal_insert_obo($obo_name, $file);
- }
- $success = tripal_cv_load_obo_v1_2($file, $jobid, $newcvs);
- if ($success) {
-
- tripal_cv_load_update_cvtermpath($newcvs, $jobid);
- print "\nDone\n";
- }
- }
- function tripal_cv_load_obo_v1_2_url($obo_name, $url, $jobid = NULL, $is_new = TRUE) {
- $newcvs = array();
-
- $temp = tempnam(sys_get_temp_dir(), 'obo_');
- print "Downloading URL $url, saving to $temp\n";
- $url_fh = fopen($url, "r");
- $obo_fh = fopen($temp, "w");
- if (!$url_fh) {
- tripal_cv_obo_quiterror("Unable to download the remote OBO file at $url. Could a firewall be blocking outgoing connections? " .
- " if you are unable to download the file you may manually downlod the OBO file and use the web interface to " .
- " specify the location of the file on your server.");
- }
- while (!feof($url_fh)) {
- fwrite($obo_fh, fread($url_fh, 255), 255);
- }
- fclose($url_fh);
- fclose($obo_fh);
- if ($is_new) {
- tripal_insert_obo($obo_name, $url);
- }
-
- $success = tripal_cv_load_obo_v1_2($temp, $jobid, $newcvs);
- if ($success) {
-
- tripal_cv_load_update_cvtermpath($newcvs, $jobid);
- print "Done\n";
- }
-
- unlink($temp);
- }
- function tripal_cv_load_update_cvtermpath($newcvs, $jobid) {
- print "\nUpdating cvtermpath table. This may take a while...\n";
- foreach ($newcvs as $namespace => $cvid) {
- tripal_update_cvtermpath($cvid, $jobid);
- }
- }
- function tripal_cv_load_obo_v1_2($file, $jobid = NULL, &$newcvs) {
- $transaction = db_transaction();
- print "\nNOTE: Loading of this OBO file is performed using a database transaction. \n" .
- "If the load fails or is terminated prematurely then the entire set of \n" .
- "insertions/updates is rolled back and will not be found in the database\n\n";
- try {
- $header = array();
-
- $ret = array();
-
- $sql = "DELETE FROM {tripal_obo_temp}";
- chado_query($sql);
- print "Step 1: Preloading File $file\n";
-
- if (!tripal_insert_db(array('name' => 'internal'))) {
- tripal_cv_obo_quiterror("Cannot add 'internal' database");
- }
- if (!tripal_insert_db(array('name' => '_global'))) {
- tripal_cv_obo_quiterror("Cannot add '_global' database");
- }
-
- $default_db = tripal_cv_obo_parse($file, $header, $jobid);
-
-
-
-
- if (array_key_exists('default-namespace', $header)) {
- $defaultcv = tripal_insert_cv($header['default-namespace'][0], '');
- if (!$defaultcv) {
- tripal_cv_obo_quiterror('Cannot add namespace ' . $header['default-namespace'][0]);
- }
- $newcvs[$header['default-namespace'][0]] = $defaultcv->cv_id;
- }
-
- else {
-
-
- if (array_key_exists('ontology', $header)) {
- $defaultcv = tripal_insert_cv(strtoupper($header['ontology'][0]), '');
- if (!$defaultcv) {
- tripal_cv_obo_quiterror('Cannot add namespace ' . strtoupper($header['ontology'][0]));
- }
- $newcvs[strtoupper(strtoupper($header['ontology'][0]))] = $defaultcv->cv_id;
- }
- else {
- $defaultcv = tripal_insert_cv('_global', '');
- $newcvs['_global'] = $defaultcv->cv_id;
- }
- watchdog('t_obo_loader', "This OBO is missing the 'default-namespace' header. It is not possible to determine which vocabulary terms without a 'namespace' key should go. Instead, those terms will be placed in the '%vocab' vocabulary.",
- array('%vocab' => $defaultcv->name), WATCHDOG_WARNING);
- }
-
- print "\nStep 2: Loading type defs...\n";
- tripal_cv_obo_load_typedefs($defaultcv, $newcvs, $default_db, $jobid);
-
- print "\nStep 3: Loading terms...\n";
- if (!tripal_cv_obo_process_terms($defaultcv, $jobid, $newcvs, $default_db)) {
- tripal_cv_obo_quiterror('Cannot add terms from this ontology');
- }
- }
- catch (Exception $e) {
- $transaction->rollback();
- print "\n";
- print "FAILED. Rolling back database changes...\n";
- watchdog_exception('T_obo_loader', $e);
- return FALSE;
- }
- return TRUE;
- }
- function tripal_cv_obo_quiterror($message) {
- tripal_report_error("T_obo_loader", TRIPAL_ERROR, $message, array());
- exit;
- }
- function tripal_cv_obo_load_typedefs($defaultcv, $newcvs, $default_db, $jobid) {
- $sql = "SELECT * FROM {tripal_obo_temp} WHERE type = 'Typedef' ";
- $typedefs = chado_query($sql);
- $sql = "
- SELECT count(*) as num_terms
- FROM {tripal_obo_temp}
- WHERE type = 'Typedef'
- ";
- $result = chado_query($sql)->fetchObject();
- $count = $result->num_terms;
-
- $interval = intval($count * 0.0001);
- if ($interval < 1) {
- $interval = 1;
- }
- $i = 0;
- foreach ($typedefs as $typedef) {
- $term = unserialize(base64_decode($typedef->stanza));
-
- if ($jobid and $i % $interval == 0) {
- $complete = ($i / $count) * 33.33333333;
- tripal_set_job_progress($jobid, intval($complete + 33.33333333));
- printf("%d of %d records. (%0.2f%%) Memory: %s bytes\r", $i, $count, $complete * 3, number_format(memory_get_usage()));
- }
- tripal_cv_obo_process_term($term, $defaultcv->name, 1, $newcvs, $default_db);
- $i++;
- }
-
- if ($jobid) {
- if ($count > 0) {
- $complete = ($i / $count) * 33.33333333;
- }
- else {
- $complete = 33.33333333;
- }
- tripal_set_job_progress($jobid, intval($complete + 33.33333333));
- printf("%d of %d records. (%0.2f%%) Memory: %s bytes\r", $i, $count, $complete * 3, number_format(memory_get_usage()));
- }
- return 1;
- }
- function tripal_cv_obo_process_terms($defaultcv, $jobid = NULL, &$newcvs, $default_db) {
- $i = 0;
-
- $sql = "
- SELECT * FROM {tripal_obo_temp}
- WHERE type = 'Term'
- ORDER BY id
- ";
- $terms = chado_query($sql);
- $sql = "
- SELECT count(*) as num_terms
- FROM {tripal_obo_temp}
- WHERE type = 'Term'
- ";
- $result = chado_query($sql)->fetchObject();
- $count = $result->num_terms;
-
- $interval = intval($count * 0.0001);
- if ($interval < 1) {
- $interval = 1;
- }
- foreach ($terms as $t) {
- $term = unserialize(base64_decode($t->stanza));
-
- if ($jobid and $i % $interval == 0) {
- $complete = ($i / $count) * 33.33333333;
- tripal_set_job_progress($jobid, intval($complete + 66.666666));
- printf("%d of %d records. (%0.2f%%) Memory: %s bytes\r", $i, $count, $complete * 3, number_format(memory_get_usage()));
- }
-
- if (!tripal_cv_obo_process_term($term, $defaultcv->name, 0, $newcvs, $default_db)) {
- tripal_cv_obo_quiterror("Failed to process terms from the ontology");
- }
- $i++;
- }
-
- if ($jobid) {
- if ($count > 0) {
- $complete = ($i / $count) * 33.33333333;
- }
- else {
- $complete = 33.33333333;
- }
- tripal_set_job_progress($jobid, intval($complete + 66.666666));
- printf("%d of %d records. (%0.2f%%) Memory: %s bytes\r", $i, $count, $complete * 3, number_format(memory_get_usage()));
- }
- return 1;
- }
- function tripal_cv_obo_process_term($term, $defaultcv, $is_relationship = 0, &$newcvs, $default_db) {
-
- if (!array_key_exists('namespace', $term) and !($defaultcv or $defaultcv == '')) {
- tripal_cv_obo_quiterror("Cannot add the term: no namespace defined. " . $term['id'][0]);
- }
-
-
- $t = array();
- $t['id'] = $term['id'][0];
- $t['name'] = $term['name'][0];
- if (array_key_exists('def', $term)) {
- $t['definition'] = $term['def'][0];
- }
- if (array_key_exists('subset', $term)) {
- $t['subset'] = $term['subset'][0];
- }
- if (array_key_exists('namespace', $term)) {
- $t['namespace'] = $term['namespace'][0];
- }
- if (array_key_exists('is_obsolete', $term)) {
- $t['is_obsolete'] = $term['is_obsolete'][0];
- }
- $t['cv_name'] = $defaultcv;
- $t['is_relationship'] = $is_relationship;
- $t['db_name'] = $default_db;
-
- $cvterm = tripal_insert_cvterm($t, array('update_existing' => TRUE));
- if (!$cvterm) {
- tripal_cv_obo_quiterror("Cannot add the term " . $term['id'][0]);
- }
- if (array_key_exists('namespace', $term)) {
- $newcvs[$term['namespace'][0]] = $cvterm->cv_id;
- }
-
- if (array_key_exists('is_anonymous', $term)) {
-
- }
- if (array_key_exists('alt_id', $term)) {
- foreach ($term['alt_id'] as $alt_id) {
- if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $alt_id)) {
- tripal_cv_obo_quiterror("Cannot add alternate id $alt_id");
- }
- }
- }
- if (array_key_exists('subset', $term)) {
-
- }
-
- if (array_key_exists('synonym', $term)) {
- if (!tripal_cv_obo_add_synonyms($term, $cvterm)) {
- tripal_cv_obo_quiterror("Cannot add synonyms");
- }
- }
-
-
- if (array_key_exists('exact_synonym', $term) or array_key_exists('narrow_synonym', $term) or array_key_exists('broad_synonym', $term)) {
- if (array_key_exists('exact_synonym', $term)) {
- foreach ($term['exact_synonym'] as $synonym) {
- $new = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 EXACT $2', $synonym);
- $term['synonym'][] = $new;
- }
- }
- if (array_key_exists('narrow_synonym', $term)) {
- foreach ($term['narrow_synonym'] as $synonym) {
- $new = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 NARROW $2', $synonym);
- $term['synonym'][] = $new;
- }
- }
- if (array_key_exists('broad_synonym', $term)) {
- foreach ($term['broad_synonym'] as $synonym) {
- $new = preg_replace('/^\s*(\".+?\")(.*?)$/', '$1 BROAD $2', $synonym);
- $term['synonym'][] = $new;
- }
- }
- if (!tripal_cv_obo_add_synonyms($term, $cvterm)) {
- tripal_cv_obo_quiterror("Cannot add/update synonyms");
- }
- }
-
- if (array_key_exists('comment', $term)) {
- $comments = $term['comment'];
- $j = 0;
- foreach ($comments as $comment) {
- if (!tripal_cv_obo_add_cvterm_prop($cvterm, 'comment', $comment, $j)) {
- tripal_cv_obo_quiterror("Cannot add/update cvterm property");
- }
- $j++;
- }
- }
-
- if (array_key_exists('xref', $term)) {
- foreach ($term['xref'] as $xref) {
- if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref)) {
- tripal_cv_obo_quiterror("Cannot add/update cvterm database reference (dbxref).");
- }
- }
- }
- if (array_key_exists('xref_analog', $term)) {
- foreach ($term['xref_analog'] as $xref) {
- if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref)) {
- tripal_cv_obo_quiterror("Cannot add/update cvterm database reference (dbxref).");
- }
- }
- }
- if (array_key_exists('xref_unk', $term)) {
- foreach ($term['xref_unk'] as $xref) {
- if (!tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref)) {
- tripal_cv_obo_quiterror("Cannot add/update cvterm database reference (dbxref).");
- }
- }
- }
-
- if (array_key_exists('is_a', $term)) {
- foreach ($term['is_a'] as $is_a) {
- if (!tripal_cv_obo_add_relationship($cvterm, $defaultcv, 'is_a', $is_a, $is_relationship, $default_db)) {
- tripal_cv_obo_quiterror("Cannot add relationship is_a: $is_a");
- }
- }
- }
- if (array_key_exists('intersection_of', $term)) {
-
- }
- if (array_key_exists('union_of', $term)) {
-
- }
- if (array_key_exists('disjoint_from', $term)) {
-
- }
- if (array_key_exists('relationship', $term)) {
- foreach ($term['relationship'] as $value) {
- $rel = preg_replace('/^(.+?)\s.+?$/', '\1', $value);
- $object = preg_replace('/^.+?\s(.+?)$/', '\1', $value);
- if (!tripal_cv_obo_add_relationship($cvterm, $defaultcv, $rel, $object, $is_relationship, $default_db)) {
- tripal_cv_obo_quiterror("Cannot add relationship $rel: $object");
- }
- }
- }
- if (array_key_exists('replaced_by', $term)) {
-
- }
- if (array_key_exists('consider', $term)) {
-
- }
- if (array_key_exists('use_term', $term)) {
-
- }
- if (array_key_exists('builtin', $term)) {
-
- }
- return 1;
- }
- function tripal_cv_obo_add_relationship($cvterm, $defaultcv, $rel,
- $objname, $object_is_relationship = 0, $default_db = 'OBO_REL') {
-
- $term = array(
- 'name' => $rel,
- 'id' => "$default_db:$rel",
- 'definition' => '',
- 'is_obsolete' => 0,
- 'cv_name' => $defaultcv,
- 'is_relationship' => TRUE,
- 'db_naame' => $default_db
- );
- $relcvterm = tripal_insert_cvterm($term, array('update_existing' => FALSE));
- if (!$relcvterm) {
-
-
- $term = array(
- 'name' => $rel,
- 'id' => "OBO_REL:$rel",
- 'definition' => '',
- 'is_obsolete' => 0,
- 'cv_name' => $defaultcv,
- 'is_relationship' => TRUE,
- 'db_name' => 'OBO_REL'
- );
- $relcvterm = tripal_insert_cvterm($term, array('update_existing' => FALSE));
- if (!$relcvterm) {
- tripal_cv_obo_quiterror("Cannot find the relationship term in the current ontology or in the relationship ontology: $rel\n");
- }
- }
-
- $oterm = tripal_cv_obo_get_term($objname);
- if (!$oterm) {
- tripal_cv_obo_quiterror("Could not find object term $objname\n");
- }
- $objterm = array();
- $objterm['id'] = $oterm['id'][0];
- $objterm['name'] = $oterm['name'][0];
- if (array_key_exists('def', $oterm)) {
- $objterm['definition'] = $oterm['def'][0];
- }
- if (array_key_exists('subset', $oterm)) {
- $objterm['subset'] = $oterm['subset'][0];
- }
- if (array_key_exists('namespace', $oterm)) {
- $objterm['namespace'] = $oterm['namespace'][0];
- }
- if (array_key_exists('is_obsolete', $oterm)) {
- $objterm['is_obsolete'] = $oterm['is_obsolete'][0];
- }
- $objterm['cv_name' ] = $defaultcv;
- $objterm['is_relationship'] = $object_is_relationship;
- $objterm['db_name'] = $default_db;
- $objcvterm = tripal_insert_cvterm($objterm, array('update_existing' => TRUE));
- if (!$objcvterm) {
- tripal_cv_obo_quiterror("Cannot add cvterm " . $oterm['name'][0]);
- }
-
- $values = array(
- 'type_id' => $relcvterm->cvterm_id,
- 'subject_id' => $cvterm->cvterm_id,
- 'object_id' => $objcvterm->cvterm_id
- );
- $result = chado_select_record('cvterm_relationship', array('*'), $values);
- if (count($result) == 0) {
- $options = array('return_record' => FALSE);
- $success = chado_insert_record('cvterm_relationship', $values, $options);
- if (!$success) {
- tripal_cv_obo_quiterror("Cannot add term relationship: '$cvterm->name' $rel '$objcvterm->name'");
- }
- }
- return TRUE;
- }
- function tripal_cv_obo_get_term($id) {
- $values = array('id' => $id);
- $result = chado_select_record('tripal_obo_temp', array('stanza'), $values);
- if (count($result) == 0) {
- return FALSE;
- }
- return unserialize(base64_decode($result[0]->stanza));
- }
- function tripal_cv_obo_add_synonyms($term, $cvterm) {
-
- $syncv = tripal_insert_cv('synonym_type', 'A vocabulary added by the Tripal CV module OBO loader for storing synonym types.');
-
- if (array_key_exists('synonym', $term)) {
- foreach ($term['synonym'] as $synonym) {
-
- $def = preg_replace('/^\s*"(.*)"\s*.*$/', '\1', $synonym);
-
- $scope = drupal_strtolower(preg_replace('/^.*"\s+(.*?)\s+.*$/', '\1', $synonym));
- if (!$scope) {
- $scope = 'exact';
- }
-
- $values = array(
- 'name' => $scope,
- 'cv_id' => array(
- 'name' => 'synonym_type',
- ),
- );
- $syntype = tripal_get_cvterm($values);
-
- if (!$syntype) {
-
- $term = array(
- 'name' => $scope,
- 'id' => "internal:$scope",
- 'definition' => '',
- 'is_obsolete' => 0,
- 'cv_name' => $syncv->name,
- 'is_relationship' => FALSE
- );
- $syntype = tripal_insert_cvterm($term, array('update_existing' => TRUE));
- if (!$syntype) {
- tripal_cv_obo_quiterror("Cannot add synonym type: internal:$scope");
- }
- }
-
- $values = array(
- 'cvterm_id' => $cvterm->cvterm_id,
- 'synonym' => $def
- );
- $results = chado_select_record('cvtermsynonym', array('*'), $values);
- if (count($results) == 0) {
- $values = array(
- 'cvterm_id' => $cvterm->cvterm_id,
- 'synonym' => $def,
- 'type_id' => $syntype->cvterm_id
- );
- $options = array('return_record' => FALSE);
- $success = chado_insert_record('cvtermsynonym', $values, $options);
- if (!$success) {
- tripal_cv_obo_quiterror("Failed to insert the synonym for term: $name ($def)");
- }
- }
-
-
-
-
-
- }
- }
- return TRUE;
- }
- function tripal_cv_obo_parse($obo_file, &$header, $jobid) {
- $in_header = 1;
- $stanza = array();
- $default_db = '_global';
- $line_num = 0;
- $num_read = 0;
- $intv_read = 0;
- $filesize = filesize($obo_file);
- $interval = intval($filesize * 0.01);
- if ($interval < 1) {
- $interval = 1;
- }
-
- $fh = fopen($obo_file, 'r');
- while ($line = fgets($fh)) {
- $line_num++;
- $size = drupal_strlen($line);
- $num_read += $size;
- $intv_read += $size;
- $line = trim($line);
-
- if ($jobid and $intv_read >= $interval) {
- $percent = sprintf("%.2f", ($num_read / $filesize) * 100);
- print "Parsing Line $line_num (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
- tripal_set_job_progress($jobid, intval(($num_read / $filesize) * 33.33333333));
- $intv_read = 0;
- }
-
- $line = rtrim($line);
-
- $line = preg_replace('/[^(\x20-\x7F)]*/', '', $line);
-
- if (strcmp($line, '') == 0) {
- continue;
- }
-
- $line = preg_replace('/^(.*?)\!.*$/', '\1', $line);
-
- if (preg_match('/^\s*\[/', $line)) {
- $in_header = 0;
-
- if (sizeof($stanza) > 0) {
-
- $values = array(
- 'id' => $stanza['id'][0],
- 'stanza' => base64_encode(serialize($stanza)),
- 'type' => $type,
- );
- $success = chado_insert_record('tripal_obo_temp', $values);
- if (!$success) {
- tripal_report_error('T_obo_loader', "ERROR: Cannot insert stanza into temporary table.", array(), 'error');
- exit;
- }
- }
-
- $type = preg_replace('/^\s*\[\s*(.+?)\s*\]\s*$/', '\1', $line);
-
- $stanza = array();
- continue;
- }
-
- preg_replace("/\\:/", "|-|-|", $line);
- $pair = explode(":", $line, 2);
- $tag = $pair[0];
- $value = ltrim(rtrim($pair[1]));
-
- $matches = array();
- if ($tag == 'id' and preg_match('/^(.+?):.*$/', $value, $matches)) {
- $default_db = $matches[1];
- }
- $tag = preg_replace("/\|-\|-\|/", "\:", $tag);
- $value = preg_replace("/\|-\|-\|/", "\:", $value);
- if ($in_header) {
- if (!array_key_exists($tag, $header)) {
- $header[$tag] = array();
- }
- $header[$tag][] = $value;
- }
- else {
- if (!array_key_exists($tag, $stanza)) {
- $stanza[$tag] = array();
- }
- $stanza[$tag][] = $value;
- }
- }
-
- if (sizeof($stanza) > 0) {
- $values = array(
- 'id' => $stanza['id'][0],
- 'stanza' => base64_encode(serialize($stanza)),
- 'type' => $type,
- );
- chado_insert_record('tripal_obo_temp', $values);
- if (!$success) {
- tripal_report_error('T_obo_loader', "ERROR: Cannot insert stanza into temporary table.", array(), 'error');
- exit;
- }
- $percent = sprintf("%.2f", ($num_read / $filesize) * 100);
- print "Parsing Line $line_num (" . $percent . "%). Memory: " . number_format(memory_get_usage()) . " bytes.\r";
- tripal_set_job_progress($jobid, intval(($num_read / $filesize) * 33.33333333));
- }
- return $default_db;
- }
- function tripal_cv_obo_add_cvterm_dbxref($cvterm, $xref) {
- $dbname = preg_replace('/^(.+?):.*$/', '$1', $xref);
- $accession = preg_replace('/^.+?:\s*(.*?)(\{.+$|\[.+$|\s.+$|\".+$|$)/', '$1', $xref);
- $description = preg_replace('/^.+?\"(.+?)\".*?$/', '$1', $xref);
- $dbxrefs = preg_replace('/^.+?\[(.+?)\].*?$/', '$1', $xref);
- if (!$accession) {
- tripal_cv_obo_quiterror();
- tripal_report_error("T_obo_loader", TRIPAL_WARNING, "Cannot add a dbxref without an accession: '$xref'", NULL);
- return FALSE;
- }
-
- if (strcmp($dbname, 'http') == 0) {
- $accession = $xref;
- $dbname = 'URL';
- }
-
- $db = tripal_insert_db(array('name' => $dbname));
- if (!$db) {
- tripal_cv_obo_quiterror("Cannot find database '$dbname' in Chado.");
- }
-
- $dbxref = tripal_cv_obo_add_dbxref($db->db_id, $accession, '', $description);
- if (!$dbxref) {
- tripal_cv_obo_quiterror("Cannot find or add the database reference (dbxref)");
- }
-
- $values = array(
- 'cvterm_id' => $cvterm->cvterm_id,
- 'dbxref_id' => $dbxref->dbxref_id,
- );
- $result = chado_select_record('cvterm_dbxref', array('*'), $values);
- if (count($result) == 0) {
- $ins_options = array('return_record' => FALSE);
- $result = chado_insert_record('cvterm_dbxref', $values, $ins_options);
- if (!$result) {
- tripal_cv_obo_quiterror("Cannot add cvterm_dbxref: $xref");
- return FALSE;
- }
- }
- return TRUE;
- }
- function tripal_cv_obo_add_cvterm_prop($cvterm, $property, $value, $rank) {
-
- $cv = tripal_insert_cv('cvterm_property_type', '');
- if (!$cv) {
- tripal_cv_obo_quiterror("Cannot add/find cvterm_property_type cvterm");
- }
-
- $values = array(
- 'name' => $property,
- 'cv_id' => $cv->cv_id,
- );
- $results = chado_select_record('cvterm', array('*'), $values);
- if (count($results) == 0) {
- $term = array(
- 'name' => $property,
- 'id' => "internal:$property",
- 'definition' => '',
- 'is_obsolete' => 0,
- 'cv_name' => $cv->name,
- 'is_relationship' => FALSE,
- );
- $cvproptype = tripal_insert_cvterm($term, array('update_existing' => FALSE));
- if (!$cvproptype) {
- tripal_cv_obo_quiterror("Cannot add cvterm property: internal:$property");
- return FALSE;
- }
- }
- else {
- $cvproptype = $results[0];
- }
-
- if ($rank == 0) {
- $values = array('cvterm_id' => $cvterm->cvterm_id);
- $success = chado_delete_record('cvtermprop', $values);
- if (!$success) {
- tripal_cv_obo_quiterror("Could not remove existing properties to update property $property for term\n");
- return FALSE;
- }
- }
-
- $values = array(
- 'cvterm_id' => $cvterm->cvterm_id,
- 'type_id' => $cvproptype->cvterm_id,
- 'value' => $value,
- 'rank' => $rank,
- );
- $options = array('return_record' => FALSE);
- $result = chado_insert_record('cvtermprop', $values, $options);
- if (!$result) {
- tripal_cv_obo_quiterror("Could not add property $property for term\n");
- return FALSE;
- }
- return TRUE;
- }
- function tripal_cv_obo_add_dbxref($db_id, $accession, $version='', $description='') {
-
- $values = array(
- 'db_id' => $db_id,
- 'accession' => $accession,
- );
- $result = chado_select_record('dbxref', array('dbxref_id'), $values);
- if (count($result) == 0) {
- $ins_values = array(
- 'db_id' => $db_id,
- 'accession' => $accession,
- 'version' => $version,
- 'description' => $description,
- );
- $ins_options = array('return_record' => FALSE);
- $result = chado_insert_record('dbxref', $ins_values, $ins_options);
- if (!$result) {
- tripal_cv_obo_quiterror("Failed to insert the dbxref record $accession");
- return FALSE;
- }
- $result = chado_select_record('dbxref', array('dbxref_id'), $values, $options);
- }
- return $result[0];
- }
|