tripal_pub.api.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. * @file
  4. * Provides an application programming interface (API) to manage chado publications
  5. */
  6. /**
  7. * @defgroup tripal_pub_api Publication Module API
  8. * @ingroup tripal_api
  9. * @{
  10. * Provides an application programming interface (API) to manage chado publications
  11. *
  12. * @stephen add documentation here for how to add a new importer.
  13. *
  14. * @}
  15. */
  16. /**
  17. * Retrieves a chado publication array
  18. *
  19. * @param $identifier
  20. * An array used to uniquely identify a publication. This array has the same
  21. * format as that used by the chado_generate_var(). The following keys can be
  22. * useful for uniquely identifying a publication as they should be unique:
  23. * - pub_id: the chado pub.pub_id primary key
  24. * - nid: the drupal nid of the publication
  25. * - uniquename: A value to matach with the pub.uniquename field
  26. * There are also some specially handled keys. They are:
  27. * - property: An array describing the property to select records for. It
  28. * should at least have either a 'type_name' key (if unique across cvs) or
  29. * 'type_id' key. Other supported keys include: 'cv_id', 'cv_name' (of the type),
  30. * 'value' and 'rank'
  31. * - dbxref: The database cross reference accession. It should be in the form
  32. * DB:ACCESSION, where DB is the database name and ACCESSION is the
  33. * unique publication identifier (e.g. PMID:4382934)
  34. * - dbxref_id: The dbxref.dbxref_id of the publication.
  35. * @param $options
  36. * An array of options. Supported keys include:
  37. * - Any keys supported by chado_generate_var(). See that function definition for
  38. * additional details.
  39. *
  40. * NOTE: the $identifier parameter can really be any array similar to $values passed into
  41. * chado_select_record(). It should fully specify the pub record to be returned.
  42. *
  43. * @return
  44. * If a singe publication is retreived using the identifiers, then a publication
  45. * array will be returned. The array is of the same format returned by the
  46. * chado_generate_var() function. Otherwise, FALSE will be returned.
  47. *
  48. * @ingroup tripal_pub_api
  49. */
  50. function tripal_get_publication($identifiers, $options = array()) {
  51. // Error Checking of parameters
  52. if (!is_array($identifiers)) {
  53. tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
  54. "chado_get_publication: The identifier passed in is expected to be an array with the key
  55. matching a column name in the pub table (ie: pub_id or name). You passed in %identifier.",
  56. array('%identifier'=> print_r($identifiers, TRUE))
  57. );
  58. }
  59. elseif (empty($identifiers)) {
  60. tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
  61. "chado_get_publication: You did not pass in anything to identify the publication you want. The identifier
  62. is expected to be an array with the key matching a column name in the pub table
  63. (ie: pub_id or name). You passed in %identifier.",
  64. array('%identifier'=> print_r($identifiers, TRUE))
  65. );
  66. }
  67. // If one of the identifiers is property then use chado_get_record_with_property()
  68. if (array_key_exists('property', $identifiers)) {
  69. $property = $identifiers['property'];
  70. unset($identifiers['property']);
  71. $pub = chado_get_record_with_property('pub', $property, $identifiers, $options);
  72. }
  73. elseif (array_key_exists('dbxref', $identifiers)) {
  74. if(preg_match('/^(.*?):(.*?)$/', $identifiers['dbxref'], $matches)) {
  75. $dbname = $matches[1];
  76. $accession = $matches[2];
  77. $values = array(
  78. 'dbxref_id' => array (
  79. 'accession' => $accession,
  80. 'db_id' => array(
  81. 'name' => $dbname
  82. ),
  83. ),
  84. );
  85. $pub_dbxref = chado_select_record('pub_dbxref', array('pub_id'), $values);
  86. if (count($pub_dbxref) > 0) {
  87. $pub = chado_generate_var('pub', array('pub_id' => $pub_dbxref[0]->pub_id), $options);
  88. }
  89. else {
  90. return FALSE;
  91. }
  92. }
  93. else {
  94. tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
  95. "chado_get_publication: The dbxref identifier is not correctly formatted.",
  96. array('%identifier'=> print_r($identifiers, TRUE))
  97. );
  98. }
  99. }
  100. elseif (array_key_exists('dbxref_id', $identifiers)) {
  101. // first get the pub_dbxref record
  102. $values = array('dbxref_id' => $identifiers['dbxref_id']);
  103. $pub_dbxref = chado_select_record('pub_dbxref', array('pub_id'), $values);
  104. // now get the pub
  105. if (count($pub_dbxref) > 0) {
  106. $pub = chado_generate_var('pub', array('pub_id' => $pub_dbxref[0]->pub_id), $options);
  107. }
  108. else {
  109. return FALSE;
  110. }
  111. }
  112. // Else we have a simple case and we can just use chado_generate_var to get the pub
  113. else {
  114. // Try to get the pub
  115. $pub = chado_generate_var('pub', $identifiers, $options);
  116. }
  117. // Ensure the pub is singular. If it's an array then it is not singular
  118. if (is_array($pub)) {
  119. tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
  120. "chado_get_publication: The identifiers did not find a single unique record. Identifiers passed: %identifier.",
  121. array('%identifier'=> print_r($identifiers, TRUE))
  122. );
  123. }
  124. // Report an error if $pub is FALSE since then chado_generate_var has failed
  125. elseif ($pub === FALSE) {
  126. tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
  127. "chado_get_publication: Could not find a publication using the identifiers
  128. provided. Check that the identifiers are correct. Identifiers passed: %identifier.",
  129. array('%identifier'=> print_r($identifiers, TRUE))
  130. );
  131. }
  132. // Else, as far we know, everything is fine so give them their pub :)
  133. else {
  134. return $pub;
  135. }
  136. }
  137. /**
  138. * The publication table of Chado only has a unique constraint for the
  139. * uniquename of the publiation, but in reality a publication can be considered
  140. * unique by a combination of the title, publication type, published year and
  141. * series name (e.g. journal name or conference name). The site administrator
  142. * can configure how publications are determined to be unique. This function
  143. * uses the configuration specified by the administrator to look for publications
  144. * that match the details specified by the $pub_details argument
  145. * and indicates if one ore more publications match the criteria.
  146. *
  147. * @param $pub_details
  148. * An associative array with details about the publications. The expected keys
  149. * are:
  150. * 'Title': The title of the publication
  151. * 'Year': The published year of the publication
  152. * 'Publication Type': An array of publication types. A publication can have more than one type.
  153. * 'Series Name': The series name of the publication
  154. * 'Journal Name': An alternative to 'Series Name'
  155. * 'Conference Name': An alternative to 'Series Name'
  156. * 'Citation': The publication citation (this is the value saved in the pub.uniquename field and must be unique)
  157. * If this key is present it will also be checked
  158. * 'Publication Dbxref': A database cross reference of the form DB:ACCESSION where DB is the name
  159. * of the database and ACCESSION is the unique identifier (e.g PMID:3483139)
  160. *
  161. * @return
  162. * An array containing the pub_id's of matching publications. Returns an
  163. * empty array if no pubs match
  164. *
  165. * @ingroup tripal_pub_api
  166. */
  167. function tripal_publication_exists($pub_details) {
  168. // first try to find the publication using the accession number if that key exists in the details array
  169. if (array_key_exists('Publication Dbxref', $pub_details)) {
  170. $pub = tripal_get_publication(array('dbxref' => $pub_details['Publication Dbxref']));
  171. if($pub) {
  172. return array($pub->pub_id);
  173. }
  174. }
  175. // make sure the citation is unique
  176. if (array_key_exists('Citation', $pub_details)) {
  177. $pub = tripal_get_publication(array('uniquename' => $pub_details['Citation']));
  178. if($pub) {
  179. return array($pub->pub_id);
  180. }
  181. }
  182. // get the publication type (use the first publication type)
  183. if (array_key_exists('Publication Type', $pub_details)) {
  184. $type_name = '';
  185. if(is_array($pub_details['Publication Type'])) {
  186. $type_name = $pub_details['Publication Type'][0];
  187. }
  188. else {
  189. $type_name = $pub_details['Publication Type'];
  190. }
  191. $identifiers = array(
  192. 'name' => $type_name,
  193. 'cv_id' => array(
  194. 'name' => 'tripal_pub',
  195. ),
  196. );
  197. $pub_type = tripal_get_cvterm($identifiers);
  198. }
  199. else {
  200. tripal_report_error('tripal_pub', TRIPAL_ERROR,
  201. "tripal_publication_exists(): The Publication Type is a " .
  202. "required property but is missing", array());
  203. return array();
  204. }
  205. if (!$pub_type) {
  206. tripal_report_error('tripal_pub', TRIPAL_ERROR,
  207. "tripal_publication_exists(): Cannot find publication type: '%type'",
  208. array('%type' => $pub_details['Publication Type'][0]));
  209. return array();
  210. }
  211. // get the series name. The pub.series_name field is only 255 chars so we must truncate to be safe
  212. $series_name = '';
  213. if (array_key_exists('Series Name', $pub_details)) {
  214. $series_name = substr($pub_details['Series Name'], 0, 255);
  215. }
  216. if (array_key_exists('Journal Name', $pub_details)) {
  217. $series_name = substr($pub_details['Journal Name'], 0, 255);
  218. }
  219. if (array_key_exists('Conference Name', $pub_details)) {
  220. $series_name = substr($pub_details['Conference Name'], 0, 255);
  221. }
  222. // make sure the publication is unique using the prefereed import duplication check
  223. $import_dups_check = variable_get('tripal_pub_import_duplicate_check', 'title_year_media');
  224. $pubs = array();
  225. switch ($import_dups_check) {
  226. case 'title_year':
  227. $identifiers = array(
  228. 'title' => $pub_details['Title'],
  229. 'pyear' => $pub_details['Year']
  230. );
  231. $pubs = chado_select_record('pub', array('pub_id'), $identifiers);
  232. break;
  233. case 'title_year_type':
  234. $identifiers = array(
  235. 'title' => $pub_details['Title'],
  236. 'pyear' => $pub_details['Year'],
  237. 'type_id' => $pub_type->cvterm_id,
  238. );
  239. $pubs = chado_select_record('pub', array('pub_id'), $identifiers);
  240. break;
  241. case 'title_year_media':
  242. $identifiers = array(
  243. 'title' => $pub_details['Title'],
  244. 'pyear' => $pub_details['Year'],
  245. 'series_name' => $series_name,
  246. );
  247. $pubs = chado_select_record('pub', array('pub_id'), $identifiers);
  248. break;
  249. }
  250. $return = array();
  251. foreach ($pubs as $pub) {
  252. $return[] = $pub->pub_id;
  253. }
  254. return $return;
  255. }