tripal_stock.api.inc 18 KB

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