gff_loader.inc 51 KB

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