gff_loader.inc 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. * @defgroup gff3_loader GFF3 Feature Loader
  8. * @{
  9. * Provides gff3 loading functionality. Creates features based on their specification in a GFF3 file.
  10. * @}
  11. * @ingroup tripal_feature
  12. */
  13. /**
  14. *
  15. *
  16. * @ingroup gff3_loader
  17. */
  18. function tripal_feature_gff3_load_form() {
  19. $form['gff_file']= array(
  20. '#type' => 'textfield',
  21. '#title' => t('GFF3 File'),
  22. '#description' => t('Please enter the full system path for the GFF file, or a path within the Drupal
  23. installation (e.g. /sites/default/files/xyz.gff). The path must be accessible to the
  24. server on which this Drupal instance is running.'),
  25. '#required' => TRUE,
  26. '#weight' => 1
  27. );
  28. // get the list of organisms
  29. $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  30. $previous_db = tripal_db_set_active('chado'); // use chado database
  31. $org_rset = db_query($sql);
  32. tripal_db_set_active($previous_db); // now use drupal database
  33. $organisms = array();
  34. $organisms[''] = '';
  35. while ($organism = db_fetch_object($org_rset)) {
  36. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  37. }
  38. $form['organism_id'] = array(
  39. '#title' => t('Organism'),
  40. '#type' => t('select'),
  41. '#description' => t("Choose the organism to which these sequences are associated"),
  42. '#required' => TRUE,
  43. '#options' => $organisms,
  44. );
  45. $form['import_options'] = array(
  46. '#type' => 'fieldset',
  47. '#title' => t('Import Options'),
  48. '#weight' => 6,
  49. '#collapsed' => TRUE
  50. );
  51. $form['import_options']['add_only']= array(
  52. '#type' => 'checkbox',
  53. '#title' => t('Import only new features'),
  54. '#required' => FALSE,
  55. '#description' => t('The job will skip features in the GFF file that already
  56. exist in the database and import only new features.'),
  57. '#weight' => 2
  58. );
  59. $form['import_options']['update']= array(
  60. '#type' => 'checkbox',
  61. '#title' => t('Import all and update'),
  62. '#required' => FALSE,
  63. '#default_value' => 'checked',
  64. '#description' => t('Existing features will be updated and new features will be added. Attributes
  65. for a feature that are not present in the GFF but which are present in the
  66. database will not be altered.'),
  67. '#weight' => 3
  68. );
  69. $form['import_options']['refresh']= array(
  70. '#type' => 'checkbox',
  71. '#title' => t('Import all and replace'),
  72. '#required' => FALSE,
  73. '#description' => t('Existing features will be updated and feature properties not
  74. present in the GFF file will be removed.'),
  75. '#weight' => 4
  76. );
  77. $form['import_options']['remove']= array(
  78. '#type' => 'checkbox',
  79. '#title' => t('Delete features'),
  80. '#required' => FALSE,
  81. '#description' => t('Features present in the GFF file that exist in the database
  82. will be removed rather than imported'),
  83. '#weight' => 5
  84. );
  85. $form['analysis'] = array(
  86. '#type' => 'fieldset',
  87. '#title' => t('Analysis Used to Derive Features'),
  88. '#weight' => 6,
  89. '#collapsed' => TRUE
  90. );
  91. $form['analysis']['desc'] = array(
  92. '#type' => 'markup',
  93. '#value' => t("Why specify an analysis for a data load? All data comes
  94. from some place, even if downloaded from Genbank. By specifying
  95. analysis details for all data uploads, it allows an end user to reproduce the
  96. data set, but at least indicates the source of the data."),
  97. );
  98. // get the list of analyses
  99. $sql = "SELECT * FROM {analysis} ORDER BY name";
  100. $previous_db = tripal_db_set_active('chado'); // use chado database
  101. $org_rset = db_query($sql);
  102. tripal_db_set_active($previous_db); // now use drupal database
  103. $analyses = array();
  104. $analyses[''] = '';
  105. while ($analysis = db_fetch_object($org_rset)) {
  106. $analyses[$analysis->analysis_id] = "$analysis->name ($analysis->program $analysis->programversion, $analysis->sourcename)";
  107. }
  108. $form['analysis']['analysis_id'] = array(
  109. '#title' => t('Analysis'),
  110. '#type' => t('select'),
  111. '#description' => t("Choose the analysis to which these features are associated"),
  112. '#required' => TRUE,
  113. '#options' => $analyses,
  114. );
  115. $form['button'] = array(
  116. '#type' => 'submit',
  117. '#value' => t('Import GFF3 file'),
  118. '#weight' => 10,
  119. );
  120. return $form;
  121. }
  122. /**
  123. *
  124. *
  125. * @ingroup gff3_loader
  126. */
  127. function tripal_feature_gff3_load_form_validate($form, &$form_state) {
  128. $gff_file = $form_state['values']['gff_file'];
  129. $organism_id = $form_state['values']['organism_id'];
  130. $add_only = $form_state['values']['add_only'];
  131. $update = $form_state['values']['update'];
  132. $refresh = $form_state['values']['refresh'];
  133. $remove = $form_state['values']['remove'];
  134. // check to see if the file is located local to Drupal
  135. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $gff_file;
  136. if (!file_exists($dfile)) {
  137. // if not local to Drupal, the file must be someplace else, just use
  138. // the full path provided
  139. $dfile = $gff_file;
  140. }
  141. if (!file_exists($dfile)) {
  142. form_set_error('gff_file', t("Cannot find the file on the system. Check that the file exists or that the web server has permissions to read the file."));
  143. }
  144. // @coder-ignore: there are no functions being called here
  145. // @todo: break each line of this conditional into separate variables to make more readable
  146. if (($add_only AND ($update OR $refresh OR $remove)) OR
  147. ($update AND ($add_only OR $refresh OR $remove)) OR
  148. ($refresh AND ($update OR $add_only OR $remove)) OR
  149. ($remove AND ($update OR $refresh OR $add_only))) {
  150. form_set_error('add_only', t("Please select only one checkbox from the import options section"));
  151. }
  152. }
  153. /**
  154. *
  155. * @ingroup gff3_loader
  156. */
  157. function tripal_feature_gff3_load_form_submit($form, &$form_state) {
  158. global $user;
  159. $gff_file = $form_state['values']['gff_file'];
  160. $organism_id = $form_state['values']['organism_id'];
  161. $add_only = $form_state['values']['add_only'];
  162. $update = $form_state['values']['update'];
  163. $refresh = $form_state['values']['refresh'];
  164. $remove = $form_state['values']['remove'];
  165. $analysis_id = $form_state['values']['analysis_id'];
  166. $args = array($gff_file, $organism_id, $analysis_id, $add_only, $update, $refresh, $remove);
  167. $type = '';
  168. if ($add_only) {
  169. $type = 'import only new features';
  170. }
  171. if ($update) {
  172. $type = 'import all and update';
  173. }
  174. if ($refresh) {
  175. $type = 'import all and replace';
  176. }
  177. if ($remove) {
  178. $type = 'delete features';
  179. }
  180. $fname = preg_replace("/.*\/(.*)/", "$1", $gff_file);
  181. tripal_add_job("$type GFF3 file: $fname", 'tripal_feature',
  182. 'tripal_feature_load_gff3', $args, $user->uid);
  183. return '';
  184. }
  185. /**
  186. *
  187. *
  188. * @ingroup gff3_loader
  189. */
  190. function tripal_feature_load_gff3($gff_file, $organism_id, $analysis_id,
  191. $add_only =0, $update = 0, $refresh = 0, $remove = 0, $job = NULL) {
  192. // Prepare log
  193. $filename = preg_replace("/.*\/(.*)/", "$1", $blastfile);
  194. $logfile = tempnam(sys_get_temp_dir(), "gff_loader_log.");
  195. $log = fopen($logfile, 'a'); // append parsing results to log file
  196. if (!$log) {
  197. print "ERROR: cannot open log file: $logfile\n";
  198. exit;
  199. }
  200. print "Writing to log file: $logfile\n";
  201. fwrite($log, date("D M j G:i:s Y") . ". Loading $gff_file\n");
  202. // this array is used to cache all of the features in the GFF file and
  203. // used to lookup parent and target relationships
  204. $gff_features = array();
  205. // check to see if the file is located local to Drupal
  206. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $gff_file;
  207. if (!file_exists($dfile)) {
  208. // if not local to Drupal, the file must be someplace else, just use
  209. // the full path provided
  210. $dfile = $gff_file;
  211. }
  212. if (!file_exists($dfile)) {
  213. print "ERROR: cannot find the file: $dfile\n";
  214. return 0;
  215. }
  216. //$previous_db = tripal_db_set_active('chado');
  217. print "Opening $gff_file\n";
  218. //$lines = file($dfile,FILE_SKIP_EMPTY_LINES);
  219. $fh = fopen($dfile, 'r');
  220. if (!$fh) {
  221. print "ERROR: cannot open file: $dfile\n";
  222. return 0;
  223. }
  224. $filesize = filesize($dfile);
  225. // get the controlled vocaubulary that we'll be using. The
  226. // default is the 'sequence' ontology
  227. // @coder-ignore: non-drupal schema thus table prefixing does not apply
  228. $sql = "SELECT * FROM cv WHERE name = '%s'";
  229. $cv = db_fetch_object(db_query($sql, 'sequence'));
  230. if (!$cv) {
  231. print "ERROR: cannot find the 'sequence' ontology\n";
  232. return '';
  233. }
  234. // get the organism for which this GFF3 file belongs
  235. // @coder-ignore: non-drupal schema thus table prefixing does not apply
  236. $sql = "SELECT * FROM organism WHERE organism_id = %d";
  237. $organism = db_fetch_object(db_query($sql, $organism_id));
  238. $interval = intval($filesize * 0.01);
  239. if ($interval == 0) {
  240. $interval = 1;
  241. }
  242. $in_fasta = 0;
  243. $line_num = 0;
  244. $num_read = 0;
  245. $intv_read = 0;
  246. // iterate through each line of the GFF file
  247. print "Parsing Line $line_num (0.00%).\r";
  248. while ($line = fgets($fh)) {
  249. $line_num++;
  250. $num_read += drupal_strlen($line);
  251. $intv_read += $num_read;
  252. // update the job status every 1% features
  253. if ($job and $intv_read >= $interval) {
  254. $intv_read = 0;
  255. $percent = sprintf("%.2f", ($num_read / $filesize) * 100);
  256. print "Parsing Line $line_num (" . $percent . "%).\r";
  257. fwrite($log, "Parsing line $line_num (" . $percent . "%).\n");
  258. tripal_job_set_progress($job, intval(($num_read / $filesize) * 100));
  259. }
  260. // check to see if we have FASTA section, if so then set the variable
  261. // to start parsing
  262. if (preg_match('/^##FASTA/i', $line)) {
  263. $in_fasta = 1;
  264. break;
  265. }
  266. // skip comments
  267. if (preg_match('/^#/', $line)) {
  268. continue;
  269. }
  270. // skip empty lines
  271. if (preg_match('/^\s*$/', $line)) {
  272. continue;
  273. }
  274. // TODO: handle FASTA section
  275. // get the columns
  276. $cols = explode("\t", $line);
  277. if (sizeof($cols) != 9) {
  278. fwrite($log, "ERROR: improper number of columns on line $line_num\n");
  279. fwrite($log, print_r($cols,1));
  280. return '';
  281. }
  282. // get the column values
  283. $landmark = $cols[0];
  284. $source = $cols[1];
  285. $type = $cols[2];
  286. $start = $cols[3];
  287. $end = $cols[4];
  288. $score = $cols[5];
  289. $strand = $cols[6];
  290. $phase = $cols[7];
  291. $attrs = explode(";", $cols[8]); // split by a semicolon
  292. // ready the start and stop for chado. Chado expects these positions
  293. // to be zero-based, so we substract 1 from the fmin
  294. $fmin = $start - 1;
  295. $fmax = $end;
  296. if ($end < $start) {
  297. $fmin = $end - 1;
  298. $fmax = $start;
  299. }
  300. // format the strand for chado
  301. if (strcmp($strand, '.') == 0) {
  302. $strand = 0;
  303. }
  304. elseif (strcmp($strand, '+') == 0) {
  305. $strand = 1;
  306. }
  307. elseif (strcmp($strand, '-') == 0) {
  308. $strand = -1;
  309. }
  310. if (strcmp($phase, '.') == 0) {
  311. $phase = '';
  312. }
  313. // get the type record
  314. if (!tripal_core_is_sql_prepared('sel_cvterm_cvid_cvtname_synonym')) {
  315. $psql = "PREPARE sel_cvterm_cvid_cvtname_synonym (int, text, text) AS
  316. SELECT CVT.cvterm_id, CVT.cv_id, CVT.name, CVT.definition,
  317. CVT.dbxref_id, CVT.is_obsolete, CVT.is_relationshiptype
  318. FROM {cvterm} CVT
  319. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  320. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  321. WHERE CV.cv_id = $1 and (CVT.name = $2 or CVTS.synonym = $3)";
  322. $status = chado_query($psql);
  323. if (!$status) {
  324. fwrite($log, "ERROR: cannot prepare statement 'sel_cvterm_cvid_cvtname_synonym' for ontology term $line_num\n");
  325. return '';
  326. }
  327. }
  328. $result = chado_query("EXECUTE sel_cvterm_cvid_cvtname_synonym (%d, '%s', '%s')", $cv->cv_id, $type, $type);
  329. $cvterm = db_fetch_object($result);
  330. if (!$cvterm) {
  331. fwrite($log, "ERROR: cannot find ontology term '$type' on line $line_num.\n");
  332. return '';
  333. }
  334. // break apart each of the attributes
  335. $tags = array();
  336. $attr_name = '';
  337. $attr_uniquename = '';
  338. $attr_residue_info = '';
  339. $attr_locgroup = 0;
  340. $attr_fmin_partial = 'f';
  341. $attr_fmax_partial = 'f';
  342. $attr_is_obsolete = 'f';
  343. $attr_is_analysis = 'f';
  344. $attr_others = '';
  345. $residues = '';
  346. foreach ($attrs as $attr) {
  347. $attr = rtrim($attr);
  348. $attr = ltrim($attr);
  349. if (strcmp($attr, '')==0) {
  350. continue;
  351. }
  352. if (!preg_match('/^[^\=]+\=.+$/', $attr)) {
  353. fwrite($log, "ERROR: attribute is not correctly formatted on line $line_num: $attr\n");
  354. return '';
  355. }
  356. // break apart each tag
  357. $tag = preg_split("/=/", $attr, 2); // split by equals sign
  358. // multiple instances of an attribute are separated by commas
  359. $tag_name = $tag[0];
  360. if (!array_key_exists($tag_name, $tags)) {
  361. $tags[$tag_name] = array();
  362. }
  363. $tags[$tag_name] = array_merge($tags[$tag_name], explode(",", $tag[1])); // split by comma
  364. // replace the URL escape codes for each tag
  365. for ($i = 0; $i < count($tags[$tag_name]); $i++) {
  366. $tags[$tag_name][$i] = urldecode($tags[$tag_name][$i]);
  367. }
  368. // get the name and ID tags
  369. if (strcmp($tag_name, 'ID') == 0) {
  370. $attr_uniquename = $tag[1];
  371. }
  372. elseif (strcmp($tag_name, 'Name') == 0) {
  373. $attr_name = $tag[1];
  374. }
  375. // get the list of non-reserved attributes
  376. elseif (strcmp($tag_name, 'Alias') !=0 and strcmp($tag_name, 'Parent') !=0 and
  377. strcmp($tag_name, 'Target') !=0 and strcmp($tag_name, 'Gap') !=0 and
  378. strcmp($tag_name, 'Derives_from') !=0 and strcmp($tag_name, 'Note') !=0 and
  379. strcmp($tag_name, 'Dbxref') !=0 and strcmp($tag_name, 'Ontology_term') !=0 and
  380. strcmp($tag_name, 'Is_circular') !=0) {
  381. foreach ($tags[$tag_name] as $value){
  382. $attr_others[$tag_name][] = $value;
  383. }
  384. }
  385. }
  386. // if neither name nor uniquename are provided then generate one
  387. if (!$attr_uniquename and !$attr_name) {
  388. if (array_key_exists('Parent', $tags)) {
  389. $attr_uniquename = $tags['Parent'][0] . "-$type-$landmark:$fmin..$fmax";
  390. }
  391. else {
  392. fwrite($log, "ERROR: cannot generate a uniquename for feature on line $line_num\n");
  393. exit;
  394. }
  395. $attr_name = $attr_uniquename;
  396. }
  397. // if a name is not specified then use the unique name
  398. if (strcmp($attr_name, '')==0) {
  399. $attr_name = $attr_uniquename;
  400. }
  401. // if an ID attribute is not specified then use the attribute name and
  402. // hope for the best
  403. if (!$attr_uniquename) {
  404. $attr_uniquename = $attr_name;
  405. }
  406. // make sure the landmark sequence exists in the database. We don't
  407. // know the type of the landmark so we'll hope that it's unique across
  408. // all types. If not we'll error out. This test is only necessary if
  409. // if the landmark and the uniquename are different. If they are the same
  410. // then this is the information for the landmark
  411. if (strcmp($landmark, $attr_uniquename) != 0 ) {
  412. $select = array(
  413. 'organism_id' => $organism_id,
  414. 'uniquename' => $landmark,
  415. );
  416. $columns = array('count(*) as num_landmarks');
  417. $options = array('statement_name' => 'sel_feature_organismid_uniquename');
  418. $count = tripal_core_chado_select('feature', $columns, $select, $options);
  419. if (!$count or $count[0]->num_landmarks == 0) {
  420. fwrite($log, "ERROR: the landmark '$landmark' cannot be found for this organism. ".
  421. "Please add the landmark and then retry the import of this GFF3 ".
  422. "file.\n");
  423. return '';
  424. }
  425. if ($count[0]->num_landmarks > 1) {
  426. fwrite($log, "ERROR: the landmark '$landmark' is not unique for this organism. ".
  427. "The features cannot be associated.\n");
  428. return '';
  429. }
  430. }
  431. // if the option is to remove or refresh then we want to remove
  432. // the feature from the database.
  433. if ($remove or $refresh) {
  434. fwrite($log, "Removing feature '$attr_uniquename'\n");
  435. $sql = "DELETE FROM {feature}
  436. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  437. $match = array(
  438. 'organism_id' => $organism->organism_id,
  439. 'uniquename' => $attr_uniquename,
  440. 'type_id' => $cvterm->cvterm_id
  441. );
  442. $result = tripal_core_chado_delete('feature',$match);
  443. if (!$result) {
  444. fwrite($log, "ERROR: cannot delete feature $attr_uniquename\n");
  445. }
  446. $feature = 0;
  447. unset($result);
  448. }
  449. // add or update the feature and all properties
  450. if ($update or $refresh or $add_only) {
  451. // add/update the feature
  452. $feature = tripal_feature_load_gff3_feature($organism, $analysis_id, $cvterm,
  453. $attr_uniquename, $attr_name, $residues, $attr_is_analysis,
  454. $attr_is_obsolete, $add_only, $score, $log);
  455. // store all of the features for use later by parent and target
  456. // relationships
  457. $gff_features[$feature->uniquename]['type'] = $type;
  458. $gff_features[$feature->uniquename]['strand'] = $strand;
  459. if ($feature) {
  460. // add/update the featureloc if the landmark and the ID are not the same
  461. // if they are the same then this entry in the GFF is probably a landmark identifier
  462. if (strcmp($landmark, $attr_uniquename) !=0 ) {
  463. tripal_feature_load_gff3_featureloc($feature, $organism,
  464. $landmark, $fmin, $fmax, $strand, $phase, $attr_fmin_partial,
  465. $attr_fmax_partial, $attr_residue_info, $attr_locgroup, $log);
  466. }
  467. // add any aliases for this feature
  468. if (array_key_exists('Alias', $tags)) {
  469. tripal_feature_load_gff3_alias($feature, $tags['Alias'], $log);
  470. }
  471. // add any dbxrefs for this feature
  472. if (array_key_exists('Dbxref', $tags)) {
  473. tripal_feature_load_gff3_dbxref($feature, $tags['Dbxref'], $log);
  474. }
  475. // add any ontology terms for this feature
  476. if (array_key_exists('Ontology_term', $tags)) {
  477. tripal_feature_load_gff3_ontology($feature, $tags['Ontology_term'], $log);
  478. }
  479. // add parent relationships
  480. if (array_key_exists('Parent', $tags)) {
  481. tripal_feature_load_gff3_parents($feature, $cvterm, $tags['Parent'], $gff_features, $organism_id, $fmin, $log);
  482. }
  483. // add target relationships
  484. if (array_key_exists('Target', $tags)) {
  485. preg_match('/^(.*)\s+\d+\s+\d+\s+(\+|\-)*$/', $tags['Target'][0], $matches);
  486. $target_feature = $matches[1];
  487. $start = $matches[2];
  488. $end = $matches[3];
  489. if ($matches[4]) {
  490. $target_strand = $matches[4];
  491. }
  492. else {
  493. $target_strand = '.';
  494. }
  495. $target_fmin = $start - 1;
  496. $target_fmax = $end;
  497. if ($end < $start) {
  498. $target_fmin = $end - 1;
  499. $target_fmax = $start;
  500. }
  501. #print "Target: $target_feature, $target_fmin-$target_fmax $target_dir\n";
  502. tripal_feature_load_gff3_featureloc($feature, $organism,
  503. $target_feature, $target_fmin, $target_fmax, $target_strand, $phase, $attr_fmin_partial,
  504. $attr_fmax_partial, $attr_residue_info, $attr_locgroup, $log);
  505. }
  506. // add gap information. This goes in simply as a property
  507. if (array_key_exists('Gap', $tags)) {
  508. foreach ($tags['Gap'] as $value) {
  509. tripal_feature_load_gff3_property($feature, 'Gap', $value, $log);
  510. }
  511. }
  512. // add notes. This goes in simply as a property
  513. if (array_key_exists('Note', $tags)) {
  514. foreach ($tags['Note'] as $value) {
  515. tripal_feature_load_gff3_property($feature, 'Note', $value, $log);
  516. }
  517. }
  518. // add the Derives_from relationship (e.g. polycistronic genes).
  519. if (array_key_exists('Derives_from', $tags)) {
  520. tripal_feature_load_gff3_derives_from($feature, $tags['Derives_from'][0], $gff_features, $organism, $log);
  521. }
  522. // add in the GFF3_source dbxref so that GBrowse can find the feature using the source column
  523. $source_ref = array('GFF_source:' . $source);
  524. tripal_feature_load_gff3_dbxref($feature, $source_ref, $log);
  525. // add any additional attributes
  526. if ($attr_others) {
  527. foreach ($attr_others as $tag_name => $values) {
  528. foreach ($values as $value){
  529. tripal_feature_load_gff3_property($feature, $tag_name, $value, $log);
  530. }
  531. }
  532. }
  533. }
  534. }
  535. }
  536. // now set the rank of any parent/child relationships. The order is based
  537. // on the fmin. The start rank is 1. This allows features with other
  538. // relationships to be '0' (the default), and doesn't interfer with the
  539. // ordering defined here.
  540. foreach ($gff_features as $parent => $details) {
  541. // only iterate through parents that have children
  542. if (array_key_exists('children',$details)) {
  543. // get the parent
  544. $values = array(
  545. 'organism_id' => $organism->organism_id,
  546. 'uniquename' => $parent,
  547. 'type_id' => array(
  548. 'cv_id' => array(
  549. 'name' => 'sequence'
  550. ),
  551. 'name' => $details['type'],
  552. ),
  553. );
  554. $options = array('statement_name' => 'sel_feature_organismid_uniquename_typeid');
  555. $result = tripal_core_chado_select('feature', array('*'), $values, $options);
  556. $pfeature = $result[0];
  557. // sort the children by order of their fmin positions (values of assoc. array)
  558. // if the parent is on the reverse strand then sort in reverse
  559. if ($details['strand'] == -1) {
  560. arsort($details['children']);
  561. }
  562. else {
  563. asort($details['children']);
  564. }
  565. // now iterate through the children and set their rank
  566. $rank = 1;
  567. fwrite($log, "Updating child ranks for $parent (" . $details['type'] . ")\n");
  568. foreach ($details['children'] as $kfeature_id => $kfmin) {
  569. $match = array(
  570. 'object_id' => $pfeature->feature_id,
  571. 'subject_id' => $kfeature_id,
  572. 'type_id' => array(
  573. 'cv_id' => array(
  574. 'name' => 'relationship'
  575. ),
  576. 'name' => 'part_of',
  577. ),
  578. );
  579. $values = array(
  580. 'rank' => $rank,
  581. );
  582. $options = array('statement_name' => 'upd_featurerelationship_all');
  583. tripal_core_chado_update('feature_relationship', $match, $values, $options);
  584. $rank++;
  585. }
  586. }
  587. }
  588. //tripal_db_set_active($previous_db);
  589. print "Done.\nSuccessful and failed entries have been saved in the log file:\n $logfile\n";
  590. fwrite($log, "\n");
  591. fclose($log);
  592. return 1;
  593. }
  594. /**
  595. *
  596. *
  597. * @ingroup gff3_loader
  598. */
  599. function tripal_feature_load_gff3_derives_from($feature, $subject, $gff_features, $organism, $log) {
  600. // first get the subject feature
  601. $match = array(
  602. 'organism_id' => $organism->organism_id,
  603. 'uniquename' => $subject,
  604. 'type_id' => array(
  605. 'name' => $gff_features[$subject]['type'],
  606. 'cv_id' => array(
  607. 'name' => 'sequence'
  608. ),
  609. ),
  610. );
  611. $options = array('statement_name' => 'sel_feature_organismid_uniquename_typeid');
  612. $sfeature = tripal_core_chado_select('feature', array('*'), $match, $options);
  613. if (count($sfeature)==0) {
  614. fwrite($log, "ERROR: could not add 'Derives_from' relationship for $feature->uniquename and $subject. Subject feature, '$subject', cannot be found\n");
  615. return;
  616. }
  617. // now check to see if the relationship already exists
  618. $values = array(
  619. 'object_id' => $sfeature[0]->feature_id,
  620. 'subject_id' => $feature->feature_id,
  621. 'type_id' => array(
  622. 'cv_id' => array(
  623. 'name' => 'relationship'
  624. ),
  625. 'name' => 'derives_from',
  626. ),
  627. 'rank' => 0
  628. );
  629. $options = array('statement_name' => 'sel_featurerelationship_objectid_subjectid_typeid_rank');
  630. $rel = tripal_core_chado_select('feature_relationship', array('*'), $values, $options);
  631. if (count($rel) > 0) {
  632. fwrite($log, " Relationship already exists: $feature->uniquename derives_from $subject\n");
  633. return;
  634. }
  635. // finally insert the relationship if it doesn't exist
  636. $options = array('statement_name' => 'ins_featurerelationship_objectid_subjectid_typeid_rank');
  637. $ret = tripal_core_chado_insert('feature_relationship', $values, $options);
  638. if (!$ret) {
  639. fwrite($log, "ERROR: could not add 'Derives_from' relationship for $feature->uniquename and $subject\n");
  640. }
  641. else {
  642. fwrite($log, " Added relationship: $feature->uniquename derives_from $subject\n");
  643. }
  644. }
  645. /**
  646. *
  647. *
  648. * @ingroup gff3_loader
  649. */
  650. function tripal_feature_load_gff3_parents($feature, $cvterm, $parents, &$gff_features, $organism_id, $fmin, $log) {
  651. $uname = $feature->uniquename;
  652. $type = $cvterm->name;
  653. $rel_type = 'part_of';
  654. // prepare these SQL statements that will be used repeatedly.
  655. if (!tripal_core_is_sql_prepared('sel_cvterm_cvname_cvtname_synonym')) {
  656. $psql = "PREPARE sel_cvterm_cvname_cvtname_synonym (text, text, text) AS
  657. SELECT CVT.cvterm_id
  658. FROM cvterm CVT
  659. INNER JOIN cv CV on CVT.cv_id = CV.cv_id
  660. LEFT JOIN cvtermsynonym CVTS on CVTS.cvterm_id = CVT.cvterm_id
  661. WHERE cv.name = $1 and (CVT.name = $2 or CVTS.synonym = $3)";
  662. $status = chado_query($psql);
  663. if (!$status) {
  664. fwrite($log, "ERROR: cannot prepare statement 'sel_cvterm_cvname_cvtname_synonym' for ontology term\n");
  665. return '';
  666. }
  667. }
  668. // iterate through the parents in the list
  669. foreach ($parents as $parent) {
  670. $parent_type = $gff_features[$parent]['type'];
  671. // try to find the parent
  672. $parentcvterm = db_fetch_object(chado_query("EXECUTE sel_cvterm_cvname_cvtname_synonym ('%s', '%s', '%s')", 'sequence', $parent_type, $parent_type));
  673. $relcvterm = db_fetch_object(chado_query("EXECUTE sel_cvterm_cvname_cvtname_synonym ('%s', '%s', '%s')", 'relationship', $rel_type, $rel_type));
  674. $values = array(
  675. 'organism_id' => $organism_id,
  676. 'uniquename' => $parent,
  677. 'type_id' => $parentcvterm->cvterm_id,
  678. );
  679. $options = array('statement_name' => 'sel_feature_organismid_uniquename_typeid');
  680. $result = tripal_core_chado_select('feature', array('*'), $values, $options);
  681. $parent_feature = $result[0];
  682. // we want to add this feature to the child list for the parent
  683. // when the loader finishes, it will go back through the parent
  684. // features and rank the children by position
  685. $gff_features[$parent]['children'][$feature->feature_id] = $fmin;
  686. // if the parent exists then add the relationship otherwise print error and skip
  687. if ($parent_feature) {
  688. // check to see if the relationship already exists
  689. $values = array(
  690. 'object_id' => $parent_feature->feature_id,
  691. 'subject_id' => $feature->feature_id,
  692. 'type_id' => $relcvterm->cvterm_id,
  693. );
  694. $options = array('statement_name' => 'sel_featurerelationship_objectid_subjectid_typeid');
  695. $rel = tripal_core_chado_select('feature_relationship', array('*'), $values, $options);
  696. if (count($rel) > 0) {
  697. fwrite($log, " Relationship already exists, skipping '$uname' ($type) $rel_type '$parent' ($parent_type)\n");
  698. }
  699. else {
  700. // the relationship doesn't already exist, so add it.
  701. $values = array(
  702. 'subject_id' => $feature->feature_id,
  703. 'object_id' => $parent_feature->feature_id,
  704. 'type_id' => $relcvterm->cvterm_id,
  705. );
  706. $options = array('statement_name' => 'ins_featurerelationship_subjectid_objectid_typeid');
  707. $result = tripal_core_chado_insert('feature_relationship', $values, $options);
  708. if (!$result) {
  709. fwrite($log, "WARNING: failed to insert feature relationship '$uname' ($type) $rel_type '$parent' ($parent_type)\n");
  710. }
  711. else {
  712. fwrite($log, " Inserted relationship relationship: '$uname' ($type) $rel_type '$parent' ($parent_type)\n");
  713. }
  714. }
  715. }
  716. else {
  717. fwrite($log, "WARNING: cannot establish relationship '$uname' ($type) $rel_type '$parent' ($parent_type): Cannot find the parent\n");
  718. }
  719. }
  720. }
  721. /**
  722. *
  723. *
  724. * @ingroup gff3_loader
  725. */
  726. function tripal_feature_load_gff3_dbxref($feature, $dbxrefs, $log) {
  727. // iterate through each of the dbxrefs
  728. foreach ($dbxrefs as $dbxref) {
  729. // get the database name from the reference. If it doesn't exist then create one.
  730. $ref = explode(":", $dbxref);
  731. $dbname = $ref[0];
  732. $accession = $ref[1];
  733. // first look for the database name if it doesn't exist then create one.
  734. // first check for the fully qualified URI (e.g. DB:<dbname>. If that
  735. // can't be found then look for the name as is. If it still can't be found
  736. // the create the database
  737. $options = array('statement_name' => 'sel_db_name');
  738. $db = tripal_core_chado_select('db', array('db_id'), array('name' => "DB:$dbname"), $options);
  739. if (count($db) == 0) {
  740. $db = tripal_core_chado_select('db', array('db_id'), array('name' => "$dbname"), $options);
  741. }
  742. if (count($db) == 0) {
  743. $options = array('statement_name' => 'ins_db_name');
  744. $ret = tripal_core_chado_insert('db', array('name' => $dbname,
  745. 'description' => 'Added automatically by the GFF loader'), $options);
  746. if ($ret) {
  747. fwrite($log, " Added new database: $dbname\n");
  748. $options = array('statement_name' => 'sel_db_name');
  749. $db = tripal_core_chado_select('db', array('db_id'), array('name' => "$dbname"), $options);
  750. }
  751. else {
  752. fwrite($log, "ERROR: cannot find or add the database $dbname\n");
  753. exit;
  754. return 0;
  755. }
  756. }
  757. $db = $db[0];
  758. // now check to see if the accession exists
  759. $options = array('statement_name' => 'sel_dbxref_accession_dbid');
  760. $dbxref = tripal_core_chado_select('dbxref', array('dbxref_id'), array(
  761. 'accession' => $accession, 'db_id' => $db->db_id), $options);
  762. // if the accession doesn't exist then we want to add it
  763. if (sizeof($dbxref) == 0) {
  764. $options = array('statement_name' => 'ins_dbxref_dbid_accession_version');
  765. $ret = tripal_core_chado_insert('dbxref', array('db_id' => $db->db_id,
  766. 'accession' => $accession, 'version' => ''), $options);
  767. $options = array('statement_name' => 'sel_dbxref_accession_dbid');
  768. $dbxref = tripal_core_chado_select('dbxref', array('dbxref_id'), array(
  769. 'accession' => $accession, 'db_id' => $db->db_id), $options);
  770. }
  771. $dbxref = $dbxref[0];
  772. // check to see if this feature dbxref already exists
  773. $options = array('statement_name' => 'sel_featuredbxref_dbxrefid_featureid');
  774. $fdbx = tripal_core_chado_select('feature_dbxref', array('feature_dbxref_id'),
  775. array('dbxref_id' => $dbxref->dbxref_id, 'feature_id' => $feature->feature_id), $options);
  776. // now associate this feature with the database reference if it doesn't
  777. // already exist
  778. if (sizeof($fdbx)==0) {
  779. $options = array('statement_name' => 'ins_featuredbxref_dbxrefid_featureid');
  780. $ret = tripal_core_chado_insert('feature_dbxref', array(
  781. 'dbxref_id' => $dbxref->dbxref_id,
  782. 'feature_id' => $feature->feature_id), $options);
  783. if ($ret) {
  784. fwrite($log, " Adding Dbxref $dbname:$accession\n");
  785. }
  786. else {
  787. fwrite($log, "ERROR: failed to insert Dbxref: $dbname:$accession\n");
  788. return 0;
  789. }
  790. }
  791. else {
  792. fwrite($log, " Dbxref already associated, skipping $dbname:$accession\n");
  793. }
  794. }
  795. return 1;
  796. }
  797. /**
  798. *
  799. *
  800. * @ingroup gff3_loader
  801. */
  802. function tripal_feature_load_gff3_ontology($feature, $dbxrefs, $log) {
  803. // iterate through each of the dbxrefs
  804. foreach ($dbxrefs as $dbxref) {
  805. // get the database name from the reference. If it doesn't exist then create one.
  806. $ref = explode(":", $dbxref);
  807. $dbname = $ref[0];
  808. $accession = $ref[1];
  809. // first look for the database name
  810. $options = array('statement_name' => 'sel_db_name');
  811. $db = tripal_core_chado_select('db', array('db_id'), array('name' => "DB:$dbname"), $options);
  812. if (sizeof($db) == 0) {
  813. $db = tripal_core_chado_select('db', array('db_id'), array('name' => "$dbname"), $options);
  814. }
  815. if (sizeof($db) == 0) {
  816. fwrite($log, "ERROR: Database, $dbname is missing for reference: $dbname:$accession\n");
  817. return 0;
  818. }
  819. $db = $db[0];
  820. // now check to see if the accession exists
  821. $options = array('statement_name' => 'sel_dbxref_accession_dbid');
  822. $dbxref = tripal_core_chado_select('dbxref', array('dbxref_id'), array(
  823. 'accession' => $accession, 'db_id' => $db->db_id), $options);
  824. if (sizeof($dbxref) == 0) {
  825. fwrite($log, "ERROR: Accession, $accession is missing for reference: $dbname:$accession\n");
  826. return 0;
  827. }
  828. $dbxref = $dbxref[0];
  829. // now check to see if the cvterm exists
  830. $options = array('statement_name' => 'sel_cvterm_dbxrefid');
  831. $cvterm = tripal_core_chado_select('cvterm', array('cvterm_id'), array(
  832. 'dbxref_id' => $dbxref->dbxref_id), $options);
  833. // if it doesn't exist in the cvterm table, look for an alternate id
  834. if (sizeof($cvterm) == 0) {
  835. $options = array('statement_name' => 'sel_cvtermdbxref_dbxrefid');
  836. $cvterm = tripal_core_chado_select('cvterm_dbxref', array('cvterm_id'), array(
  837. 'dbxref_id' => $dbxref->dbxref_id), $options);
  838. }
  839. if (sizeof($cvterm) == 0) {
  840. fwrite($log, "ERROR: CV Term is missing for reference: $dbname:$accession\n");
  841. return 0;
  842. }
  843. $cvterm = $cvterm[0];
  844. // check to see if this feature cvterm already exists
  845. $options = array('statement_name' => 'sel_featurecvterm_cvtermid_featureid');
  846. $fcvt = tripal_core_chado_select('feature_cvterm', array('feature_cvterm_id'),
  847. array('cvterm_id' => $cvterm->cvterm_id, 'feature_id' => $feature->feature_id),
  848. $options);
  849. // now associate this feature with the cvterm if it doesn't already exist
  850. if (sizeof($fcvt)==0) {
  851. $values = array(
  852. 'cvterm_id' => $cvterm->cvterm_id,
  853. 'feature_id' => $feature->feature_id,
  854. 'pub_id' => array(
  855. 'uniquename' => 'null',
  856. ),
  857. );
  858. $options = array('statement_name' => 'ins_featurecvterm_cvtermid_featureid_pubid');
  859. $ret = tripal_core_chado_insert('feature_cvterm', $values, $options);
  860. if ($ret) {
  861. fwrite($log, " Adding ontology term $dbname:$accession\n");
  862. }
  863. else {
  864. fwrite($log, "ERROR: failed to insert ontology term: $dbname:$accession\n");
  865. return 0;
  866. }
  867. }
  868. else {
  869. fwrite($log, " Ontology term already associated, skipping $dbname:$accession\n");
  870. }
  871. }
  872. return 1;
  873. }
  874. /**
  875. *
  876. *
  877. * @ingroup gff3_loader
  878. */
  879. function tripal_feature_load_gff3_alias($feature, $aliases, $log) {
  880. // make sure we have a 'synonym_type' vocabulary
  881. $select = array('name' => 'synonym_type');
  882. $options = array('statement_name' => 'sel_cv_name');
  883. $results = tripal_core_chado_select('cv', array('*'), $selct, $options);
  884. if (count($results) == 0) {
  885. // insert the 'synonym_type' vocabulary
  886. $values = array(
  887. 'name' => 'synonym_type',
  888. 'definition' => 'vocabulary for synonym types',
  889. );
  890. $options = array('statement_name' => 'ins_cv_name_definition');
  891. $result = tripal_core_chado_insert('cv', $values, $options);
  892. if (!result) {
  893. fwrite($log, "ERROR: Failed to add the synonyms type vocabulary");
  894. return 0;
  895. }
  896. // now that we've added the cv we need to get the record
  897. $options = array('statement_name' => 'sel_cv_name');
  898. $results = tripal_core_chado_select('cv', array('*'), $select, $options);
  899. $syncv = $results[0];
  900. }
  901. else {
  902. $syncv = $results[0];
  903. }
  904. // get the 'exact' cvterm, which is the type of synonym we're adding
  905. $select = array(
  906. 'name' => 'exact',
  907. 'cv_id' => array(
  908. 'name' => 'synonym_type'
  909. ),
  910. );
  911. $options = array('statement_name' => 'sel_cvterm_name_cvid');
  912. $result = tripal_core_chado_select('cvterm', array('*'), $select, $options);
  913. $syntype = $result[0];
  914. if (!$syntype) {
  915. $term = array(
  916. 'name' => 'exact',
  917. 'id' => "internal:exact",
  918. 'definition' => '',
  919. 'is_obsolete' => 0,
  920. );
  921. // TODO: fix the function so it uses prepared statements
  922. $syntype = tripal_cv_add_cvterm($term, $syncv->name, 0, 1);
  923. if (!$syntype) {
  924. fwrite($log, "Cannot add synonym type: internal:$type");
  925. return 0;
  926. }
  927. }
  928. // iterate through all of the aliases and add each one
  929. foreach ($aliases as $alias) {
  930. fwrite($log, " Adding Alias $alias\n");
  931. // check to see if the alias already exists in the synonym table
  932. // if not, then add it
  933. $select = array(
  934. 'name' => $alias,
  935. 'type_id' => $syntype->cvterm_id,
  936. );
  937. $options = array('statement_name' => 'sel_synonym_name_typeid');
  938. $result = tripal_core_chado_select('synonym', array('*'), $select, $options);
  939. $synonym = $result[0];
  940. if (!$synonym) {
  941. $values = array(
  942. 'name' => $alias,
  943. 'type_id' => $syntype->cvterm_id,
  944. 'synonym_sgml' => '',
  945. );
  946. $options = array('statement_name' => 'ins_synonym_name_typeid_synonymsgml');
  947. $result = tripal_core_chado_insert('synonym', $values, $options);
  948. if (!$result) {
  949. fwrite($log, "ERROR: cannot add alias $alias to synonym table\n");
  950. }
  951. }
  952. $options = array('statement_name' => 'sel_synonym_name_typeid');
  953. $result = tripal_core_chado_select('synonym', array('*'), $select, $options);
  954. $synonym = $result[0];
  955. // check to see if we have a NULL publication in the pub table. If not,
  956. // then add one.
  957. // @coder-ignore: non-drupal schema thus table prefixing does not apply
  958. $select = array('uniquename' => 'null');
  959. $options = array('statement_name' => 'sel_pub_uniquename');
  960. $result = tripal_core_chado_select('pub', array('*'), $select, $options);
  961. $pub = $result[0];
  962. if (!$pub) {
  963. // prepare the statement
  964. if (!tripal_core_is_sql_prepared('ins_pub_uniquename_typeid')) {
  965. $psql = "PREPARE ins_pub_uniquename_typeid (text, text) AS
  966. INSERT INTO pub (uniquename,type_id) VALUES ('%s',
  967. (SELECT cvterm_id
  968. FROM cvterm CVT
  969. INNER JOIN dbxref DBX on DBX.dbxref_id = CVT.dbxref_id
  970. INNER JOIN db DB on DB.db_id = DBX.db_id
  971. WHERE CVT.name = $1 and DB.name = $2)";
  972. $status = chado_query($psql);
  973. if (!$status) {
  974. fwrite($log, "ERROR: cannot prepare statement 'ins_pub_uniquename_typeid'\n");
  975. return 0;
  976. }
  977. }
  978. // insert the null pub
  979. $result = db_fetch_object(chado_query("EXECUTE ins_pub_uniquename_typeid ('%s', '%s')", 'null', 'null'));
  980. if (!$result) {
  981. fwrite($log, "ERROR: cannot add null publication needed for setup of alias\n");
  982. return 0;
  983. }
  984. }
  985. $options = array('statement_name' => 'sel_pub_uniquename');
  986. $result = tripal_core_chado_select('pub', array('*'), $select, $options);
  987. $pub = $result[0];
  988. // check to see if the synonym exists in the feature_synonym table
  989. // if not, then add it.
  990. $select = array(
  991. 'synonym_id' => $synonym->synonym_id,
  992. 'feature_id' => $feature->feature_id,
  993. 'pub_id' => $pub->pub_id,
  994. );
  995. $options = array('statement_name', 'sel_synonymfeature_synonymid_featureid_pubid');
  996. $result = tripal_core_chado_select('feature_synonym', array('*'), $select, $options);
  997. $fsyn = $result[0];
  998. if (!$fsyn) {
  999. $values = array(
  1000. 'synonym_id' => $synonym->synonym_id,
  1001. 'feature_id' => $feature->feature_id,
  1002. 'pub_id' => $pub->pub_id,
  1003. );
  1004. $options = array('statement_name', 'ins_synonymfeature_synonymid_featureid_pubid');
  1005. $result = tripal_core_chado_insert('feature_synonym', $values, $options);
  1006. if (!$result) {
  1007. fwrite($log, "ERROR: cannot add alias $alias to feature synonym table\n");
  1008. return 0;
  1009. }
  1010. }
  1011. else {
  1012. fwrite($log, " Synonym $alias already exists. Skipping\n");
  1013. }
  1014. }
  1015. return 1;
  1016. }
  1017. /**
  1018. *
  1019. *
  1020. * @ingroup gff3_loader
  1021. */
  1022. function tripal_feature_load_gff3_feature($organism, $analysis_id, $cvterm, $uniquename, $name,
  1023. $residues, $is_analysis='f', $is_obsolete='f', $add_only, $score, $log) {
  1024. // check to see if the feature already exists
  1025. $fselect = array(
  1026. 'organism_id' => $organism->organism_id,
  1027. 'uniquename' => $uniquename,
  1028. 'type_id' => $cvterm->cvterm_id
  1029. );
  1030. $options = array('statement_name' => 'sel_feature_organismid_uniquename_typeid');
  1031. $result = tripal_core_chado_select('feature', array('*'), $fselect, $options);
  1032. $feature = $result[0];
  1033. if (strcmp($is_obsolete, 'f')==0 or $is_obsolete == 0) {
  1034. $is_obsolete = 'false';
  1035. }
  1036. if (strcmp($is_obsolete, 't')==0 or $is_obsolete == 1) {
  1037. $is_obsolete = 'true';
  1038. }
  1039. if (strcmp($is_analysis, 'f')==0 or $is_analysis == 0) {
  1040. $is_analysis = 'false';
  1041. }
  1042. if (strcmp($is_analysis, 't')==0 or $is_analysis == 1) {
  1043. $is_analysis = 'true';
  1044. }
  1045. // insert the feature if it does not exist otherwise perform an update
  1046. if (!$feature) {
  1047. fwrite($log, " Adding feature '$uniquename' ($cvterm->name)\n");
  1048. $values = array(
  1049. 'organism_id' => $organism->organism_id,
  1050. 'name' => $name,
  1051. 'uniquename' => $uniquename,
  1052. 'residues' => $residues,
  1053. 'seqlen' => drupal_strlen($residues),
  1054. 'md5checksum' => md5($residues),
  1055. 'type_id' => $cvterm->cvterm_id,
  1056. 'is_analysis' => $is_analysis,
  1057. 'is_obsolete' => $is_obsolete,
  1058. );
  1059. $options = array('statement_name' => 'ins_feature_all');
  1060. $result = tripal_core_chado_insert('feature', $values, $options);
  1061. if (!$result) {
  1062. fwrite($log, "ERROR: failed to insert feature '$uniquename' ($cvterm->name)\n");
  1063. return 0;
  1064. }
  1065. }
  1066. elseif (!$add_only) {
  1067. fwrite($log, "Updating feature '$uniquename' ($cvterm->name)\n");
  1068. $values = array(
  1069. 'name' => $name,
  1070. 'residues' => $residues,
  1071. 'seqlen' => drupal_strlen($residues),
  1072. 'md5checksum' => md5($residues),
  1073. 'is_analysis' => $is_analysis,
  1074. 'is_obsolete' => $is_obsolete,
  1075. );
  1076. $match = array(
  1077. 'organism_id' => $organism->organism_id,
  1078. 'uniquename' => $uniquename,
  1079. 'type_id' => $cvterm->cvterm_id,
  1080. );
  1081. $options = array('statement_name' => 'upd_feature');
  1082. $result = tripal_core_chado_update('feature', $match, $values, $options);
  1083. if (!$result) {
  1084. fwrite($log, "ERROR: failed to update feature '$uniquename' ($cvterm->name)\n");
  1085. return 0;
  1086. }
  1087. }
  1088. else {
  1089. // the feature exists and we don't want to update it so return
  1090. // a value of 0. This will stop all downstream property additions
  1091. fwrite($log, "Skipping existing feature: '$uniquename' ($cvterm->name).\n");
  1092. return 0;
  1093. }
  1094. // get the newly added feature
  1095. $options = array('statement_name' => 'sel_feature_organismid_uniquename_typeid');
  1096. $result = tripal_core_chado_select('feature', array('*'), $fselect, $options);
  1097. $feature = $result[0];
  1098. // add the analysisfeature entry to the analysisfeature table if it doesn't already exist
  1099. $af_values = array(
  1100. 'analysis_id' => $analysis_id,
  1101. 'feature_id' => $feature->feature_id
  1102. );
  1103. $options = array('statement_name' => 'sel_analysisfeature_analysisid_featureid');
  1104. $afeature = tripal_core_chado_select('analysisfeature', array('analysisfeature_id'), $af_values, $options);
  1105. if (count($afeature)==0) {
  1106. // if a score is available then set that to be the significance field
  1107. if (strcmp($score, '.') != 0) {
  1108. $af_values['significance'] = $score;
  1109. $options = array('statement_name' => 'ins_analysisfeature_analysisid_featureid_significance');
  1110. }
  1111. else {
  1112. $options = array('statement_name' => 'ins_analysisfeature_analysisid_featureid');
  1113. }
  1114. if (!tripal_core_chado_insert('analysisfeature', $af_values, $options)) {
  1115. fwrite($log, "ERROR: could not add analysisfeature record: $analysis_id, $feature->feature_id\n");
  1116. }
  1117. else {
  1118. fwrite($log, " Added analysisfeature record\n");
  1119. }
  1120. }
  1121. else {
  1122. // if a score is available then set that to be the significance field
  1123. $new_vals = array();
  1124. if (strcmp($score, '.')!=0) {
  1125. $new_vals['significance'] = $score;
  1126. }
  1127. else {
  1128. $new_vals['significance'] = '__NULL__';
  1129. }
  1130. if (!$add_only) {
  1131. $options = array('statement_name' => 'upd_analysisfeature');
  1132. $ret = tripal_core_chado_update('analysisfeature', $af_values, $new_vals, $options);
  1133. if (!$ret) {
  1134. fwrite($log, "ERROR: could not update analysisfeature record: $analysis_id, $feature->feature_id\n");
  1135. }
  1136. else {
  1137. fwrite($log, " Updated analysisfeature record\n");
  1138. }
  1139. }
  1140. }
  1141. return $feature;
  1142. }
  1143. /**
  1144. *
  1145. *
  1146. * @ingroup gff3_loader
  1147. */
  1148. function tripal_feature_load_gff3_featureloc($feature, $organism, $landmark, $fmin,
  1149. $fmax, $strand, $phase, $is_fmin_partial, $is_fmax_partial, $residue_info, $locgroup, $log) {
  1150. $select = array(
  1151. 'organism_id' => $organism->organism_id,
  1152. 'uniquename' => $landmark,
  1153. );
  1154. $options = array('statement_name' => 'sel_feature_organism_id_uniquename');
  1155. $r = tripal_core_chado_select('feature', array('*'), $select, $options);
  1156. if (count($r)==0) {
  1157. // so we couldn't find it using the uniquename. Let's try the 'name'.
  1158. // if we return only a singe result then we can proceed. Otherwise give an
  1159. // error message
  1160. $select = array(
  1161. 'organism_id' => $organism->organism_id,
  1162. 'name' => $landmark,
  1163. );
  1164. $options = array('statement_name' => 'sel_feature_organism_id_name');
  1165. $r = tripal_core_chado_select('feature', array('*'), $select, $options);
  1166. if (count($r) == 0){
  1167. fwrite($log, "ERROR: cannot find landmark feature: '$landmark'. Cannot add the feature location record\n");
  1168. return 0;
  1169. }
  1170. elseif (count($r) > 1) {
  1171. fwrite($log, "ERROR: multiple landmarks exist with the name: '$landmark'. Cannot resolve which one to use. Cannot add the feature location record\n");
  1172. return 0;
  1173. }
  1174. }
  1175. $srcfeature = $r[0];
  1176. // TODO: create an attribute that recognizes the residue_info,locgroup,
  1177. // is_fmin_partial and is_fmax_partial, right now these are
  1178. // hardcoded to be false and 0 below.
  1179. // check to see if this featureloc already exists, but also keep track of the
  1180. // last rank value
  1181. $rank = 0;
  1182. $exists = 0;
  1183. $select = array('feature_id' => $feature->feature_id);
  1184. $options = array(
  1185. 'statement_name' => 'sel_featureloc_feature_id',
  1186. 'order_by' => array(
  1187. 'rank' => 'ASC'
  1188. ),
  1189. );
  1190. $locrecs = tripal_core_chado_select('featureloc', array('*'), $select, $options);
  1191. foreach ($locrecs as $featureloc) {
  1192. $select = array('feature_id' => $featureloc->srcfeature_id);
  1193. $options = array('statement_name' => 'sel_feature_feature_id');
  1194. $locsfeature = tripal_core_chado_select('feature', array('*'), $select, $options);
  1195. // check to make sure we don't already have this featureloc record
  1196. // if we do we don't want to readd it
  1197. // print "Src Name:'" . $locsfeature[0]->name . "' == '$landmark'\n";
  1198. // print "Fmin: '$featureloc->fmin' == '$fmin'\n";
  1199. // print "Fmax: '$featureloc->fmax' == '$fmax'\n";
  1200. // print "Strand: '$featureloc->strand' == '$strand'\n";
  1201. // the source feature name and at least the fmin and fmax must be the same
  1202. // for an update of the featureloc, otherwise we'll insert a new record.
  1203. if (strcmp($locsfeature[0]->name, $landmark)==0 and
  1204. ($featureloc->fmin == $fmin or $featureloc->fmax == $fmax)) {
  1205. $match = array('featureloc_id' => $featureloc->featureloc_id);
  1206. $values = array();
  1207. $exists = 1;
  1208. if ($featureloc->fmin != $fmin) {
  1209. $values['fmin'] = $fmin;
  1210. }
  1211. if ($featureloc->fmax != $fmax) {
  1212. $values['fmax'] = $fmax;
  1213. }
  1214. if ($featureloc->strand != $strand) {
  1215. $values['strand'] = $strand;
  1216. }
  1217. if (count($values) > 0) {
  1218. $options = array('statement_name' => 'upd_featureloc_all');
  1219. tripal_core_chado_update('featureloc', $match, $values, $options);
  1220. fwrite($log, " Updated featureloc\n");
  1221. }
  1222. else {
  1223. fwrite($log, " No change to featureloc\n");
  1224. }
  1225. }
  1226. $rank = $featureloc->rank + 1;
  1227. }
  1228. if (!$exists) {
  1229. // this feature location is new so add it
  1230. if (!$phase) {
  1231. $phase = 'NULL';
  1232. }
  1233. if (strcmp($is_fmin_partial, 'f')==0 or !$is_fmin_partial) {
  1234. $is_fmin_partial = 'false';
  1235. }
  1236. elseif (strcmp($is_fmin_partial, 't')==0 or $is_fmin_partial = 1) {
  1237. $is_fmin_partial = 'true';
  1238. }
  1239. if (strcmp($is_fmax_partial, 'f')==0 or !$is_fmax_partial) {
  1240. $is_fmax_partial = 'false';
  1241. }
  1242. elseif (strcmp($is_fmax_partial, 't')==0 or $is_fmax_partial = 1) {
  1243. $is_fmax_partial = 'true';
  1244. }
  1245. fwrite($log, " Adding featureloc $srcfeature->uniquename fmin: $fmin (is_partial: $is_fmin_partial), fmax: $fmax (is_partial: $is_fmin_partial), strand: $strand, phase: $phase, rank: $rank\n");
  1246. $values = array(
  1247. 'feature_id' => $feature->feature_id,
  1248. 'srcfeature_id' => $srcfeature->feature_id,
  1249. 'fmin' => $fmin,
  1250. 'is_fmin_partial' => $is_fmin_partial,
  1251. 'fmax' => $fmax,
  1252. 'is_fmax_partial' => $is_fmax_partial,
  1253. 'strand' => $strand,
  1254. 'phase' => $phase,
  1255. 'residue_info' => $residue_info,
  1256. 'locgroup' => $locgroup,
  1257. 'rank' => $rank
  1258. );
  1259. $options = array('statement_name' => 'ins_featureloc_all');
  1260. $success = tripal_core_chado_insert('featureloc', $values, $options);
  1261. if (!$success) {
  1262. fwrite($log, "ERROR: failed to insert featureloc\n");
  1263. exit;
  1264. return 0;
  1265. }
  1266. }
  1267. return 1;
  1268. }
  1269. /**
  1270. *
  1271. *
  1272. * @ingroup gff3_loader
  1273. */
  1274. function tripal_feature_load_gff3_property($feature, $property, $value, $log) {
  1275. // first make sure the cvterm exists. if not, then add it
  1276. $select = array(
  1277. 'name' => $property,
  1278. 'cv_id' => array(
  1279. 'name' => 'feature_property',
  1280. ),
  1281. );
  1282. $options = array('statement_name' => 'sel_cvterm_name_cvid');
  1283. $result = tripal_core_chado_select('cvterm', array('*'), $select, $options);
  1284. $cvterm = $result[0];
  1285. if (!$cvterm) {
  1286. $term = array(
  1287. 'id' => "null:$property",
  1288. 'name' => $property,
  1289. 'namespace' => 'feature_property',
  1290. 'is_obsolete' => 0,
  1291. );
  1292. fwrite($log, " Adding cvterm, $property\n");
  1293. $cvterm = (object) tripal_cv_add_cvterm($term, 'feature_property', 0, 0);
  1294. }
  1295. if (!$cvterm) {
  1296. fwrite($log, "ERROR: cannot add cvterm, $property\n");
  1297. exit;
  1298. }
  1299. // check to see if the property already exists for this feature
  1300. // if it does but the value is unique then increment the rank and add it.
  1301. // if the value is not unique then don't add it.
  1302. $add = 1;
  1303. $rank = 0;
  1304. $select = array(
  1305. 'feature_id' => $feature->feature_id,
  1306. 'type_id' => $cvterm->cvterm_id,
  1307. );
  1308. $options = array(
  1309. 'statement_name' => 'sel_featureprop_featureid_typeid',
  1310. 'order_by' => array(
  1311. 'rank' => 'ASC',
  1312. ),
  1313. );
  1314. $results = tripal_core_chado_select('featureprop', array('*'), $select, $options);
  1315. foreach ($results as $prop){
  1316. if (strcmp($prop->value, $value)==0) {
  1317. $add = NULL; // don't add it, it already exists
  1318. fwrite($log, " Property already exists, skipping\n");
  1319. }
  1320. $rank = $prop->rank + 1;
  1321. }
  1322. // add the property if we pass the check above
  1323. if ($add) {
  1324. fwrite($log, " Setting feature property. $property: $value\n");
  1325. $values = array(
  1326. 'feature_id' => $feature->feature_id,
  1327. 'type_id' => $cvterm->cvterm_id,
  1328. 'value' => $value,
  1329. 'rank' => $rank,
  1330. );
  1331. $options = array('statement_name' => 'ins_featureprop_all');
  1332. $result = tripal_core_chado_insert('featureprop', $values, $options);
  1333. if(!$result){
  1334. fwrite($log, "ERROR: cannot add featureprop, $property\n");
  1335. }
  1336. }
  1337. }