tripal_stock.api.inc 19 KB

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