123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563 |
- <?php
- /**
- * Implements hook_services_resources().
- * Hooks provided by Services for the definition of new services,
- */
- function blast_ui_services_resources()
- {
- return array(
- 'blast' => array(
- 'retrieve' => array(
- 'help' => 'Retrieves a blastResult',
- 'callback' => 'getBlastData',
- 'access callback' => 'user_access',
- 'access arguments' => array(
- 'access content'
- ),
- 'access arguments append' => FALSE,
- 'args' => array(
- array(
- 'name' => 'job_id',
- 'type' => 'int',
- 'description' => 'The information needed',
- 'source' => array(
- 'path' => '0'
- ),
- 'optional' => FALSE
- )
- )
- ),
- 'create' => array(
- 'help' => 'Creates a blast_ui',
- 'callback' => '_blast_ui_create',
- 'access arguments' => array(
- 'access content'
- ),
- 'access arguments append' => false,
- 'args' => array(
- array(
- 'name' => 'param',
- 'type' => 'array',
- 'description' => 'The information needed',
- 'source' => 'data',
- 'optional' => FALSE
- )
- )
- ),
- 'update' => array(
- 'help' => 'Updates a blast_ui',
- 'callback' => '_blast_ui_update',
- 'access callback' => 'user_access',
- 'access arguments' => array(
- 'access user profiles'
- ),
- 'args' => array(
- array(
- 'name' => 'id',
- 'type' => 'int',
- 'description' => 'The information needed',
- 'source' => array(
- 'path' => '0'
- ),
- 'optional' => FALSE
- ),
- array(
- 'name' => 'data',
- 'type' => 'struct',
- 'description' => 'The information needed',
- 'source' => 'data',
- 'optional' => FALSE
- )
- )
- ),
- 'delete' => array(
- 'help' => 'Deletes a blast_ui',
- 'callback' => '_blast_ui_delete',
- 'access callback' => 'user_access',
- 'access arguments' => array(
- 'access content'
- ),
- 'access arguments append' => TRUE,
- 'args' => array(
- array(
- 'name' => 'nid',
- 'type' => 'int',
- 'description' => 'The information needed',
- 'source' => array(
- 'path' => '0'
- ),
- 'optional' => FALSE
- )
- )
- ),
- 'index' => array(
- 'help' => 'Retrieves a listing of blast_ui',
- 'callback' => '_blast_ui_index',
- 'access callback' => 'user_access',
- 'access arguments' => array(
- 'access content'
- ),
- 'access arguments append' => FALSE,
- 'args' => array()
- ),
-
- 'actions' => array(
- 'getJobId' => array(
- 'help' => 'Retrieves a listing of database',
- 'callback' => '_blast_ui_getJobId',
- 'access callback' => 'user_access',
- 'access arguments' => array(
- 'access content'
- ),
- 'args' => array(
- array(
- 'name' => 'data',
- 'type' => 'array',
- 'description' => 'Retrieve blast job id',
- 'source' => 'data',
- 'optional' => FALSE
- )
- ),
- 'access arguments append' => FALSE
- ),
-
- 'getDatabaseOptions' => array(
- 'help' => 'Retrieves a listing of database',
- 'callback' => '_blast_ui_getDatabaseOption',
- 'access callback' => 'user_access',
- 'access arguments' => array(
- 'access content'
- ),
- 'args' => array(
- array(
- 'name' => 'data',
- 'type' => 'array',
- 'description' => 'The set database_type information',
- 'source' => 'data',
- 'optional' => FALSE
- )
- ),
- 'access arguments append' => false
- )
- )
- )
- );
-
- }
- function _blast_ui_create($param)
- {
-
- return services_error('Missing _blast_ui_create', 406);
- }
- /**
- * Callback for updating blast services.
- *
- * @param int $id
- * @param object $data
- * @return object
- */
- function _blast_ui_update($id, $data)
- {
- return services_error('Missing _blast_ui_update', 406);
- }
- /**
- * Callback for retrieving blast services.
- *
- * @param int $id
- * @return object
- */
- function _blast_ui_retrieve($id)
- {
- return getBlastData($id);
- }
- /**
- * Callback for deleting blast services.
- *
- * @param int $id
- * @return object
- */
- function _blast_ui_delete($id)
- {
- return services_error('Missing _blast_ui_delete', 406);
- }
- /**
- * Callback for Index blast services.
- */
- function _blast_ui_index()
- {
- return array(
-
- 'query_type' => array(
- '1' => 'nucleotide',
- '2' => 'protein'
- ),
- 'db_type' => array(
- '1' => 'nucleotide',
- '2' => 'protein'
- ),
- 'blast_program' => array(
- '1' => 'blastn',
- '2' => 'blastx',
- '3' => 'tblastn',
- '4' => 'blastp'
- )
-
- );
- }
- function _blast_ui_getDatabaseOption($data)
- {
-
- if (!isset($data[ query_type ]))
- {
- return services_error('Missing blast attribute $query_type [ set necleotide or protein in post request of query_type ]', 406);
- }
- if (!isset($data[ db_type ]))
- {
- return services_error('Missing blast attribute $db_type [ set necleotide or protein in post request of db_type ]', 406);
- }
- if (!isset($data[ blast_program ]))
- {
- return services_error('Missing blast attribute $blast_program [ set blastn, blastx, tblastn, blastp in post request of blast_program ]', 406);
- }
-
- $query_type = $data[ db_type ];
- $db_type = $data[ db_type ];
- $blast_program = $data[ blast_program ];
- $options_database = get_blast_database_options($db_type);
-
-
-
- if ($blast_program == 'blastn')
- {
- return array(
- 'Select_Database' => $options_database,
- 'Max_target_sequences' => _get_max_target($blast_program),
- 'Word_size' => _get_word_size($blast_program),
- 'Match_Mismatch_Scores' => _get_match_mismatch($blast_program),
- 'Gap_Costs' => _get_gap($blast_program),
- 'Default_Values' => _get_default_values('')
- );
- }
- elseif ($blast_program == 'blastx')
- {
- return array(
- 'Select_Database' => $options_database,
- 'Max_target_sequences' => _get_max_target($blast_program),
- 'Word_size' => _get_word_size($blast_program),
- 'Matrix_options' => _get_matrix_options()
-
- );
- }
- }
- function blast_validation($query_type, $db_type, $data)
- {
-
- if ($query_type == 'nucleotide')
- {
- if ($db_type == 'nucleotide')
- {
-
- if (!isset($data[ Select_Database ]))
- {
- return services_error('Missing blast attribute $Select_Database [ query blast/getDatabaseOptions for options :- Add database you need to use]', 406);
- }
- if (!isset($data[ Max_target_sequences ]))
- {
- return services_error('Missing blast attribute $Max_target_sequences [ query blast/getDatabaseOptions for options ]', 406);
- }
- if (!isset($data[ Word_size ]))
- {
- return services_error('Missing blast attribute $Word_size [ query blast/getDatabaseOptions for options ]', 406);
- }
- if (!isset($data[ Match_Mismatch_Scores ]))
- {
- return services_error('Missing blast attribute $Match_Mismatch_Scores [ query blast/getDatabaseOptions for options]', 406);
- }
- if (!isset($data[ Gap_Costs ]))
- {
- return services_error('Missing blast attribute $Gap_Costs [ query blast/getDatabaseOptions for options ]', 406);
- }
- if (!isset($data[ Default_Values ]))
- {
- return services_error('Missing blast attribute $Default_Values [ query blast/getDatabaseOptions for options]', 406);
- }
- if (!isset($data[ seqQuery ]))
- {
- return services_error('Missing blast attribute $seqQuery [ Add sequence query for blast]', 406);
- }
-
- }
- elseif ($db_type == 'protein')
- {
-
- if (!isset($data[ Select_Database ]))
- {
- return services_error('Missing blast attribute $Select_Database [ query blast/getDatabaseOptions for options :- Add database used by you]', 406);
- }
- if (!isset($data[ Max_target_sequences ]))
- {
- return services_error('Missing blast attribute $Max_target_sequences [ query blast/getDatabaseOptions for options ]', 406);
- }
- if (!isset($data[ Word_size ]))
- {
- return services_error('Missing blast attribute $Word_size [ query blast/getDatabaseOptions for options ]', 406);
- }
- if (!isset($data[ Matrix_options ]))
- {
- return services_error('Missing blast attribute $Matrix_options [ query blast/getDatabaseOptions for options ]', 406);
- }
- if (!isset($data[ seqQuery ]))
- {
- return services_error('Missing blast attribute $seqQuery [ Add sequence query for blast]', 406);
- }
-
- }
- }
- /*
- elseif ($query_type == 'protein') {
- if ($db_type == 'nucleotide') {
- $blast_program = 'tblastn';
- }
- elseif ($db_type == 'protein') {
- $blast_program = 'blastp';
- }
- }
-
-
- */
-
- }
- function _blast_ui_getJobId($data)
- {
-
- $query_type;
- $db_type;
- $eVal = 0.001;
- $qRange = 0;
-
-
-
-
- /* check values in post query */
-
- if (!isset($data[ query_type ]))
- {
- return services_error('Missing blast attribute $query_type [ set necleotide or protein in post request of query_type ]', 406);
- }
- if (!isset($data[ db_type ]))
- {
- return services_error('Missing blast attribute $db_type [ set necleotide or protein in post request of db_type ]', 406);
- }
-
- $query_type = $data[ query_type ];
- $db_type = $data[ db_type ];
-
- blast_validation($query_type, $db_type, $data);
-
- if (isset($data[ $eVal ]))
- {
- $eVal = $data[ $eVal ];
- }
- if (isset($data[ $culling_limit ]))
- {
- $qRange = $data[ $culling_limit ];
- }
-
- if ($query_type == 'nucleotide')
- {
- if ($db_type == 'nucleotide')
- {
- $blast_program = 'blastn';
- // code not working for wordvalue = 7 ?
- $wordvalue = 11;
- // $wordvalue = intval($data[Word_size]);
- $numAlign = intval($data[ Max_target_sequences ]);
- $options_database = get_blast_database_options($data[ db_type ]);
- $Databasekey = array_search($data[ Select_Database ], $options_database);
- $advanced_options = advanced_options($blast_program, $data[ Gap_Costs ], $data[ Match_Mismatch_Scores ], $wordvalue, $numAlign, $eVal, $qRange);
-
- }
- elseif ($db_type == 'protein')
- {
- $blast_program = 'blastx';
- return 'working';
- }
- }
- elseif ($query_type == 'protein')
- {
- if ($db_type == 'nucleotide')
- {
- $blast_program = 'tblastn';
- }
- elseif ($db_type == 'protein')
- {
- $blast_program = 'blastp';
- }
- }
-
-
- return blast_ui_getblastJobId($data[ query_type ], $data[ db_type ], $data[ seqQuery ], $Databasekey, $advanced_options);
-
-
- }
- // Define the advanced Options for Blast
- function advanced_options($blast_program, $gapCost, $Match_Mismatch_Scores, $wordvalue, $numAlign, $eVal, $qRange)
- {
-
- $Data_GapCost = _get_gap($blast_program);
- $gapkey = array_search($gapCost, $Data_GapCost);
- $gap = _set_gap($gapkey);
-
-
- $Data_Mismatch_Scores = _get_match_mismatch($blast_program);
- $Mismatch_Scores_key = array_search($Match_Mismatch_Scores, $Data_Mismatch_Scores);
- $m_m = _set_match_mismatch($Mismatch_Scores_key);
-
-
- return array(
- 'max_target_seqs' => $numAlign,
- 'evalue' => $eVal,
- 'word_size' => $wordvalue,
- 'gapopen' => $gap[ 'gapOpen' ],
- 'gapextend' => $gap[ 'gapExtend' ],
- 'penalty' => $m_m[ 'penalty' ],
- 'reward' => $m_m[ 'reward' ],
- 'culling_limit' => $qRange
-
- );
- }
- function blast_ui_getblastJobId($query_type, $db_type, $seqQuery, $Databasekey, $adv_options)
- {
-
-
-
- if ($query_type == 'nucleotide')
- {
- if ($db_type == 'nucleotide')
- {
- $blast_program = 'blastn';
- }
- elseif ($db_type == 'protein')
- {
- $blast_program = 'blastx';
- }
- }
- elseif ($query_type == 'protein')
- {
- if ($db_type == 'nucleotide')
- {
- $blast_program = 'tblastn';
- }
- elseif ($db_type == 'protein')
- {
- $blast_program = 'blastp';
- }
- }
-
- $query = '/tmp/' . date('YMd_His') . '_query.fasta';
- file_put_contents($query, $seqQuery);
- $blastdb_node = node_load($Databasekey);
- $blastdb_name = $blastdb_node->db_name;
- $blastdb_with_path = $blastdb_node->db_path;
-
- // Now let each program process its own advanced options.
-
-
- $advanced_options = $adv_options;
- global $user;
-
- $output_filestub = date('YMd_His');
- $job_args = array(
- 'program' => $blast_program,
- 'query' => $query,
- 'database' => $blastdb_with_path,
- 'output_filename' => $output_filestub,
- 'options' => $advanced_options
- );
-
- $job_id = tripal_add_job(t('BLAST (@program): @query', array(
- '@program' => $blast_program,
- '@query' => $query
- )), 'blast_job', 'run_BLAST_tripal_job', $job_args, $user->uid);
-
- ob_start();
- tripal_launch_job(1, $job_id);
- ob_end_clean();
- return $job_id;
- // return getBlastData($job_id);
- }
- function getBlaststatus($job_id)
- {
- /* $sql = "SELECT status FROM tripal_jobs WHERE job_id = :job_id ";
- $job_res = db_query($sql, array(':job_id' => $job_id))->fetchField();
- return $job_res; */
- }
- function getBlastData($job_id)
- {
- $result;
- $sql = "SELECT job_name FROM tripal_jobs WHERE job_id = :job_id ";
- $job_res = db_query($sql, array(
- ':job_id' => $job_id
- ))->fetchField();
- $file_path = str_replace('_query.fasta', '.blast.asn', $job_res);
- $file_path = str_replace('BLAST (blastn): /', '/', $file_path);
-
- if (file_exists($file_path))
- {
- $result = file_get_contents($file_path);
- }
- else
- {
- $result = "The file $file_path does not exist";
- }
- return $result;
- }
|