gff_loader.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  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. // TODO: The rank column on the feature_relationship table needs to be used to
  14. // make sure the ordering of CDS (exons) is correct.
  15. // The entries in the GFF file are not in order so the order of the relationships
  16. // is not put in correctly.
  17. /**
  18. *
  19. *
  20. * @ingroup gff3_loader
  21. */
  22. function tripal_feature_gff3_load_form() {
  23. $form['gff_file']= array(
  24. '#type' => 'textfield',
  25. '#title' => t('GFF3 File'),
  26. '#description' => t('Please enter the full system path for the GFF file, or a path within the Drupal
  27. installation (e.g. /sites/default/files/xyz.gff). The path must be accessible to the
  28. server on which this Drupal instance is running.'),
  29. '#required' => TRUE,
  30. '#weight' => 1
  31. );
  32. // get the list of organisms
  33. $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  34. $previous_db = tripal_db_set_active('chado'); // use chado database
  35. $org_rset = db_query($sql);
  36. tripal_db_set_active($previous_db); // now use drupal database
  37. $organisms = array();
  38. $organisms[''] = '';
  39. while ($organism = db_fetch_object($org_rset)) {
  40. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  41. }
  42. $form['organism_id'] = array(
  43. '#title' => t('Organism'),
  44. '#type' => t('select'),
  45. '#description' => t("Choose the organism to which these sequences are associated"),
  46. '#required' => TRUE,
  47. '#options' => $organisms,
  48. );
  49. $form['import_options'] = array(
  50. '#type' => 'fieldset',
  51. '#title' => t('Import Options'),
  52. '#weight' => 6,
  53. '#collapsed' => TRUE
  54. );
  55. $form['import_options']['add_only']= array(
  56. '#type' => 'checkbox',
  57. '#title' => t('Import only new features'),
  58. '#required' => FALSE,
  59. '#description' => t('The job will skip features in the GFF file that already
  60. exist in the database and import only new features.'),
  61. '#weight' => 2
  62. );
  63. $form['import_options']['update']= array(
  64. '#type' => 'checkbox',
  65. '#title' => t('Import all and update'),
  66. '#required' => FALSE,
  67. '#default_value' => 'checked',
  68. '#description' => t('Existing features will be updated and new features will be added. Attributes
  69. for a feature that are not present in the GFF but which are present in the
  70. database will not be altered.'),
  71. '#weight' => 3
  72. );
  73. $form['import_options']['refresh']= array(
  74. '#type' => 'checkbox',
  75. '#title' => t('Import all and replace'),
  76. '#required' => FALSE,
  77. '#description' => t('Existing features will be updated and feature properties not
  78. present in the GFF file will be removed.'),
  79. '#weight' => 4
  80. );
  81. $form['import_options']['remove']= array(
  82. '#type' => 'checkbox',
  83. '#title' => t('Delete features'),
  84. '#required' => FALSE,
  85. '#description' => t('Features present in the GFF file that exist in the database
  86. will be removed rather than imported'),
  87. '#weight' => 5
  88. );
  89. $form['analysis'] = array(
  90. '#type' => 'fieldset',
  91. '#title' => t('Analysis Used to Derive Features'),
  92. '#weight' => 6,
  93. '#collapsed' => TRUE
  94. );
  95. $form['analysis']['desc'] = array(
  96. '#type' => 'markup',
  97. '#value' => t("Why specify an analysis for a data load? All data comes
  98. from some place, even if downloaded from Genbank. By specifying
  99. analysis details for all data uploads, it allows an end user to reproduce the
  100. data set, but at least indicates the source of the data."),
  101. );
  102. // get the list of analyses
  103. $sql = "SELECT * FROM {analysis} ORDER BY name";
  104. $previous_db = tripal_db_set_active('chado'); // use chado database
  105. $org_rset = db_query($sql);
  106. tripal_db_set_active($previous_db); // now use drupal database
  107. $analyses = array();
  108. $analyses[''] = '';
  109. while ($analysis = db_fetch_object($org_rset)) {
  110. $analyses[$analysis->analysis_id] = "$analysis->name ($analysis->program $analysis->programversion, $analysis->sourcename)";
  111. }
  112. $form['analysis']['analysis_id'] = array(
  113. '#title' => t('Analysis'),
  114. '#type' => t('select'),
  115. '#description' => t("Choose the analysis to which these features are associated"),
  116. '#required' => TRUE,
  117. '#options' => $analyses,
  118. );
  119. $form['button'] = array(
  120. '#type' => 'submit',
  121. '#value' => t('Import GFF3 file'),
  122. '#weight' => 10,
  123. );
  124. return $form;
  125. }
  126. /**
  127. *
  128. *
  129. * @ingroup gff3_loader
  130. */
  131. function tripal_feature_gff3_load_form_validate($form, &$form_state) {
  132. $gff_file = $form_state['values']['gff_file'];
  133. $organism_id = $form_state['values']['organism_id'];
  134. $add_only = $form_state['values']['add_only'];
  135. $update = $form_state['values']['update'];
  136. $refresh = $form_state['values']['refresh'];
  137. $remove = $form_state['values']['remove'];
  138. // check to see if the file is located local to Drupal
  139. $dfile = $_SERVER['DOCUMENT_ROOT'] . base_path() . $gff_file;
  140. if (!file_exists($dfile)) {
  141. // if not local to Drupal, the file must be someplace else, just use
  142. // the full path provided
  143. $dfile = $gff_file;
  144. }
  145. if (!file_exists($dfile)) {
  146. 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."));
  147. }
  148. if (($add_only AND ($update OR $refresh OR $remove)) OR
  149. ($update AND ($add_only OR $refresh OR $remove)) OR
  150. ($refresh AND ($update OR $add_only OR $remove)) OR
  151. ($remove AND ($update OR $refresh OR $add_only))) {
  152. form_set_error('add_only', t("Please select only one checkbox from the import options section"));
  153. }
  154. }
  155. /**
  156. *
  157. * @ingroup gff3_loader
  158. */
  159. function tripal_feature_gff3_load_form_submit($form, &$form_state) {
  160. global $user;
  161. $gff_file = $form_state['values']['gff_file'];
  162. $organism_id = $form_state['values']['organism_id'];
  163. $add_only = $form_state['values']['add_only'];
  164. $update = $form_state['values']['update'];
  165. $refresh = $form_state['values']['refresh'];
  166. $remove = $form_state['values']['remove'];
  167. $analysis_id = $form_state['values']['analysis_id'];
  168. $args = array($gff_file, $organism_id, $analysis_id, $add_only, $update, $refresh, $remove);
  169. $type = '';
  170. if ($add_only) {
  171. $type = 'import only new features';
  172. }
  173. if ($update) {
  174. $type = 'import all and update';
  175. }
  176. if ($refresh) {
  177. $type = 'import all and replace';
  178. }
  179. if ($remove) {
  180. $type = 'delete features';
  181. }
  182. tripal_add_job("$type GFF3 file $gff_file", '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, $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. print "ERROR: cannot find the file: $dfile\n";
  204. return 0;
  205. }
  206. $previous_db = tripal_db_set_active('chado');
  207. print "Opening $gff_file\n";
  208. //$lines = file($dfile,FILE_SKIP_EMPTY_LINES);
  209. $fh = fopen($dfile, 'r');
  210. if (!$fh) {
  211. print "ERROR: cannot open file: $dfile\n";
  212. return 0;
  213. }
  214. $filesize = filesize($dfile);
  215. // get the controlled vocaubulary that we'll be using. The
  216. // default is the 'sequence' ontology
  217. $sql = "SELECT * FROM cv WHERE name = '%s'";
  218. $cv = db_fetch_object(db_query($sql, 'sequence'));
  219. if (!$cv) {
  220. print "ERROR: cannot find the 'sequence' ontology\n";
  221. return '';
  222. }
  223. // get the organism for which this GFF3 file belongs
  224. $sql = "SELECT * FROM organism WHERE organism_id = %d";
  225. $organism = db_fetch_object(db_query($sql, $organism_id));
  226. $interval = intval($filesize * 0.01);
  227. if ($interval == 0) {
  228. $interval = 1;
  229. }
  230. $in_fasta = 0;
  231. // foreach ($lines as $line_num => $line) {
  232. $line_num = 0;
  233. $num_read = 0;
  234. while ($line = fgets($fh)) {
  235. $line_num++;
  236. $num_read += drupal_strlen($line);
  237. // update the job status every 1% features
  238. if ($job and $num_read % $interval == 0) {
  239. tripal_job_set_progress($job, intval(($num_read/$filesize)*100));
  240. }
  241. // check to see if we have FASTA section, if so then set the variable
  242. // to start parsing
  243. if (preg_match('/^##FASTA/i', $line)) {
  244. $in_fasta = 1;
  245. break;
  246. }
  247. // skip comments
  248. if (preg_match('/^#/', $line)) {
  249. continue;
  250. }
  251. // skip empty lines
  252. if (preg_match('/^\s*$/', $line)) {
  253. continue;
  254. }
  255. // handle FASTA section
  256. // TODO: handle URL encoding
  257. // remove URL encoding and get the columns
  258. $cols = explode("\t", $line);
  259. if (sizeof($cols) != 9) {
  260. print "ERROR: improper number of columns on line $line_num\n";
  261. print_r($cols);
  262. return '';
  263. }
  264. // get the column values
  265. $landmark = $cols[0];
  266. $source = $cols[1];
  267. $type = $cols[2];
  268. $start = $cols[3];
  269. $end = $cols[4];
  270. $score = $cols[5];
  271. $strand = $cols[6];
  272. $phase = $cols[7];
  273. $attrs = explode(";", $cols[8]); // split by a semi-colon
  274. // ready the start and stop for chado. Chado expects these positions
  275. // to be zero-based, so we substract 1 from the fmin
  276. $fmin = $start - 1;
  277. $fmax = $end;
  278. if ($end < $start) {
  279. $fmin = $end - 1;
  280. $fmax = $start;
  281. }
  282. // format the strand for chado
  283. if (strcmp($strand, '.')==0) {
  284. $strand = 0;
  285. }
  286. elseif (strcmp($strand, '+')==0) {
  287. $strand = 1;
  288. }
  289. elseif (strcmp($strand, '-')==0) {
  290. $strand = -1;
  291. }
  292. if (strcmp($phase, '.')==0) {
  293. $phase = '';
  294. }
  295. // get the type record
  296. $cvtermsql = "SELECT CVT.cvterm_id, CVT.cv_id, CVT.name, CVT.definition,
  297. CVT.dbxref_id, CVT.is_obsolete, CVT.is_relationshiptype
  298. FROM {cvterm} CVT
  299. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  300. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  301. WHERE CV.cv_id = %d and (CVT.name = '%s' or CVTS.synonym = '%s')";
  302. $cvterm = db_fetch_object(db_query($cvtermsql, $cv->cv_id, $type, $type));
  303. if (!$cvterm) {
  304. print "ERROR: cannot find ontology term '$type' on line $line_num.\n";
  305. return '';
  306. }
  307. // break apart each of the attributes
  308. $tags = array();
  309. $attr_name = '';
  310. $attr_uniquename = '';
  311. $attr_residue_info = '';
  312. $attr_locgroup = 0;
  313. $attr_fmin_partial = 'f';
  314. $attr_fmax_partial = 'f';
  315. $attr_is_obsolete = 'f';
  316. $attr_is_analysis = 'f';
  317. $attr_others = '';
  318. $residues = '';
  319. foreach ($attrs as $attr) {
  320. $attr = rtrim($attr);
  321. $attr = ltrim($attr);
  322. if (strcmp($attr, '')==0) {
  323. continue;
  324. }
  325. if (!preg_match('/^[^\=]+\=[^\=]+$/', $attr)) {
  326. print "ERROR: attribute is not correctly formatted on line $line_num: $attr\n";
  327. return '';
  328. }
  329. // break apart each tag
  330. $tag = explode("=", $attr); // split by equals sign
  331. // multiple instances of an attribute are separated by commas
  332. $tags[$tag[0]] = explode(",", $tag[1]); // split by comma
  333. if (strcmp($tag[0], 'ID')==0) {
  334. $attr_uniquename = $tag[1];
  335. }
  336. elseif (strcmp($tag[0], 'Name')==0) {
  337. $attr_name = $tag[1];
  338. }
  339. // get the list of other attributes other than those reserved ones.
  340. elseif (strcmp($tag[0], 'Alias')!=0 and strcmp($tag[0], 'Parent')!=0 and
  341. strcmp($tag[0], 'Target')!=0 and strcmp($tag[0], 'Gap')!=0 and
  342. strcmp($tag[0], 'Derives_from')!=0 and strcmp($tag[0], 'Note')!=0 and
  343. strcmp($tag[0], 'Dbxref')!=0 and strcmp($tag[0], 'Ontology_term')!=0 and
  344. strcmp($tag[0], 'Is_circular')!=0) {
  345. $attr_others[$tag[0]] = $tag[1];
  346. }
  347. }
  348. // if neither name nor uniquename are provided then generate one
  349. if (!$attr_uniquename and !$attr_name) {
  350. if (array_key_exists('Parent', $tags)) {
  351. $attr_uniquename = $tags['Parent'][0] . "-$type-$landmark:$fmin..$fmax";
  352. }
  353. else {
  354. print "ERROR: cannot generate a uniquename for feature on line $line_num\n";
  355. exit;
  356. }
  357. $attr_name = $attr_uniquename;
  358. }
  359. // if a name is not specified then use the unique name
  360. if (strcmp($attr_name, '')==0) {
  361. $attr_name = $attr_uniquename;
  362. }
  363. // if an ID attribute is not specified then use the attribute name and
  364. // hope for the best
  365. if (!$attr_uniquename) {
  366. $attr_uniquename = $attr_name;
  367. }
  368. // make sure the landmark sequence exists in the database. We don't
  369. // know the type of the landmark so we'll hope that it's unique across
  370. // all types. If not we'll error out. This test is only necessary if
  371. // if the landmark and the uniquename are different. If they are the same
  372. // then this is the information for the landmark
  373. if (strcmp($landmark, $attr_uniquename)!=0) {
  374. $feature_sql = "SELECT count(*) as num_landmarks
  375. FROM {feature}
  376. WHERE organism_id = %d and uniquename = '%s'";
  377. $count = db_fetch_object(db_query($feature_sql, $organism_id, $landmark));
  378. if (!$count or $count->num_landmarks == 0) {
  379. print "ERROR: the landmark '$landmark' cannot be found for this organism. ".
  380. "Please add the landmark and then retry the import of this GFF3 ".
  381. "file.\n";
  382. return '';
  383. }
  384. if ($count->num_landmarks > 1) {
  385. print "ERROR: the landmark '$landmark' is not unique for this organism. ".
  386. "The features cannot be associated.\n";
  387. return '';
  388. }
  389. }
  390. // if the option is to remove or refresh then we want to remove
  391. // the feature from the database.
  392. if ($remove or $refresh) {
  393. print "Removing feature '$attr_uniquename'\n";
  394. $sql = "DELETE FROM {feature}
  395. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  396. $result = db_query($sql, $organism->organism_id, $attr_uniquename, $cvterm->cvterm_id);
  397. if (!$result) {
  398. print "ERROR: cannot delete feature $attr_uniquename\n";
  399. }
  400. $feature = 0;
  401. }
  402. // add or update the feature and all properties
  403. if ($update or $refresh or $add_only) {
  404. // add/update the feature
  405. print "line $line_num, " . intval(($num_read/$filesize)*100) . "%. ";
  406. $feature = tripal_feature_load_gff3_feature($organism, $analysis_id, $cvterm,
  407. $attr_uniquename, $attr_name, $residues, $attr_is_analysis,
  408. $attr_is_obsolete, $add_only, $score);
  409. // store all of the features for use later by parent and target
  410. // relationships
  411. $gff_features[$feature->uniquename]['type'] = $type;
  412. $gff_features[$feature->uniquename]['strand'] = $strand;
  413. if ($feature) {
  414. // add/update the featureloc if the landmark and the ID are not the same
  415. // if they are the same then this entry in the GFF is probably a landmark identifier
  416. if (strcmp($landmark, $attr_uniquename)!=0) {
  417. tripal_feature_load_gff3_featureloc($feature, $organism,
  418. $landmark, $fmin, $fmax, $strand, $phase, $attr_fmin_partial,
  419. $attr_fmax_partial, $attr_residue_info, $attr_locgroup);
  420. }
  421. // add any aliases for this feature
  422. if (array_key_exists('Alias', $tags)) {
  423. tripal_feature_load_gff3_alias($feature, $tags['Alias']);
  424. }
  425. // add any dbxrefs for this feature
  426. if (array_key_exists('Dbxref', $tags)) {
  427. tripal_feature_load_gff3_dbxref($feature, $tags['Dbxref']);
  428. }
  429. // add any ontology terms for this feature
  430. if (array_key_exists('Ontology_term', $tags)) {
  431. tripal_feature_load_gff3_ontology($feature, $tags['Ontology_term']);
  432. }
  433. // add parent relationships
  434. if (array_key_exists('Parent', $tags)) {
  435. tripal_feature_load_gff3_parents($feature, $cvterm, $tags['Parent'], $gff_features, $organism_id, $fmin);
  436. }
  437. // add target relationships
  438. if (array_key_exists('Target', $tags)) {
  439. $target = explode(" ", $tags['Target'][0]);
  440. $target_feature = $target[0];
  441. $target_start = $target[1];
  442. $target_end = $target[2];
  443. $target_dir = $target[3];
  444. #print "Target: $target_feature, $target_start-$target_end\n";
  445. tripal_feature_load_gff3_featureloc($feature, $organism,
  446. $target_feature, $target_start, $target_end, $strand, $phase, $attr_fmin_partial,
  447. $attr_fmax_partial, $attr_residue_info, $attr_locgroup);
  448. }
  449. // add gap information. This goes in simply as a property
  450. if (array_key_exists('Gap', $tags)) {
  451. tripal_feature_load_gff3_property($feature, 'Gap', $tags['Gap'][0]);
  452. }
  453. // add notes. This goes in simply as a property
  454. if (array_key_exists('Note', $tags)) {
  455. tripal_feature_load_gff3_property($feature, 'Note', $tags['Note'][0]);
  456. }
  457. // add the Derives_from relationship (e.g. polycistronic genes).
  458. if (array_key_exists('Derives_from', $tags)) {
  459. tripal_feature_load_gff3_derives_from($feature, $tags['Derives_from'][0], $gff_features, $organism);
  460. }
  461. // add in the GFF3_source dbxref so that GBrowse can find the feature using the source column
  462. $source_ref = array('GFF_source:' . $source);
  463. tripal_feature_load_gff3_dbxref($feature, $source_ref);
  464. // add any additional attributes
  465. if ($attr_others) {
  466. foreach ($attr_others as $property => $value) {
  467. tripal_feature_load_gff3_property($feature, $property, $value);
  468. }
  469. }
  470. }
  471. }
  472. }
  473. // now set the rank of any parent/child relationships. The order is based
  474. // on the fmin. The start rank is 1. This allows features with other
  475. // relationships to be '0' (the default), and doesn't interfer with the
  476. // ordering defined here.
  477. foreach ($gff_features as $parent => $details) {
  478. // only iterate through parents that have children
  479. if ($details['children']) {
  480. // get the parent
  481. $values = array(
  482. 'uniquename' => $parent,
  483. 'type_id' => array(
  484. 'cv_id' => array(
  485. 'name' => 'sequence'
  486. ),
  487. 'name' => $details['type'],
  488. ),
  489. 'organism_id' => $organism->organism_id,
  490. );
  491. $pfeature = tripal_core_chado_select('feature', array('*'), $values);
  492. // sort the children by order of their fmin positions (values of assoc. array)
  493. // if the parent is on the reverse strand then sort in reverse
  494. if ($details['strand'] == -1) {
  495. arsort($details['children']);
  496. }
  497. else {
  498. asort($details['children']);
  499. }
  500. // now iterate through the children and set their rank
  501. $rank = 1;
  502. print "Updating child ranks for $parent (" . $details['type'] . ")\n";
  503. foreach ($details['children'] as $kfeature_id => $kfmin) {
  504. $match = array(
  505. 'object_id' => $pfeature[0]->feature_id,
  506. 'subject_id' => $kfeature_id,
  507. 'type_id' => array(
  508. 'cv_id' => array(
  509. 'name' => 'relationship'
  510. ),
  511. 'name' => 'part_of',
  512. ),
  513. );
  514. $values = array(
  515. 'rank' => $rank,
  516. );
  517. tripal_core_chado_update('feature_relationship', $match, $values);
  518. $rank++;
  519. }
  520. }
  521. }
  522. tripal_db_set_active($previous_db);
  523. return '';
  524. }
  525. /**
  526. *
  527. *
  528. * @ingroup gff3_loader
  529. */
  530. function tripal_feature_load_gff3_derives_from($feature, $subject, $gff_features, $organism) {
  531. // first get the subject feature
  532. $match = array(
  533. 'organism_id' => $organism->organism_id,
  534. 'uniquename' => $subject,
  535. 'type_id' => array(
  536. 'name' => $gff_features[$subject]['type'],
  537. 'cv_id' => array(
  538. 'name' => 'sequence'
  539. ),
  540. ),
  541. );
  542. $sfeature = tripal_core_chado_select('feature', array('*'), $match);
  543. if (count($sfeature)==0) {
  544. print "ERROR: could not add 'Derives_from' relationship for $feature->uniquename and $subject. Subject feature, '$subject', cannot be found\n";
  545. return;
  546. }
  547. // now check to see if the relationship already exists
  548. $values = array(
  549. 'object_id' => $sfeature[0]->feature_id,
  550. 'subject_id' => $feature->feature_id,
  551. 'type_id' => array(
  552. 'cv_id' => array(
  553. 'name' => 'relationship'
  554. ),
  555. 'name' => 'derives_from',
  556. ),
  557. 'rank' => 0
  558. );
  559. $rel = tripal_core_chado_select('feature_relationship', array('*'), $values);
  560. if (count($rel) > 0) {
  561. print " Relationship already exists: $feature->uniquename derives_from $subject\n";
  562. return;
  563. }
  564. // finally insert the relationship if it doesn't exist
  565. $ret = tripal_core_chado_insert('feature_relationship', $values);
  566. if (!$ret) {
  567. print "ERROR: could not add 'Derives_from' relationship for $feature->uniquename and $subject\n";
  568. }
  569. else {
  570. print " Added relationship: $feature->uniquename derives_from $subject\n";
  571. }
  572. }
  573. /**
  574. *
  575. *
  576. * @ingroup gff3_loader
  577. */
  578. function tripal_feature_load_gff3_parents($feature, $cvterm, $parents, &$gff_features, $organism_id, $fmin) {
  579. $uname = $feature->uniquename;
  580. $type = $cvterm->name;
  581. $rel_type = 'part_of';
  582. // create these SQL statements that will be used repeatedly below.
  583. $cvtermsql = "SELECT CVT.cvterm_id
  584. FROM {cvterm} CVT
  585. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  586. LEFT JOIN {cvtermsynonym} CVTS on CVTS.cvterm_id = CVT.cvterm_id
  587. WHERE cv.name = '%s' and (CVT.name = '%s' or CVTS.synonym = '%s')";
  588. $feature_sql = "SELECT * FROM {feature}
  589. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  590. // iterate through the parents in the list
  591. foreach ($parents as $parent) {
  592. $parent_type = $gff_features[$parent]['type'];
  593. // try to find the parent
  594. $parentcvterm = db_fetch_object(db_query($cvtermsql, 'sequence', $parent_type, $parent_type));
  595. $relcvterm = db_fetch_object(db_query($cvtermsql, 'relationship', $rel_type, $rel_type));
  596. $parent_feature = db_fetch_object(db_query($feature_sql, $organism_id, $parent, $parentcvterm->cvterm_id));
  597. // we want to add this feature to the child list for the parent
  598. // when the loader finishes, it will go back through the parent
  599. // features and rank the children by position
  600. $gff_features[$parent]['children'][$feature->feature_id] = $fmin;
  601. // if the parent exists then add the relationship otherwise print error and skip
  602. if ($parent_feature) {
  603. // check to see if the relationship already exists
  604. $sql = "SELECT * FROM {feature_relationship} WHERE subject_id = %d and object_id = %d and type_id = %d";
  605. $rel = db_fetch_object(db_query($sql, $feature->feature_id, $parent_feature->feature_id, $relcvterm->cvterm_id));
  606. if ($rel) {
  607. print " Relationship already exists, skipping '$uname' ($type) $rel_type '$parent' ($parent_type)\n";
  608. }
  609. else {
  610. // the relationship doesn't already exist, so add it.
  611. $sql = "INSERT INTO {feature_relationship} (subject_id,object_id,type_id)
  612. VALUES (%d,%d,%d)";
  613. $result = db_query($sql, $feature->feature_id, $parent_feature->feature_id, $relcvterm->cvterm_id);
  614. if (!$result) {
  615. print "WARNING: failed to insert feature relationship '$uname' ($type) $rel_type '$parent' ($parent_type)\n";
  616. }
  617. else {
  618. print " Inserted relationship relationship: '$uname' ($type) $rel_type '$parent' ($parent_type)\n";
  619. }
  620. }
  621. }
  622. else {
  623. print "WARNING: cannot establish relationship '$uname' ($type) $rel_type '$parent' ($parent_type): Cannot find the parent\n";
  624. }
  625. }
  626. }
  627. /**
  628. *
  629. *
  630. * @ingroup gff3_loader
  631. */
  632. function tripal_feature_load_gff3_dbxref($feature, $dbxrefs) {
  633. // iterate through each of the dbxrefs
  634. foreach ($dbxrefs as $dbxref) {
  635. // get the database name from the reference. If it doesn't exist then create one.
  636. $ref = explode(":", $dbxref);
  637. $dbname = $ref[0];
  638. $accession = $ref[1];
  639. // first look for the database name if it doesn't exist then create one.
  640. // first check for the fully qualified URI (e.g. DB:<dbname>. If that
  641. // can't be found then look for the name as is. If it still can't be found
  642. // the create the database
  643. $db = tripal_core_chado_select('db', array('db_id'), array('name' => "DB:$dbname"));
  644. if (sizeof($db) == 0) {
  645. $db = tripal_core_chado_select('db', array('db_id'), array('name' => "$dbname"));
  646. }
  647. if (sizeof($db) == 0) {
  648. $ret = tripal_core_chado_insert('db', array('name' => $dbname,
  649. 'description' => 'Added automatically by the GFF loader'));
  650. if ($ret) {
  651. print " Added new database: $dbname\n";
  652. $db = tripal_core_chado_select('db', array('db_id'), array('name' => "$dbname"));
  653. }
  654. else {
  655. print "ERROR: cannot find or add the database $dbname\n";
  656. return 0;
  657. }
  658. }
  659. $db = $db[0];
  660. // now check to see if the accession exists
  661. $dbxref = tripal_core_chado_select('dbxref', array('dbxref_id'), array(
  662. 'accession' => $accession, 'db_id' => $db->db_id));
  663. // if the accession doesn't exist then we want to add it
  664. if (sizeof($dbxref) == 0) {
  665. $ret = tripal_core_chado_insert('dbxref', array('db_id' => $db->db_id,
  666. 'accession' => $accession, 'version' => ''));
  667. $dbxref = tripal_core_chado_select('dbxref', array('dbxref_id'), array(
  668. 'accession' => $accession, 'db_id' => $db->db_id));
  669. }
  670. $dbxref = $dbxref[0];
  671. // check to see if this feature dbxref already exists
  672. $fdbx = tripal_core_chado_select('feature_dbxref', array('feature_dbxref_id'),
  673. array('dbxref_id' => $dbxref->dbxref_id, 'feature_id' => $feature->feature_id));
  674. // now associate this feature with the database reference if it doesn't
  675. // already exist
  676. if (sizeof($fdbx)==0) {
  677. $ret = tripal_core_chado_insert('feature_dbxref', array(
  678. 'feature_id' => $feature->feature_id,
  679. 'dbxref_id' => $dbxref->dbxref_id));
  680. if ($ret) {
  681. print " Adding Dbxref $dbname:$accession\n";
  682. }
  683. else {
  684. print "ERROR: failed to insert Dbxref: $dbname:$accession\n";
  685. return 0;
  686. }
  687. }
  688. else {
  689. print " Dbxref already associated, skipping $dbname:$accession\n";
  690. }
  691. }
  692. return 1;
  693. }
  694. /**
  695. *
  696. *
  697. * @ingroup gff3_loader
  698. */
  699. function tripal_feature_load_gff3_ontology($feature, $dbxrefs) {
  700. // iterate through each of the dbxrefs
  701. foreach ($dbxrefs as $dbxref) {
  702. // get the database name from the reference. If it doesn't exist then create one.
  703. $ref = explode(":", $dbxref);
  704. $dbname = $ref[0];
  705. $accession = $ref[1];
  706. // first look for the database name
  707. $db = tripal_core_chado_select('db', array('db_id'), array('name' => "DB:$dbname"));
  708. if (sizeof($db) == 0) {
  709. $db = tripal_core_chado_select('db', array('db_id'), array('name' => "$dbname"));
  710. }
  711. if (sizeof($db) == 0) {
  712. print "ERROR: Database, $dbname is missing for reference: $dbname:$accession\n";
  713. return 0;
  714. }
  715. $db = $db[0];
  716. // now check to see if the accession exists
  717. $dbxref = tripal_core_chado_select('dbxref', array('dbxref_id'), array(
  718. 'accession' => $accession, 'db_id' => $db->db_id));
  719. if (sizeof($dbxref) == 0) {
  720. print "ERROR: Accession, $accession is missing for reference: $dbname:$accession\n";
  721. return 0;
  722. }
  723. $dbxref = $dbxref[0];
  724. // now check to see if the cvterm exists
  725. $cvterm = tripal_core_chado_select('cvterm', array('cvterm_id'), array(
  726. 'dbxref_id' => $dbxref->dbxref_id));
  727. // if it doesn't exist in the cvterm table, look for an alternate id
  728. if (sizeof($cvterm) == 0) {
  729. $cvterm = tripal_core_chado_select('cvterm_dbxref', array('cvterm_id'), array(
  730. 'dbxref_id' => $dbxref->dbxref_id));
  731. }
  732. if (sizeof($cvterm) == 0) {
  733. print "ERROR: CVTerm is missing for reference: $dbname:$accession\n";
  734. return 0;
  735. }
  736. $cvterm = $cvterm[0];
  737. // check to see if this feature cvterm already exists
  738. $fcvt = tripal_core_chado_select('feature_cvterm', array('feature_cvterm_id'),
  739. array('cvterm_id' => $cvterm->cvterm_id, 'feature_id' => $feature->feature_id));
  740. // now associate this feature with the cvterm if it doesn't already exist
  741. if (sizeof($fcvt)==0) {
  742. $values = array(
  743. 'feature_id' => $feature->feature_id,
  744. 'cvterm_id' => $cvterm->cvterm_id,
  745. 'pub_id' => array(
  746. 'uniquename' => 'null',
  747. ),
  748. );
  749. $ret = tripal_core_chado_insert('feature_cvterm', $values);
  750. if ($ret) {
  751. print " Adding ontology term $dbname:$accession\n";
  752. }
  753. else {
  754. print "ERROR: failed to insert ontology term: $dbname:$accession\n";
  755. return 0;
  756. }
  757. }
  758. else {
  759. print " Ontology term already associated, skipping $dbname:$accession\n";
  760. }
  761. }
  762. return 1;
  763. }
  764. /**
  765. *
  766. *
  767. * @ingroup gff3_loader
  768. */
  769. function tripal_feature_load_gff3_alias($feature, $aliases) {
  770. // make sure we have a 'synonym_type' vocabulary
  771. $sql = "SELECT * FROM {cv} WHERE name='synonym_type'";
  772. $syncv = db_fetch_object(db_query($sql));
  773. if (!$syncv) {
  774. $sql = "INSERT INTO {cv} (name,definition) VALUES ('synonym_type','')";
  775. if (!db_query($sql)) {
  776. print("ERROR: Failed to add the synonyms type vocabulary");
  777. return 0;
  778. }
  779. $syncv = db_fetch_object(db_query($sql));
  780. }
  781. // get the 'exact' cvterm, which is the type of synonym we're adding
  782. $cvtsql = "
  783. SELECT * FROM {cvterm} CVT
  784. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  785. WHERE CVT.name = '%s' and CV.name = '%s'
  786. ";
  787. $syntype = db_fetch_object(db_query($cvtsql, 'exact', 'synonym_type'));
  788. if (!$syntype) {
  789. $term = array(
  790. 'name' => array('exact'),
  791. 'id' => array("internal:exact"),
  792. 'definition' => array(''),
  793. 'is_obsolete' => array(0),
  794. );
  795. $syntype = tripal_cv_add_cvterm($term, $syncv->name, 0, 1);
  796. if (!$syntype) {
  797. print("Cannot add synonym type: internal:$type");
  798. return 0;
  799. }
  800. }
  801. // iterate through all of the aliases and add each one
  802. foreach ($aliases as $alias) {
  803. print " Adding Alias $alias\n";
  804. // check to see if the alias already exists in the synonym table
  805. // if not, then add it
  806. $synsql = "SELECT * FROM {synonym}
  807. WHERE name = '%s' and type_id = %d";
  808. $synonym = db_fetch_object(db_query($synsql, $alias, $syntype->cvterm_id));
  809. if (!$synonym) {
  810. $sql = "INSERT INTO {synonym}
  811. (name,type_id,synonym_sgml)
  812. VALUES ('%s',%d,'%s')";
  813. $result = db_query($sql, $alias, $syntype->cvterm_id, '');
  814. if (!$result) {
  815. print "ERROR: cannot add alias $alias to synonym table\n";
  816. }
  817. }
  818. $synonym = db_fetch_object(db_query($synsql, $alias, $syntype->cvterm_id));
  819. // check to see if we have a NULL publication in the pub table. If not,
  820. // then add one.
  821. $pubsql = "SELECT * FROM {pub} WHERE uniquename = 'null'";
  822. $pub = db_fetch_object(db_query($pubsql));
  823. if (!$pub) {
  824. $sql = "INSERT INTO pub (uniquename,type_id) VALUES ('%s',
  825. (SELECT cvterm_id
  826. FROM cvterm CVT
  827. INNER JOIN dbxref DBX on DBX.dbxref_id = CVT.dbxref_id
  828. INNER JOIN db DB on DB.db_id = DBX.db_id
  829. WHERE CVT.name = 'null' and DB.name = 'null')";
  830. $result = db_query($sql, 'null');
  831. if (!$result) {
  832. print "ERROR: cannot add null publication needed for setup of alias\n";
  833. return 0;
  834. }
  835. }
  836. $pub = db_fetch_object(db_query($pubsql));
  837. // check to see if the synonym exists in the feature_synonym table
  838. // if not, then add it.
  839. $synsql = "SELECT * FROM {feature_synonym}
  840. WHERE synonym_id = %d and feature_id = %d and pub_id = %d";
  841. $fsyn = db_fetch_object(db_query($synsql, $synonym->synonym_id, $feature->feature_id, $pub->pub_id));
  842. if (!$fsyn) {
  843. $sql = "INSERT INTO {feature_synonym}
  844. (synonym_id,feature_id,pub_id)
  845. VALUES (%d,%d,%d)";
  846. $result = db_query($sql, $synonym->synonym_id, $feature->feature_id, $pub->pub_id);
  847. if (!$result) {
  848. print "ERROR: cannot add alias $alias to feature synonym table\n";
  849. return 0;
  850. }
  851. }
  852. else {
  853. print " Synonym $alias already exists. Skipping\n";
  854. }
  855. }
  856. return 1;
  857. }
  858. /**
  859. *
  860. *
  861. * @ingroup gff3_loader
  862. */
  863. function tripal_feature_load_gff3_feature($organism, $analysis_id, $cvterm, $uniquename, $name,
  864. $residues, $is_analysis='f', $is_obsolete='f', $add_only, $score) {
  865. // check to see if the feature already exists
  866. $feature_sql = "SELECT * FROM {feature}
  867. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  868. $feature = db_fetch_object(db_query($feature_sql, $organism->organism_id, $uniquename, $cvterm->cvterm_id));
  869. if (strcmp($is_obsolete, 'f')==0) {
  870. $is_obsolete = 'false';
  871. }
  872. if (strcmp($is_analysis, 'f')==0) {
  873. $is_analysis = 'false';
  874. }
  875. // insert the feature if it does not exist otherwise perform an update
  876. if (!$feature) {
  877. print "Adding feature '$uniquename' ($cvterm->name)\n";
  878. $isql = "INSERT INTO {feature} (organism_id, name, uniquename, residues, seqlen,
  879. md5checksum, type_id,is_analysis,is_obsolete)
  880. VALUES(%d,'%s','%s','%s',%d, '%s', %d, %s, %s)";
  881. $result = db_query($isql, $organism->organism_id, $name, $uniquename, $residues, drupal_strlen($residues),
  882. md5($residues), $cvterm->cvterm_id, $is_analysis, $is_obsolete);
  883. if (!$result) {
  884. print "ERROR: failed to insert feature '$uniquename' ($cvterm->name)\n";
  885. return 0;
  886. }
  887. }
  888. elseif (!$add_only) {
  889. print "Updating feature '$uniquename' ($cvterm->name)\n";
  890. $usql = "UPDATE {feature}
  891. SET name = '%s', residues = '%s', seqlen = '%s', md5checksum = '%s',
  892. is_analysis = %s, is_obsolete = %s
  893. WHERE organism_id = %d and uniquename = '%s' and type_id = %d";
  894. $result = db_query($usql, $name, $residues, drupal_strlen($residues), md5($residues), $is_analysis, $is_obsolete,
  895. $organism_id, $uniquename, $cvterm->cvterm_id);
  896. if (!$result) {
  897. print "ERROR: failed to update feature '$uniquename' ($cvterm->name)\n";
  898. return 0;
  899. }
  900. }
  901. else {
  902. // the feature exists and we don't want to update it so return
  903. // a value of 0. This will stop all downstream property additions
  904. print "Skipping existing feature: '$uniquename' ($cvterm->name).\n";
  905. return 0;
  906. }
  907. // get the newly added feature
  908. $feature = db_fetch_object(db_query($feature_sql, $organism->organism_id, $uniquename, $cvterm->cvterm_id));
  909. // add the analysisfeature entry to the analysisfeature table if it doesn't already exist
  910. $af_values = array(
  911. 'analysis_id' => $analysis_id,
  912. 'feature_id' => $feature->feature_id
  913. );
  914. $afeature = tripal_core_chado_select('analysisfeature', array('analysisfeature_id'), $af_values, array('has_record'));
  915. if (count($afeature)==0) {
  916. // if a score is avaialble then set that to be the significance field
  917. if (strcmp($score, '.')!=0) {
  918. $af_values['significance'] = $score;
  919. }
  920. if (!tripal_core_chado_insert('analysisfeature', $af_values)) {
  921. print "ERROR: could not add analysisfeature record: $analysis_id, $feature->feature_id\n";
  922. }
  923. else {
  924. print " Added analysisfeature record\n";
  925. }
  926. }
  927. else {
  928. // if a score is available then set that to be the significance field
  929. $new_vals = array();
  930. if (strcmp($score, '.')!=0) {
  931. $new_vals['significance'] = $score;
  932. }
  933. else {
  934. $new_vals['significance'] = '__NULL__';
  935. }
  936. if (!$add_only) {
  937. $ret = tripal_core_chado_update('analysisfeature', $af_values, $new_vals);
  938. if (!$ret) {
  939. print "ERROR: could not update analysisfeature record: $analysis_id, $feature->feature_id\n";
  940. }
  941. else {
  942. print " Updated analysisfeature record\n";
  943. }
  944. }
  945. }
  946. return $feature;
  947. }
  948. /**
  949. *
  950. *
  951. * @ingroup gff3_loader
  952. */
  953. function tripal_feature_load_gff3_featureloc($feature, $organism, $landmark, $fmin,
  954. $fmax, $strand, $phase, $is_fmin_partial, $is_fmax_partial, $residue_info, $locgroup) {
  955. // get the source feature
  956. $sql = "SELECT * FROM {feature}
  957. WHERE organism_id = %d and uniquename = '%s'";
  958. $srcfeature = db_fetch_object(db_query($sql, $organism->organism_id, $landmark));
  959. if (!$srcfeature) {
  960. print "ERROR: cannot find landmark feature $landmark. Cannot add the feature location record\n";
  961. return 0;
  962. }
  963. // TODO: create an attribute that recognizes the residue_info,locgroup, is_fmin_partial and is_fmax_partial, right now these are
  964. // hardcoded to be false and 0 below.
  965. // check to see if this featureloc already exists, but also keep track of the
  966. // last rank value
  967. $rank = 0;
  968. $exists = 0;
  969. $featureloc_sql = "SELECT FL.featureloc_id,FL.fmin,FL.fmax,F.uniquename as srcname,
  970. rank
  971. FROM {featureloc} FL
  972. INNER JOIN {feature} F on F.feature_id = FL.srcfeature_id
  973. WHERE FL.feature_id = %d
  974. ORDER BY rank ASC";
  975. $recs = db_query($featureloc_sql, $feature->feature_id);
  976. while ($featureloc = db_fetch_object($recs)) {
  977. if (strcmp($featureloc->srcname, $landmark)==0 and
  978. $featureloc->fmin == $fmin and $featureloc->fmax == $fmax) {
  979. // this is the same featureloc, so do nothing... no need to update
  980. //TODO: need more checks here
  981. print " No change to featureloc\n";
  982. $exists = 1;
  983. }
  984. $rank = $featureloc->rank + 1;
  985. }
  986. if (!$exists) {
  987. $rank++;
  988. // this feature location is new so add it
  989. if (!$phase) {
  990. $phase = 'NULL';
  991. }
  992. if (strcmp($is_fmin_partial, 'f')==0) {
  993. $is_fmin_partial = 'false';
  994. }
  995. elseif (strcmp($is_fmin_partial, 't')==0) {
  996. $is_fmin_partial = 'true';
  997. }
  998. if (strcmp($is_fmax_partial, 'f')==0) {
  999. $is_fmax_partial = 'false';
  1000. }
  1001. elseif (strcmp($is_fmax_partial, 't')==0) {
  1002. $is_fmax_partial = 'true';
  1003. }
  1004. print " Adding featureloc $srcfeature->uniquename fmin: $fmin, fmax: $fmax, strand: $strand, phase: $phase, rank: $rank\n";
  1005. $fl_isql = "INSERT INTO {featureloc}
  1006. (feature_id, srcfeature_id, fmin, is_fmin_partial, fmax, is_fmax_partial,
  1007. strand, phase, residue_info, locgroup, rank)
  1008. VALUES (%d,%d,%d,%s,%d,%s,%d,%s,'%s',%d,%d)";
  1009. $result = db_query($fl_isql, $feature->feature_id, $srcfeature->feature_id, $fmin, $is_fmin_partial, $fmax, $is_fmax_partial,
  1010. $strand, $phase, $residue_info, $locgroup, $rank);
  1011. if (!$result) {
  1012. print "ERROR: failed to insert featureloc\n";
  1013. exit;
  1014. return 0;
  1015. }
  1016. }
  1017. return 1;
  1018. }
  1019. /**
  1020. *
  1021. *
  1022. * @ingroup gff3_loader
  1023. */
  1024. function tripal_feature_load_gff3_property($feature, $property, $value) {
  1025. // first make sure the cvterm exists. If the term already exists then
  1026. // the function should return it of not, then add it
  1027. $cvt_sql = "SELECT * FROM {cvterm} CVT
  1028. INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
  1029. WHERE CV.name = '%s' and CVT.name = '%s'";
  1030. $cvterm = db_fetch_object(db_query($cvt_sql, 'feature_property', $property));
  1031. if (!$cvterm) {
  1032. $term = array(
  1033. 'id' => "null:$property",
  1034. 'name' => $property,
  1035. 'namespace' => 'feature_property',
  1036. 'is_obsolete' => 0,
  1037. );
  1038. print " Adding cvterm, $property\n";
  1039. $cvterm = (object) tripal_cv_add_cvterm($term, 'feature_property', 0, 0);
  1040. }
  1041. if (!$cvterm) {
  1042. print "ERROR: cannot add cvterm, $property\n";
  1043. exit;
  1044. }
  1045. // check to see if the property already exists for this feature
  1046. // if it does but the value is unique then increment the rank and add it.
  1047. // if the value is not unique then don't add it.
  1048. $add = 1;
  1049. $rank = 0;
  1050. $sql = "SELECT rank,value FROM {featureprop}
  1051. WHERE feature_id = %d and type_id = %d
  1052. ORDER BY rank ASC";
  1053. $result = db_query($sql, $feature->feature_id, $cvterm->cvterm_id);
  1054. while ($prop = db_fetch_object($result)) {
  1055. if (strcmp($prop->value, $value)==0) {
  1056. $add = NULL; // don't add it, it already exists
  1057. print " Property already exists, skipping\n";
  1058. }
  1059. $rank = $prop->rank + 1;
  1060. }
  1061. // add the property if we pass the check above
  1062. if ($add) {
  1063. print " Setting feature property. $property: $value\n";
  1064. $isql = "INSERT INTO {featureprop} (feature_id,type_id,value,rank)
  1065. VALUES (%d,%d,'%s',%d)";
  1066. db_query($isql, $feature->feature_id, $cvterm->cvterm_id, $value, $rank);
  1067. }
  1068. }
  1069. /*
  1070. function tripal_feature_load_gff3_property($feature,$property,$value){
  1071. // first make sure the cvterm exists. If the term already exists then
  1072. // the function should return it
  1073. $match = array(
  1074. 'name' => $property,
  1075. 'cv_id' => array(
  1076. 'name' => 'feature_property',
  1077. ),
  1078. );
  1079. $cvterm = tripal_core_chado_select('cvterm',array('*'),$match);
  1080. if(sizeof($cvterm) == 0){
  1081. $term = array(
  1082. 'id' => "null:$property",
  1083. 'name' => $property,
  1084. 'namespace' => 'feature_property',
  1085. 'is_obsolete' => 0,
  1086. );
  1087. print " Adding cvterm, $property\n";
  1088. $cvterm = tripal_cv_add_cvterm($term,'feature_property',0,0);
  1089. }
  1090. if(!$cvterm){
  1091. print "ERROR: cannot add cvterm, $property, before adding property\n";
  1092. exit;
  1093. }
  1094. // next give the feature the property
  1095. tripal_core_insert_property('feature',$feature->feature_id,$property,'feature_property',$value,1);
  1096. }
  1097. */