tripal_stock.api.inc 18 KB

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