tripal_stock.api.inc 18 KB

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