tripal_stock.api.inc 18 KB

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