tripal_stock.api.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. <?php
  2. /**
  3. * Purpose: Return a given stock node using the nid
  4. *
  5. * @param $nid
  6. * The node ID of the stock you want to load
  7. *
  8. * @return
  9. * stock node with the passed in node ID
  10. *
  11. * @ingroup tripal_api
  12. */
  13. function triapl_stock_get_stock_by_nid ($nid) {
  14. return node_load($nid);
  15. }
  16. /**
  17. * Purpose: Return a given stock object using the stock id
  18. *
  19. * @return
  20. * Stock object created by node load
  21. *
  22. * @ingroup tripal_api
  23. */
  24. function tripal_stock_get_stock_by_stock_id ($stock_id) {
  25. $sql = "SELECT nid FROM {chado_stock} WHERE stock_id=%d";
  26. $r = db_fetch_object(db_query($sql, $stock_id));
  27. if (!empty($r->nid)) {
  28. return node_load($r->nid);
  29. } else {
  30. watchdog('tripal_stock', 'tripal_stock_get_stock_by_stock_id(!stock_id): no stock with that stock_id is sync\'d with drupal', array('!stock_id' => $stock_id), WATCHDOG_WARNING);
  31. }
  32. return 0;
  33. }
  34. /**
  35. * Purpose:
  36. *
  37. * @param $stock_id
  38. * the unique identifier for the stock to load properties of
  39. *
  40. * @return
  41. * an array of properties where each element is a property object with
  42. * the following fields:
  43. * - stockprop_id (stockprop):
  44. * - value (stockprop):
  45. * - rank (stockprop):
  46. * - type_id (stockprop):
  47. * - type_name (cvterm):
  48. * - type_definition (cvterm):
  49. * - type_is_obsolete (cvterm):
  50. * - type_is_relationshiptype (cvterm):
  51. * - type_cv_id (cvterm):
  52. * - type_db_reference_id (cvterm):
  53. * - type_cv_name (cv):
  54. * - type_cv_definition (cv):
  55. * - type_db_reference_acession (dbxref):
  56. * - type_db_reference_version (dbxref):
  57. * - type_db_reference_description (dbxref):
  58. * - type_db_reference_db_id (dbxref):
  59. * - type_db_reference_db_name (db):
  60. * - type_db_reference_db_description (db):
  61. * - type_db_reference_db_url (db):
  62. * - type_db_reference_db_urlprefix (db):
  63. *
  64. * @ingroup tripal_api
  65. */
  66. function tripal_stock_load_properties($stock_id) {
  67. $properties = tripal_core_chado_select(
  68. 'stockprop',
  69. array('stockprop_id', 'value', 'rank','type_id'),
  70. array('stock_id' => $stock_id)
  71. );
  72. return $properties;
  73. }
  74. /**
  75. * Purpose: Returns all stocks currently sync'd with drupal
  76. *
  77. * @return
  78. * An array of node objects keyed by stock_id
  79. *
  80. * @ingroup tripal_api
  81. */
  82. function tripal_stock_get_all_stocks() {
  83. $sql = "SELECT stock_id, nid from {chado_stock}";
  84. $resource = db_query($sql);
  85. $stocks = array();
  86. while ($r = db_fetch_object($resource)) {
  87. $node = node_load($r->nid);
  88. if ($node) {
  89. $stocks[$r->stock_id] = $node;
  90. }
  91. }
  92. return $stocks;
  93. }
  94. /**
  95. * Purpose: Return all stocks that match a given criteria
  96. *
  97. * @param $values
  98. * An associative array containing the values for filtering the results.
  99. *
  100. * @return
  101. * An array of matching stock objects (produced using node_load)
  102. * matching the given criteria
  103. *
  104. * Example usage:
  105. * @code
  106. * $values = array(
  107. * 'organism_id' => array(
  108. * 'genus' => 'Lens',
  109. * 'species' => 'culinaris',
  110. * ),
  111. * 'name' => 'CDC Redberry',
  112. * 'type_id' => array (
  113. * 'cv_id' => array (
  114. * 'name' => 'germplasm',
  115. * ),
  116. * 'name' => 'registered_cultivar',
  117. * 'is_obsolete' => 0
  118. * ),
  119. * );
  120. * $result = tripal_stock_get_stocks($values);
  121. * @endcode
  122. * The above code selects a record from the chado stock table using three fields with values which
  123. * identify a stock or multiple stocks. Then the node for each stock identified is returned, if it
  124. * exists. The $values array is nested such that the organism is identified by way of the
  125. * organism_id foreign key constraint by specifying the genus and species. The cvterm is also
  126. * specified using its foreign key and the cv_id for the cvterm is nested as well.
  127. *
  128. * @ingroup tripal_api
  129. */
  130. function tripal_stock_get_stocks($values) {
  131. $stock_ids = tripal_core_chado_select('stock',array('stock_id'),$values);
  132. // Change from stock_ids to nodes-----------------------------------
  133. $stock_ids = array_filter($stock_ids);
  134. $stock_ids = array_unique($stock_ids);
  135. $stocks = array();
  136. foreach ($stock_ids as $stock_id) {
  137. $node = tripal_stock_get_stock_by_stock_id($stock_id->stock_id);
  138. if ($node) {
  139. $stocks[] = $node;
  140. }
  141. }
  142. return $stocks;
  143. }
  144. /**
  145. * Purpose: Retrieve stocks based on associated stock properties
  146. *
  147. * @param $stockprop_values
  148. * An array of column_name => value where column_name is any column in the stockprop table
  149. * and value is the value you want that column to be. This is used as a tripal_core_chado_select
  150. * values array so nesting is allowed.
  151. * @param $stock_values
  152. * An array of column_name => value where column_name is any column in the stock table
  153. * and value is the value you want that column to be. This is used as a tripal_core_chado_select
  154. * values array so nesting is allowed.
  155. *
  156. * @return
  157. * An array of stock node objects
  158. *
  159. * Example usage:
  160. * @code
  161. * $stockprop_values = array(
  162. * 'value' => 'CDC Redberry',
  163. * 'type_id' => array (
  164. * 'cv_id' => array (
  165. * 'name' => 'stock_properties',
  166. * ),
  167. * 'name' => 'synonym',
  168. * 'is_obsolete' => 0
  169. * ),
  170. * );
  171. * $stock_values = array(
  172. * 'organism_id' => array(
  173. * 'genus' => 'Lens',
  174. * 'species' => 'culinaris',
  175. * ),
  176. * );
  177. * $result = tripal_stock_get_stocks_by_stockprop($stockprop_values, $stock_values);
  178. * @endcode
  179. * The above code selects all Lens culinaris stocks with the synonym (stock property) CDC Redberry.
  180. * The nodes for each stock selected are loaded and returned in an array.
  181. *
  182. * @ingroup tripal_api
  183. */
  184. function tripal_stock_get_stocks_by_stockprop($stockprop_values, $stock_values) {
  185. //add stock values to stockprop values
  186. if (!empty($stock_values)) {
  187. $stockprop_values['stock_id'] = $stock_values;
  188. }
  189. //get stock_ids from stockprop table
  190. $stock_ids = tripal_core_chado_select('stockprop',array('stock_id'),$stockprop_values);
  191. // Change from stock_ids to nodes-----------------------------------
  192. $stock_ids = array_filter($stock_ids);
  193. $stock_ids = array_unique($stock_ids);
  194. $stocks = array();
  195. foreach ($stock_ids as $stock_id) {
  196. $node = tripal_stock_get_stock_by_stock_id($stock_id->stock_id);
  197. if ($node) {
  198. $stocks[] = $node;
  199. }
  200. }
  201. return $stocks;
  202. }
  203. /**
  204. * Purpose: Return all stocks with a given name identifier
  205. * which might match stock.name, stock.uniquename, dbxref.accession,
  206. * stockprop.value where stockprop.type='synonym'
  207. *
  208. * @param $name
  209. * The name identfier to be used
  210. * @param $organism_id
  211. * The stock.organism_id of the stock to be selected
  212. *
  213. * @return
  214. * An array of stock node objects
  215. *
  216. * @ingroup tripal_api
  217. */
  218. function tripal_stock_get_stock_by_name_identifier($name, $organism_id) {
  219. $stock_ids = array();
  220. // where name_identifier = stock.name-------------------------------
  221. $current_stocks = tripal_core_chado_select('stock',array('stock_id'),
  222. array(
  223. 'name' => $name,
  224. 'organism_id' => $organism_id,
  225. )
  226. );
  227. if (!empty($current_stocks)) {
  228. foreach ($current_stocks as $c) { $stock_ids[] = $c->stock_id; }
  229. }
  230. // where name_identifier = stock.uniquename-------------------------------
  231. $current_stocks = tripal_core_chado_select('stock',array('stock_id'),
  232. array(
  233. 'uniquename' => $name,
  234. 'organism_id' => $organism_id,
  235. )
  236. );
  237. if (!empty($current_stocks)) {
  238. foreach ($current_stocks as $c) { $stock_ids[] = $c->stock_id; }
  239. }
  240. // where name_identifier = dbxref.accession-------------------------------
  241. // linked to stock through stock.dbxref
  242. $current_stocks = tripal_core_chado_select('stock',array('stock_id'),
  243. array(
  244. 'dbxref_id' => array(
  245. 'accession' => $name,
  246. ),
  247. 'organism_id' => $organism_id,
  248. )
  249. );
  250. if (!empty($current_stocks)) {
  251. foreach ($current_stocks as $c) { $stock_ids[] = $c->stock_id; }
  252. }
  253. // linked to stock through stock_dbxref?
  254. $current_stocks = tripal_core_chado_select('stock_dbxref',array('stock_id'),
  255. array(
  256. 'dbxref_id' => array(
  257. 'accession' => $name,
  258. ),
  259. 'stock_id' => array(
  260. 'organism_id' => $organism_id,
  261. ),
  262. )
  263. );
  264. if (!empty($current_stocks)) {
  265. foreach ($current_stocks as $c) { $stock_ids[] = $c->stock_id; }
  266. }
  267. // where name_identifier = stockprop.value-------------------------------
  268. // where type='synonym'
  269. $current_stocks = tripal_core_chado_select('stockprop',array('stock_id'),
  270. array(
  271. 'stock_id' => array(
  272. 'organism_id' => $organism_id,
  273. ),
  274. 'type_id' => array(
  275. 'cv_id' => variable_get('chado_stock_prop_types_cv', 'null'),
  276. 'name' => 'synonym',
  277. ),
  278. 'value' => $name,
  279. )
  280. );
  281. if (!empty($current_stocks)) {
  282. foreach ($current_stocks as $c) { $stock_ids[] = $c->stock_id; }
  283. }
  284. // Change from stock_ids to nodes-----------------------------------
  285. $stock_ids = array_filter($stock_ids);
  286. $stock_ids = array_unique($stock_ids);
  287. $stocks = array();
  288. foreach ($stock_ids as $stock_id) {
  289. $node = tripal_stock_get_stock_by_stock_id($stock_id);
  290. if ($node) {
  291. $stocks[] = $node;
  292. }
  293. }
  294. return $stocks;
  295. }
  296. /**
  297. * Implements hook_chado_stock_schema()
  298. *
  299. * Purpose: To add descriptions and foreign keys to default table description
  300. * Note: This array will be merged with the array from all other implementations
  301. *
  302. * @return
  303. * Array describing the stock table
  304. *
  305. * @ingroup tripal_api
  306. */
  307. function tripal_stock_chado_stock_schema() {
  308. $description = array();
  309. $description['description'] = 'Any stock can be globally identified by the combination of organism, uniquename and stock type. A stock is the physical entities, either living or preserved, held by collections. Stocks belong to a collection; they have IDs, type, organism, description and may have a genotype.';
  310. $description['foreign keys']['organism'] = array(
  311. 'table' => 'organism',
  312. 'columns' => array(
  313. 'organism_id' => 'organism_id',
  314. ),
  315. );
  316. $description['foreign keys']['dbxref'] = array(
  317. 'table' => 'dbxref',
  318. 'columns' => array(
  319. 'dbxref_id' => 'dbxref_id',
  320. ),
  321. );
  322. $description['foreign keys']['cvterm'] = array(
  323. 'table' => 'cvterm',
  324. 'columns' => array(
  325. 'type_id' => 'cvterm_id',
  326. ),
  327. );
  328. $referring_tables = array(
  329. 'stock_cvterm',
  330. 'stock_dbxref',
  331. 'stock_genotype',
  332. 'stock_pub',
  333. 'stock_relationship',
  334. 'stockcollection_stock',
  335. 'stockprop'
  336. );
  337. $description['referring_tables'] = $referring_tables;
  338. return $description;
  339. }
  340. /**
  341. * Implements hook_chado_stockprop_schema()
  342. *
  343. * Purpose: To add descriptions and foreign keys to default table description
  344. * Note: This array will be merged with the array from all other implementations
  345. *
  346. * @return
  347. * Array describing the stockprop table
  348. *
  349. * @ingroup tripal_api
  350. */
  351. function tripal_stock_chado_stockprop_schema() {
  352. $description = array();
  353. $description['foreign keys']['cvterm'] = array(
  354. 'table' => 'cvterm',
  355. 'columns' => array(
  356. 'type_id' => 'cvterm_id',
  357. ),
  358. );
  359. $description['foreign keys']['stock'] = array(
  360. 'table' => 'stock',
  361. 'columns' => array(
  362. 'stock_id' => 'stock_id',
  363. ),
  364. );
  365. return $description;
  366. }
  367. /**
  368. * Implements hook_chado_stockprop_pub_schema()
  369. *
  370. * Purpose: To add descriptions and foreign keys to default table description
  371. * Note: This array will be merged with the array from all other implementations
  372. *
  373. * @return
  374. * Array describing the stockprop_pub table
  375. *
  376. * @ingroup tripal_api
  377. */
  378. function tripal_stock_chado_stockprop_pub_schema() {
  379. $description = array();
  380. $description['foreign keys']['pub'] = array(
  381. 'table' => 'pub',
  382. 'columns' => array(
  383. 'pub_id' => 'pub_id',
  384. ),
  385. );
  386. $description['foreign keys']['stockprop'] = array(
  387. 'table' => 'stockprop',
  388. 'columns' => array(
  389. 'stockprop_id' => 'stockprop_id',
  390. ),
  391. );
  392. return $description;
  393. }
  394. /**
  395. * Implements hook_chado_stock_cvterm_schema()
  396. *
  397. * Purpose: To add descriptions and foreign keys to default table description
  398. * Note: This array will be merged with the array from all other implementations
  399. *
  400. * @return
  401. * Array describing the stock_cvterm table
  402. *
  403. * @ingroup tripal_api
  404. */
  405. function tripal_stock_chado_stock_cvterm_schema() {
  406. $description = array();
  407. $description['foreign keys']['cvterm'] = array(
  408. 'table' => 'cvterm',
  409. 'columns' => array(
  410. 'cvterm_id' => 'cvterm_id',
  411. ),
  412. );
  413. $description['foreign keys']['stock'] = array(
  414. 'table' => 'stock',
  415. 'columns' => array(
  416. 'stock_id' => 'stock_id',
  417. ),
  418. );
  419. $description['foreign keys']['pub'] = array(
  420. 'table' => 'pub',
  421. 'columns' => array(
  422. 'pub_id' => 'pub_id',
  423. ),
  424. );
  425. return $description;
  426. }
  427. /**
  428. * Implements hook_chado_stock_dbxref_schema()
  429. *
  430. * Purpose: To add descriptions and foreign keys to default table description
  431. * Note: This array will be merged with the array from all other implementations
  432. *
  433. * @return
  434. * Array describing the stock_dbxref table
  435. *
  436. * @ingroup tripal_api
  437. */
  438. function tripal_stock_chado_stock_dbxref_schema() {
  439. $description = array();
  440. $description['foreign keys']['dbxref'] = array(
  441. 'table' => 'dbxref',
  442. 'columns' => array(
  443. 'dbxref_id' => 'dbxref_id',
  444. ),
  445. );
  446. $description['foreign keys']['stock'] = array(
  447. 'table' => 'stock',
  448. 'columns' => array(
  449. 'stock_id' => 'stock_id',
  450. ),
  451. );
  452. return $description;
  453. }
  454. /**
  455. * Implements hook_chado_stock_genotype_schema()
  456. *
  457. * Purpose: To add descriptions and foreign keys to default table description
  458. * Note: This array will be merged with the array from all other implementations
  459. *
  460. * @return
  461. * Array describing the stock_genotype table
  462. *
  463. * @ingroup tripal_api
  464. */
  465. function tripal_stock_chado_stock_genotype_schema() {
  466. $description = array();
  467. $description['foreign keys']['genotype'] = array(
  468. 'table' => 'genotype',
  469. 'columns' => array(
  470. 'genotype_id' => 'genotype_id',
  471. ),
  472. );
  473. $description['foreign keys']['stock'] = array(
  474. 'table' => 'stock',
  475. 'columns' => array(
  476. 'stock_id' => 'stock_id',
  477. ),
  478. );
  479. return $description;
  480. }
  481. /**
  482. * Implements hook_chado_stock_pub_schema()
  483. *
  484. * Purpose: To add descriptions and foreign keys to default table description
  485. * Note: This array will be merged with the array from all other implementations
  486. *
  487. * @return
  488. * Array describing the stock_pub table
  489. *
  490. * @ingroup tripal_api
  491. */
  492. function tripal_stock_chado_stock_pub_schema() {
  493. $description = array();
  494. $description['foreign keys']['pub'] = array(
  495. 'table' => 'pub',
  496. 'columns' => array(
  497. 'pub_id' => 'pub_id',
  498. ),
  499. );
  500. $description['foreign keys']['stock'] = array(
  501. 'table' => 'stock',
  502. 'columns' => array(
  503. 'stock_id' => 'stock_id',
  504. ),
  505. );
  506. return $description;
  507. }
  508. /**
  509. * Implements hook_chado_stock_relationship_schema()
  510. *
  511. * Purpose: To add descriptions and foreign keys to default table description
  512. * Note: This array will be merged with the array from all other implementations
  513. *
  514. * @return
  515. * Array describing the stock_relationship table
  516. *
  517. * @ingroup tripal_api
  518. */
  519. function tripal_stock_chado_stock_relationship_schema() {
  520. $description = array();
  521. $description['foreign keys']['cvterm'] = array(
  522. 'table' => 'cvterm',
  523. 'columns' => array(
  524. 'type_id' => 'cvterm_id',
  525. ),
  526. );
  527. $description['foreign keys']['stock'] = array(
  528. 'table' => 'stock',
  529. 'columns' => array(
  530. 'subject_id' => 'stock_id',
  531. 'object_id' => 'stock_id',
  532. ),
  533. );
  534. return $description;
  535. }
  536. /**
  537. * Implements hook_chado_stock_relationship_pub_schema()
  538. *
  539. * Purpose: To add descriptions and foreign keys to default table description
  540. * Note: This array will be merged with the array from all other implementations
  541. *
  542. * @return
  543. * Array describing the stock_relationship_pub table
  544. *
  545. * @ingroup tripal_api
  546. */
  547. function tripal_stock_chado_stock_relationship_pub_schema() {
  548. $description = array();
  549. $description['foreign keys']['pub'] = array(
  550. 'table' => 'pub',
  551. 'columns' => array(
  552. 'pub_id' => 'pub_id',
  553. ),
  554. );
  555. $description['foreign keys']['stock_relationship'] = array(
  556. 'table' => 'stock_relationship',
  557. 'columns' => array(
  558. 'stock_relationship_id' => 'stock_relationship_id',
  559. ),
  560. );
  561. return $description;
  562. }
  563. /**
  564. * Implements hook_chado_stockcollection_schema()
  565. *
  566. * Purpose: To add descriptions and foreign keys to default table description
  567. * Note: This array will be merged with the array from all other implementations
  568. *
  569. * @return
  570. * Array describing the stockcollection table
  571. *
  572. * @ingroup tripal_api
  573. */
  574. function tripal_stock_chado_stockcollection_schema() {
  575. $description = array();
  576. $description['foreign keys']['cvterm'] = array(
  577. 'table' => 'cvterm',
  578. 'columns' => array(
  579. 'type_id' => 'cvterm_id',
  580. ),
  581. );
  582. $description['foreign keys']['contact'] = array(
  583. 'table' => 'contact',
  584. 'columns' => array(
  585. 'contact_id' => 'contact_id',
  586. ),
  587. );
  588. return $description;
  589. }
  590. /**
  591. * Implements hook_chado_stockcollection_stock_schema()
  592. *
  593. * Purpose: To add descriptions and foreign keys to default table description
  594. * Note: This array will be merged with the array from all other implementations
  595. *
  596. * @return
  597. * Array describing the stockcollection_stock table
  598. *
  599. * @ingroup tripal_api
  600. */
  601. function tripal_stock_chado_stockcollection_stock_schema() {
  602. $description = array();
  603. $description['foreign keys']['stock'] = array(
  604. 'table' => 'stock',
  605. 'columns' => array(
  606. 'stock_id' => 'stock_id',
  607. ),
  608. );
  609. $description['foreign keys']['stockcollection'] = array(
  610. 'table' => 'stockcollection',
  611. 'columns' => array(
  612. 'stockcollection_id' => 'stockcollection_id',
  613. ),
  614. );
  615. return $description;
  616. }
  617. /**
  618. * Implements hook_chado_stockcollectionprop_schema()
  619. *
  620. * Purpose: To add descriptions and foreign keys to default table description
  621. * Note: This array will be merged with the array from all other implementations
  622. *
  623. * @return
  624. * Array describing the stockcollectionprop table
  625. *
  626. * @ingroup tripal_api
  627. */
  628. function tripal_stock_chado_stockcollectionprop_schema() {
  629. $description = array();
  630. $description['foreign keys']['cvterm'] = array(
  631. 'table' => 'cvterm',
  632. 'columns' => array(
  633. 'type_id' => 'cvterm_id',
  634. ),
  635. );
  636. $description['foreign keys']['stockcollection'] = array(
  637. 'table' => 'stockcollection',
  638. 'columns' => array(
  639. 'stockcollection_id' => 'stockcollection_id',
  640. ),
  641. );
  642. return $description;
  643. }