tripal_pub.api.inc

  1. 2.x tripal_pub/api/tripal_pub.api.inc
  2. 1.x tripal_pub/api/tripal_pub.api.inc

Provides an application programming interface (API) to manage chado publications

File

tripal_pub/api/tripal_pub.api.inc
View source
  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(
  72. array('table' => 'pub', 'base_records' => $identifiers),
  73. array('type_name' => $property),
  74. $options
  75. );
  76. }
  77. elseif (array_key_exists('dbxref', $identifiers)) {
  78. if(preg_match('/^(.*?):(.*?)$/', $identifiers['dbxref'], $matches)) {
  79. $dbname = $matches[1];
  80. $accession = $matches[2];
  81. // First make sure the dbxref is present.
  82. $values = array(
  83. 'accession' => $accession,
  84. 'db_id' => array(
  85. 'name' => $dbname
  86. ),
  87. );
  88. $dbxref = chado_select_record('dbxref', array('dbxref_id'), $values);
  89. if (count($dbxref) == 0) {
  90. return FALSE;
  91. }
  92. $pub_dbxref = chado_select_record('pub_dbxref', array('pub_id'), array('dbxref_id' => $dbxref[0]->dbxref_id));
  93. if (count($pub_dbxref) == 0) {
  94. return FALSE;
  95. }
  96. $pub = chado_generate_var('pub', array('pub_id' => $pub_dbxref[0]->pub_id), $options);
  97. }
  98. else {
  99. tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
  100. "chado_get_publication: The dbxref identifier is not correctly formatted.",
  101. array('%identifier'=> print_r($identifiers, TRUE))
  102. );
  103. }
  104. }
  105. elseif (array_key_exists('dbxref_id', $identifiers)) {
  106. // first get the pub_dbxref record
  107. $values = array('dbxref_id' => $identifiers['dbxref_id']);
  108. $pub_dbxref = chado_select_record('pub_dbxref', array('pub_id'), $values);
  109. // now get the pub
  110. if (count($pub_dbxref) > 0) {
  111. $pub = chado_generate_var('pub', array('pub_id' => $pub_dbxref[0]->pub_id), $options);
  112. }
  113. else {
  114. return FALSE;
  115. }
  116. }
  117. // Else we have a simple case and we can just use chado_generate_var to get the pub
  118. else {
  119. // Try to get the pub
  120. $pub = chado_generate_var('pub', $identifiers, $options);
  121. }
  122. // Ensure the pub is singular. If it's an array then it is not singular
  123. if (is_array($pub)) {
  124. tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
  125. "chado_get_publication: The identifiers did not find a single unique record. Identifiers passed: %identifier.",
  126. array('%identifier'=> print_r($identifiers, TRUE))
  127. );
  128. }
  129. // Report an error if $pub is FALSE since then chado_generate_var has failed
  130. elseif ($pub === FALSE) {
  131. tripal_report_error('tripal_pub_api', TRIPAL_ERROR,
  132. "chado_get_publication: Could not find a publication using the identifiers
  133. provided. Check that the identifiers are correct. Identifiers passed: %identifier.",
  134. array('%identifier'=> print_r($identifiers, TRUE))
  135. );
  136. }
  137. // Else, as far we know, everything is fine so give them their pub :)
  138. else {
  139. return $pub;
  140. }
  141. }
  142. /**
  143. * The publication table of Chado only has a unique constraint for the
  144. * uniquename of the publiation, but in reality a publication can be considered
  145. * unique by a combination of the title, publication type, published year and
  146. * series name (e.g. journal name or conference name). The site administrator
  147. * can configure how publications are determined to be unique. This function
  148. * uses the configuration specified by the administrator to look for publications
  149. * that match the details specified by the $pub_details argument
  150. * and indicates if one ore more publications match the criteria.
  151. *
  152. * @param $pub_details
  153. * An associative array with details about the publications. The expected keys
  154. * are:
  155. * 'Title': The title of the publication
  156. * 'Year': The published year of the publication
  157. * 'Publication Type': An array of publication types. A publication can have more than one type.
  158. * 'Series Name': The series name of the publication
  159. * 'Journal Name': An alternative to 'Series Name'
  160. * 'Conference Name': An alternative to 'Series Name'
  161. * 'Citation': The publication citation (this is the value saved in the pub.uniquename field and must be unique)
  162. * If this key is present it will also be checked
  163. * 'Publication Dbxref': A database cross reference of the form DB:ACCESSION where DB is the name
  164. * of the database and ACCESSION is the unique identifier (e.g PMID:3483139)
  165. *
  166. * @return
  167. * An array containing the pub_id's of matching publications. Returns an
  168. * empty array if no pubs match
  169. *
  170. * @ingroup tripal_pub_api
  171. */
  172. function tripal_publication_exists($pub_details) {
  173. // First try to find the publication using the accession number if that key
  174. // exists in the details array
  175. if (array_key_exists('Publication Dbxref', $pub_details)) {
  176. $pub = tripal_get_publication(array('dbxref' => $pub_details['Publication Dbxref']));
  177. if($pub) {
  178. return array($pub->pub_id);
  179. }
  180. }
  181. // Make sure the citation is unique
  182. if (array_key_exists('Citation', $pub_details)) {
  183. $pub = tripal_get_publication(array('uniquename' => $pub_details['Citation']));
  184. if($pub) {
  185. return array($pub->pub_id);
  186. }
  187. }
  188. // Gget the publication type (use the first publication type)
  189. if (array_key_exists('Publication Type', $pub_details)) {
  190. $type_name = '';
  191. if(is_array($pub_details['Publication Type'])) {
  192. $type_name = $pub_details['Publication Type'][0];
  193. }
  194. else {
  195. $type_name = $pub_details['Publication Type'];
  196. }
  197. $identifiers = array(
  198. 'name' => $type_name,
  199. 'cv_id' => array(
  200. 'name' => 'tripal_pub',
  201. ),
  202. );
  203. $pub_type = tripal_get_cvterm($identifiers);
  204. }
  205. else {
  206. tripal_report_error('tripal_pub', TRIPAL_ERROR,
  207. "tripal_publication_exists(): The Publication Type is a " .
  208. "required property but is missing", array());
  209. return array();
  210. }
  211. if (!$pub_type) {
  212. tripal_report_error('tripal_pub', TRIPAL_ERROR,
  213. "tripal_publication_exists(): Cannot find publication type: '%type'",
  214. array('%type' => $pub_details['Publication Type'][0]));
  215. return array();
  216. }
  217. // get the series name. The pub.series_name field is only 255 chars so we must truncate to be safe
  218. $series_name = '';
  219. if (array_key_exists('Series Name', $pub_details)) {
  220. $series_name = substr($pub_details['Series Name'], 0, 255);
  221. }
  222. if (array_key_exists('Journal Name', $pub_details)) {
  223. $series_name = substr($pub_details['Journal Name'], 0, 255);
  224. }
  225. if (array_key_exists('Conference Name', $pub_details)) {
  226. $series_name = substr($pub_details['Conference Name'], 0, 255);
  227. }
  228. // make sure the publication is unique using the prefereed import duplication check
  229. $import_dups_check = variable_get('tripal_pub_import_duplicate_check', 'title_year_media');
  230. $pubs = array();
  231. switch ($import_dups_check) {
  232. case 'title_year':
  233. $identifiers = array(
  234. 'title' => $pub_details['Title'],
  235. 'pyear' => $pub_details['Year']
  236. );
  237. $pubs = chado_select_record('pub', array('pub_id'), $identifiers);
  238. break;
  239. case 'title_year_type':
  240. $identifiers = array(
  241. 'title' => $pub_details['Title'],
  242. 'pyear' => $pub_details['Year'],
  243. 'type_id' => $pub_type->cvterm_id,
  244. );
  245. $pubs = chado_select_record('pub', array('pub_id'), $identifiers);
  246. break;
  247. case 'title_year_media':
  248. $identifiers = array(
  249. 'title' => $pub_details['Title'],
  250. 'pyear' => $pub_details['Year'],
  251. 'series_name' => $series_name,
  252. );
  253. $pubs = chado_select_record('pub', array('pub_id'), $identifiers);
  254. break;
  255. }
  256. $return = array();
  257. foreach ($pubs as $pub) {
  258. $return[] = $pub->pub_id;
  259. }
  260. return $return;
  261. }