gff_loader.inc 52 KB

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