tripal_stock.api.inc 20 KB

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