tripal_stock.api.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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:
  36. *
  37. * @param $stock_id
  38. * the unique identifier for the stock to load properties of
  39. *
  40. * @return
  41. * an array of properties where each element is a property object with
  42. * the following fields:
  43. * - stockprop_id (stockprop):
  44. * - value (stockprop):
  45. * - rank (stockprop):
  46. * - type_id (stockprop):
  47. * - type_name (cvterm):
  48. * - type_definition (cvterm):
  49. * - type_is_obsolete (cvterm):
  50. * - type_is_relationshiptype (cvterm):
  51. * - type_cv_id (cvterm):
  52. * - type_db_reference_id (cvterm):
  53. * - type_cv_name (cv):
  54. * - type_cv_definition (cv):
  55. * - type_db_reference_acession (dbxref):
  56. * - type_db_reference_version (dbxref):
  57. * - type_db_reference_description (dbxref):
  58. * - type_db_reference_db_id (dbxref):
  59. * - type_db_reference_db_name (db):
  60. * - type_db_reference_db_description (db):
  61. * - type_db_reference_db_url (db):
  62. * - type_db_reference_db_urlprefix (db):
  63. *
  64. * @ingroup tripal_api
  65. */
  66. function tripal_stock_load_properties($stock_id) {
  67. $properties = tripal_core_chado_select(
  68. 'stockprop',
  69. array('stockprop_id', 'value', 'rank','type_id'),
  70. array('stock_id' => $stock_id)
  71. );
  72. dpm($properties);
  73. return $properties;
  74. }
  75. /**
  76. * Purpose: Returns all stocks currently sync'd with drupal
  77. *
  78. * @return
  79. * An array of node objects keyed by stock_id
  80. *
  81. * @ingroup tripal_api
  82. */
  83. function tripal_stock_get_all_stocks() {
  84. $sql = "SELECT stock_id, nid from {chado_stock}";
  85. $resource = db_query($sql);
  86. $stocks = array();
  87. while ($r = db_fetch_object($resource)) {
  88. $node = node_load($r->nid);
  89. if ($node) {
  90. $stocks[$r->stock_id] = $node;
  91. }
  92. }
  93. return $stocks;
  94. }
  95. /**
  96. * Purpose: Return all stocks that match a given criteria
  97. *
  98. * @param $values
  99. * An associative array containing the values for filtering the results.
  100. *
  101. * @return
  102. * An array of matching stock objects (produced using node_load)
  103. * matching the given criteria
  104. *
  105. * Example usage:
  106. * @code
  107. * $values = array(
  108. * 'organism_id' => array(
  109. * 'genus' => 'Lens',
  110. * 'species' => 'culinaris',
  111. * ),
  112. * 'name' => 'CDC Redberry',
  113. * 'type_id' => array (
  114. * 'cv_id' => array (
  115. * 'name' => 'germplasm',
  116. * ),
  117. * 'name' => 'registered_cultivar',
  118. * 'is_obsolete' => 0
  119. * ),
  120. * );
  121. * $result = tripal_stock_get_stocks($values);
  122. * @endcode
  123. * The above code selects a record from the chado stock table using three fields with values which
  124. * identify a stock or multiple stocks. Then the node for each stock identified is returned, if it
  125. * exists. The $values array is nested such that the organism is identified by way of the
  126. * organism_id foreign key constraint by specifying the genus and species. The cvterm is also
  127. * specified using its foreign key and the cv_id for the cvterm is nested as well.
  128. *
  129. * @ingroup tripal_api
  130. */
  131. function tripal_stock_get_stocks($values) {
  132. $stock_ids = tripal_core_chado_select('stock',array('stock_id'),$values);
  133. // Change from stock_ids to nodes-----------------------------------
  134. $stock_ids = array_filter($stock_ids);
  135. $stock_ids = array_unique($stock_ids);
  136. $stocks = array();
  137. foreach ($stock_ids as $stock_id) {
  138. $node = tripal_stock_get_stock_by_stock_id($stock_id->stock_id);
  139. if ($node) {
  140. $stocks[] = $node;
  141. }
  142. }
  143. return $stocks;
  144. }
  145. /**
  146. * Purpose: Retrieve stocks based on associated stock properties
  147. *
  148. * @param $stockprop_values
  149. * An array of column_name => value where column_name is any column in the stockprop table
  150. * and value is the value you want that column to be. This is used as a tripal_core_chado_select
  151. * values array so nesting is allowed.
  152. * @param $stock_values
  153. * An array of column_name => value where column_name is any column in the stock table
  154. * and value is the value you want that column to be. This is used as a tripal_core_chado_select
  155. * values array so nesting is allowed.
  156. *
  157. * @return
  158. * An array of stock node objects
  159. *
  160. * Example usage:
  161. * @code
  162. * $stockprop_values = array(
  163. * 'value' => 'CDC Redberry',
  164. * 'type_id' => array (
  165. * 'cv_id' => array (
  166. * 'name' => 'stock_properties',
  167. * ),
  168. * 'name' => 'synonym',
  169. * 'is_obsolete' => 0
  170. * ),
  171. * );
  172. * $stock_values = array(
  173. * 'organism_id' => array(
  174. * 'genus' => 'Lens',
  175. * 'species' => 'culinaris',
  176. * ),
  177. * );
  178. * $result = tripal_stock_get_stocks_by_stockprop($stockprop_values, $stock_values);
  179. * @endcode
  180. * The above code selects all Lens culinaris stocks with the synonym (stock property) CDC Redberry.
  181. * The nodes for each stock selected are loaded and returned in an array.
  182. *
  183. * @ingroup tripal_api
  184. */
  185. function tripal_stock_get_stocks_by_stockprop($stockprop_values, $stock_values) {
  186. //add stock values to stockprop values
  187. if (!empty($stock_values)) {
  188. $stockprop_values['stock_id'] = $stock_values;
  189. }
  190. //get stock_ids from stockprop table
  191. $stock_ids = tripal_core_chado_select('stockprop',array('stock_id'),$stockprop_values);
  192. // Change from stock_ids to nodes-----------------------------------
  193. $stock_ids = array_filter($stock_ids);
  194. $stock_ids = array_unique($stock_ids);
  195. $stocks = array();
  196. foreach ($stock_ids as $stock_id) {
  197. $node = tripal_stock_get_stock_by_stock_id($stock_id->stock_id);
  198. if ($node) {
  199. $stocks[] = $node;
  200. }
  201. }
  202. return $stocks;
  203. }
  204. /**
  205. * Purpose: Return all stocks with a given name identifier
  206. * which might match stock.name, stock.uniquename, dbxref.accession,
  207. * stockprop.value where stockprop.type='synonym'
  208. *
  209. * @param $name
  210. * The name identfier to be used
  211. * @param $organism_id
  212. * The stock.organism_id of the stock to be selected
  213. *
  214. * @return
  215. * An array of stock node objects
  216. *
  217. * @ingroup tripal_api
  218. */
  219. function tripal_stock_get_stock_by_name_identifier($name, $organism_id) {
  220. $stock_ids = array();
  221. // where name_identifier = stock.name-------------------------------
  222. $current_stocks = tripal_core_chado_select('stock',array('stock_id'),
  223. array(
  224. 'name' => $name,
  225. 'organism_id' => $organism_id,
  226. )
  227. );
  228. if (!empty($current_stocks)) {
  229. foreach ($current_stocks as $c) { $stock_ids[] = $c->stock_id; }
  230. }
  231. // where name_identifier = stock.uniquename-------------------------------
  232. $current_stocks = tripal_core_chado_select('stock',array('stock_id'),
  233. array(
  234. 'uniquename' => $name,
  235. 'organism_id' => $organism_id,
  236. )
  237. );
  238. if (!empty($current_stocks)) {
  239. foreach ($current_stocks as $c) { $stock_ids[] = $c->stock_id; }
  240. }
  241. // where name_identifier = dbxref.accession-------------------------------
  242. // linked to stock through stock.dbxref
  243. $current_stocks = tripal_core_chado_select('stock',array('stock_id'),
  244. array(
  245. 'dbxref_id' => array(
  246. 'accession' => $name,
  247. ),
  248. 'organism_id' => $organism_id,
  249. )
  250. );
  251. if (!empty($current_stocks)) {
  252. foreach ($current_stocks as $c) { $stock_ids[] = $c->stock_id; }
  253. }
  254. // linked to stock through stock_dbxref?
  255. $current_stocks = tripal_core_chado_select('stock_dbxref',array('stock_id'),
  256. array(
  257. 'dbxref_id' => array(
  258. 'accession' => $name,
  259. ),
  260. 'stock_id' => array(
  261. 'organism_id' => $organism_id,
  262. ),
  263. )
  264. );
  265. if (!empty($current_stocks)) {
  266. foreach ($current_stocks as $c) { $stock_ids[] = $c->stock_id; }
  267. }
  268. // where name_identifier = stockprop.value-------------------------------
  269. // where type='synonym'
  270. $current_stocks = tripal_core_chado_select('stockprop',array('stock_id'),
  271. array(
  272. 'stock_id' => array(
  273. 'organism_id' => $organism_id,
  274. ),
  275. 'type_id' => array(
  276. 'cv_id' => variable_get('chado_stock_prop_types_cv', 'null'),
  277. 'name' => 'synonym',
  278. ),
  279. 'value' => $name,
  280. )
  281. );
  282. if (!empty($current_stocks)) {
  283. foreach ($current_stocks as $c) { $stock_ids[] = $c->stock_id; }
  284. }
  285. // Change from stock_ids to nodes-----------------------------------
  286. $stock_ids = array_filter($stock_ids);
  287. $stock_ids = array_unique($stock_ids);
  288. $stocks = array();
  289. foreach ($stock_ids as $stock_id) {
  290. $node = tripal_stock_get_stock_by_stock_id($stock_id);
  291. if ($node) {
  292. $stocks[] = $node;
  293. }
  294. }
  295. return $stocks;
  296. }
  297. /**
  298. * Implements hook_chado_stock_schema()
  299. *
  300. * Purpose: To add descriptions and foreign keys to default table description
  301. * Note: This array will be merged with the array from all other implementations
  302. *
  303. * @return
  304. * Array describing the stock table
  305. *
  306. * @ingroup tripal_api
  307. */
  308. function tripal_stock_chado_stock_schema() {
  309. $description = array();
  310. $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.';
  311. $description['foreign keys']['organism'] = array(
  312. 'table' => 'organism',
  313. 'columns' => array(
  314. 'organism_id' => 'organism_id',
  315. ),
  316. );
  317. $description['foreign keys']['dbxref'] = array(
  318. 'table' => 'dbxref',
  319. 'columns' => array(
  320. 'dbxref_id' => 'dbxref_id',
  321. ),
  322. );
  323. $description['foreign keys']['cvterm'] = array(
  324. 'table' => 'cvterm',
  325. 'columns' => array(
  326. 'type_id' => 'cvterm_id',
  327. ),
  328. );
  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_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_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_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_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_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_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_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_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_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_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_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. }