blast_ui.services.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. <?php
  2. /**
  3. * Implements hook_services_resources().
  4. * Hooks provided by Services for the definition of new services,
  5. */
  6. function blast_ui_services_resources()
  7. {
  8. return array(
  9. 'blast' => array(
  10. 'retrieve' => array(
  11. 'help' => 'Retrieves a blastResult',
  12. 'callback' => 'getBlastData',
  13. 'access callback' => 'user_access',
  14. 'access arguments' => array(
  15. 'access content'
  16. ),
  17. 'access arguments append' => FALSE,
  18. 'args' => array(
  19. array(
  20. 'name' => 'job_id',
  21. 'type' => 'int',
  22. 'description' => 'The information needed',
  23. 'source' => array(
  24. 'path' => '0'
  25. ),
  26. 'optional' => FALSE
  27. )
  28. )
  29. ),
  30. 'index' => array(
  31. 'help' => 'Retrieves a listing of blast_ui',
  32. 'callback' => '_blast_ui_index',
  33. 'access callback' => 'user_access',
  34. 'access arguments' => array(
  35. 'access content'
  36. ),
  37. 'access arguments append' => FALSE,
  38. 'args' => array ()
  39. ),
  40. 'actions' => array(
  41. 'getJobId' => array(
  42. 'help' => 'Retrieves a listing of database',
  43. 'callback' => '_blast_ui_getJobId',
  44. 'access callback' => 'user_access',
  45. 'access arguments' => array(
  46. 'access content'
  47. ),
  48. 'args' => array(
  49. array(
  50. 'name' => 'data',
  51. 'type' => 'array',
  52. 'description' => 'Retrieve blast job id',
  53. 'source' => 'data',
  54. 'optional' => FALSE
  55. )
  56. ),
  57. 'access arguments append' => FALSE
  58. ),
  59. 'getDatabaseOptions' => array(
  60. 'help' => 'Retrieves a listing of database',
  61. 'callback' => '_blast_ui_getDatabaseOption',
  62. 'access callback' => 'user_access',
  63. 'access arguments' => array(
  64. 'access content'
  65. ),
  66. 'args' => array(
  67. array(
  68. 'name' => 'data',
  69. 'type' => 'array',
  70. 'description' => 'The set database_type information',
  71. 'source' => 'data',
  72. 'optional' => FALSE
  73. )
  74. ),
  75. 'access arguments append' => false
  76. )
  77. )
  78. )
  79. );
  80. }
  81. /**
  82. * Callback for retrieving blast services.
  83. *
  84. * @param int $id
  85. * @return object
  86. */
  87. function _blast_ui_retrieve( $id )
  88. {
  89. return getBlastData( $id );
  90. }
  91. /**
  92. * Callback for Index blast services.
  93. * @return object
  94. */
  95. function _blast_ui_index()
  96. {
  97. return array(
  98. 'query_type' => array(
  99. '1' => 'nucleotide',
  100. '2' => 'protein'
  101. ),
  102. 'db_type' => array(
  103. '1' => 'nucleotide',
  104. '2' => 'protein'
  105. ),
  106. 'blast_program' => array(
  107. '1' => 'blastn',
  108. '2' => 'blastx',
  109. '3' => 'tblastn',
  110. '4' => 'blastp'
  111. )
  112. );
  113. }
  114. /*
  115. ** Method returns Advanced options and Database Type
  116. ** @param Object $data
  117. ** web service url : http://localhost:8080/restapi/blast/getDatabaseOptions.json
  118. */
  119. function _blast_ui_getDatabaseOption( $data )
  120. {
  121. if ( !isset( $data[ query_type ] ) )
  122. {
  123. return services_error( 'Missing blast attribute $query_type [ set necleotide or protein in post request of query_type ]', 406 );
  124. }
  125. if ( !isset( $data[ db_type ] ) )
  126. {
  127. return services_error( 'Missing blast attribute $db_type [ set necleotide or protein in post request of db_type ]', 406 );
  128. }
  129. if ( !isset( $data[ blast_program ] ) )
  130. {
  131. return services_error( 'Missing blast attribute $blast_program [ set blastn, blastx, tblastn, blastp in post request of blast_program ]', 406 );
  132. }
  133. $query_type = $data[ db_type ];
  134. $db_type = $data[ db_type ];
  135. $blast_program = $data[ blast_program ];
  136. $options_database = get_blast_database_options( $db_type );
  137. // Data Set send to user for selection of Blastn
  138. if ( $blast_program == 'blastn' )
  139. {
  140. return array(
  141. 'Select_Database' => $options_database,
  142. 'Max_target_sequences' => _get_max_target( $blast_program ),
  143. 'Word_size' => _get_word_size( $blast_program ),
  144. 'Match_Mismatch_Scores' => _get_match_mismatch( $blast_program ),
  145. 'Gap_Costs' => _get_gap( $blast_program )
  146. );
  147. }
  148. // Data Set send to user for selection of Blastx
  149. elseif ( $blast_program == 'blastx' )
  150. {
  151. return array(
  152. 'Select_Database' => $options_database,
  153. 'Max_target_sequences' => _get_max_target( $blast_program ),
  154. 'Word_size' => _get_word_size( $blast_program ),
  155. 'Matrix_options' => _get_matrix_options()
  156. );
  157. }
  158. //Data Set send to user for selection of tblastn
  159. elseif ( $blast_program == 'tblastn' )
  160. {
  161. return array(
  162. 'Select_Database' => $options_database,
  163. 'Max_target_sequences' => _get_max_target( $blast_program ),
  164. 'Word_size' => _get_word_size( $blast_program ),
  165. 'Matrix_options' => _get_matrix_options(),
  166. 'Gap_Costs' => getGap_cost()
  167. );
  168. }
  169. //Data Set send to user for selection of blastp
  170. elseif ( $blast_program == 'blastp' )
  171. {
  172. return array(
  173. 'Select_Database' => $options_database,
  174. 'Max_target_sequences' => _get_max_target( $blast_program ),
  175. 'Word_size' => _get_word_size( $blast_program ),
  176. 'Matrix_options' => _get_matrix_options(),
  177. 'Gap_Costs' => getGap_cost()
  178. );
  179. }
  180. }
  181. /*
  182. ** validate the data
  183. */
  184. function blast_validation( $query_type, $db_type, $data )
  185. {
  186. if ( $query_type == 'nucleotide' )
  187. {
  188. if ( $db_type == 'nucleotide' )
  189. {
  190. if ( !isset( $data[ Select_Database ] ) )
  191. {
  192. return services_error( 'Missing blast attribute $Select_Database [ query blast/getDatabaseOptions for options :- Add database you need to use]', 406 );
  193. }
  194. if ( !isset( $data[ Max_target_sequences ] ) )
  195. {
  196. return services_error( 'Missing blast attribute $Max_target_sequences [ query blast/getDatabaseOptions for options ]', 406 );
  197. }
  198. if ( !isset( $data[ Word_size ] ) )
  199. {
  200. return services_error( 'Missing blast attribute $Word_size [ query blast/getDatabaseOptions for options ]', 406 );
  201. }
  202. if ( !isset( $data[ Match_Mismatch_Scores ] ) )
  203. {
  204. return services_error( 'Missing blast attribute $Match_Mismatch_Scores [ query blast/getDatabaseOptions for options]', 406 );
  205. }
  206. if ( !isset( $data[ Gap_costs ] ) )
  207. {
  208. return services_error( 'Missing blast attribute $Gap_Costs [ query blast/getDatabaseOptions for options ]', 406 );
  209. }
  210. if ( isset( $data[ seqQuery ] ) )
  211. {
  212. if ( validate_fasta_sequence( $query_type, $data[ seqQuery ] ) == 1 )
  213. {
  214. return services_error( 'Please enter validate_fasta_sequence', 406 );
  215. }
  216. }
  217. else
  218. {
  219. return services_error( 'Missing blast attribute data[seqQuery]', 406 );
  220. }
  221. }
  222. else
  223. {
  224. if ( !isset( $data[ Select_Database ] ) )
  225. {
  226. return services_error( 'Missing blast attribute $Select_Database [ query blast/getDatabaseOptions for options :- Add database used by you]', 406 );
  227. }
  228. if ( !isset( $data[ Max_target_sequences ] ) )
  229. {
  230. return services_error( 'Missing blast attribute $Max_target_sequences [ query blast/getDatabaseOptions for options ]', 406 );
  231. }
  232. if ( !isset( $data[ Word_size ] ) )
  233. {
  234. return services_error( 'Missing blast attribute $Word_size [ query blast/getDatabaseOptions for options ]', 406 );
  235. }
  236. if ( !isset( $data[ Matrix_options ] ) )
  237. {
  238. return services_error( 'Missing blast attribute $Matrix_options [ query blast/getDatabaseOptions for options ]', 406 );
  239. }
  240. if ( isset( $data[ seqQuery ] ) )
  241. {
  242. if ( validate_fasta_sequence( $query_type, $data[ seqQuery ] ) == 1 )
  243. {
  244. return services_error( 'Please enter validate_fasta_sequence', 406 );
  245. }
  246. }
  247. else
  248. {
  249. return services_error( 'Missing blast attribute data[seqQuery]', 406 );
  250. }
  251. }
  252. }
  253. else
  254. {
  255. if ( $db_type == 'nucleotide' )
  256. {
  257. if ( !isset( $data[ Select_Database ] ) )
  258. {
  259. return services_error( 'Missing blast attribute $Select_Database [ query blast/getDatabaseOptions for options :- Add database you need to use]', 406 );
  260. }
  261. if ( !isset( $data[ Max_target_sequences ] ) )
  262. {
  263. return services_error( 'Missing blast attribute $Max_target_sequences [ query blast/getDatabaseOptions for options ]', 406 );
  264. }
  265. if ( !isset( $data[ Word_size ] ) )
  266. {
  267. return services_error( 'Missing blast attribute $Word_size [ query blast/getDatabaseOptions for options ]', 406 );
  268. }
  269. if ( !isset( $data[ Matrix_options ] ) )
  270. {
  271. return services_error( 'Missing blast attribute $Matrix_options [ query blast/getDatabaseOptions for options ]', 406 );
  272. }
  273. if ( !isset( $data[ Gap_costs ] ) )
  274. {
  275. return services_error( 'Missing blast attribute [ query blast/getDatabaseOptions for options ]', 406 );
  276. }
  277. if ( isset( $data[ seqQuery ] ) )
  278. {
  279. if ( validate_fasta_sequence( $query_type, $data[ seqQuery ] ) == 1 )
  280. {
  281. return services_error( 'Please enter validate_fasta_sequence', 406 );
  282. }
  283. }
  284. else
  285. {
  286. return services_error( 'Missing blast attribute data[seqQuery]', 406 );
  287. }
  288. }
  289. else
  290. {
  291. //return $data;
  292. if ( !isset( $data[ Select_Database ] ) )
  293. {
  294. return services_error( 'Missing blast attribute $Select_Database [ query blast/getDatabaseOptions for options :- Add database used by you]', 406 );
  295. }
  296. if ( !isset( $data[ Max_target_sequences ] ) )
  297. {
  298. return services_error( 'Missing blast attribute $Max_target_sequences [ query blast/getDatabaseOptions for options ]', 406 );
  299. }
  300. if ( !isset( $data[ Word_size ] ) )
  301. {
  302. return services_error( 'Missing blast attribute $Word_size [ query blast/getDatabaseOptions for options ]', 406 );
  303. }
  304. if ( !isset( $data[ Matrix_options ] ) )
  305. {
  306. return services_error( 'Missing blast attribute $Matrix_options [ query blast/getDatabaseOptions for options ]', 406 );
  307. }
  308. if ( isset( $data[ seqQuery ] ) )
  309. {
  310. if ( validate_fasta_sequence( $query_type, $data[ seqQuery ] ) == 1 )
  311. {
  312. return services_error( 'Please enter validate_fasta_sequence', 406 );
  313. }
  314. }
  315. else
  316. {
  317. return services_error( 'Missing blast attribute data[seqQuery]', 406 );
  318. }
  319. }
  320. }
  321. }
  322. /*
  323. ** Method call the blast_ui_getblastJobId by setting all the needed
  324. ** parameters
  325. ** web service url : http://localhost:8080/restapi/blast/getJobId.json
  326. */
  327. function _blast_ui_getJobId( $data )
  328. {
  329. $query_type;
  330. $db_type;
  331. // Setting the default value of qRange and eValue from blast_ui.admin
  332. $eVal = variable_get( 'eVal', '' );
  333. $qRange = variable_get( 'qRange', '' );
  334. /* check values in post query */
  335. if ( !isset( $data[ query_type ] ) )
  336. {
  337. return services_error( 'Missing blast attribute $query_type [ set necleotide or protein in post request of query_type ]', 406 );
  338. }
  339. if ( !isset( $data[ db_type ] ) )
  340. {
  341. return services_error( 'Missing blast attribute $db_type [ set necleotide or protein in post request of db_type ]', 406 );
  342. }
  343. $query_type = $data[ query_type ];
  344. $db_type = $data[ db_type ];
  345. // check the user input in $data
  346. blast_validation( $query_type, $db_type, $data );
  347. if ( isset( $data[ $eVal ] ) )
  348. {
  349. $eVal = $data[ $eVal ];
  350. }
  351. if ( isset( $data[ $culling_limit ] ) )
  352. {
  353. $qRange = $data[ $culling_limit ];
  354. }
  355. if ( $query_type == 'nucleotide' )
  356. {
  357. if ( $db_type == 'nucleotide' )
  358. {
  359. $blast_program = 'blastn';
  360. // code not working for wordvalue = 7 ?
  361. $options_database = get_blast_database_options( $data[ db_type ] );
  362. $Databasekey = array_search( $data[ Select_Database ], $options_database );
  363. $advanced_options = advanced_options( $blast_program, $eVal, $qRange, $data );
  364. }
  365. elseif ( $db_type == 'protein' )
  366. {
  367. $blast_program = 'blastx';
  368. // how to get gap cost - question
  369. $options_database = get_blast_database_options( $data[ db_type ] );
  370. $Databasekey = array_search( $data[ Select_Database ], $options_database );
  371. $advanced_options = advanced_options( $blast_program, $eVal, $qRange, $data );
  372. }
  373. }
  374. elseif ( $query_type == 'protein' )
  375. {
  376. if ( $db_type == 'nucleotide' )
  377. {
  378. $blast_program = 'tblastn';
  379. $options_database = get_blast_database_options( $data[ db_type ] );
  380. $Databasekey = array_search( $data[ Select_Database ], $options_database );
  381. $advanced_options = advanced_options( $blast_program, $eVal, $qRange, $data );
  382. }
  383. elseif ( $db_type == 'protein' )
  384. {
  385. $blast_program = 'blastp';
  386. $options_database = get_blast_database_options( $data[ db_type ] );
  387. $Databasekey = array_search( $data[ Select_Database ], $options_database );
  388. $advanced_options = advanced_options( $blast_program, $eVal, $qRange, $data );
  389. }
  390. }
  391. return blast_ui_getblastJobId( $data[ query_type ], $data[ db_type ], $data[ seqQuery ], $Databasekey, $advanced_options );
  392. }
  393. /*
  394. ** Define the advanced Options for Blast
  395. ** @return : advanced_options according to blast_program
  396. */
  397. function advanced_options( $blast_program, $eVal, $qRange, $data )
  398. {
  399. if ( $blast_program == 'blastn' )
  400. {
  401. $wordvalue = intval( $data[ Word_size ] );
  402. $numAlign = intval( $data[ Max_target_sequences ] );
  403. $Data_GapCost = _get_gap( $blast_program );
  404. $gapkey = array_search( $data[ Gap_Costs ], $Data_GapCost );
  405. $gap = _set_gap( $gapkey );
  406. $Data_Mismatch_Scores = _get_match_mismatch( $blast_program );
  407. $Mismatch_Scores_key = array_search( $data[ Match_Mismatch_Scores ], $Data_Mismatch_Scores );
  408. $m_m = _set_match_mismatch( $Mismatch_Scores_key );
  409. return array(
  410. 'max_target_seqs' => $numAlign,
  411. 'evalue' => $eVal,
  412. 'word_size' => $wordvalue,
  413. 'gapopen' => $gap[ 'gapOpen' ],
  414. 'gapextend' => $gap[ 'gapExtend' ],
  415. 'penalty' => $m_m[ 'penalty' ],
  416. 'reward' => $m_m[ 'reward' ],
  417. 'culling_limit' => $qRange
  418. );
  419. }
  420. elseif ( $blast_program == 'blastx' )
  421. {
  422. $wordSize = intval( $data[ Word_size ] );
  423. $numAlign = intval( $data[ Max_target_sequences ] );
  424. $matrix = $data[ Matrix_options ];
  425. $gap = getGap( $matrix, 1 );
  426. return array(
  427. 'max_target_seqs' => $numAlign,
  428. 'evalue' => $eVal,
  429. 'word_size' => $wordSize,
  430. 'gapopen' => $gap[ 'gapOpen' ],
  431. 'gapextend' => $gap[ 'gapExtend' ],
  432. 'culling_limit' => $qRange,
  433. 'matrix' => $matrix
  434. );
  435. }
  436. elseif ( $blast_program == 'tblastn' )
  437. {
  438. $wordSize = intval( $data[ Word_size ] );
  439. $numAlign = intval( $data[ Max_target_sequences ] );
  440. $matrix = $data[ Matrix_options ];
  441. $gap = getGap( $matrix, 1 );
  442. return array(
  443. 'max_target_seqs' => $numAlign,
  444. 'evalue' => $eVal,
  445. 'word_size' => $wordSize,
  446. 'gapopen' => $gap[ 'gapOpen' ],
  447. 'gapextend' => $gap[ 'gapExtend' ],
  448. 'culling_limit' => $qRange,
  449. 'matrix' => $matrix
  450. );
  451. }
  452. elseif ( $blast_program == 'blastp' )
  453. {
  454. $wordSize = intval( $data[ Word_size ] );
  455. $numAlign = intval( $data[ Max_target_sequences ] );
  456. $matrix = $data[ Matrix_options ];
  457. $gap = getGap( $matrix, 1 );
  458. return array(
  459. 'max_target_seqs' => $numAlign,
  460. 'evalue' => $eVal,
  461. 'word_size' => $wordSize,
  462. 'gapopen' => $gap[ 'gapOpen' ],
  463. 'gapextend' => $gap[ 'gapExtend' ],
  464. 'culling_limit' => $qRange,
  465. 'matrix' => $matrix
  466. );
  467. }
  468. }
  469. /*
  470. ** Call the core API of Triple
  471. ** @return : Job_ID
  472. */
  473. function blast_ui_getblastJobId( $query_type, $db_type, $seqQuery, $Databasekey, $adv_options )
  474. {
  475. if ( $query_type == 'nucleotide' )
  476. {
  477. if ( $db_type == 'nucleotide' )
  478. {
  479. $blast_program = 'blastn';
  480. }
  481. elseif ( $db_type == 'protein' )
  482. {
  483. $blast_program = 'blastx';
  484. }
  485. }
  486. elseif ( $query_type == 'protein' )
  487. {
  488. if ( $db_type == 'nucleotide' )
  489. {
  490. $blast_program = 'tblastn';
  491. }
  492. elseif ( $db_type == 'protein' )
  493. {
  494. $blast_program = 'blastp';
  495. }
  496. }
  497. $query = variable_get('file_temporary_path', file_directory_temp()) . '/' . date( 'YMd_His' ) . '_query.fasta';
  498. file_put_contents( $query, $seqQuery );
  499. $blastdb_node = node_load( $Databasekey );
  500. $blastdb_name = $blastdb_node->db_name;
  501. $blastdb_with_path = $blastdb_node->db_path;
  502. // Now let each program process its own advanced options.
  503. $advanced_options = $adv_options;
  504. global $user;
  505. $output_filestub = date( 'YMd_His' );
  506. $job_args = array(
  507. 'program' => $blast_program,
  508. 'query' => $query,
  509. 'database' => $blastdb_with_path,
  510. 'output_filename' => $output_filestub,
  511. 'options' => $advanced_options
  512. );
  513. $job_id = tripal_add_job( t( 'BLAST (@program): @query', array(
  514. '@program' => $blast_program,
  515. '@query' => $query
  516. ) ), 'blast_job', 'run_BLAST_tripal_job', $job_args, $user->uid );
  517. ob_start();
  518. tripal_launch_job( 1, $job_id );
  519. ob_end_clean();
  520. return $job_id;
  521. // return getBlastData($job_id);
  522. }
  523. function getBlaststatus( $job_id )
  524. {
  525. /* $sql = "SELECT status FROM tripal_jobs WHERE job_id = :job_id ";
  526. $job_res = db_query($sql, array(':job_id' => $job_id))->fetchField();
  527. return $job_res; */
  528. }
  529. /*
  530. **Method returns file output data
  531. ** @param int $job_id
  532. ** web service url : http://localhost:8080/restapi/blast/job_id
  533. */
  534. function getBlastData( $job_id )
  535. {
  536. $result;
  537. $sql = "SELECT job_name FROM tripal_jobs WHERE job_id = :job_id ";
  538. $job_res = db_query( $sql, array(
  539. ':job_id' => $job_id
  540. ) )->fetchField();
  541. $file_path = str_replace( '_query.fasta', '.blast.asn', $job_res );
  542. if ( stristr( $file_path, "tblastn" ) )
  543. {
  544. $file_path = str_replace( 'BLAST (tblastn): /', '/', $file_path );
  545. }
  546. elseif ( stristr( $file_path, "blastx" ) )
  547. {
  548. $file_path = str_replace( 'BLAST (blastx): /', '/', $file_path );
  549. }
  550. elseif ( stristr( $file_path, "blastn" ) )
  551. {
  552. $file_path = str_replace( 'BLAST (blastn): /', '/', $file_path );
  553. }
  554. elseif ( stristr( $file_path, "blastp" ) )
  555. {
  556. $file_path = str_replace( 'BLAST (blastp): /', '/', $file_path );
  557. }
  558. if ( file_exists( $file_path ) )
  559. {
  560. $result = file_get_contents( $file_path );
  561. }
  562. else
  563. {
  564. $result = "The file $file_path does not exist";
  565. }
  566. return $result;
  567. }
  568. /*
  569. ** Used for tblastn
  570. */
  571. function getGap_cost()
  572. {
  573. return array(
  574. 'PAM30' => _get_gap_for_matrix( 'PAM30' ),
  575. 'PAM70' => _get_gap_for_matrix( 'PAM70' ),
  576. 'PAM250' => _get_gap_for_matrix( 'PAM250' ),
  577. 'BLOSUM80' => _get_gap_for_matrix( 'BLOSUM80' ),
  578. 'BLOSUM62' => _get_gap_for_matrix( 'BLOSUM62' ),
  579. 'BLOSUM45' => _get_gap_for_matrix( 'BLOSUM45' ),
  580. 'BLOSUM50' => _get_gap_for_matrix( 'BLOSUM50' ),
  581. 'BLOSUM90' => _get_gap_for_matrix( 'BLOSUM90' )
  582. );
  583. }