blast_ui.services.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. 'create' => array(
  31. 'help' => 'Creates a blast_ui',
  32. 'callback' => '_blast_ui_create',
  33. 'access arguments' => array(
  34. 'access content'
  35. ),
  36. 'access arguments append' => false,
  37. 'args' => array(
  38. array(
  39. 'name' => 'param',
  40. 'type' => 'array',
  41. 'description' => 'The information needed',
  42. 'source' => 'data',
  43. 'optional' => FALSE
  44. )
  45. )
  46. ),
  47. 'update' => array(
  48. 'help' => 'Updates a blast_ui',
  49. 'callback' => '_blast_ui_update',
  50. 'access callback' => 'user_access',
  51. 'access arguments' => array(
  52. 'access user profiles'
  53. ),
  54. 'args' => array(
  55. array(
  56. 'name' => 'id',
  57. 'type' => 'int',
  58. 'description' => 'The information needed',
  59. 'source' => array(
  60. 'path' => '0'
  61. ),
  62. 'optional' => FALSE
  63. ),
  64. array(
  65. 'name' => 'data',
  66. 'type' => 'struct',
  67. 'description' => 'The information needed',
  68. 'source' => 'data',
  69. 'optional' => FALSE
  70. )
  71. )
  72. ),
  73. 'delete' => array(
  74. 'help' => 'Deletes a blast_ui',
  75. 'callback' => '_blast_ui_delete',
  76. 'access callback' => 'user_access',
  77. 'access arguments' => array(
  78. 'access content'
  79. ),
  80. 'access arguments append' => TRUE,
  81. 'args' => array(
  82. array(
  83. 'name' => 'nid',
  84. 'type' => 'int',
  85. 'description' => 'The information needed',
  86. 'source' => array(
  87. 'path' => '0'
  88. ),
  89. 'optional' => FALSE
  90. )
  91. )
  92. ),
  93. 'index' => array(
  94. 'help' => 'Retrieves a listing of blast_ui',
  95. 'callback' => '_blast_ui_index',
  96. 'access callback' => 'user_access',
  97. 'access arguments' => array(
  98. 'access content'
  99. ),
  100. 'access arguments append' => FALSE,
  101. 'args' => array()
  102. ),
  103. 'actions' => array(
  104. 'getJobId' => array(
  105. 'help' => 'Retrieves a listing of database',
  106. 'callback' => '_blast_ui_getJobId',
  107. 'access callback' => 'user_access',
  108. 'access arguments' => array(
  109. 'access content'
  110. ),
  111. 'args' => array(
  112. array(
  113. 'name' => 'data',
  114. 'type' => 'array',
  115. 'description' => 'Retrieve blast job id',
  116. 'source' => 'data',
  117. 'optional' => FALSE
  118. )
  119. ),
  120. 'access arguments append' => FALSE
  121. ),
  122. 'getDatabaseOptions' => array(
  123. 'help' => 'Retrieves a listing of database',
  124. 'callback' => '_blast_ui_getDatabaseOption',
  125. 'access callback' => 'user_access',
  126. 'access arguments' => array(
  127. 'access content'
  128. ),
  129. 'args' => array(
  130. array(
  131. 'name' => 'data',
  132. 'type' => 'array',
  133. 'description' => 'The set database_type information',
  134. 'source' => 'data',
  135. 'optional' => FALSE
  136. )
  137. ),
  138. 'access arguments append' => false
  139. )
  140. )
  141. )
  142. );
  143. }
  144. function _blast_ui_create($param)
  145. {
  146. return services_error('Missing _blast_ui_create', 406);
  147. }
  148. /**
  149. * Callback for updating blast services.
  150. *
  151. * @param int $id
  152. * @param object $data
  153. * @return object
  154. */
  155. function _blast_ui_update($id, $data)
  156. {
  157. return services_error('Missing _blast_ui_update', 406);
  158. }
  159. /**
  160. * Callback for retrieving blast services.
  161. *
  162. * @param int $id
  163. * @return object
  164. */
  165. function _blast_ui_retrieve($id)
  166. {
  167. return getBlastData($id);
  168. }
  169. /**
  170. * Callback for deleting blast services.
  171. *
  172. * @param int $id
  173. * @return object
  174. */
  175. function _blast_ui_delete($id)
  176. {
  177. return services_error('Missing _blast_ui_delete', 406);
  178. }
  179. /**
  180. * Callback for Index blast services.
  181. */
  182. function _blast_ui_index()
  183. {
  184. return array(
  185. 'query_type' => array(
  186. '1' => 'nucleotide',
  187. '2' => 'protein'
  188. ),
  189. 'db_type' => array(
  190. '1' => 'nucleotide',
  191. '2' => 'protein'
  192. ),
  193. 'blast_program' => array(
  194. '1' => 'blastn',
  195. '2' => 'blastx',
  196. '3' => 'tblastn',
  197. '4' => 'blastp'
  198. )
  199. );
  200. }
  201. function _blast_ui_getDatabaseOption($data)
  202. {
  203. if (!isset($data[ query_type ]))
  204. {
  205. return services_error('Missing blast attribute $query_type [ set necleotide or protein in post request of query_type ]', 406);
  206. }
  207. if (!isset($data[ db_type ]))
  208. {
  209. return services_error('Missing blast attribute $db_type [ set necleotide or protein in post request of db_type ]', 406);
  210. }
  211. if (!isset($data[ blast_program ]))
  212. {
  213. return services_error('Missing blast attribute $blast_program [ set blastn, blastx, tblastn, blastp in post request of blast_program ]', 406);
  214. }
  215. $query_type = $data[ db_type ];
  216. $db_type = $data[ db_type ];
  217. $blast_program = $data[ blast_program ];
  218. $options_database = get_blast_database_options($db_type);
  219. if ($blast_program == 'blastn')
  220. {
  221. return array(
  222. 'Select_Database' => $options_database,
  223. 'Max_target_sequences' => _get_max_target($blast_program),
  224. 'Word_size' => _get_word_size($blast_program),
  225. 'Match_Mismatch_Scores' => _get_match_mismatch($blast_program),
  226. 'Gap_Costs' => _get_gap($blast_program),
  227. 'Default_Values' => _get_default_values('')
  228. );
  229. }
  230. elseif ($blast_program == 'blastx')
  231. {
  232. return array(
  233. 'Select_Database' => $options_database,
  234. 'Max_target_sequences' => _get_max_target($blast_program),
  235. 'Word_size' => _get_word_size($blast_program),
  236. 'Matrix_options' => _get_matrix_options()
  237. );
  238. }
  239. }
  240. function blast_validation($query_type, $db_type, $data)
  241. {
  242. if ($query_type == 'nucleotide')
  243. {
  244. if ($db_type == 'nucleotide')
  245. {
  246. if (!isset($data[ Select_Database ]))
  247. {
  248. return services_error('Missing blast attribute $Select_Database [ query blast/getDatabaseOptions for options :- Add database you need to use]', 406);
  249. }
  250. if (!isset($data[ Max_target_sequences ]))
  251. {
  252. return services_error('Missing blast attribute $Max_target_sequences [ query blast/getDatabaseOptions for options ]', 406);
  253. }
  254. if (!isset($data[ Word_size ]))
  255. {
  256. return services_error('Missing blast attribute $Word_size [ query blast/getDatabaseOptions for options ]', 406);
  257. }
  258. if (!isset($data[ Match_Mismatch_Scores ]))
  259. {
  260. return services_error('Missing blast attribute $Match_Mismatch_Scores [ query blast/getDatabaseOptions for options]', 406);
  261. }
  262. if (!isset($data[ Gap_Costs ]))
  263. {
  264. return services_error('Missing blast attribute $Gap_Costs [ query blast/getDatabaseOptions for options ]', 406);
  265. }
  266. if (!isset($data[ Default_Values ]))
  267. {
  268. return services_error('Missing blast attribute $Default_Values [ query blast/getDatabaseOptions for options]', 406);
  269. }
  270. if (!isset($data[ seqQuery ]))
  271. {
  272. return services_error('Missing blast attribute $seqQuery [ Add sequence query for blast]', 406);
  273. }
  274. }
  275. elseif ($db_type == 'protein')
  276. {
  277. if (!isset($data[ Select_Database ]))
  278. {
  279. return services_error('Missing blast attribute $Select_Database [ query blast/getDatabaseOptions for options :- Add database used by you]', 406);
  280. }
  281. if (!isset($data[ Max_target_sequences ]))
  282. {
  283. return services_error('Missing blast attribute $Max_target_sequences [ query blast/getDatabaseOptions for options ]', 406);
  284. }
  285. if (!isset($data[ Word_size ]))
  286. {
  287. return services_error('Missing blast attribute $Word_size [ query blast/getDatabaseOptions for options ]', 406);
  288. }
  289. if (!isset($data[ Matrix_options ]))
  290. {
  291. return services_error('Missing blast attribute $Matrix_options [ query blast/getDatabaseOptions for options ]', 406);
  292. }
  293. if (!isset($data[ seqQuery ]))
  294. {
  295. return services_error('Missing blast attribute $seqQuery [ Add sequence query for blast]', 406);
  296. }
  297. }
  298. }
  299. /*
  300. elseif ($query_type == 'protein') {
  301. if ($db_type == 'nucleotide') {
  302. $blast_program = 'tblastn';
  303. }
  304. elseif ($db_type == 'protein') {
  305. $blast_program = 'blastp';
  306. }
  307. }
  308. */
  309. }
  310. function _blast_ui_getJobId($data)
  311. {
  312. $query_type;
  313. $db_type;
  314. $eVal = 0.001;
  315. $qRange = 0;
  316. /* check values in post query */
  317. if (!isset($data[ query_type ]))
  318. {
  319. return services_error('Missing blast attribute $query_type [ set necleotide or protein in post request of query_type ]', 406);
  320. }
  321. if (!isset($data[ db_type ]))
  322. {
  323. return services_error('Missing blast attribute $db_type [ set necleotide or protein in post request of db_type ]', 406);
  324. }
  325. $query_type = $data[ query_type ];
  326. $db_type = $data[ db_type ];
  327. blast_validation($query_type, $db_type, $data);
  328. if (isset($data[ $eVal ]))
  329. {
  330. $eVal = $data[ $eVal ];
  331. }
  332. if (isset($data[ $culling_limit ]))
  333. {
  334. $qRange = $data[ $culling_limit ];
  335. }
  336. if ($query_type == 'nucleotide')
  337. {
  338. if ($db_type == 'nucleotide')
  339. {
  340. $blast_program = 'blastn';
  341. // code not working for wordvalue = 7 ?
  342. $wordvalue = 11;
  343. // $wordvalue = intval($data[Word_size]);
  344. $numAlign = intval($data[ Max_target_sequences ]);
  345. $options_database = get_blast_database_options($data[ db_type ]);
  346. $Databasekey = array_search($data[ Select_Database ], $options_database);
  347. $advanced_options = advanced_options($blast_program, $data[ Gap_Costs ], $data[ Match_Mismatch_Scores ], $wordvalue, $numAlign, $eVal, $qRange);
  348. }
  349. elseif ($db_type == 'protein')
  350. {
  351. $blast_program = 'blastx';
  352. return 'working';
  353. }
  354. }
  355. elseif ($query_type == 'protein')
  356. {
  357. if ($db_type == 'nucleotide')
  358. {
  359. $blast_program = 'tblastn';
  360. }
  361. elseif ($db_type == 'protein')
  362. {
  363. $blast_program = 'blastp';
  364. }
  365. }
  366. return blast_ui_getblastJobId($data[ query_type ], $data[ db_type ], $data[ seqQuery ], $Databasekey, $advanced_options);
  367. }
  368. // Define the advanced Options for Blast
  369. function advanced_options($blast_program, $gapCost, $Match_Mismatch_Scores, $wordvalue, $numAlign, $eVal, $qRange)
  370. {
  371. $Data_GapCost = _get_gap($blast_program);
  372. $gapkey = array_search($gapCost, $Data_GapCost);
  373. $gap = _set_gap($gapkey);
  374. $Data_Mismatch_Scores = _get_match_mismatch($blast_program);
  375. $Mismatch_Scores_key = array_search($Match_Mismatch_Scores, $Data_Mismatch_Scores);
  376. $m_m = _set_match_mismatch($Mismatch_Scores_key);
  377. return array(
  378. 'max_target_seqs' => $numAlign,
  379. 'evalue' => $eVal,
  380. 'word_size' => $wordvalue,
  381. 'gapopen' => $gap[ 'gapOpen' ],
  382. 'gapextend' => $gap[ 'gapExtend' ],
  383. 'penalty' => $m_m[ 'penalty' ],
  384. 'reward' => $m_m[ 'reward' ],
  385. 'culling_limit' => $qRange
  386. );
  387. }
  388. function blast_ui_getblastJobId($query_type, $db_type, $seqQuery, $Databasekey, $adv_options)
  389. {
  390. if ($query_type == 'nucleotide')
  391. {
  392. if ($db_type == 'nucleotide')
  393. {
  394. $blast_program = 'blastn';
  395. }
  396. elseif ($db_type == 'protein')
  397. {
  398. $blast_program = 'blastx';
  399. }
  400. }
  401. elseif ($query_type == 'protein')
  402. {
  403. if ($db_type == 'nucleotide')
  404. {
  405. $blast_program = 'tblastn';
  406. }
  407. elseif ($db_type == 'protein')
  408. {
  409. $blast_program = 'blastp';
  410. }
  411. }
  412. $query = '/tmp/' . date('YMd_His') . '_query.fasta';
  413. file_put_contents($query, $seqQuery);
  414. $blastdb_node = node_load($Databasekey);
  415. $blastdb_name = $blastdb_node->db_name;
  416. $blastdb_with_path = $blastdb_node->db_path;
  417. // Now let each program process its own advanced options.
  418. $advanced_options = $adv_options;
  419. global $user;
  420. $output_filestub = date('YMd_His');
  421. $job_args = array(
  422. 'program' => $blast_program,
  423. 'query' => $query,
  424. 'database' => $blastdb_with_path,
  425. 'output_filename' => $output_filestub,
  426. 'options' => $advanced_options
  427. );
  428. $job_id = tripal_add_job(t('BLAST (@program): @query', array(
  429. '@program' => $blast_program,
  430. '@query' => $query
  431. )), 'blast_job', 'run_BLAST_tripal_job', $job_args, $user->uid);
  432. ob_start();
  433. tripal_launch_job(1, $job_id);
  434. ob_end_clean();
  435. return $job_id;
  436. // return getBlastData($job_id);
  437. }
  438. function getBlaststatus($job_id)
  439. {
  440. /* $sql = "SELECT status FROM tripal_jobs WHERE job_id = :job_id ";
  441. $job_res = db_query($sql, array(':job_id' => $job_id))->fetchField();
  442. return $job_res; */
  443. }
  444. function getBlastData($job_id)
  445. {
  446. $result;
  447. $sql = "SELECT job_name FROM tripal_jobs WHERE job_id = :job_id ";
  448. $job_res = db_query($sql, array(
  449. ':job_id' => $job_id
  450. ))->fetchField();
  451. $file_path = str_replace('_query.fasta', '.blast.asn', $job_res);
  452. $file_path = str_replace('BLAST (blastn): /', '/', $file_path);
  453. if (file_exists($file_path))
  454. {
  455. $result = file_get_contents($file_path);
  456. }
  457. else
  458. {
  459. $result = "The file $file_path does not exist";
  460. }
  461. return $result;
  462. }