bulk_loader.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <?php
  2. /*************************************************************************
  3. *
  4. */
  5. function tripal_core_bulk_loader_create (){
  6. return drupal_get_form('tripal_core_bulk_loader_create_form');
  7. }
  8. /*************************************************************************
  9. *
  10. */
  11. function tripal_core_bulk_loader_create_form (&$form_state = NULL){
  12. $form = array();
  13. // get the step used by this multistep form
  14. $step = 1;
  15. if(isset($form_state['storage'])){
  16. $step = (int)$form_state['storage']['step'];
  17. }
  18. $form = array();
  19. if($step == 1){
  20. tripal_core_bulk_loader_create_form_step1 ($form,$form_state);
  21. $form_state['storage']['step'] = $step + 1;
  22. }
  23. if($step == 2){
  24. tripal_core_bulk_loader_create_form_step2 ($form,$form_state);
  25. }
  26. return $form;
  27. }
  28. /*************************************************************************
  29. *
  30. */
  31. function tripal_core_bulk_loader_create_form_validate ($form,&$form_state) {
  32. }
  33. /*************************************************************************
  34. *
  35. */
  36. function tripal_core_bulk_loader_create_form_submit ($form,&$form_state) {
  37. if($form_State['storage']['step'] < 4){
  38. return;
  39. }
  40. }
  41. /*************************************************************************
  42. *
  43. */
  44. function tripal_core_bulk_loader_create_form_step2 (&$form,$form_state){
  45. if(isset($form_state['values']['columns'])){
  46. $form_state['storage']['columns'] = $form_state['values']['columns'];
  47. }
  48. $form['bulk_loader'] = array(
  49. '#type' => 'fieldset',
  50. '#title' => t('Step 2: Define the columns of the file'),
  51. );
  52. $columns = $form_state['storage']['columns'];
  53. // $form['debug']= array(
  54. // '#value' => "Columns: $columns",
  55. // '#weight' => -1
  56. // );
  57. $fields = array();
  58. $fields[''] = '';
  59. /*
  60. $form_fields = array(
  61. 'Feature details' => array (
  62. 'name' => array(
  63. 'description' => 'Feature Name (human readable)',
  64. 'table' => array(
  65. 'table name' => 'feature',
  66. 'fields' => array('name'),
  67. ),
  68. ),
  69. 'uniquename' => array(
  70. 'description' => 'Unique Name (must be unique for this organism and feature type)',
  71. 'table' => array(
  72. 'table name' => 'feature',
  73. 'fields' => array('uniquename'),
  74. ),
  75. ),
  76. 'type_id' => array(
  77. 'description' => 'Feature type (must be a valid SO term',
  78. 'table' => array(
  79. 'table name' => 'feature',
  80. 'fields' => array('uniquename'),
  81. ),
  82. ),
  83. 'residues' => array(
  84. 'description' => 'Residues',
  85. 'table' => array(
  86. 'table name' => 'feature',
  87. 'fields' => array('residues'),
  88. ),
  89. ),
  90. ),
  91. 'Organism specification' => array (
  92. 'organism_id' => array(
  93. 'description' => 'Organism ID number',
  94. 'table' => array(
  95. 'table name' => 'organism',
  96. 'fields' => array('organism_id'),
  97. ),
  98. ),
  99. 'genus' => array(
  100. 'description' => 'Genus',
  101. 'table' => array(
  102. 'table name' => 'organism',
  103. 'fields' => array('genus'),
  104. ),
  105. 'valid values' => 'this'
  106. ),
  107. 'species' => array(
  108. 'description' => 'Species',
  109. 'table' => array(
  110. 'table name' => 'organism',
  111. 'fields' => array('species'),
  112. ),
  113. ),
  114. 'full_name' => array(
  115. 'description' => 'Scientific Name (genus + species)',
  116. 'table' => array(
  117. 'table name' => 'organism',
  118. 'fields' => array('genus','species'),
  119. 'parser' => '/^(.*?)\s+(.*?)$/',
  120. ),
  121. ),
  122. 'common_name' => array(
  123. 'description' => 'Common Name',
  124. 'table' => array(
  125. 'table name' => 'organism',
  126. 'fields' => array('common_name'),
  127. ),
  128. ),
  129. ),
  130. 1. allow the user to specify the term from a specific vocabulary
  131. 2. allow the user to specify the vocabulary to choose from
  132. 3.
  133. 'Feature Property' => array (
  134. 'property' => array(
  135. 'description' => 'Feature property value',
  136. 'table' => array(
  137. 'table name' => 'featureprop',
  138. 'fields' => array('value'),
  139. 'check' => array(
  140. 'type_id' => {
  141. ),
  142. ).
  143. ),
  144. ),
  145. );
  146. */
  147. // these fields correspond with the columns of the feature table and
  148. // the foreign key contraints for the feature table.
  149. $fields = array(
  150. '' => '',
  151. 'ignore' => 'Ignore this column',
  152. 'Feature details' => array (
  153. 'name' => 'Feature Name (human readable)',
  154. 'uniquename' => 'Unique Feature Name',
  155. 'type' => 'Feature type (must be a valid SO term)',
  156. 'alt_type' => 'Additional ontology term',
  157. 'residues' => 'Residues',
  158. ),
  159. 'Organism specification' => array (
  160. 'organism_id' => 'Organism ID number',
  161. 'genus' => 'Genus',
  162. 'species' => 'Species',
  163. 'full_name' => 'Scientific Name (genus + species)',
  164. 'common_name' => 'Common Name',
  165. ),
  166. 'External Database Cross-Reference' => array(
  167. 'db_id' => 'Database ID number',
  168. 'db_name' => 'Database name (must exists in the database)',
  169. 'accession' => 'Accession',
  170. ),
  171. 'Feature Relationship' => array (
  172. 'rel_type' => 'Relationship Type (must be a valid relationship term)',
  173. 'parent' => 'Parent unique name',
  174. 'parent type' => 'Parent Type (must be a valid SO term)',
  175. ),
  176. 'Feature Location' => array (
  177. 'srcfeature' => 'Reference feature (must already exist in the database)',
  178. 'srcfeature_type' => 'Reference feature type (must be a valid SO term)',
  179. 'fmin' => 'Start position',
  180. 'fmax' => 'Stop position',
  181. 'strand' => 'Strand (valid values: 0,-1,+1)',
  182. 'phase' => 'Phase (valid values: (+,-,.)',
  183. ),
  184. 'Feature Property' => array (
  185. 'property' => 'Feature property value',
  186. ),
  187. 'Feature Synonym' => array (
  188. 'syn_name' => 'Synonym name',
  189. ),
  190. 'Library specification' => array (
  191. 'library_id' => 'Library ID number',
  192. 'library_name' => 'Library name',
  193. ),
  194. 'Analysis specification' => array (
  195. 'analysis_id' => 'Analysis ID number',
  196. 'analysis_source' => 'Analysis identifying name (sourcename column in Chado)',
  197. 'analysis_desc' => 'Analysis description',
  198. 'analysis_program' => 'Analysis program',
  199. 'analysis_program_version' => 'Analysis program version'
  200. ),
  201. );
  202. // organism foreign key identifies. These are used to find the organism
  203. // for which the feature belongs.
  204. $form['columns'] = array(
  205. '#type' => 'hidden',
  206. '#value' => $columns,
  207. );
  208. // get the list of organisms
  209. $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  210. $previous_db = tripal_db_set_active('chado'); // use chado database
  211. $org_rset = db_query($sql);
  212. tripal_db_set_active($previous_db); // now use drupal database
  213. $organisms = array();
  214. $genus = array();
  215. $species = array();
  216. $common_names = array();
  217. $genera[''] = '';
  218. $species[''] = '';
  219. $common_names[''] = '';
  220. $full_names[''] = '';
  221. while($organism = db_fetch_object($org_rset)){
  222. $full_names["$organism->genus $organism->species"] = "$organism->genus $organism->species";
  223. $genera[$organism->genus] = "$organism->genus";
  224. $species[$organism->species] = "$organism->species";
  225. $common_names[$organism->common_name] = "$organism->common_name";
  226. }
  227. for($i = 1; $i <= $columns; $i++){
  228. $form['bulk_loader']["col_$i"] = array(
  229. '#type' => 'fieldset',
  230. '#title' => t("Column $i of the input file"),
  231. );
  232. $form['bulk_loader']["col_$i"]["col_type_$i"] = array(
  233. '#title' => t('Field Selection'),
  234. '#type' => 'select',
  235. '#options' => $fields,
  236. '#weight' => 0,
  237. // this field will use AJAX to populate and information needed
  238. // for specific types, such as for feature properties. It
  239. // calls the step2_get_type URL and passes the column ($i).
  240. // the return value is additional form items or text
  241. '#ahah' => array(
  242. 'event' => 'change',
  243. 'wrapper' => "type_cols_$i",
  244. 'path' => "/admin/tripal/bulk_load/step2_get_type/$i",
  245. 'effect' => 'fade',
  246. 'method' => 'replace'
  247. ),
  248. );
  249. // these next fields are hidden (access = false) and will be shown
  250. // if the user selects the feature property type in the drop down
  251. // above.
  252. //-------------------------------------------------------------------------
  253. // default text box for allowed values
  254. $form['bulk_loader']["col_$i"]["col_prop_valid_$i"] = array(
  255. '#title' => t('Allowed Values'),
  256. '#type' => 'textarea',
  257. '#weight' => 0,
  258. '#description' => 'Please provide a list of allowed values for this field. Separate these values with a space. Leave blank to allow any value',
  259. '#access' => FALSE
  260. );
  261. //-------------------------------------------------------------------------
  262. // Organism allowed values
  263. $form['bulk_loader']["col_$i"]["col_prop_genera_$i"] = array (
  264. '#title' => t('Allowed Values'),
  265. '#type' => t('select'),
  266. '#description' => t("Choose all allowed genera values for this column (ctrl+click to select multiple values). Select none to allow all"),
  267. '#required' => FALSE,
  268. '#options' => $genera,
  269. '#weight' => 2,
  270. '#multiple' => TRUE,
  271. '#size' => 10,
  272. '#access' => FALSE
  273. );
  274. $form['bulk_loader']["col_$i"]["col_prop_species_$i"] = array (
  275. '#title' => t('Allowed Values'),
  276. '#type' => t('select'),
  277. '#description' => t("Choose all allowed species values for this column (ctrl+click to select multiple values). Select none to allow all"),
  278. '#required' => FALSE,
  279. '#options' => $species,
  280. '#weight' => 2,
  281. '#multiple' => TRUE,
  282. '#size' => 10,
  283. '#access' => FALSE
  284. );
  285. $form['bulk_loader']["col_$i"]["col_prop_common_name_$i"] = array (
  286. '#title' => t('Allowed Values'),
  287. '#type' => t('select'),
  288. '#description' => t("Choose all allowed values for this column (ctrl+click to select multiple values). Select none to allow all"),
  289. '#required' => FALSE,
  290. '#options' => $common_names,
  291. '#weight' => 2,
  292. '#multiple' => TRUE,
  293. '#size' => 10,
  294. '#access' => FALSE
  295. );
  296. $form['bulk_loader']["col_$i"]["col_prop_full_name_$i"] = array (
  297. '#title' => t('Allowed Values'),
  298. '#type' => t('select'),
  299. '#description' => t("Choose all allowed values for this column (shift+click to select multiple values). Select none to allow all"),
  300. '#required' => FALSE,
  301. '#options' => $full_names,
  302. '#weight' => 2,
  303. '#multiple' => TRUE,
  304. '#size' => 10,
  305. '#access' => FALSE
  306. );
  307. //-------------------------------------------------------------------------
  308. // feature property fields
  309. $form['bulk_loader']["col_$i"]["col_prop_name_$i"] = array(
  310. '#title' => t('Property Name'),
  311. '#type' => 'textfield',
  312. '#weight' => 1,
  313. '#description' => 'Please provide a name for this property. It should be a single
  314. word with only alphanumeric characters and underscores. If this
  315. name exists as a CV term in Chado already it will be reused, otherwise
  316. a new CV term with this name will be added.',
  317. '#required' => TRUE,
  318. '#access' => FALSE
  319. );
  320. $form['bulk_loader']["col_$i"]["col_prop_full_name_$i"] = array(
  321. '#title' => t('Property Full Name'),
  322. '#type' => 'textfield',
  323. '#weight' => 3,
  324. '#description' => 'Please provide a human readable name for this property. This will be used when
  325. displaying the property title',
  326. '#access' => FALSE
  327. );
  328. $form['bulk_loader']["col_$i"]["col_prop_desc_$i"] = array(
  329. '#title' => t('Property Description'),
  330. '#type' => 'textarea',
  331. '#weight' => 4,
  332. '#description' => 'Please provide a description of this property.',
  333. '#access' => FALSE
  334. );
  335. // this is an empty div box that gets put on the form. This is the
  336. // box where the hidden form elements will be rendered when the
  337. // AJAX call returns
  338. $form['bulk_loader']["col_$i"]["type_wrap_$i"] = array(
  339. '#prefix' => "<div class=\"clear-block\" id=\"type_cols_$i\">",
  340. '#value' => ' ',
  341. '#suffix' => '</div>',
  342. );
  343. }
  344. $form['submit'] = array (
  345. '#type' => 'submit',
  346. '#value' => t('Next'),
  347. '#weight' => 5,
  348. '#executes_submit_callback' => TRUE,
  349. );
  350. }
  351. /*************************************************************************
  352. *
  353. */
  354. function tripal_core_bulk_loader_ahah_step2_feature_type($column){
  355. // create a form_state array to hold the form variables
  356. $form_state = array('storage' => NULL, 'submitted' => FALSE);
  357. $form_state['storage']['step'] = 2;
  358. // retreive the form from the cache
  359. $form_build_id = $_POST['form_build_id'];
  360. $form = form_get_cache($form_build_id, $form_state);
  361. // get the form name
  362. $args = $form['#parameters'];
  363. $form_id = array_shift($args);
  364. // add the post variables to the form and set a few other items
  365. $form['#post'] = $_POST;
  366. $form['#redirect'] = FALSE;
  367. $form['#programmed'] = FALSE;
  368. $form_state['post'] = $_POST;
  369. drupal_process_form($form_id, $form, $form_state);
  370. $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  371. // get the column
  372. $i = $column;
  373. // check to see what type of field has been selected and provide the
  374. // additional fields as appropriate
  375. $type = $form_state["post"]["col_type_$i"];
  376. if(strcmp($type,'genus')==0){
  377. $propform['bulk_loader']["col_$i"]["col_prop_genera_$i"] = $form['bulk_loader']["col_$i"]["col_prop_genera_$i"];
  378. $propform['bulk_loader']["col_$i"]["col_prop_genera_$i"]['#access'] = TRUE;
  379. }
  380. elseif(strcmp($type,'species')==0){
  381. $propform['bulk_loader']["col_$i"]["col_prop_species_$i"] = $form['bulk_loader']["col_$i"]["col_prop_species_$i"];
  382. $propform['bulk_loader']["col_$i"]["col_prop_species_$i"]['#access'] = TRUE;
  383. }
  384. elseif(strcmp($type,'common_name')==0){
  385. $propform['bulk_loader']["col_$i"]["col_prop_common_name_$i"] = $form['bulk_loader']["col_$i"]["col_prop_common_name_$i"];
  386. $propform['bulk_loader']["col_$i"]["col_prop_common_name_$i"]['#access'] = TRUE;
  387. }
  388. elseif(strcmp($type,'full_name')==0){
  389. $propform['bulk_loader']["col_$i"]["col_prop_full_name_$i"] = $form['bulk_loader']["col_$i"]["col_prop_full_name_$i"];
  390. $propform['bulk_loader']["col_$i"]["col_prop_full_name_$i"]['#access'] = TRUE;
  391. }
  392. elseif(strcmp($type,'property')==0){
  393. // we just want to render the property fields that were previously not visible
  394. $propform['bulk_loader']["col_$i"]["col_prop_name_$i"] = $form['bulk_loader']["col_$i"]["col_prop_name_$i"];
  395. $propform['bulk_loader']["col_$i"]["col_prop_full_name_$i"] = $form['bulk_loader']["col_$i"]["col_prop_full_name_$i"];
  396. $propform['bulk_loader']["col_$i"]["col_prop_desc_$i"] = $form['bulk_loader']["col_$i"]["col_prop_desc_$i"];
  397. $propform['bulk_loader']["col_$i"]["col_prop_name_$i"]['#access'] = TRUE;
  398. $propform['bulk_loader']["col_$i"]["col_prop_full_name_$i"]['#access'] = TRUE;
  399. $propform['bulk_loader']["col_$i"]["col_prop_desc_$i"]['#access'] = TRUE;
  400. }
  401. else {
  402. // use a default valid values textbox if we have no special needs for the type
  403. $propform['bulk_loader']["col_$i"]["col_prop_valid_$i"] = $form['bulk_loader']["col_$i"]["col_prop_valid_$i"];
  404. $propform['bulk_loader']["col_$i"]["col_prop_valid_$i"]['#access'] = TRUE;
  405. }
  406. $output = theme('status_messages') . drupal_render($propform);
  407. // Final rendering callback.
  408. drupal_json(array('status' => TRUE, 'data' => $output));
  409. }
  410. /*************************************************************************
  411. *
  412. */
  413. function tripal_core_bulk_loader_create_form_step1 (&$form,$form_state){
  414. $modules = array();
  415. $modules['feature'] = 'Feature';
  416. $modules['organism'] = 'Organism';
  417. $modules['library'] = 'Library';
  418. $modules['analysis'] = 'Analysis';
  419. // set the fieldset title to indicate the step
  420. $form['bulk_loader'] = array(
  421. '#type' => 'fieldset',
  422. '#title' => t('Step 1: Select the Chado module'),
  423. );
  424. $form['bulk_loader']['chado_module'] = array(
  425. '#title' => t('Chado Module'),
  426. '#description' => t('Please select the module for which you would like to create an importer'),
  427. '#type' => 'select',
  428. '#options' => $modules,
  429. '#weight' => 0,
  430. '#required' => TRUE
  431. );
  432. $form['bulk_loader']['columns']= array(
  433. '#type' => 'textfield',
  434. '#title' => t('Number of Columns'),
  435. '#description' => t('Please specify the number of columns in the input file.'),
  436. '#weight' => 2,
  437. '#required' => TRUE
  438. );
  439. $form['submit'] = array (
  440. '#type' => 'submit',
  441. '#value' => t('Next'),
  442. '#weight' => 5,
  443. '#executes_submit_callback' => TRUE,
  444. );
  445. }