tripal_jbrowse_mgmt.api.inc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. <?php
  2. /**
  3. * @file
  4. */
  5. /**
  6. * Get saved settings.
  7. *
  8. * @return null
  9. */
  10. function tripal_jbrowse_mgmt_get_settings() {
  11. $default = [
  12. 'bin_path' => '',
  13. 'link' => '',
  14. 'data_dir' => '',
  15. 'data_path' => '',
  16. 'menu_template' => [],
  17. ];
  18. $variable = variable_get('tripal_jbrowse_mgmt_settings', json_encode($default));
  19. $settings = json_decode($variable, TRUE) + $default;
  20. return $settings;
  21. }
  22. /**
  23. * Save settings.
  24. *
  25. * @param array $settings
  26. *
  27. * @return array The final merged settings
  28. */
  29. function tripal_jbrowse_mgmt_save_settings($settings) {
  30. $default = [
  31. 'bin_path' => '',
  32. 'link' => '',
  33. 'data_dir' => '',
  34. 'menu_template' => [],
  35. ];
  36. $final = $settings + $default;
  37. variable_set('tripal_jbrowse_mgmt_settings', json_encode($final));
  38. return $final;
  39. }
  40. /**
  41. * Get an array to instances.
  42. *
  43. * @return mixed
  44. */
  45. function tripal_jbrowse_mgmt_get_instances($conditions = NULL) {
  46. static $users = [];
  47. static $organisms = [];
  48. static $analysis = [];
  49. $instances = db_select('tripal_jbrowse_mgmt_instances', 'H')->fields('H');
  50. if ($conditions) {
  51. foreach ($conditions as $field => $value) {
  52. $instances->condition($field, $value);
  53. }
  54. }
  55. $instances = $instances->execute()->fetchAll();
  56. foreach ($instances as $key => &$instance) {
  57. if (!isset($users[$instance->uid])) {
  58. $users[$instance->uid] = user_load($instance->uid);
  59. }
  60. $instance->user = $users[$instance->uid];
  61. if (!isset($organisms[$instance->organism_id])) {
  62. $organisms[$instance->organism_id] = chado_query('SELECT * FROM {organism} WHERE organism_id=:id',
  63. [':id' => $instance->organism_id])->fetchObject();
  64. }
  65. $instance->organism = $organisms[$instance->organism_id];
  66. if($instance->analysis_id){
  67. if (!isset($analysis[$instance->analysis_id])){
  68. $analysis[$instance->analysis_id] = chado_query('SELECT * FROM {analysis} WHERE analysis_id=:id',
  69. [':id' => $instance->analysis_id])->fetchObject();
  70. }
  71. $instance->analysis = $analysis[$instance->analysis_id];
  72. $instance->analysis->entity_id = chado_get_record_entity_by_table(
  73. 'analysis', $instance->analysis_id);
  74. $instance->analysis->url = NULL;
  75. if ($instance->analysis->entity_id) {
  76. $instance->analysis->url = url('/bio_data/' . $instance->analysis->entity_id);
  77. }
  78. }
  79. }
  80. return $instances;
  81. }
  82. /**
  83. * Create a new instance.
  84. *
  85. * @param $data
  86. *
  87. * @return \DatabaseStatementInterface|int
  88. *
  89. * @throws \Exception
  90. */
  91. function tripal_jbrowse_mgmt_create_instance($data) {
  92. global $user;
  93. // Check that required fields are provided.
  94. $required = ['organism_id', 'created_at'];
  95. foreach ($required as $key) {
  96. if (!isset($data[$key])) {
  97. tripal_report_error(
  98. 'tripal_jbrowse_mgmt',
  99. TRIPAL_ERROR,
  100. 'Unable to create instance due to missing :key',
  101. [':key' => $key]
  102. );
  103. return FALSE;
  104. }
  105. }
  106. // If they are all present, then insert into the database.
  107. return db_insert('tripal_jbrowse_mgmt_instances')->fields([
  108. 'uid' => $user->uid,
  109. 'organism_id' => $data['organism_id'],
  110. 'analysis_id' => $data['analysis_id'],
  111. 'title' => $data['title'],
  112. 'description' => isset($data['description']) ? $data['description'] : '',
  113. 'created_at' => $data['created_at'],
  114. 'file' => $data['file'],
  115. ])->execute();
  116. }
  117. /**
  118. * Get an instance by id.
  119. *
  120. * @param $instance_id
  121. *
  122. * @return mixed
  123. */
  124. function tripal_jbrowse_mgmt_get_instance($instance_id) {
  125. $instance = tripal_jbrowse_mgmt_get_instances(['id' => $instance_id]);
  126. return reset($instance);
  127. }
  128. /**
  129. * Get analysis_id from string(autocomplete analysis)
  130. *
  131. * @param $analysis_string (in format: ******** (###))
  132. *
  133. * @return int
  134. */
  135. function tripal_jbrowse_mgmt_get_analysis_id_from_string($analysis_string){
  136. preg_match_all('!\d+!', $analysis_string, $match_analysis);
  137. $analysis_id = array_pop($match_analysis[0]);
  138. return $analysis_id;
  139. }
  140. /**
  141. * Update an instance.
  142. *
  143. * @param int $id
  144. * @param array $data
  145. *
  146. * @return \DatabaseStatementInterface
  147. */
  148. function tripal_jbrowse_mgmt_update_instance($id, $data) {
  149. return db_update('tripal_jbrowse_mgmt_instances')
  150. ->fields($data)
  151. ->condition('id', $id)
  152. ->execute();
  153. }
  154. /**
  155. * @param int|object $instance
  156. *
  157. * @return int
  158. * @throws \Exception
  159. */
  160. function tripal_jbrowse_mgmt_delete_instance($instance) {
  161. if (is_object($instance) && property_exists($instance, 'id')) {
  162. $id = $instance->id;
  163. }
  164. elseif (is_numeric($instance)) {
  165. $id = $instance;
  166. }
  167. else {
  168. throw new Exception('Unable to extract instance ID. Please provide a valid ID to delete the instance.');
  169. }
  170. return db_delete('tripal_jbrowse_mgmt_instances')
  171. ->condition('id', $id)
  172. ->execute();
  173. }
  174. /**
  175. * Create a new JBrowse track for a given instance.
  176. *
  177. * @param $data
  178. *
  179. * @return bool|int Track ID or FALSE if an error occurs.
  180. *
  181. * @throws \Exception
  182. */
  183. function tripal_jbrowse_mgmt_create_track($instance, $data) {
  184. global $user;
  185. return db_insert('tripal_jbrowse_mgmt_tracks')->fields([
  186. 'uid' => $user->uid,
  187. 'instance_id' => $instance->id,
  188. 'organism_id' => $instance->organism_id,
  189. 'label' => $data['label'],
  190. 'track_type' => $data['track_type'],
  191. 'file_type' => $data['file_type'],
  192. 'created_at' => $data['created_at'],
  193. 'file' => $data['file'],
  194. ])->execute();
  195. }
  196. /**
  197. * Delete a track by ID.
  198. *
  199. * @param $track_id
  200. *
  201. * @return int
  202. */
  203. function tripal_jbrowse_mgmt_delete_track($track_id) {
  204. return db_delete('tripal_jbrowse_mgmt_tracks')
  205. ->condition('id', $track_id)
  206. ->execute();
  207. }
  208. /**
  209. * Get attached tracks with users pre-loaded.
  210. *
  211. * @param $instance
  212. *
  213. * @return mixed
  214. */
  215. function tripal_jbrowse_mgmt_get_tracks($instance, array $conditions = []) {
  216. static $users = [];
  217. $tracks = db_select('tripal_jbrowse_mgmt_tracks', 'HJT')->fields('HJT');
  218. foreach ($conditions as $field => $value) {
  219. $tracks->condition($field, $value);
  220. }
  221. $tracks = $tracks->condition('instance_id', $instance->id)
  222. ->execute()
  223. ->fetchAll();
  224. foreach ($tracks as &$track) {
  225. if (!isset($users[$track->uid])) {
  226. $users[$track->uid] = user_load($track->uid);
  227. }
  228. $track->user = $users[$track->uid];
  229. }
  230. return $tracks;
  231. }
  232. /**
  233. * Get a track with instance and user data attached.
  234. *
  235. * @param $track_id
  236. *
  237. * @return mixed
  238. */
  239. function tripal_jbrowse_mgmt_get_track($track_id) {
  240. $track = db_select('tripal_jbrowse_mgmt_tracks', 'HJT')
  241. ->fields('HJT')
  242. ->condition('id', $track_id)
  243. ->execute()
  244. ->fetchObject();
  245. $track->user = user_load($track->uid);
  246. $track->instance = tripal_jbrowse_mgmt_get_instance($track->instance_id);
  247. return $track;
  248. }
  249. /**
  250. * @param $track
  251. * @param array $fields
  252. *
  253. * @return \DatabaseStatementInterface
  254. */
  255. function tripal_jbrowse_mgmt_update_track($track, array $fields) {
  256. return db_update('tripal_jbrowse_mgmt_tracks')
  257. ->fields($fields)
  258. ->condition('id', $track->id)
  259. ->execute();
  260. }
  261. /**
  262. * Get a list of organisms.
  263. *
  264. * @return mixed
  265. */
  266. function tripal_jbrowse_mgmt_get_organisms_list() {
  267. return db_select('chado.organism', 'CO')
  268. ->fields('CO', ['organism_id', 'genus', 'species', 'common_name'])
  269. ->execute()
  270. ->fetchAll();
  271. }
  272. /**
  273. * parse analysis to obtain analysis_id
  274. *
  275. * @return mixed
  276. */
  277. function tripal_jbrowse_mgmt_analysis_parse_4id($name_id) {
  278. return $analysis_id;
  279. }
  280. /**
  281. * Format the name of the organism to `Genus species (Common Name)`.
  282. *
  283. * @param object $organism
  284. * The organism object as retrieved by
  285. * db_query()->fetchObject().
  286. *
  287. * @return string
  288. */
  289. function tripal_jbrowse_mgmt_construct_organism_name($organism) {
  290. $name = $organism->genus;
  291. $name .= " $organism->species";
  292. if (!empty($organism->common_name)) {
  293. $name .= " ($organism->common_name)";
  294. }
  295. return $name;
  296. }
  297. /**
  298. * Sanitize the string for the URL.
  299. *
  300. * @param string $string
  301. * The string to sanitize.
  302. *
  303. * @return string
  304. * The sanitized string.
  305. */
  306. function tripal_jbrowse_mgmt_make_slug($string) {
  307. $slug = str_replace(' ', '_', $string);
  308. $slug = str_replace('(', '_', $slug);
  309. $slug = str_replace(')', '_', $slug);
  310. $slug = str_replace('[', '_', $slug);
  311. $slug = str_replace(']', '_', $slug);
  312. $slug = str_replace('!', '_', $slug);
  313. $slug = str_replace('?', '_', $slug);
  314. $slug = str_replace('"', '_', $slug);
  315. $slug = str_replace('\'', '_', $slug);
  316. $slug = str_replace('\\', '_', $slug);
  317. $slug = str_replace(':', '_', $slug);
  318. return strtolower(trim($slug, '_'));
  319. }
  320. /**
  321. * @param $field
  322. *
  323. * @return bool|\stdClass
  324. */
  325. function tripal_jbrowse_mgmt_upload_file($field) {
  326. $file = file_save_upload($field, [
  327. 'file_validate_extensions' => ['fasta faa fna fastq txt gff gff3 vcf wig gz tbi bw bam bai cram'],
  328. // Make it 20 GB max.
  329. 'file_validate_size' => [1024 * 1024 * 1024 * 20],
  330. ]);
  331. // drupal_realpath($file->uri);.
  332. return !$file ? FALSE : $file;
  333. }
  334. /**
  335. * Moves a file to an intermediate directory, then to the destination, if given.
  336. *
  337. * @param $file
  338. * The file object.
  339. *
  340. * @param $path
  341. * The path to the directory of the new object.
  342. * If the directory provided does not exist, it will be created.
  343. *
  344. * @return
  345. * The path to the moved file, or NULL on fail.
  346. */
  347. function tripal_jbrowse_mgmt_move_file($file, $path = NULL) {
  348. $directory = 'public://tripal/tripal_jbrowse_mgmt';
  349. file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
  350. $file = file_move($file, $directory, FILE_EXISTS_REPLACE);
  351. if (isset($path)) {
  352. file_prepare_directory($path, FILE_CREATE_DIRECTORY);
  353. $oldname = drupal_realpath($file->uri);
  354. $newname = $path . '/' . $file->filename;
  355. if (!rename($oldname, $newname)) {
  356. return NULL;
  357. }
  358. }
  359. return isset($path) ? $newname : drupal_realpath($file->uri);
  360. }
  361. /**
  362. * Copy a file into a new directory.
  363. * If the directory provided does not exist, it will be created.
  364. *
  365. * @param $source
  366. * File path of the source file.
  367. * @param $destination
  368. * File path to the destination directory.
  369. * @param $symbolic_link
  370. * bool indicates create symbolic_link or copy
  371. * @return bool
  372. */
  373. function tripal_jbrowse_mgmt_copy_file($source, $destination, $sym_link) {
  374. if (empty($destination)) {
  375. throw new Exception('Please provide a valid destination path to copy the source to.');
  376. }
  377. file_prepare_directory($destination, FILE_CREATE_DIRECTORY);
  378. if (isset($sym_link) AND ($sym_link == true)){
  379. $destination_symlink = $destination.'/'.basename($source);
  380. if (file_exists($destination_symlink)){
  381. return TRUE;
  382. }else{
  383. return symlink($source, $destination_symlink);
  384. }
  385. }
  386. else{
  387. return file_unmanaged_copy($source, $destination, FILE_EXISTS_ERROR);
  388. }
  389. }
  390. /**
  391. * Build the http query for a given instance to link to JBrowse.
  392. *
  393. * @param $instance
  394. *
  395. * @return array
  396. */
  397. function tripal_jbrowse_mgmt_build_http_query($instance) {
  398. $path = tripal_jbrowse_mgmt_make_slug($instance->title);
  399. if(isset($instance->analysis_id)){
  400. $path = $path . '_' . $instance->analysis_id;
  401. }
  402. $added_tracks = tripal_jbrowse_mgmt_get_tracks($instance);
  403. $properties = tripal_jbrowse_mgmt_get_instance_properties($instance->id);
  404. // Determine the tracks to display.
  405. $tracks_path = '';
  406. // If the start-tracks property is set then use that directly.
  407. if (isset($properties['start-tracks'])) {
  408. $tracks_path = $properties['start-tracks'];
  409. }
  410. // Otherwise, show all tracks that were added using this module.
  411. elseif (!empty($added_tracks)) {
  412. $tracks_path = implode(',', array_map(function ($track) {
  413. return tripal_jbrowse_mgmt_make_slug($track->label);
  414. }, $added_tracks));
  415. }
  416. $settings = tripal_jbrowse_mgmt_get_settings();
  417. $data_path = (empty($settings['data_path'])) ? 'data' : '/' . $settings['data_path'];
  418. return [
  419. 'data' => "$data_path/$path/data",
  420. 'tracks' => $tracks_path,
  421. ];
  422. // Now we need to add start location if it's set.
  423. if (isset($properties['start-loc'])) {
  424. $query_params['loc'] = $properties['start-loc'];
  425. }
  426. return $query_params;
  427. }
  428. /**
  429. * Get trackList.json for an instance.
  430. *
  431. * @param object $instance
  432. *
  433. * @return array Decoded json array.
  434. *
  435. * @throws \Exception
  436. */
  437. function tripal_jbrowse_mgmt_get_json($instance) {
  438. $path = tripal_jbrowse_mgmt_get_track_list_file_path($instance);
  439. if (file_exists($path)) {
  440. $contents = file_get_contents($path);
  441. if (!$contents) {
  442. throw new Exception('Unable to find ' . $path . ' file');
  443. }
  444. return json_decode($contents, TRUE);
  445. }
  446. else {
  447. return [];
  448. }
  449. }
  450. /**
  451. * Get the json for a given track.
  452. *
  453. * @param object $track
  454. * The track from the database.
  455. *
  456. * @return array
  457. * @throws \Exception
  458. */
  459. function tripal_jbrowse_mgmt_get_track_json($track) {
  460. if (!$track->instance) {
  461. $track->instance = tripal_jbrowse_mgmt_get_instance($track->instance_id);
  462. }
  463. $json = tripal_jbrowse_mgmt_get_json($track->instance);
  464. $key = tripal_jbrowse_mgmt_make_slug($track->label);
  465. $track_json = NULL;
  466. foreach ($json['tracks'] as $index => $jtrack) {
  467. if ($jtrack['label'] === $key) {
  468. $track_json = $jtrack;
  469. break;
  470. }
  471. }
  472. return $track_json;
  473. }
  474. /**
  475. * @param object $track
  476. * Track object.
  477. * @param array $track_json
  478. * Edited track array.
  479. *
  480. * @throws \Exception
  481. */
  482. function tripal_jbrowse_mgmt_save_track_json($track, $track_json) {
  483. if (!$track->instance) {
  484. $track->instance = tripal_jbrowse_mgmt_get_instance($track->instance_id);
  485. }
  486. $json = tripal_jbrowse_mgmt_get_json($track->instance);
  487. $key = tripal_jbrowse_mgmt_make_slug($track->label);
  488. foreach ($json['tracks'] as $index => $jtrack) {
  489. if ($jtrack['label'] === $key) {
  490. $json['tracks'][$index] = $track_json;
  491. break;
  492. }
  493. }
  494. return tripal_jbrowse_mgmt_save_json($track->instance, $json);
  495. }
  496. /**
  497. * @param object $instance
  498. * @param array $data
  499. *
  500. * @throws \Exception
  501. * @return bool|int
  502. */
  503. function tripal_jbrowse_mgmt_save_json($instance, $data) {
  504. $path = tripal_jbrowse_mgmt_get_track_list_file_path($instance);
  505. $default = tripal_jbrowse_mgmt_get_json($instance);
  506. $json = $data + $default;
  507. $encoded = json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
  508. return file_put_contents($path, $encoded);
  509. }
  510. /**
  511. * @param $instance
  512. *
  513. * @return string
  514. */
  515. function tripal_jbrowse_mgmt_get_track_list_file_path($instance) {
  516. $settings = tripal_jbrowse_mgmt_get_settings();
  517. $path = $settings['data_dir'];
  518. $path .= '/' . tripal_jbrowse_mgmt_make_slug($instance->title);
  519. if(isset($instance->analysis_id)){
  520. $path = $path . '_' . $instance->analysis_id;
  521. }
  522. $path .= '/data/trackList.json';
  523. return $path;
  524. }
  525. /**
  526. * Gets a list of supported track types.
  527. *
  528. * @return array
  529. */
  530. function tripal_jbrowse_mgmt_get_track_types() {
  531. return [
  532. 'FeatureTrack',
  533. 'CanvasFeatures',
  534. 'Alignment',
  535. 'HTMLFeatures',
  536. 'HTMLVariants',
  537. 'XYPlot',
  538. ];
  539. }
  540. /**
  541. * Save properties for a given instance.
  542. *
  543. * @param $id
  544. * The instance ID that properties should be associated with.
  545. * @param $data
  546. * An array of properties where each is
  547. * key: property_type, value: property value.
  548. */
  549. function tripal_jbrowse_mgmt_save_instance_properties($id, $data) {
  550. // For each property...
  551. foreach ($data as $type => $value) {
  552. tripal_jbrowse_mgmt_save_instance_property($id, $type, $value);
  553. }
  554. }
  555. /**
  556. * Save the details for a specific property of a given instance.
  557. *
  558. * @param $id
  559. * The instance ID that properties should be associated with.
  560. * @param $type
  561. * The name of the property type to be saved.
  562. * @param $value
  563. * The value of the property to be saved.
  564. */
  565. function tripal_jbrowse_mgmt_save_instance_property($id, $type, $value) {
  566. // Check to see if the property already exists.
  567. $result = db_select('tripal_jbrowse_mgmt_instanceprop', 'p')
  568. ->fields('p', ['value'])
  569. ->condition('instance_id', $id)
  570. ->condition('property_type', $type)
  571. ->execute()->fetchObject();
  572. // If the property doesn't already exist then insert it.
  573. if (empty($result)) {
  574. return db_insert('tripal_jbrowse_mgmt_instanceprop')
  575. ->fields([
  576. 'instance_id' => $id,
  577. 'property_type' => $type,
  578. 'value' => $value,
  579. ])->execute();
  580. }
  581. // Otherwise, we need to update it.
  582. else {
  583. return db_update('tripal_jbrowse_mgmt_instanceprop')
  584. ->fields([
  585. 'value' => $value,
  586. ])
  587. ->condition('instance_id', $id)
  588. ->condition('property_type', $type)
  589. ->execute();
  590. }
  591. }
  592. /**
  593. * Retrieves the value for a given instance property.
  594. *
  595. * @param $id
  596. * The instance ID.
  597. * @param $type
  598. * The name of the property type.
  599. * @return
  600. * The value of the property for a given instance.
  601. */
  602. function tripal_jbrowse_mgmt_get_instance_property($id, $type) {
  603. return db_select('tripal_jbrowse_mgmt_instanceprop', 'p')
  604. ->fields('p', ['value'])
  605. ->condition('instance_id', $id)
  606. ->condition('property_type', $type)
  607. ->execute()->fetchField();
  608. }
  609. /**
  610. * Retrieves all properties for a given instance.
  611. *
  612. * @param $id
  613. * The instance ID.
  614. * @return
  615. * An array of properties for the specified instance
  616. * where the key: property type and value: property value.
  617. */
  618. function tripal_jbrowse_mgmt_get_instance_properties($id) {
  619. return db_select('tripal_jbrowse_mgmt_instanceprop', 'p')
  620. ->fields('p', ['property_type', 'value'])
  621. ->condition('instance_id', $id)
  622. ->execute()->fetchAllKeyed(0,1);
  623. }
  624. /**
  625. * validate folder upload (file with index)
  626. * eg. vcf, bam, cram
  627. *
  628. * @param $file
  629. * the path and file name of upload file
  630. * @param $index
  631. * the path and file name of upload file
  632. * @param $local_directory
  633. * the path and file name of upload file
  634. * @return
  635. * warning message or NULL if no warning
  636. */
  637. function tripal_jbrowse_mgmt_validate_folder_upload($file, $index, $path, $form_state) {
  638. $values = $form_state['values'];
  639. $local_directory = isset($values['dir_path']) ? $values['dir_path'] : NULL;
  640. $symbolic_link = $values['symbolic_link'];
  641. if (empty($file) && empty($index) && empty($local_directory)) {
  642. form_set_error('Please provide a local directory path or upload files.');
  643. }
  644. elseif (empty($file) && empty($index) && !empty($local_directory)) {
  645. if (!file_exists($local_directory)) {
  646. form_set_error('The directory provided does not exist.');
  647. }
  648. else {
  649. if (!is_dir($local_directory)) {
  650. form_set_error('file_path',
  651. 'The file provided is not a directory.');
  652. }
  653. else {
  654. // Retrieve an array of data file and index
  655. // to ensure there is only one of each.
  656. foreach (scandir($local_directory) as $f) {
  657. $fparts = pathinfo($f);
  658. if (in_array($fparts['extension'], ['gz', 'bam', 'cram'])) {
  659. $file_file[] = $local_directory . '/' . $f;
  660. }
  661. if (in_array($fparts['extension'], ['csi','tbi','idx','bai','crai'])) {
  662. $file_index[] = $local_directory . '/' . $f;
  663. }
  664. }
  665. // CHECK: Only a single data file and index.
  666. if (count($file_file) != 1 || count($file_index) != 1) {
  667. form_set_error('file_path',
  668. 'Please provide a directory with exactly one data file and one index file.');
  669. }
  670. else {
  671. try {
  672. if (!tripal_jbrowse_mgmt_copy_file($file_file[0], $path, $symbolic_link)) {
  673. form_set_error('file_path', 'Failed to copy file' . $file_file[0] . ' to ' . $path.'. If this track is expected to create by existed file, please have Symbolic Link selected.');
  674. }
  675. else {
  676. if (!tripal_jbrowse_mgmt_copy_file($file_index[0], $path, $symbolic_link)) {
  677. form_set_error('file_path', 'Failed to copy file' . $file_index[0] . ' to ' . $path.'. If this track is expected to create by existed file, please have Symbolic Link selected.');
  678. }
  679. }
  680. } catch (Exception $exception) {
  681. form_set_error($exception->getMessage());
  682. }
  683. }
  684. }
  685. }
  686. }
  687. elseif (empty($file) && !empty($index)) {
  688. form_set_error('file', 'Please upload both a data file and an index file.');
  689. }
  690. elseif (!empty($file) && empty($index)) {
  691. form_set_error('file2', 'Please upload both a data file and an index file.');
  692. }
  693. else {
  694. $file_data_uploaded = tripal_jbrowse_mgmt_upload_file('file');
  695. if (!$file_data_uploaded) {
  696. form_set_error('file', 'Unable to upload file.');
  697. }
  698. else {
  699. $file_data_uploaded = tripal_jbrowse_mgmt_move_file($file_data_uploaded, $path);
  700. if (!isset($file_data_uploaded)) {
  701. form_set_error('file', 'Failed to move data file to ' . $path . '.');
  702. }
  703. else {
  704. $form_state['values']['uploaded_file_data'] = $file_data_uploaded;
  705. }
  706. }
  707. $index_uploaded = tripal_jbrowse_mgmt_upload_file('file2');
  708. if (!$index_uploaded) {
  709. form_set_error('file2', 'Unable to upload index.');
  710. }
  711. else {
  712. $index_uploaded = tripal_jbrowse_mgmt_move_file($index_uploaded, $path);
  713. if (!isset($index_uploaded)) {
  714. form_set_error('file2', 'Failed to move index file to ' . $path . '.');
  715. }
  716. else {
  717. $form_state['values']['uploaded_file_index'] = $index_uploaded;
  718. }
  719. }
  720. }
  721. return $form_state;
  722. }