tripal_stock.api.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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. $referring_tables = array(
  330. 'stock_cvterm',
  331. 'stock_dbxref',
  332. 'stock_genotype',
  333. 'stock_pub',
  334. 'stock_relationship',
  335. 'stockcollection_stock',
  336. 'stockprop'
  337. );
  338. $description['referring_tables'] = $referring_tables;
  339. return $description;
  340. }
  341. /**
  342. * Implements hook_chado_stockprop_schema()
  343. *
  344. * Purpose: To add descriptions and foreign keys to default table description
  345. * Note: This array will be merged with the array from all other implementations
  346. *
  347. * @return
  348. * Array describing the stockprop table
  349. *
  350. * @ingroup tripal_api
  351. */
  352. function tripal_stock_chado_stockprop_schema() {
  353. $description = array();
  354. $description['foreign keys']['cvterm'] = array(
  355. 'table' => 'cvterm',
  356. 'columns' => array(
  357. 'type_id' => 'cvterm_id',
  358. ),
  359. );
  360. $description['foreign keys']['stock'] = array(
  361. 'table' => 'stock',
  362. 'columns' => array(
  363. 'stock_id' => 'stock_id',
  364. ),
  365. );
  366. return $description;
  367. }
  368. /**
  369. * Implements hook_chado_stockprop_pub_schema()
  370. *
  371. * Purpose: To add descriptions and foreign keys to default table description
  372. * Note: This array will be merged with the array from all other implementations
  373. *
  374. * @return
  375. * Array describing the stockprop_pub table
  376. *
  377. * @ingroup tripal_api
  378. */
  379. function tripal_stock_chado_stockprop_pub_schema() {
  380. $description = array();
  381. $description['foreign keys']['pub'] = array(
  382. 'table' => 'pub',
  383. 'columns' => array(
  384. 'pub_id' => 'pub_id',
  385. ),
  386. );
  387. $description['foreign keys']['stockprop'] = array(
  388. 'table' => 'stockprop',
  389. 'columns' => array(
  390. 'stockprop_id' => 'stockprop_id',
  391. ),
  392. );
  393. return $description;
  394. }
  395. /**
  396. * Implements hook_chado_stock_cvterm_schema()
  397. *
  398. * Purpose: To add descriptions and foreign keys to default table description
  399. * Note: This array will be merged with the array from all other implementations
  400. *
  401. * @return
  402. * Array describing the stock_cvterm table
  403. *
  404. * @ingroup tripal_api
  405. */
  406. function tripal_stock_chado_stock_cvterm_schema() {
  407. $description = array();
  408. $description['foreign keys']['cvterm'] = array(
  409. 'table' => 'cvterm',
  410. 'columns' => array(
  411. 'cvterm_id' => 'cvterm_id',
  412. ),
  413. );
  414. $description['foreign keys']['stock'] = array(
  415. 'table' => 'stock',
  416. 'columns' => array(
  417. 'stock_id' => 'stock_id',
  418. ),
  419. );
  420. $description['foreign keys']['pub'] = array(
  421. 'table' => 'pub',
  422. 'columns' => array(
  423. 'pub_id' => 'pub_id',
  424. ),
  425. );
  426. return $description;
  427. }
  428. /**
  429. * Implements hook_chado_stock_dbxref_schema()
  430. *
  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_dbxref table
  436. *
  437. * @ingroup tripal_api
  438. */
  439. function tripal_stock_chado_stock_dbxref_schema() {
  440. $description = array();
  441. $description['foreign keys']['dbxref'] = array(
  442. 'table' => 'dbxref',
  443. 'columns' => array(
  444. 'dbxref_id' => 'dbxref_id',
  445. ),
  446. );
  447. $description['foreign keys']['stock'] = array(
  448. 'table' => 'stock',
  449. 'columns' => array(
  450. 'stock_id' => 'stock_id',
  451. ),
  452. );
  453. return $description;
  454. }
  455. /**
  456. * Implements hook_chado_stock_genotype_schema()
  457. *
  458. * Purpose: To add descriptions and foreign keys to default table description
  459. * Note: This array will be merged with the array from all other implementations
  460. *
  461. * @return
  462. * Array describing the stock_genotype table
  463. *
  464. * @ingroup tripal_api
  465. */
  466. function tripal_stock_chado_stock_genotype_schema() {
  467. $description = array();
  468. $description['foreign keys']['genotype'] = array(
  469. 'table' => 'genotype',
  470. 'columns' => array(
  471. 'genotype_id' => 'genotype_id',
  472. ),
  473. );
  474. $description['foreign keys']['stock'] = array(
  475. 'table' => 'stock',
  476. 'columns' => array(
  477. 'stock_id' => 'stock_id',
  478. ),
  479. );
  480. return $description;
  481. }
  482. /**
  483. * Implements hook_chado_stock_pub_schema()
  484. *
  485. * Purpose: To add descriptions and foreign keys to default table description
  486. * Note: This array will be merged with the array from all other implementations
  487. *
  488. * @return
  489. * Array describing the stock_pub table
  490. *
  491. * @ingroup tripal_api
  492. */
  493. function tripal_stock_chado_stock_pub_schema() {
  494. $description = array();
  495. $description['foreign keys']['pub'] = array(
  496. 'table' => 'pub',
  497. 'columns' => array(
  498. 'pub_id' => 'pub_id',
  499. ),
  500. );
  501. $description['foreign keys']['stock'] = array(
  502. 'table' => 'stock',
  503. 'columns' => array(
  504. 'stock_id' => 'stock_id',
  505. ),
  506. );
  507. return $description;
  508. }
  509. /**
  510. * Implements hook_chado_stock_relationship_schema()
  511. *
  512. * Purpose: To add descriptions and foreign keys to default table description
  513. * Note: This array will be merged with the array from all other implementations
  514. *
  515. * @return
  516. * Array describing the stock_relationship table
  517. *
  518. * @ingroup tripal_api
  519. */
  520. function tripal_stock_chado_stock_relationship_schema() {
  521. $description = array();
  522. $description['foreign keys']['cvterm'] = array(
  523. 'table' => 'cvterm',
  524. 'columns' => array(
  525. 'type_id' => 'cvterm_id',
  526. ),
  527. );
  528. $description['foreign keys']['stock'] = array(
  529. 'table' => 'stock',
  530. 'columns' => array(
  531. 'subject_id' => 'stock_id',
  532. 'object_id' => 'stock_id',
  533. ),
  534. );
  535. return $description;
  536. }
  537. /**
  538. * Implements hook_chado_stock_relationship_pub_schema()
  539. *
  540. * Purpose: To add descriptions and foreign keys to default table description
  541. * Note: This array will be merged with the array from all other implementations
  542. *
  543. * @return
  544. * Array describing the stock_relationship_pub table
  545. *
  546. * @ingroup tripal_api
  547. */
  548. function tripal_stock_chado_stock_relationship_pub_schema() {
  549. $description = array();
  550. $description['foreign keys']['pub'] = array(
  551. 'table' => 'pub',
  552. 'columns' => array(
  553. 'pub_id' => 'pub_id',
  554. ),
  555. );
  556. $description['foreign keys']['stock_relationship'] = array(
  557. 'table' => 'stock_relationship',
  558. 'columns' => array(
  559. 'stock_relationship_id' => 'stock_relationship_id',
  560. ),
  561. );
  562. return $description;
  563. }
  564. /**
  565. * Implements hook_chado_stockcollection_schema()
  566. *
  567. * Purpose: To add descriptions and foreign keys to default table description
  568. * Note: This array will be merged with the array from all other implementations
  569. *
  570. * @return
  571. * Array describing the stockcollection table
  572. *
  573. * @ingroup tripal_api
  574. */
  575. function tripal_stock_chado_stockcollection_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']['contact'] = array(
  584. 'table' => 'contact',
  585. 'columns' => array(
  586. 'contact_id' => 'contact_id',
  587. ),
  588. );
  589. return $description;
  590. }
  591. /**
  592. * Implements hook_chado_stockcollection_stock_schema()
  593. *
  594. * Purpose: To add descriptions and foreign keys to default table description
  595. * Note: This array will be merged with the array from all other implementations
  596. *
  597. * @return
  598. * Array describing the stockcollection_stock table
  599. *
  600. * @ingroup tripal_api
  601. */
  602. function tripal_stock_chado_stockcollection_stock_schema() {
  603. $description = array();
  604. $description['foreign keys']['stock'] = array(
  605. 'table' => 'stock',
  606. 'columns' => array(
  607. 'stock_id' => 'stock_id',
  608. ),
  609. );
  610. $description['foreign keys']['stockcollection'] = array(
  611. 'table' => 'stockcollection',
  612. 'columns' => array(
  613. 'stockcollection_id' => 'stockcollection_id',
  614. ),
  615. );
  616. return $description;
  617. }
  618. /**
  619. * Implements hook_chado_stockcollectionprop_schema()
  620. *
  621. * Purpose: To add descriptions and foreign keys to default table description
  622. * Note: This array will be merged with the array from all other implementations
  623. *
  624. * @return
  625. * Array describing the stockcollectionprop table
  626. *
  627. * @ingroup tripal_api
  628. */
  629. function tripal_stock_chado_stockcollectionprop_schema() {
  630. $description = array();
  631. $description['foreign keys']['cvterm'] = array(
  632. 'table' => 'cvterm',
  633. 'columns' => array(
  634. 'type_id' => 'cvterm_id',
  635. ),
  636. );
  637. $description['foreign keys']['stockcollection'] = array(
  638. 'table' => 'stockcollection',
  639. 'columns' => array(
  640. 'stockcollection_id' => 'stockcollection_id',
  641. ),
  642. );
  643. return $description;
  644. }