tripal_chado.setup.inc 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  1. <?php
  2. /**
  3. * @file
  4. * Functions to install chado schema through Drupal
  5. */
  6. /**
  7. * Prepares Chado for Tripal use
  8. */
  9. function tripal_chado_prepare_form($form, $form_state) {
  10. $form = array();
  11. if (variable_get('tripal_chado_is_prepared') == TRUE) {
  12. drupal_set_message('Your site is prepared.');
  13. }
  14. $form['instructions'] = array(
  15. '#type' => 'item',
  16. '#title' => 'Prepare Drupal for Chado.',
  17. '#description' => t("Before a Drupal site can use Chado (via Tripal), both
  18. Chado and Drupal must be prepared a bit more. Tripal will add some new
  19. materialized views, custom tables and controlled vocabularies to Chado.
  20. It will also add some management tables to Drupal. You only are
  21. required to prepare your Drupal site if this is a brand-new Drupal
  22. installation or if Chado was installed outside of Tripal. If you
  23. installed Chado using Tripal then you do not need to run this step.
  24. If you are upgrading from a previous version of Tripal, you do not
  25. need to prepare your site, and you can click the 'Skip' button."),
  26. );
  27. $form['prepare-button'] = array(
  28. '#type' => 'submit',
  29. '#value' => t('Prepare this site'),
  30. '#name' => 'prepare-chado',
  31. );
  32. $form['skip-button'] = array(
  33. '#type' => 'submit',
  34. '#value' => t('Skip'),
  35. '#name' => 'prepare-skip',
  36. );
  37. return $form;
  38. }
  39. /**
  40. * Submit function for the tripal_chado_prepare_form().
  41. *
  42. * @param $form
  43. * @param $form_state
  44. */
  45. function tripal_chado_prepare_form_submit($form, $form_state) {
  46. if ($form_state['clicked_button']['#name'] == "prepare-chado") {
  47. global $user;
  48. $args = array();
  49. $includes = array(
  50. module_load_include('inc', 'tripal_chado', 'includes/tripal_chado.setup'),
  51. module_load_include('inc', 'tripal_chado', 'includes/loaders/tripal_chado.obo_loader'),
  52. );
  53. tripal_add_job('Prepare Chado', 'tripal_chado',
  54. 'tripal_chado_prepare_chado', $args,
  55. $user->uid, 10, $includes);
  56. }
  57. if ($form_state['clicked_button']['#name'] == "prepare-skip") {
  58. variable_set('tripal_chado_is_prepared', TRUE);
  59. }
  60. }
  61. /**
  62. * Prepares Chado for use by Tripal.
  63. */
  64. function tripal_chado_prepare_chado() {
  65. try {
  66. // we want to force the version of Chado to be set properly
  67. $real_version = chado_get_version(TRUE);
  68. // get the effective version. Pass true as second argument
  69. // to warn the user if the current version is not compatible
  70. $version = chado_get_version(FALSE, FALSE);
  71. ///////////////////////////////////////////////////////////////////////////
  72. // Chado DB Module
  73. ///////////////////////////////////////////////////////////////////////////
  74. tripal_insert_db(array(
  75. 'name' => 'null',
  76. 'description' => 'No online database.'
  77. ));
  78. tripal_insert_db(array(
  79. 'name' => 'local',
  80. 'description' => variable_get('site_name', 'This site.'),
  81. ));
  82. ///////////////////////////////////////////////////////////////////////////
  83. // Chado CV Module
  84. ///////////////////////////////////////////////////////////////////////////
  85. // Add the cv_root_mview.
  86. tripal_cv_add_cv_root_mview();
  87. // Add defaults to the tables that correlate OBO files/references with
  88. // a chado CV.
  89. tripal_cv_add_obo_defaults();
  90. // Create the temp table we will use for loading OBO files.
  91. tripal_cv_create_tripal_obo_temp();
  92. // Add the synonym_type vocabulary.
  93. tripal_insert_cv(
  94. 'synonym_type',
  95. 'A vocabulary used for synonym types (e.g. exact, broad, narrow, related).'
  96. );
  97. // Add the Chado ontology CV.
  98. //$obo_path = '{tripal_chado}/files/cv_property.obo';
  99. //$obo_id = tripal_insert_obo('Chado CV Properties', $obo_path);
  100. //tripal_chado_load_obo_v1_2_id($obo_id);
  101. //tripal_submit_obo_job(array('obo_id' => $obo_id));
  102. ///////////////////////////////////////////////////////////////////////////
  103. // Chado Misc
  104. ///////////////////////////////////////////////////////////////////////////
  105. tripal_insert_cv(
  106. 'synonym_type',
  107. 'A vocabulary for storing synonym types.'
  108. );
  109. // set the default vocabularies
  110. tripal_set_default_cv('synonym', 'type_id', 'synonym_type');
  111. // Add cvterms.
  112. tripal_insert_misc_cvterms();
  113. /////////////////////////////////////////////////////////////////////////////
  114. // Chado Organism Module
  115. /////////////////////////////////////////////////////////////////////////////
  116. tripal_insert_cv(
  117. 'organism_property',
  118. 'Contains properties for organisms'
  119. );
  120. // Set the default vocabularies.
  121. tripal_set_default_cv('organismprop', 'type_id', 'organism_property');
  122. // Add taxonomic terms.
  123. $obo_id = tripal_insert_obo('Taxonomic Rank', '{tripal_chado}/files/taxrank.obo');
  124. tripal_submit_obo_job(array('obo_id' => $obo_id));
  125. /////////////////////////////////////////////////////////////////////////////
  126. // Chado CompAnalysis Module
  127. /////////////////////////////////////////////////////////////////////////////
  128. // we may need the analysisfeatureprop table if it doesn't already exist
  129. tripal_analysis_create_analysisfeatureprop();
  130. // add cvterms
  131. tripal_analysis_add_cvterms();
  132. // add materialized views
  133. tripal_analysis_add_mview_analysis_organism();
  134. // set the default vocabularies
  135. tripal_set_default_cv('analysisprop', 'type_id', 'analysis_property');
  136. /////////////////////////////////////////////////////////////////////////////
  137. // Chado Contact Module
  138. /////////////////////////////////////////////////////////////////////////////
  139. // Add the contactprop table to Chado.
  140. tripal_contact_add_custom_tables();
  141. tripal_insert_db(array(
  142. 'name' => 'TContact',
  143. 'description' => 'The Tripal Contact controlled vocabulary.',
  144. ));
  145. tripal_insert_db(array(
  146. 'name' => 'TPUB',
  147. 'description' => 'The Tripal Publication controlled vocabulary.',
  148. ));
  149. // Add loading of the the tripal contact ontology to the job queue.
  150. $obo_path = '{tripal_chado}/files/tcontact.obo';
  151. $obo_id = tripal_insert_obo('Tripal Contacts', $obo_path);
  152. tripal_chado_load_obo_v1_2_id($obo_id);
  153. //tripal_submit_obo_job(array('obo_id' => $obo_id));
  154. // Add cvterms for relationship types.
  155. tripal_contact_add_cvs();
  156. // Set the default vocabularies.
  157. tripal_set_default_cv('contact', 'type_id', 'tripal_contact');
  158. tripal_set_default_cv('contactprop', 'type_id', 'tripal_contact');
  159. tripal_set_default_cv('contact_relationship', 'type_id', 'contact_relationship');
  160. /////////////////////////////////////////////////////////////////////////////
  161. // Chado Feature Module
  162. /////////////////////////////////////////////////////////////////////////////
  163. // Note: the feature_property OBO that came with Chado v1.2 should not
  164. // be automatically installed. Some of the terms are duplicates of
  165. // others in better maintained vocabularies. New Tripal sites should
  166. // use those.
  167. // $obo_path = '{tripal_feature}/files/feature_property.obo';
  168. // $obo_id = tripal_insert_obo('Chado Feature Properties', $obo_path);
  169. // tripal_chado_load_obo_v1_2_id($obo_id);
  170. //// tripal_submit_obo_job(array('obo_id' => $obo_id));
  171. // Load the sequence ontology.
  172. $obo = tripal_get_obo(array('obo_name' => 'Sequence Ontology'));
  173. if ($obo) {
  174. print "Installing the Sequence Ontology... This may take a while...\n";
  175. tripal_chado_load_obo_v1_2_id($obo->obo_id);
  176. }
  177. // Add the materialized view.
  178. tripal_feature_add_organism_count_mview();
  179. // Add the custom tables.
  180. tripal_feature_add_tripal_gff_temp_table();
  181. tripal_feature_add_tripal_gffcds_temp_table();
  182. tripal_feature_add_tripal_gffprotein_temp_table();
  183. // Add the vocabularies used by the feature module.
  184. tripal_feature_add_cvs();
  185. // Set the default vocabularies.
  186. tripal_set_default_cv('feature', 'type_id', 'sequence');
  187. tripal_set_default_cv('featureprop', 'type_id', 'feature_property');
  188. tripal_set_default_cv('feature_relationship', 'type_id', 'feature_relationship');
  189. /////////////////////////////////////////////////////////////////////////////
  190. // Chado Map Module
  191. /////////////////////////////////////////////////////////////////////////////
  192. // add the featuremapprop table to Chado
  193. tripal_featuremap_add_custom_tables();
  194. // Add cvterms
  195. tripal_featuremap_add_cvs();
  196. tripal_featuremap_add_cvterms();
  197. // set the default vocabularies
  198. tripal_set_default_cv('featuremapprop', 'type_id', 'featuremap_property');
  199. tripal_set_default_cv('featureposprop', 'type_id', 'featurepos_property');
  200. tripal_set_default_cv('featuremap', 'unittype_id', 'featuremap_units');
  201. /////////////////////////////////////////////////////////////////////////////
  202. // Chado Library Module
  203. /////////////////////////////////////////////////////////////////////////////
  204. // add the materialized view
  205. tripal_library_add_mview_library_feature_count();
  206. // add cvterms
  207. tripal_library_add_cvs();
  208. tripal_library_add_cvterms();
  209. // set the default vocabularies
  210. tripal_set_default_cv('libraryprop', 'type_id', 'library_property');
  211. tripal_set_default_cv('library', 'type_id', 'library_type');
  212. /////////////////////////////////////////////////////////////////////////////
  213. // Chado NatDiv Module
  214. /////////////////////////////////////////////////////////////////////////////
  215. // add cvterms
  216. tripal_natural_diversity_add_cvterms();
  217. /////////////////////////////////////////////////////////////////////////////
  218. // Chado Project Module
  219. /////////////////////////////////////////////////////////////////////////////
  220. tripal_project_add_cvs();
  221. tripal_project_add_cvterms();
  222. // set the default vocabularies
  223. tripal_set_default_cv('projectprop', 'type_id', 'project_property');
  224. tripal_set_default_cv('project_relationship', 'type_id', 'project_relationship');
  225. /////////////////////////////////////////////////////////////////////////////
  226. // Chado Pub Module
  227. /////////////////////////////////////////////////////////////////////////////
  228. global $base_path;
  229. // add loading of the the tripal pub ontology to the job queue
  230. $obo_path = '{tripal_chado}/files/tpub.obo';
  231. $obo_id = tripal_insert_obo('Tripal Publication', $obo_path);
  232. tripal_chado_load_obo_v1_2_id($obo_id);
  233. //tripal_submit_obo_job(array('obo_id' => $obo_id));
  234. tripal_pub_add_cvs();
  235. tripal_pub_add_dbs();
  236. // add the custom tables
  237. tripal_pub_add_custom_tables();
  238. // set the default vocabularies
  239. tripal_set_default_cv('pub', 'type_id', 'tripal_pub');
  240. tripal_set_default_cv('pubprop', 'type_id', 'tripal_pub');
  241. tripal_set_default_cv('pub_relationship', 'type_id', 'pub_relationship');
  242. // Add the supported loaders
  243. variable_set('tripal_pub_supported_dbs', array('PMID', 'AGL'));
  244. /////////////////////////////////////////////////////////////////////////////
  245. // Chado Stock Module
  246. /////////////////////////////////////////////////////////////////////////////
  247. // add some controlled vocabularies
  248. tripal_stock_add_cvs();
  249. // set the default vocabularies
  250. tripal_set_default_cv('stock', 'type_id', 'stock_type');
  251. tripal_set_default_cv('stockprop', 'type_id', 'stock_property');
  252. tripal_set_default_cv('stock_relationship', 'type_id', 'stock_relationship');
  253. // add the materialized view
  254. tripal_stock_add_organism_count_mview();
  255. /////////////////////////////////////////////////////////////////////////////
  256. // Entity Bundles
  257. /////////////////////////////////////////////////////////////////////////////
  258. // Unfortunately, some Chado base tables do not have a type_id, so we must
  259. // take special action for those tables. These include: organism and
  260. // analysis. Until we can find an appropriate controlled vocabulary
  261. // that is well supported by the community with types for these tables we
  262. // will have to use in-house terms.
  263. // Add a term to be used for an inherent 'type_id' for the organism table.
  264. tripal_insert_cvterm(array(
  265. 'id' => 'local:organism',
  266. 'name' => 'organism',
  267. 'definition' => 'An individual form of life, such as a bacterium, protist, ' .
  268. 'fungus, plant, or animal, composed of a single cell or a complex of cells ' .
  269. 'in which organelles or organs work together to carry out the various ' .
  270. 'processes of life. (American Heritage® Dictionary of the English ' .
  271. 'Language, Fifth Edition. Copyright © 2011 by Houghton Mifflin ' .
  272. 'Harcourt Publishing Company).',
  273. 'cv_name' => 'local',
  274. ));
  275. // Add a term to be used for an inherent 'type_id' for the organism table.
  276. tripal_insert_cvterm(array(
  277. 'id' => 'local:analysis',
  278. 'name' => 'analysis',
  279. 'definition' => 'A process as a method of studying the nature of something ' .
  280. 'or of determining its essential features and their relations. ' .
  281. '(Random House Kernerman Webster\'s College Dictionary, © 2010 K ' .
  282. 'Dictionaries Ltd).',
  283. 'cv_name' => 'local',
  284. ));
  285. tripal_insert_cvterm(array(
  286. 'id' => 'local:project',
  287. 'name' => 'project',
  288. 'definition' => 'A plan or proposal for accomplishing something. ' .
  289. '(American Heritage® Dictionary of the English Language, Fifth Edition. ' .
  290. 'Copyright © 2011 by Houghton Mifflin Harcourt Publishing Company).',
  291. 'cv_name' => 'local',
  292. ));
  293. // For the TripalBundle entities we will want to associate the cvterm_id,
  294. // and the chado table and field that it maps to. We will use a few
  295. // variables to do this:
  296. tripal_insert_variable(
  297. 'chado_cvterm_id',
  298. 'The cvterm_id that a TripalBundle maps to.'
  299. );
  300. tripal_insert_variable(
  301. 'chado_table',
  302. 'The name of the table to which a TripalBundle maps.'
  303. );
  304. tripal_insert_variable(
  305. 'chado_column',
  306. 'The name of the column within the table that a TripalBundle maps to.'
  307. );
  308. // We want to provide a set of commonly used entity types by default. This
  309. // way when a user first installs Tripal there are some commonly used
  310. // formats.
  311. module_load_include('inc', 'tripal', 'api/tripal.api');
  312. module_load_include('inc', 'tripal', 'includes/tripal.admin');
  313. // Create the 'Organism' entity type. This uses the local:organism term.
  314. $error = '';
  315. $term = array('name' => 'organism', 'cv_id' => array('name' => 'local'));
  316. $cvterm = chado_generate_var('cvterm', $term);
  317. if (!tripal_create_bundle('local', 'organism', 'organism', $error)) {
  318. throw new Exception($error);
  319. }
  320. // Create the 'Analysis' entity type. This uses the local:analysis term.
  321. $error = '';
  322. $term = array('name' => 'analysis', 'cv_id' => array('name' => 'local'));
  323. $cvterm = chado_generate_var('cvterm', $term);
  324. if (!tripal_create_bundle('local', 'analysis', 'analysis', $error)) {
  325. throw new Exception($error);
  326. }
  327. // Create the 'Project' entity type. This uses the local:project term.
  328. $error = '';
  329. $term = array('name' => 'project', 'cv_id' => array('name' => 'local'));
  330. $cvterm = chado_generate_var('cvterm', $term);
  331. if (!tripal_create_bundle('local', 'project', 'project', $error)) {
  332. throw new Exception($error);
  333. }
  334. // Set a variable to indicate the site is prepared.
  335. variable_set('tripal_chado_is_prepared', TRUE);
  336. }
  337. catch (Exception $e) {
  338. throw new Exception($e);
  339. }
  340. }
  341. /**
  342. * Creates a materialized view that stores the type & number of stocks per organism
  343. *
  344. * @ingroup tripal_stock
  345. */
  346. function tripal_stock_add_organism_count_mview() {
  347. $view_name = 'organism_stock_count';
  348. $comment = 'Stores the type and number of stocks per organism';
  349. $schema = array(
  350. 'description' => $comment,
  351. 'table' => $view_name,
  352. 'fields' => array(
  353. 'organism_id' => array(
  354. 'type' => 'int',
  355. 'not null' => TRUE,
  356. ),
  357. 'genus' => array(
  358. 'type' => 'varchar',
  359. 'length' => '255',
  360. 'not null' => TRUE,
  361. ),
  362. 'species' => array(
  363. 'type' => 'varchar',
  364. 'length' => '255',
  365. 'not null' => TRUE,
  366. ),
  367. 'common_name' => array(
  368. 'type' => 'varchar',
  369. 'length' => '255',
  370. 'not null' => FALSE,
  371. ),
  372. 'num_stocks' => array(
  373. 'type' => 'int',
  374. 'not null' => TRUE,
  375. ),
  376. 'cvterm_id' => array(
  377. 'type' => 'int',
  378. 'not null' => TRUE,
  379. ),
  380. 'stock_type' => array(
  381. 'type' => 'varchar',
  382. 'length' => '255',
  383. 'not null' => TRUE,
  384. ),
  385. ),
  386. 'indexes' => array(
  387. 'organism_stock_count_idx1' => array('organism_id'),
  388. 'organism_stock_count_idx2' => array('cvterm_id'),
  389. 'organism_stock_count_idx3' => array('stock_type'),
  390. ),
  391. );
  392. $sql = "
  393. SELECT
  394. O.organism_id, O.genus, O.species, O.common_name,
  395. count(S.stock_id) as num_stocks,
  396. CVT.cvterm_id, CVT.name as stock_type
  397. FROM organism O
  398. INNER JOIN stock S ON O.Organism_id = S.organism_id
  399. INNER JOIN cvterm CVT ON S.type_id = CVT.cvterm_id
  400. GROUP BY
  401. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  402. ";
  403. tripal_add_mview($view_name, 'tripal_stock', $schema, $sql, $comment);
  404. }
  405. /**
  406. * Add cvs related to publications
  407. *
  408. * @ingroup tripal_pub
  409. */
  410. function tripal_stock_add_cvs() {
  411. // Add cv for relationship types
  412. tripal_insert_cv(
  413. 'stock_relationship',
  414. 'Contains types of relationships between stocks.'
  415. );
  416. tripal_insert_cv(
  417. 'stock_property',
  418. 'Contains properties for stocks.'
  419. );
  420. tripal_insert_cv(
  421. 'stock_type',
  422. 'Contains a list of types for stocks.'
  423. );
  424. }
  425. /**
  426. * Add custom table related to publications
  427. * - pubauthor_contact
  428. *
  429. * @ingroup tripal_pub
  430. */
  431. function tripal_pub_add_custom_tables() {
  432. $schema = array (
  433. 'table' => 'pubauthor_contact',
  434. 'fields' => array (
  435. 'pubauthor_contact_id' => array (
  436. 'type' => 'serial',
  437. 'not null' => true,
  438. ),
  439. 'contact_id' => array (
  440. 'type' => 'int',
  441. 'not null' => true,
  442. ),
  443. 'pubauthor_id' => array (
  444. 'type' => 'int',
  445. 'not null' => true,
  446. ),
  447. ),
  448. 'primary key' => array (
  449. 0 => 'pubauthor_contact_id',
  450. ),
  451. 'unique keys' => array (
  452. 'pubauthor_contact_c1' => array (
  453. 0 => 'contact_id',
  454. 1 => 'pubauthor_id',
  455. ),
  456. ),
  457. 'foreign keys' => array (
  458. 'contact' => array (
  459. 'table' => 'contact',
  460. 'columns' => array (
  461. 'contact_id' => 'contact_id',
  462. ),
  463. ),
  464. 'pubauthor' => array (
  465. 'table' => 'pubauthor',
  466. 'columns' => array (
  467. 'pubauthor_id' => 'pubauthor_id',
  468. ),
  469. ),
  470. ),
  471. );
  472. chado_create_custom_table('pubauthor_contact', $schema, TRUE);
  473. }
  474. /**
  475. * Adds dbs related to publications
  476. *
  477. * @ingroup tripal_pub
  478. */
  479. function tripal_pub_add_dbs() {
  480. // make sure we have our supported databases
  481. tripal_insert_db(
  482. array(
  483. 'name' => 'PMID',
  484. 'description' => 'PubMed',
  485. 'url' => 'http://www.ncbi.nlm.nih.gov/pubmed',
  486. 'urlprefix' => 'http://www.ncbi.nlm.nih.gov/pubmed/'
  487. ),
  488. array('update_existing' => TRUE)
  489. );
  490. tripal_insert_db(
  491. array(
  492. 'name' => 'AGL',
  493. 'description' => 'USDA National Agricultural Library',
  494. 'url' => 'http://agricola.nal.usda.gov/'
  495. ),
  496. array('update_existing' => TRUE)
  497. );
  498. }
  499. /**
  500. * Add cvs related to publications
  501. *
  502. * @ingroup tripal_pub
  503. */
  504. function tripal_pub_add_cvs() {
  505. // Add the cv for pub properties
  506. tripal_insert_cv(
  507. 'tripal_pub',
  508. 'A heirarchical set of terms for describing a publication. It is intended to be used as the default vocabularies in Tripal for publication types and contact properties.'
  509. );
  510. // Add the cv for pub types
  511. tripal_insert_cv(
  512. 'pub_type',
  513. 'Contains types of publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  514. );
  515. // Add the cv for pub properties
  516. tripal_insert_cv(
  517. 'pub_property',
  518. 'Contains properties for publications. This can be used if the tripal_pub vocabulary (which is default for publications in Tripal) is not desired.'
  519. );
  520. // Add cv for relationship types
  521. tripal_insert_cv(
  522. 'pub_relationship',
  523. 'Contains types of relationships between publications.'
  524. );
  525. }
  526. /**
  527. * Add cvs pertaining to projects
  528. *
  529. * @ingroup tripal_project
  530. */
  531. function tripal_project_add_cvs() {
  532. // Add the cv for project properties
  533. tripal_insert_cv(
  534. 'project_property',
  535. 'Contains properties for projects'
  536. );
  537. // Add cv for relationship types
  538. tripal_insert_cv(
  539. 'project_relationship',
  540. 'Contains Types of relationships between projects.'
  541. );
  542. }
  543. /**
  544. * Add cvterms pertaining to projects
  545. *
  546. * @ingroup tripal_project
  547. */
  548. function tripal_project_add_cvterms() {
  549. // Insert cvterm 'Project Description' into cvterm table of chado
  550. // database. This CV term is used to keep track of the project
  551. // description in the projectprop table.
  552. tripal_insert_cvterm(
  553. array(
  554. 'name' => 'Project Description',
  555. 'definition' => 'Description of a project',
  556. 'cv_name' => 'project_property',
  557. 'is_relationship' => 0,
  558. 'db_name' => 'local'
  559. ),
  560. array('update_existing' => TRUE)
  561. );
  562. }
  563. /**
  564. * Add cvterms related to natural diversity
  565. *
  566. * @ingroup tripal_natural_diversity
  567. */
  568. function tripal_natural_diversity_add_cvterms(){
  569. // add cvterms for the nd_experiment_types
  570. tripal_insert_cvterm(
  571. array(
  572. 'name' => 'Genotyping',
  573. 'definition' => 'An experiment where genotypes of individuals are identified.',
  574. 'cv_name' => 'nd_experiment_types',
  575. 'is_relationship' => 0,
  576. 'db_name' => 'local'
  577. ),
  578. array('update_existing' => TRUE)
  579. );
  580. tripal_insert_cvterm(
  581. array(
  582. 'name' => 'Phenotyping',
  583. 'definition' => 'An experiment where phenotypes of individuals are identified.',
  584. 'cv_name' => 'nd_experiment_types',
  585. 'is_relationship' => 0,
  586. 'db_name' => 'local'
  587. ),
  588. array('update_existing' => TRUE)
  589. );
  590. tripal_insert_cvterm(
  591. array(
  592. 'name' => 'Location',
  593. 'definition' => 'The name of the location.',
  594. 'cv_name' => 'nd_geolocation_property',
  595. 'is_relationship' => 0,
  596. 'db_name' => 'local'
  597. ),
  598. array('update_existing' => TRUE)
  599. );
  600. }
  601. /**
  602. * Adds a materialized view keeping track of the type of features associated with each library
  603. *
  604. * @ingroup tripal_library
  605. */
  606. function tripal_library_add_mview_library_feature_count(){
  607. $view_name = 'library_feature_count';
  608. $comment = 'Provides count of feature by type that are associated with all libraries';
  609. $schema = array(
  610. 'table' => $view_name,
  611. 'description' => $comment,
  612. 'fields' => array(
  613. 'library_id' => array(
  614. 'type' => 'int',
  615. 'not null' => TRUE,
  616. ),
  617. 'name' => array(
  618. 'type' => 'varchar',
  619. 'length' => 255,
  620. 'not null' => TRUE,
  621. ),
  622. 'num_features' => array(
  623. 'type' => 'int',
  624. 'not null' => TRUE,
  625. ),
  626. 'feature_type' => array(
  627. 'type' => 'varchar',
  628. 'length' => 255,
  629. 'not null' => TRUE,
  630. ),
  631. ),
  632. 'indexes' => array(
  633. 'library_feature_count_idx1' => array('library_id'),
  634. ),
  635. );
  636. $sql = "
  637. SELECT
  638. L.library_id, L.name,
  639. count(F.feature_id) as num_features,
  640. CVT.name as feature_type
  641. FROM library L
  642. INNER JOIN library_feature LF ON LF.library_id = L.library_id
  643. INNER JOIN feature F ON LF.feature_id = F.feature_id
  644. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  645. GROUP BY L.library_id, L.name, CVT.name
  646. ";
  647. tripal_add_mview($view_name, 'tripal_library', $schema, $sql, $comment);
  648. }
  649. /**
  650. * Adds cvterms needed for the library module
  651. *
  652. * @ingroup tripal_library
  653. */
  654. function tripal_library_add_cvterms() {
  655. // Insert cvterm 'library_description' into cvterm table of chado
  656. // database. This CV term is used to keep track of the library
  657. // description in the libraryprop table.
  658. tripal_insert_cvterm(
  659. array(
  660. 'id' => 'local:library_description',
  661. 'name' => 'Library Description',
  662. 'definition' => 'Description of a library',
  663. 'cv_name' => 'library_property',
  664. 'is_relationship' => 0,
  665. 'db_name' => 'local'
  666. ),
  667. array('update_existing' => TRUE)
  668. );
  669. // add cvterms for the map unit types
  670. tripal_insert_cvterm(
  671. array(
  672. 'id' => 'local:cdna_library',
  673. 'name' => 'cdna_library',
  674. 'definition' => 'cDNA library',
  675. 'cv_name' => 'library_type',
  676. 'is_relationship' => 0,
  677. 'db_name' => 'local'
  678. ),
  679. array('update_existing' => TRUE)
  680. );
  681. tripal_insert_cvterm(
  682. array(
  683. 'id' => 'local:bac_library',
  684. 'name' => 'bac_library',
  685. 'definition' => 'Bacterial Artifical Chromsome (BAC) library',
  686. 'cv_name' => 'library_type',
  687. 'is_relationship' => 0,
  688. 'db_name' => 'local'
  689. ),
  690. array('update_existing' => TRUE)
  691. );
  692. tripal_insert_cvterm(
  693. array(
  694. 'id' => 'local:fosmid_library',
  695. 'name' => 'fosmid_library',
  696. 'definition' => 'Fosmid library',
  697. 'cv_name' => 'library_type',
  698. 'is_relationship' => 0,
  699. 'db_name' => 'local'
  700. ),
  701. array('update_existing' => TRUE)
  702. );
  703. tripal_insert_cvterm(
  704. array(
  705. 'id' => 'local:cosmid_library',
  706. 'name' => 'cosmid_library',
  707. 'definition' => 'Cosmid library',
  708. 'cv_name' => 'library_type',
  709. 'is_relationship' => 0,
  710. 'db_name' => 'local'
  711. ),
  712. array('update_existing' => TRUE)
  713. );
  714. tripal_insert_cvterm(
  715. array(
  716. 'id' => 'local:yac_library',
  717. 'name' => 'yac_library',
  718. 'definition' => 'Yeast Artificial Chromosome (YAC) library',
  719. 'cv_name' => 'library_type',
  720. 'is_relationship' => 0,
  721. 'db_name' => 'local'
  722. ),
  723. array('update_existing' => TRUE)
  724. );
  725. tripal_insert_cvterm(
  726. array(
  727. 'id' => 'local:genomic_library',
  728. 'name' => 'genomic_library',
  729. 'definition' => 'Genomic Library',
  730. 'cv_name' => 'library_type',
  731. 'is_relationship' => 0,
  732. 'db_name' => 'local'
  733. ),
  734. array('update_existing' => TRUE)
  735. );
  736. }
  737. /**
  738. * Adds new CV's used by this module
  739. *
  740. * @ingroup tripal_library
  741. */
  742. function tripal_library_add_cvs(){
  743. tripal_insert_cv(
  744. 'library_property',
  745. 'Contains properties for libraries.'
  746. );
  747. tripal_insert_cv(
  748. 'library_type',
  749. 'Contains terms for types of libraries (e.g. BAC, cDNA, FOSMID, etc).'
  750. );
  751. }
  752. /**
  753. * Add custom tables needed by the feature map module
  754. * - featuremapprop
  755. * - featuremap_dbxref
  756. * - featureposprop
  757. *
  758. * @ingroup tripal_featuremap
  759. */
  760. function tripal_featuremap_add_custom_tables(){
  761. // add the featuremaprop table to Chado
  762. $schema = array (
  763. 'table' => 'featuremapprop',
  764. 'fields' => array (
  765. 'featuremapprop_id' => array (
  766. 'type' => 'serial',
  767. 'not null' => true,
  768. ),
  769. 'featuremap_id' => array (
  770. 'type' => 'int',
  771. 'not null' => true,
  772. ),
  773. 'type_id' => array (
  774. 'type' => 'int',
  775. 'not null' => true,
  776. ),
  777. 'value' => array (
  778. 'type' => 'text',
  779. 'not null' => false,
  780. ),
  781. 'rank' => array (
  782. 'type' => 'int',
  783. 'not null' => true,
  784. 'default' => 0,
  785. ),
  786. ),
  787. 'primary key' => array (
  788. 0 => 'featuremapprop_id',
  789. ),
  790. 'unique keys' => array (
  791. 'featuremapprop_c1' => array (
  792. 0 => 'featuremap_id',
  793. 1 => 'type_id',
  794. 2 => 'rank',
  795. ),
  796. ),
  797. 'indexes' => array (
  798. 'featuremapprop_idx1' => array (
  799. 0 => 'featuremap_id',
  800. ),
  801. 'featuremapprop_idx2' => array (
  802. 0 => 'type_id',
  803. ),
  804. ),
  805. 'foreign keys' => array (
  806. 'cvterm' => array (
  807. 'table' => 'cvterm',
  808. 'columns' => array (
  809. 'type_id' => 'cvterm_id',
  810. ),
  811. ),
  812. 'featuremap' => array (
  813. 'table' => 'featuremap',
  814. 'columns' => array (
  815. 'featuremap_id' => 'featuremap_id',
  816. ),
  817. ),
  818. ),
  819. );
  820. chado_create_custom_table('featuremapprop', $schema, TRUE);
  821. // add the featuremap_dbxref table to Chado
  822. $schema = array (
  823. 'table' => 'featuremap_dbxref',
  824. 'fields' => array (
  825. 'featuremap_dbxref_id' => array (
  826. 'type' => 'serial',
  827. 'not null' => true,
  828. ),
  829. 'featuremap_id' => array (
  830. 'type' => 'int',
  831. 'not null' => true,
  832. ),
  833. 'dbxref_id' => array (
  834. 'type' => 'int',
  835. 'not null' => true,
  836. ),
  837. ),
  838. 'primary key' => array (
  839. 0 => 'featuremap_dbxref_id',
  840. ),
  841. 'unique keys' => array (
  842. 'featuremap_dbxref_c1' => array (
  843. 0 => 'featuremap_id',
  844. 1 => 'dbxref_id',
  845. ),
  846. ),
  847. 'indexes' => array (
  848. 'featuremap_dbxref_idx1' => array (
  849. 0 => 'featuremap_dbxref_id',
  850. ),
  851. 'featuremap_dbxref_idx2' => array (
  852. 0 => 'dbxref_id',
  853. ),
  854. ),
  855. 'foreign keys' => array (
  856. 'dbxref' => array (
  857. 'table' => 'dbxref',
  858. 'columns' => array (
  859. 'dbxref_id' => 'dbxref_id',
  860. ),
  861. ),
  862. 'featuremap' => array (
  863. 'table' => 'featuremap',
  864. 'columns' => array (
  865. 'featuremap_id' => 'featuremap_id',
  866. ),
  867. ),
  868. ),
  869. 'referring_tables' => NULL,
  870. );
  871. chado_create_custom_table('featuremap_dbxref', $schema, TRUE);
  872. $schema = array (
  873. 'table' => 'featureposprop',
  874. 'fields' => array (
  875. 'featureposprop_id' => array (
  876. 'type' => 'serial',
  877. 'not null' => true,
  878. ),
  879. 'featurepos_id' => array (
  880. 'type' => 'int',
  881. 'not null' => true,
  882. ),
  883. 'type_id' => array (
  884. 'type' => 'int',
  885. 'not null' => true,
  886. ),
  887. 'value' => array (
  888. 'type' => 'text',
  889. 'not null' => false,
  890. ),
  891. 'rank' => array (
  892. 'type' => 'int',
  893. 'not null' => true,
  894. 'default' => 0,
  895. ),
  896. ),
  897. 'primary key' => array (
  898. 0 => 'featureposprop_id',
  899. ),
  900. 'unique keys' => array (
  901. 'featureposprop_id' => array (
  902. 0 => 'featurepos_id',
  903. 1 => 'type_id',
  904. 2 => 'rank',
  905. ),
  906. ),
  907. 'indexes' => array (
  908. 'featureposprop_c1' => array (
  909. 0 => 'featurepos_id',
  910. ),
  911. 'featureposprop_idx2' => array (
  912. 0 => 'type_id',
  913. ),
  914. ),
  915. 'foreign keys' => array (
  916. 'cvterm' => array (
  917. 'table' => 'cvterm',
  918. 'columns' => array (
  919. 'type_id' => 'cvterm_id',
  920. ),
  921. ),
  922. 'featurepos' => array (
  923. 'table' => 'featurepos',
  924. 'columns' => array (
  925. 'featurepos_id' => 'featurepos_id',
  926. ),
  927. ),
  928. ),
  929. );
  930. chado_create_custom_table('featureposprop', $schema, TRUE);
  931. }
  932. /**
  933. * Add cv terms needed by the featuremap module
  934. *
  935. * @ingroup tripal_featuremap
  936. */
  937. function tripal_featuremap_add_cvterms() {
  938. // add cvterms for the map unit types
  939. tripal_insert_cvterm(
  940. array(
  941. 'name' => 'cM',
  942. 'definition' => 'Centimorgan units',
  943. 'cv_name' => 'featuremap_units',
  944. 'is_relationship' => 0,
  945. 'db_name' => 'local'
  946. ),
  947. array('update_existing' => TRUE)
  948. );
  949. tripal_insert_cvterm(
  950. array(
  951. 'name' => 'bp',
  952. 'definition' => 'Base pairs units',
  953. 'cv_name' => 'featuremap_units',
  954. 'is_relationship' => 0,
  955. 'db_name' => 'local'
  956. ),
  957. array('update_existing' => TRUE)
  958. );
  959. tripal_insert_cvterm(
  960. array(
  961. 'name' => 'bin_unit',
  962. 'definition' => 'The bin unit',
  963. 'cv_name' => 'featuremap_units',
  964. 'is_relationship' => 0,
  965. 'db_name' => 'local'
  966. ),
  967. array('update_existing' => TRUE)
  968. );
  969. tripal_insert_cvterm(
  970. array(
  971. 'name' => 'marker_order',
  972. 'definition' => 'Units simply to define marker order.',
  973. 'cv_name' => 'featuremap_units',
  974. 'is_relationship' => 0,
  975. 'db_name' => 'local'
  976. ),
  977. array('update_existing' => TRUE)
  978. );
  979. tripal_insert_cvterm(
  980. array(
  981. 'name' => 'undefined',
  982. 'definition' => 'A catch-all for an undefined unit type',
  983. 'cv_name' => 'featuremap_units',
  984. 'is_relationship' => 0,
  985. 'db_name' => 'local'
  986. ),
  987. array('update_existing' => TRUE)
  988. );
  989. // featurepos properties
  990. tripal_insert_cvterm(
  991. array(
  992. 'name' => 'start',
  993. 'definition' => 'The start coordinate for a map feature.',
  994. 'cv_name' => 'featurepos_property',
  995. 'is_relationship' => 0,
  996. 'db_name' => 'local'
  997. ),
  998. array('update_existing' => TRUE)
  999. );
  1000. tripal_insert_cvterm(
  1001. array(
  1002. 'name' => 'stop',
  1003. 'definition' => 'The end coordinate for a map feature',
  1004. 'cv_name' => 'featurepos_property',
  1005. 'is_relationship' => 0,
  1006. 'db_name' => 'local'
  1007. ),
  1008. array('update_existing' => TRUE)
  1009. );
  1010. // add cvterms for map properties
  1011. tripal_insert_cvterm(
  1012. array(
  1013. 'name' => 'Map Dbxref',
  1014. 'definition' => 'A unique identifer for the map in a remote database. The '
  1015. . 'format is a database abbreviation and a unique accession separated '
  1016. . 'by a colon. (e.g. Gramene:tsh1996a)',
  1017. 'cv_name' => 'featuremap_property',
  1018. 'is_relationship' => 0,
  1019. 'db_name' => 'local'
  1020. ),
  1021. array('update_existing' => TRUE)
  1022. );
  1023. tripal_insert_cvterm(
  1024. array(
  1025. 'name' => 'Map Type',
  1026. 'definition' => 'The type of Map (e.g. QTL, Physical, etc.)',
  1027. 'cv_name' => 'featuremap_property',
  1028. 'is_relationship' => 0,
  1029. 'db_name' => 'local'
  1030. ),
  1031. array('update_existing' => TRUE)
  1032. );
  1033. tripal_insert_cvterm(
  1034. array(
  1035. 'name' => 'Genome Group',
  1036. 'definition' => '',
  1037. 'cv_name' => 'featuremap_property',
  1038. 'is_relationship' => 0,
  1039. 'db_name' => 'local'
  1040. ),
  1041. array('update_existing' => TRUE)
  1042. );
  1043. tripal_insert_cvterm(
  1044. array(
  1045. 'name' => 'URL',
  1046. 'definition' => 'A univeral resource locator (URL) reference where the '
  1047. . 'publication can be found. For maps found online, this would be '
  1048. . 'the web address for the map.',
  1049. 'cv_name' => 'featuremap_property',
  1050. 'is_relationship' => 0,
  1051. 'db_name' => 'local'
  1052. ),
  1053. array('update_existing' => TRUE)
  1054. );
  1055. tripal_insert_cvterm(
  1056. array(
  1057. 'name' => 'Population Type',
  1058. 'definition' => 'A brief description of the population type used to generate '
  1059. . 'the map (e.g. RIL, F2, BC1, etc).',
  1060. 'cv_name' => 'featuremap_property',
  1061. 'is_relationship' => 0,
  1062. 'db_name' => 'local'
  1063. ),
  1064. array('update_existing' => TRUE)
  1065. );
  1066. tripal_insert_cvterm(
  1067. array(
  1068. 'name' => 'Population Size',
  1069. 'definition' => 'The size of the population used to construct the map.',
  1070. 'cv_name' => 'featuremap_property',
  1071. 'is_relationship' => 0,
  1072. 'db_name' => 'local'
  1073. ),
  1074. array('update_existing' => TRUE)
  1075. );
  1076. tripal_insert_cvterm(
  1077. array(
  1078. 'name' => 'Methods',
  1079. 'definition' => 'A brief description of the methods used to construct the map.',
  1080. 'cv_name' => 'featuremap_property',
  1081. 'is_relationship' => 0,
  1082. 'db_name' => 'local'
  1083. ),
  1084. array('update_existing' => TRUE)
  1085. );
  1086. tripal_insert_cvterm(
  1087. array(
  1088. 'name' => 'Software',
  1089. 'definition' => 'The software used to construct the map.',
  1090. 'cv_name' => 'featuremap_property',
  1091. 'is_relationship' => 0,
  1092. 'db_name' => 'local'
  1093. ),
  1094. array('update_existing' => TRUE)
  1095. );
  1096. }
  1097. /**
  1098. * Add cvs needed by the featuremap module
  1099. *
  1100. * @ingroup tripal_featuremap
  1101. */
  1102. function tripal_featuremap_add_cvs() {
  1103. tripal_insert_cv(
  1104. 'featuremap_units',
  1105. 'Contains map unit types for the unittype_id column of the featuremap table.'
  1106. );
  1107. tripal_insert_cv(
  1108. 'featurepos_property',
  1109. 'Contains terms map properties.'
  1110. );
  1111. tripal_insert_cv(
  1112. 'featuremap_property',
  1113. 'Contains positional types for the feature positions'
  1114. );
  1115. }
  1116. /**
  1117. * Add cvs related to features
  1118. *
  1119. * @ingroup tripal_pub
  1120. */
  1121. function tripal_feature_add_cvs() {
  1122. // Add cv for relationship types
  1123. tripal_insert_cv(
  1124. 'feature_relationship',
  1125. 'Contains types of relationships between features.'
  1126. );
  1127. // The feature_property CV may already exists. It comes with Chado, but
  1128. // we need to add it just in case it doesn't get added before the feature
  1129. // module is installed. But as of Tripal v3.0 the Chado version of this
  1130. // vocabulary is no longer loaded by default.
  1131. tripal_insert_cv(
  1132. 'feature_property',
  1133. 'Stores properties about features'
  1134. );
  1135. // the feature type vocabulary should be the sequence ontology, and even though
  1136. // this ontology should get loaded we will create it here just so that we can
  1137. // set the default vocabulary for the feature.type_id field
  1138. tripal_insert_cv(
  1139. 'sequence',
  1140. 'The Sequence Ontology'
  1141. );
  1142. }
  1143. function tripal_feature_add_tripal_gff_temp_table() {
  1144. $schema = array(
  1145. 'table' => 'tripal_gff_temp',
  1146. 'fields' => array(
  1147. 'feature_id' => array(
  1148. 'type' => 'int',
  1149. 'not null' => TRUE,
  1150. ),
  1151. 'organism_id' => array(
  1152. 'type' => 'int',
  1153. 'not null' => TRUE,
  1154. ),
  1155. 'uniquename' => array(
  1156. 'type' => 'text',
  1157. 'not null' => TRUE,
  1158. ),
  1159. 'type_name' => array(
  1160. 'type' => 'varchar',
  1161. 'length' => '1024',
  1162. 'not null' => TRUE,
  1163. ),
  1164. ),
  1165. 'indexes' => array(
  1166. 'tripal_gff_temp_idx0' => array('feature_id'),
  1167. 'tripal_gff_temp_idx0' => array('organism_id'),
  1168. 'tripal_gff_temp_idx1' => array('uniquename'),
  1169. ),
  1170. 'unique keys' => array(
  1171. 'tripal_gff_temp_uq0' => array('feature_id'),
  1172. 'tripal_gff_temp_uq1' => array('uniquename', 'organism_id', 'type_name'),
  1173. ),
  1174. );
  1175. chado_create_custom_table('tripal_gff_temp', $schema, TRUE);
  1176. }
  1177. /**
  1178. *
  1179. */
  1180. function tripal_feature_add_tripal_gffcds_temp_table($skip_recreate = TRUE) {
  1181. $schema = array(
  1182. 'table' => 'tripal_gffcds_temp',
  1183. 'fields' => array(
  1184. 'feature_id' => array(
  1185. 'type' => 'int',
  1186. 'not null' => TRUE,
  1187. ),
  1188. 'parent_id' => array(
  1189. 'type' => 'int',
  1190. 'not null' => TRUE,
  1191. ),
  1192. 'phase' => array(
  1193. 'type' => 'int',
  1194. 'not null' => TRUE,
  1195. ),
  1196. 'strand' => array(
  1197. 'type' => 'int',
  1198. 'not null' => TRUE,
  1199. ),
  1200. 'fmin' => array(
  1201. 'type' => 'int',
  1202. 'not null' => TRUE,
  1203. ),
  1204. 'fmax' => array(
  1205. 'type' => 'int',
  1206. 'not null' => TRUE,
  1207. ),
  1208. ),
  1209. 'indexes' => array(
  1210. 'tripal_gff_temp_idx0' => array('feature_id'),
  1211. 'tripal_gff_temp_idx0' => array('parent_id'),
  1212. ),
  1213. );
  1214. chado_create_custom_table('tripal_gffcds_temp', $schema, $skip_recreate);
  1215. }
  1216. /**
  1217. *
  1218. */
  1219. function tripal_feature_add_tripal_gffprotein_temp_table() {
  1220. $schema = array(
  1221. 'table' => 'tripal_gffprotein_temp',
  1222. 'fields' => array(
  1223. 'feature_id' => array(
  1224. 'type' => 'int',
  1225. 'not null' => TRUE,
  1226. ),
  1227. 'parent_id' => array(
  1228. 'type' => 'int',
  1229. 'not null' => TRUE,
  1230. ),
  1231. 'fmin' => array(
  1232. 'type' => 'int',
  1233. 'not null' => TRUE,
  1234. ),
  1235. 'fmax' => array(
  1236. 'type' => 'int',
  1237. 'not null' => TRUE,
  1238. ),
  1239. ),
  1240. 'indexes' => array(
  1241. 'tripal_gff_temp_idx0' => array('feature_id'),
  1242. 'tripal_gff_temp_idx0' => array('parent_id'),
  1243. ),
  1244. 'unique keys' => array(
  1245. 'tripal_gff_temp_uq0' => array('feature_id'),
  1246. ),
  1247. );
  1248. chado_create_custom_table('tripal_gffprotein_temp', $schema, TRUE);
  1249. }
  1250. /**
  1251. * Creates a materialized view that stores the type & number of features per organism
  1252. *
  1253. * @ingroup tripal_feature
  1254. */
  1255. function tripal_feature_add_organism_count_mview() {
  1256. $view_name = 'organism_feature_count';
  1257. $comment = 'Stores the type and number of features per organism';
  1258. $schema = array(
  1259. 'description' => $comment,
  1260. 'table' => $view_name,
  1261. 'fields' => array(
  1262. 'organism_id' => array(
  1263. 'type' => 'int',
  1264. 'not null' => TRUE,
  1265. ),
  1266. 'genus' => array(
  1267. 'type' => 'varchar',
  1268. 'length' => '255',
  1269. 'not null' => TRUE,
  1270. ),
  1271. 'species' => array(
  1272. 'type' => 'varchar',
  1273. 'length' => '255',
  1274. 'not null' => TRUE,
  1275. ),
  1276. 'common_name' => array(
  1277. 'type' => 'varchar',
  1278. 'length' => '255',
  1279. 'not null' => FALSE,
  1280. ),
  1281. 'num_features' => array(
  1282. 'type' => 'int',
  1283. 'not null' => TRUE,
  1284. ),
  1285. 'cvterm_id' => array(
  1286. 'type' => 'int',
  1287. 'not null' => TRUE,
  1288. ),
  1289. 'feature_type' => array(
  1290. 'type' => 'varchar',
  1291. 'length' => '255',
  1292. 'not null' => TRUE,
  1293. ),
  1294. ),
  1295. 'indexes' => array(
  1296. 'organism_feature_count_idx1' => array('organism_id'),
  1297. 'organism_feature_count_idx2' => array('cvterm_id'),
  1298. 'organism_feature_count_idx3' => array('feature_type'),
  1299. ),
  1300. );
  1301. $sql = "
  1302. SELECT
  1303. O.organism_id, O.genus, O.species, O.common_name,
  1304. count(F.feature_id) as num_features,
  1305. CVT.cvterm_id, CVT.name as feature_type
  1306. FROM organism O
  1307. INNER JOIN feature F ON O.Organism_id = F.organism_id
  1308. INNER JOIN cvterm CVT ON F.type_id = CVT.cvterm_id
  1309. GROUP BY
  1310. O.Organism_id, O.genus, O.species, O.common_name, CVT.cvterm_id, CVT.name
  1311. ";
  1312. tripal_add_mview($view_name, 'tripal_feature', $schema, $sql, $comment);
  1313. }
  1314. /**
  1315. * Adds any cvs needed by this module.
  1316. *
  1317. * @ingroup tripal_contact
  1318. */
  1319. function tripal_contact_add_cvs() {
  1320. // Add the cv for contact properties. This is a default vocabulary in the event
  1321. // that a user does not want to use the tripal_contact vocabulary
  1322. tripal_insert_cv(
  1323. 'contact_property',
  1324. 'Contains properties for contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  1325. );
  1326. // add the cv for the contact type. This is a default vocabulary in the event
  1327. // that a user does not want to use the tripal_contact vocabulary
  1328. tripal_insert_cv(
  1329. 'contact_type',
  1330. 'Contains types of contacts. This can be used if the tripal_contact vocabulary (which is default for contacts in Tripal) is not desired.'
  1331. );
  1332. // Add the cv for the tripal_contact vocabulary which is loaded via the OBO
  1333. tripal_insert_cv(
  1334. 'tripal_contact',
  1335. 'A heirarchical set of terms for describing a contact. It is intended to be used as the default vocabularies in Tripal for contact types and contact properties.'
  1336. );
  1337. // add the cv for contact relationships
  1338. tripal_insert_cv(
  1339. 'contact_relationship',
  1340. 'Contains types of relationships between contacts.'
  1341. );
  1342. }
  1343. /**
  1344. * Add any custom tables needed by this module.
  1345. * - Contactprop: keep track of properties of contact
  1346. *
  1347. * @ingroup tripal_contact
  1348. */
  1349. function tripal_contact_add_custom_tables(){
  1350. $schema = array (
  1351. 'table' => 'contactprop',
  1352. 'fields' => array (
  1353. 'contactprop_id' => array (
  1354. 'type' => 'serial',
  1355. 'not null' => true,
  1356. ),
  1357. 'contact_id' => array (
  1358. 'type' => 'int',
  1359. 'not null' => true,
  1360. ),
  1361. 'type_id' => array (
  1362. 'type' => 'int',
  1363. 'not null' => true,
  1364. ),
  1365. 'value' => array (
  1366. 'type' => 'text',
  1367. 'not null' => false,
  1368. ),
  1369. 'rank' => array (
  1370. 'type' => 'int',
  1371. 'not null' => true,
  1372. 'default' => 0,
  1373. ),
  1374. ),
  1375. 'primary key' => array (
  1376. 0 => 'contactprop_id',
  1377. ),
  1378. 'unique keys' => array (
  1379. 'contactprop_c1' => array (
  1380. 0 => 'contact_id',
  1381. 1 => 'type_id',
  1382. 2 => 'rank',
  1383. ),
  1384. ),
  1385. 'indexes' => array (
  1386. 'contactprop_idx1' => array (
  1387. 0 => 'contact_id',
  1388. ),
  1389. 'contactprop_idx2' => array (
  1390. 0 => 'type_id',
  1391. ),
  1392. ),
  1393. 'foreign keys' => array (
  1394. 'cvterm' => array (
  1395. 'table' => 'cvterm',
  1396. 'columns' => array (
  1397. 'type_id' => 'cvterm_id',
  1398. ),
  1399. ),
  1400. 'contact' => array (
  1401. 'table' => 'contact',
  1402. 'columns' => array (
  1403. 'contact_id' => 'contact_id',
  1404. ),
  1405. ),
  1406. ),
  1407. );
  1408. chado_create_custom_table('contactprop', $schema, TRUE);
  1409. }
  1410. /**
  1411. * Create a legacy custom chado table (analysisfeatureprop) to store properties of
  1412. * analysisfeature links.
  1413. *
  1414. * @ingroup tripal_analysis
  1415. */
  1416. function tripal_analysis_create_analysisfeatureprop() {
  1417. // Create analysisfeatureprop table in chado. This is needed for Chado
  1418. // version 1.11, the table exists in Chado 1.2.
  1419. if (!db_table_exists('chado.analysisfeatureprop')) {
  1420. $sql = "
  1421. CREATE TABLE {analysisfeatureprop} (
  1422. analysisfeatureprop_id SERIAL PRIMARY KEY,
  1423. analysisfeature_id INTEGER NOT NULL,
  1424. type_id INTEGER NOT NULL,
  1425. value TEXT,
  1426. rank INTEGER NOT NULL,
  1427. CONSTRAINT analysisfeature_id_type_id_rank UNIQUE (analysisfeature_id, type_id, rank),
  1428. CONSTRAINT analysisfeatureprop_analysisfeature_id_fkey FOREIGN KEY (analysisfeature_id) REFERENCES {analysisfeature}(analysisfeature_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
  1429. CONSTRAINT analysisfeatureprop_type_id_fkey FOREIGN KEY (type_id) REFERENCES {cvterm}(cvterm_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
  1430. )
  1431. ";
  1432. chado_query($sql);
  1433. }
  1434. }
  1435. /**
  1436. * Creates a view showing the link between an organism & it's analysis through associated features.
  1437. *
  1438. * @ingroup tripal_analysis
  1439. */
  1440. function tripal_analysis_add_mview_analysis_organism() {
  1441. $view_name = 'analysis_organism';
  1442. $comment = t('This view is for associating an organism (via it\'s associated features) to an analysis.');
  1443. // this is the SQL used to identify the organism to which an analsysis
  1444. // has been used. This is obtained though the analysisfeature -> feature -> organism
  1445. // joins
  1446. $sql = "
  1447. SELECT DISTINCT A.analysis_id, O.organism_id
  1448. FROM analysis A
  1449. INNER JOIN analysisfeature AF ON A.analysis_id = AF.analysis_id
  1450. INNER JOIN feature F ON AF.feature_id = F.feature_id
  1451. INNER JOIN organism O ON O.organism_id = F.organism_id
  1452. ";
  1453. // the schema array for describing this view
  1454. $schema = array(
  1455. 'table' => $view_name,
  1456. 'description' => $comment,
  1457. 'fields' => array(
  1458. 'analysis_id' => array(
  1459. 'type' => 'int',
  1460. 'not null' => TRUE,
  1461. ),
  1462. 'organism_id' => array(
  1463. 'type' => 'int',
  1464. 'not null' => TRUE,
  1465. ),
  1466. ),
  1467. 'indexes' => array(
  1468. 'networkmod_qtl_indx0' => array('analysis_id'),
  1469. 'networkmod_qtl_indx1' => array('organism_id'),
  1470. ),
  1471. 'foreign keys' => array(
  1472. 'analysis' => array(
  1473. 'table' => 'analysis',
  1474. 'columns' => array(
  1475. 'analysis_id' => 'analysis_id',
  1476. ),
  1477. ),
  1478. 'organism' => array(
  1479. 'table' => 'organism',
  1480. 'columns' => array(
  1481. 'organism_id' => 'organism_id',
  1482. ),
  1483. ),
  1484. ),
  1485. );
  1486. // add the view
  1487. tripal_add_mview($view_name, 'tripal_analysis', $schema, $sql, $comment);
  1488. }
  1489. /**
  1490. * Adds controlled vocabulary terms needed by this module.
  1491. *
  1492. * @ingroup tripal_analysis
  1493. */
  1494. function tripal_analysis_add_cvterms() {
  1495. tripal_insert_cv(
  1496. 'tripal_analysis',
  1497. 'Terms used for managing analyses in Tripal'
  1498. );
  1499. // add analysis_date. This is no longer used (as far as we can tell) but we don't
  1500. // get rid of it in case it is used, so just keep it in the Tripal CV
  1501. tripal_insert_cvterm(
  1502. array(
  1503. 'name' => 'analysis_date',
  1504. 'definition' => 'The date that an analysis was performed.',
  1505. 'cv_name' => 'tripal_analysis',
  1506. 'is_relationship' => 0,
  1507. 'db_name' => 'local'
  1508. ),
  1509. array('update_existing' => TRUE)
  1510. );
  1511. // add analysis_short_name. This is no longer used (as far as we can tell) but we don't
  1512. // get rid of it in case it is used, so just keep it in the Tripal CV
  1513. tripal_insert_cvterm(
  1514. array(
  1515. 'name' => 'analysis_short_name',
  1516. 'definition' => 'A computer legible (no spaces or special characters) '
  1517. . 'abbreviation for the analysis.',
  1518. 'cv_name' => 'tripal_analysis',
  1519. 'is_relationship' => 0,
  1520. 'db_name' => 'local'
  1521. ),
  1522. array('update_existing' => TRUE)
  1523. );
  1524. // the 'analysis_property' vocabulary is for user definable properties wo we
  1525. // will add an 'Analysis Type' to this vocubulary
  1526. tripal_insert_cvterm(
  1527. array(
  1528. 'name' => 'Analysis Type',
  1529. 'definition' => 'The type of analysis that was performed.',
  1530. 'cv_name' => 'analysis_property',
  1531. 'is_relationship' => 0,
  1532. 'db_name' => 'local'
  1533. ),
  1534. array('update_existing' => TRUE)
  1535. );
  1536. }
  1537. /**
  1538. * Add's defaults to the tripal_cv_obo table
  1539. *
  1540. * @ingroup tripal_cv
  1541. */
  1542. function tripal_cv_add_obo_defaults() {
  1543. // Insert commonly used ontologies into the tables.
  1544. $ontologies = array(
  1545. // array('Relationship Ontology', 'http://purl.obolibrary.org/obo/ro.obo'),
  1546. // array('Relationship Ontology (older deprecated version)', 'http://www.obofoundry.org/ro/ro.obo'),
  1547. array('Sequence Ontology', 'https://raw.githubusercontent.com/The-Sequence-Ontology/SO-Ontologies/master/so-xp-simple.obo'),
  1548. array('Gene Ontology', 'http://geneontology.org/ontology/go.obo'),
  1549. // array('Cell Ontology', 'https://raw.githubusercontent.com/obophenotype/cell-ontology/master/cl.obo'),
  1550. // array('Plant Structure Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_anatomy.obo?view=co'),
  1551. // array('Plant Growth and Development Stages Ontology', 'http://palea.cgrb.oregonstate.edu/viewsvn/Poc/trunk/ontology/OBO_format/po_temporal.obo?view=co')
  1552. );
  1553. foreach ($ontologies as $o) {
  1554. db_query("INSERT INTO {tripal_cv_obo} (name,path) VALUES (:name, :path)", array(':name' => $o[0], ':path' => $o[1]));
  1555. }
  1556. }
  1557. /**
  1558. * Add a materialized view of root terms for all chado cvs. This is needed for viewing cv trees
  1559. *
  1560. * @ingroup tripal_cv
  1561. */
  1562. function tripal_cv_add_cv_root_mview() {
  1563. $mv_name = 'cv_root_mview';
  1564. $comment = 'A list of the root terms for all controlled vocabularies. This is needed for viewing CV trees';
  1565. $schema = array(
  1566. 'table' => $mv_name,
  1567. 'description' => $comment,
  1568. 'fields' => array(
  1569. 'name' => array(
  1570. 'type' => 'varchar',
  1571. 'length' => 255,
  1572. 'not null' => TRUE,
  1573. ),
  1574. 'cvterm_id' => array(
  1575. 'type' => 'int',
  1576. 'not null' => TRUE,
  1577. ),
  1578. 'cv_id' => array(
  1579. 'type' => 'int',
  1580. 'not null' => TRUE,
  1581. ),
  1582. 'cv_name' => array(
  1583. 'type' => 'varchar',
  1584. 'length' => 255,
  1585. 'not null' => TRUE,
  1586. ),
  1587. ),
  1588. 'indexes' => array(
  1589. 'cv_root_mview_indx1' => array('cvterm_id'),
  1590. 'cv_root_mview_indx2' => array('cv_id'),
  1591. ),
  1592. );
  1593. $sql = "
  1594. SELECT DISTINCT CVT.name,CVT.cvterm_id, CV.cv_id, CV.name
  1595. FROM cvterm_relationship CVTR
  1596. INNER JOIN cvterm CVT on CVTR.object_id = CVT.cvterm_id
  1597. INNER JOIN cv CV on CV.cv_id = CVT.cv_id
  1598. WHERE CVTR.object_id not in
  1599. (SELECT subject_id FROM cvterm_relationship)
  1600. ";
  1601. // Create the MView
  1602. tripal_add_mview($mv_name, 'tripal_cv', $schema, $sql, $comment);
  1603. }
  1604. /**
  1605. * Creates a temporary table to store obo details while loading an obo file
  1606. *
  1607. * @ingroup tripal_cv
  1608. */
  1609. function tripal_cv_create_tripal_obo_temp() {
  1610. // the tripal_obo_temp table is used for temporary housing of records when loading OBO files
  1611. // we create it here using plain SQL because we want it to be in the chado schema but we
  1612. // do not want to use the Tripal Custom Table API because we don't want it to appear in the
  1613. // list of custom tables. It needs to be available for the Tripal Chado API so we create it
  1614. // here and then define it in the tripal_cv/api/tripal_cv.schema.api.inc
  1615. if (!db_table_exists('chado.tripal_obo_temp')) {
  1616. $sql = "
  1617. CREATE TABLE {tripal_obo_temp} (
  1618. id character varying(255) NOT NULL,
  1619. stanza text NOT NULL,
  1620. type character varying(50) NOT NULL,
  1621. CONSTRAINT tripal_obo_temp_uq0 UNIQUE (id)
  1622. );
  1623. ";
  1624. chado_query($sql);
  1625. $sql = "CREATE INDEX tripal_obo_temp_idx0 ON {tripal_obo_temp} USING btree (id)";
  1626. chado_query($sql);
  1627. $sql = "CREATE INDEX tripal_obo_temp_idx1 ON {tripal_obo_temp} USING btree (type)";
  1628. chado_query($sql);
  1629. }
  1630. }
  1631. /**
  1632. * Adds generic CVterms.
  1633. */
  1634. function tripal_insert_misc_cvterms() {
  1635. tripal_insert_cvterm(
  1636. array(
  1637. 'name' => 'exact',
  1638. 'id' => "local:exact",
  1639. 'definition' => 'An exact equivalent; interchangeable with the term name.',
  1640. 'is_obsolete' => 0,
  1641. 'cv_name' => 'synonym_type',
  1642. 'is_relationship' => FALSE
  1643. ),
  1644. array('update_existing' => TRUE)
  1645. );
  1646. tripal_insert_cvterm(
  1647. array(
  1648. 'name' => 'broad',
  1649. 'id' => "local:broad",
  1650. 'definition' => 'The synonym is broader than the name.',
  1651. 'is_obsolete' => 0,
  1652. 'cv_name' => 'synonym_type',
  1653. 'is_relationship' => FALSE
  1654. ),
  1655. array('update_existing' => TRUE)
  1656. );
  1657. tripal_insert_cvterm(
  1658. array(
  1659. 'name' => 'narrow',
  1660. 'id' => "local:narrow",
  1661. 'definition' => 'The synonym is narrower or more precise than the name.',
  1662. 'is_obsolete' => 0,
  1663. 'cv_name' => 'synonym_type',
  1664. 'is_relationship' => FALSE
  1665. ),
  1666. array('update_existing' => TRUE)
  1667. );
  1668. tripal_insert_cvterm(
  1669. array(
  1670. 'name' => 'related',
  1671. 'id' => "local:related",
  1672. 'definition' => 'The synonym is related in some way.',
  1673. 'is_obsolete' => 0,
  1674. 'cv_name' => 'synonym_type',
  1675. 'is_relationship' => FALSE
  1676. ),
  1677. array('update_existing' => TRUE)
  1678. );
  1679. }