12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982 |
- <?php
- function tripal_analysis_blast_init(){
-
- drupal_add_css(drupal_get_path('theme', 'tripal').'/css/tripal_analysis_blast.css');
- drupal_add_js(drupal_get_path('theme', 'tripal').'/js/tripal_analysis_blast.js');
- }
- function tripal_analysis_blast_menu() {
-
- $items['tripal_top_blast'] = array(
- 'path' => 'top_blast',
- 'title' => t('Blast Hits'),
- 'page callback' => 'tripal_get_feature_blast_results_ajax',
- 'page arguments' => array(1,2,3),
- 'access arguments' => array('access content'),
- 'type' => MENU_CALLBACK
- );
-
- $items['admin/tripal/tripal_analysis/tripal_blast_regex/%'] = array(
- 'title' => t('Blast Regex'),
- 'page callback' => 'tripal_get_blast_regex',
- 'page arguments' => array(4),
- 'access arguments' => array('administer site configuration'),
- 'type' => MENU_CALLBACK
- );
- $items['tripal_blast_report'] = array(
- 'title' => t('Homology Report'),
- 'page callback' => 'tripal_get_blast_report',
- 'page arguments' => array(1,2,3,4,5),
- 'access arguments' => array('access chado_analysis_blast content'),
- 'type' => MENU_CALLBACK,
- 'file' => 'tripal_analysis_blast_htmlreport.inc'
- );
- return $items;
- }
- function tripal_analysis_blast_nodeapi(&$node, $op, $teaser, $page) {
- switch ($op) {
- case 'view':
-
- $types_to_show = variable_get('tripal_analysis_blast_setting',
- array('chado_feature'));
-
- if (!in_array($node->type, $types_to_show, TRUE)) {
- break;
- }
-
- if (!$teaser && $node->feature->feature_id) {
- if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
- $node->content['tripal_analysis_blast_index_version'] = array(
- '#value' => theme('tripal_analysis_blast_results_index_version',$node),
- '#weight' => 8,
- );
- } else {
- if(strcmp($node->type,'chado_feature')==0){
-
- $node->content['tripal_feature_blast_results'] = array(
- '#value' => theme('tripal_feature_blast_results', $node),
- '#weight' => 8
- );
- }
- }
- }
- }
- }
- function tripal_analysis_blast_theme () {
- return array(
- 'tripal_analysis_blast_results_index_version' => array (
- 'arguments' => array('node'),
- ),
- 'tripal_feature_blast_results' => array(
- 'arguments' => array('node'=> null),
- 'template' => 'tripal_feature_blast_results',
- )
- );
- }
- function tripal_get_feature_blast_results_ajax($feature_id, $db_id, $max){
- $sql = "SELECT nid FROM {chado_feature} WHERE feature_id = %d";
- $nid = db_fetch_object(db_query($sql,$feature_id));
- $node = node_load($nid->nid);
-
- $node->db_id = $db_id;
- $node->max = $max;
-
- drupal_json(array('update' => theme('tripal_feature_blast_results',$node)));
- }
- function tripal_analysis_blast_preprocess_tripal_feature_blast_results(&$variables){
- $feature = $variables['node']->feature;
- $db_id = $variables['node']->db_id;
-
- $max = 10;
- if(isset($variables['node']->max)){
- $max = $variables['node']->max;
- }
- $blast_results = tripal_get_feature_blast_results($feature->feature_id, $db_id, $max);
- $variables['tripal_analysis_blast']['blast_results_list'] = $blast_results;
- }
- function theme_tripal_analysis_blast_results_index_version ($node) {
- $feature = $node->feature;
- $content = tripal_get_blast_results_index_version($feature->feature_id);
- return $content;
- }
- function tripal_get_feature_blast_results($feature_id, $db_id, $max){
-
-
-
-
- $sql = "SELECT AP.value AS apvalue, AFP.value AS afpvalue, AF.analysis_id AS aid
- FROM {analysisfeatureprop} AFP
- INNER JOIN {analysisfeature} AF ON AF.analysisfeature_id = AFP.analysisfeature_id
- INNER JOIN {analysisprop} AP ON AP.analysis_id = AF.analysis_id
- INNER JOIN {cvterm} CVT on AFP.type_id = CVT.cvterm_id
- INNER JOIN {cv} CV on CVT.cv_id = CV.cv_id
- WHERE AF.feature_id = %d AND CV.name = '%s' AND
- CVT.name = '%s' AND AP.value like '%|%' ";
- $previous_db = tripal_db_set_active('chado');
- $result = db_query($sql, $feature_id,'tripal','analysis_blast_output_iteration_hits');
- tripal_db_set_active($previous_db);
-
- $blast_obj_array = array ();
- $blast_obj_counter = 0;
- while ($analysisfeatureprop = db_fetch_object($result)) {
-
-
-
- $blastsettings = explode("|", $analysisfeatureprop->apvalue);
-
-
- if(count($blastsettings) != 3){
- continue;
- }
- if(!$blastsettings[0]){
- continue;
- }
- $att_db_id = $blastsettings[0];
-
- $previous_db = tripal_db_set_active('chado');
- $sql = "SELECT analysis_id AS aid, name, to_char(timeexecuted, 'MM-DD-YYYY') AS time
- FROM {analysis}
- WHERE analysis_id = %d";
- $analysis = db_fetch_object(db_query($sql, $analysisfeatureprop->aid));
- tripal_db_set_active($previous_db);
-
- $previous_db = tripal_db_set_active('chado');
- $sql = "SELECT * FROM {db} WHERE db_id=%d";
- $db = db_fetch_object(db_query($sql, $att_db_id));
- tripal_db_set_active($previous_db);
-
-
-
- if(!$db_id or ($db_id and $att_db_id == $db_id)) {
- $blast_obj = tripal_analysis_blast_get_result_object($analysisfeatureprop->afpvalue,$db,$max,$feature_id, $analysis);
- $blast_obj->analysis = $analysis;
- $blast_obj_array [$blast_obj_counter] = $blast_obj;
- $blast_obj_counter ++;
- }
- }
- return $blast_obj_array;
- }
- function tripal_get_blast_results_index_version ($feature_id){
-
-
- $previous_db = tripal_db_set_active('chado');
- $sql = "SELECT CVT.cvterm_id FROM {cvterm} CVT ".
- "INNER JOIN cv ON cv.cv_id = CVT.cv_id ".
- "WHERE CVT.name = 'analysis_blast_output_iteration_hits' ".
- "AND CV.name = 'tripal'";
- $type_id = db_result(db_query($sql));
-
-
- $sql = "SELECT AP.value AS apvalue, AFP.value AS afpvalue, AF.analysis_id AS aid
- FROM {analysisfeatureprop} AFP
- INNER JOIN analysisfeature AF ON AF.analysisfeature_id = AFP.analysisfeature_id
- INNER JOIN analysisprop AP ON AP.analysis_id = AF.analysis_id
- WHERE feature_id = %d
- AND AFP.type_id = %d ";
- $result = db_query($sql, $feature_id, $type_id);
- tripal_db_set_active($previous_db);
-
- while ($analysisfeatureprop = db_fetch_object($result)) {
-
- $previous_db = tripal_db_set_active('chado');
- $sql = "SELECT analysis_id AS aid, name, to_char(timeexecuted, 'MM-DD-YYYY') AS time
- FROM {analysis} WHERE analysis_id = %d";
- $analysis = db_fetch_object(db_query($sql, $analysisfeatureprop->aid));
- tripal_db_set_active($previous_db);
- $blastsettings = explode("|", $analysisfeatureprop->apvalue);
- $att_db_id = $blastsettings [0];
-
- $previous_db = tripal_db_set_active('chado');
- $sql = "SELECT * FROM {db} WHERE db_id=%d";
- $db = db_fetch_object(db_query($sql, $att_db_id));
- tripal_db_set_active($previous_db);
-
- $max = 10;
- $content .= parse_NCBI_Blast_XML_index_version($analysisfeatureprop->afpvalue,$db,$max,$feature_id,$ajax, $analysis);
-
- }
- return $content;
- }
- function parse_NCBI_Blast_XML_index_version($xml_string,$db,$feature_id) {
-
- $sql = "SELECT * FROM {tripal_analysis_blast} WHERE db_id = %d";
- $parser = db_fetch_object(db_query($sql, $db->db_id));
- $db_name = $parser->displayname;
- $is_genbank = $parser->genbank_style;
- $regex_hit_id = $parser->regex_hit_id;
- $regex_hit_def = $parser->regex_hit_def;
- $regex_hit_accession = $parser->regex_hit_accession;
-
- if(!$regex_hit_id){
- $regex_hit_id = '/^(.*?)\s.*$/';
- } else {
- $regex_hit_id = '/'.$regex_hit_id.'/';
- }
- if(!$regex_hit_def){
- $regex_hit_def = '/^.*?\s(.*)$/';
- } else {
- $regex_hit_def = '/'.$regex_hit_def.'/';
- }
- if(!$regex_hit_accession){
- $regex_hit_accession = '/^(.*?)\s.*$/';
- } else {
- $regex_hit_accession = '/'.$regex_hit_accession.'/';
- }
- $html_out .= "<h3>$db_name</h3>";
-
-
-
-
- $xml_output = simplexml_load_string($xml_string);
- $iteration = '';
-
- if ($xml_output->getName() == 'Iteration') {
- foreach ($xml_output->children() as $xml_tag) {
- if ($xml_tag->getName() == 'Iteration_query-def') {
-
- $html_out .= "Query: $xml_tag<br>";
- } else if ($xml_tag->getName() == 'Iteration_hits') {
- $iteration = $xml_tag;
- }
- }
-
- } else {
- $iteration = $xml_output;
- }
-
-
- foreach($iteration->children() as $hits){
- $best_evalue = 0;
- foreach($hits->children() as $hit){
- $best_evalue = 0;
- $element_name = $hit->getName();
- if($element_name == 'Hit_id'){
-
- if ($is_genbank) {
- $hit_name = $hit;
- }
- } else if($element_name == 'Hit_def'){
- if($is_genbank){
- $description = $hit;
- } else {
- $accession = preg_replace($regex_hit_accession,"$1",$hit);
- $hit_name = preg_replace($regex_hit_id,"$1",$hit);
- $description = preg_replace($regex_hit_def,"$1",$hit);
- }
- } else if($element_name == 'Hit_accession'){
-
- if ($is_genbank){
- $accession = $hit;
- }
-
- }
- }
- $html_out .= "<p>$hit_name<br>";
- $html_out .= "$accession<br>";
- $html_out .= "<b>$description</b></br>";
- $hsp_html_out = '';
- }
- return $html_out;
- }
- function tripal_analysis_blast_get_settings() {
-
- $options = node_get_types('names');
-
- $allowedoptions ['chado_feature'] = "Show blast results on feature pages";
- $form['description'] = array(
- '#type' => 'item',
- '#value' => t("Most chado features were analyzed by blast against major sequence databases. This option allows user to display the blast analysis results. Please read user manual for storage and display of blast files. Check the box to enable the analysis results. Uncheck to disable it."),
- '#weight' => 0,
- );
- $form['tripal_analysis_blast_setting'] = array(
- '#type' => 'checkboxes',
- '#options' => $allowedoptions,
- '#default_value' => variable_get('tripal_analysis_blast_setting',
- array('chado_feature')),
- );
- $form['blast_parser'] = array(
- '#title' => t('Blast Parser Settings'),
- '#type' => 'fieldset',
- '#description' => t('Configure parsers for showing blast results. Each database is '.
- 'allowed to have one xml parser.'),
- '#weight' => 10
- );
- $previous_db = tripal_db_set_active('chado');
-
- $sql = 'SELECT db_id, name FROM {db} ORDER BY lower(name)';
- $results = db_query ($sql);
- $blastdbs = array();
- while ($db = db_fetch_object($results)){
- $blastdbs[$db->db_id] = $db->name;
- }
- $form['db_options'] = array(
- '#type' => 'value',
- '#value' => $blastdbs
- );
- $form['blast_parser']['blastdb'] = array(
- '#title' => t('Database'),
- '#type' => 'select',
- '#description' => t('The database used for the blast analysis.'),
- '#options' => $form['db_options']['#value'],
- '#attributes' => array(
- 'onChange' => "return tripal_update_regex(this)",
- )
- );
- $form['blast_parser']['displayname'] = array(
- '#title' => t('Title for the blast analysis'),
- '#type' => 'textfield',
- );
- $form['blast_parser']['gb_style_parser'] = array(
- '#title' => t('Use Genebank style parser. This will clear all regular expression settings for the selected database.'),
- '#type' => 'checkbox',
- '#attributes' => array(
- 'onClick' => "return tripal_set_genbank_style(this)",
- )
- );
- $form['blast_parser']['hit_id'] = array(
- '#title' => t('Regular expression for Hit Name'),
- '#type' => 'textfield',
- );
- $form['blast_parser']['hit_def'] = array(
- '#title' => t('Regular expression for Hit Description'),
- '#type' => 'textfield',
- );
- $form['blast_parser']['hit_accession'] = array(
- '#title' => t('Regular expression for Hit Accession'),
- '#type' => 'textfield',
- );
- $form['blast_parser']['button'] = array(
- '#type' => 'submit',
- '#value' => t('Save settings')
- );
- tripal_db_set_active($previous_db);
- $settings->form = $form;
- $settings->title = "Tripal Blast";
- return $settings;
- }
- function tripal_analysis_blast_parseXMLFile ($analysis_id, $blastdb, $blastfile, $no_parsed, $job_id) {
-
- $filename = preg_replace("/.*\/(.*)/", "$1", $blastfile);
- $logfile = file_directory_path() . "/tripal/tripal_analysis_blast/load_$filename.log";
- $log = fopen($logfile, 'a');
-
- if (is_file($blastfile)) {
-
- print "Parsing File:".$blastfile." ...\n";
- fwrite($log, date("D M j G:i:s Y").". Loading $blastfile\n");
- if ($no_parsed == 'all') {
- print "Parsing all hits...\n";
- } else {
- print "Parsing top $no_parsed hits...\n";
- }
-
-
-
- $previous_db = tripal_db_set_active('chado');
- $sql = "SELECT CVT.cvterm_id FROM {cvterm} CVT ".
- "INNER JOIN cv ON cv.cv_id = CVT.cv_id ".
- "WHERE CVT.name = 'analysis_blast_output_iteration_hits' ".
- "AND CV.name = 'tripal'";
- $type_id = db_result(db_query($sql));
-
- $blastoutput = simplexml_load_file($blastfile);
- $no_iterations = 0;
- foreach($blastoutput->children() as $tmp) {
- if ($tmp->getName() == 'BlastOutput_iterations') {
- foreach($tmp->children() as $itr) {
- if ($itr->getName() == 'Iteration') {
- $no_iterations ++;
- }
- }
- }
- }
- print "$no_iterations iterations to be processed.\n";
-
- $interval = intval($no_iterations * 0.01);
- $idx_iterations = 0;
- foreach ($blastoutput->children() as $blastoutput_tags) {
- if ($blastoutput_tags->getName() == 'BlastOutput_iterations') {
- foreach($blastoutput_tags->children() as $iterations) {
- if ($iterations->getName() == 'Iteration') {
-
- $idx_iterations ++;
- if ($idx_iterations % $interval == 0) {
- $percentage = (int) ($idx_iterations / $no_iterations * 100);
- tripal_db_set_active($previous_db);
- tripal_job_set_progress($job_id, $percentage);
- $previous_db = tripal_db_set_active('chado');
- print $percentage."% ";
- }
-
-
- $feature_id = 0;
- foreach($iterations->children() as $iteration_tags) {
-
-
- $featurenaem_xml = '';
- if($iteration_tags->getName() == 'Iteration_query-def'){
-
-
- if (preg_match("/^(\d+)\|.+/", $iteration_tags, $matches)) {
- $feature_id = $matches[1];
-
- } else {
-
- $first_word = $iteration_tags;
- if (preg_match('/^(.*?)\s.*$/', $iteration_tags, $matches)) {
- $first_word = $matches[1];
- }
-
- $sql = "SELECT count(feature_id) FROM {feature} ".
- "WHERE uniquename = '%s' ";
- $no_features = db_result(db_query($sql, $first_word));
-
- if ($no_features == 1) {
- $sql = "SELECT feature_id FROM {feature} ".
- "WHERE uniquename = '%s' ";
- $feature_id = db_result(db_query($sql, $first_word));
-
- } else if ($no_features > 1) {
- fwrite($log, "Ambiguous: ".$first_word." matches more than one feature and is not processed.\n");
- continue;
-
- } else {
- fwrite($log, "Failed: ".$first_word."\n");
- }
- }
-
- if ($feature_id) {
- fwrite($log, "Succeeded: ".$first_word." => feature id:".$feature_id);
- $featurename_xml = $iteration_tags->asXML();
- }
-
- } else if($iteration_tags->getName() == 'Iteration_hits'){
- if ($feature_id) {
-
- $sql = "SELECT analysisfeatureprop_id FROM {analysisfeatureprop} AFP ".
- "INNER JOIN analysisfeature AF ON AF.analysisfeature_id = AFP.analysisfeature_id ".
- "WHERE feature_id=%d ".
- "AND analysis_id=%d ".
- "AND type_id=%d ";
- $result = db_query($sql, $feature_id, $analysis_id, $type_id);
- $analysisfeatureprop = db_fetch_object($result);
- $xml_content = "<Iteration>\n".$featurename_xml."\n";
-
- if ($no_parsed == 'all') {
- $xml_content .= $iteration_tags->asXML();
-
-
- } else {
- $counter = 0;
- $xml_content .= "<Iteration_hits>\n";
- foreach ($iteration_tags->children() As $hit) {
- if ($counter < $no_parsed) {
- $xml_content .= $hit->asXML();
- } else {
- break;
- }
- $counter ++;
- }
- $xml_content .= "</Iteration_hits>";
- }
- $xml_content .= "\n</Iteration>";
-
- if ($analysisfeatureprop) {
- $sql = "UPDATE {analysisfeatureprop} ".
- "SET value = '%s' ".
- "WHERE analysisfeatureprop_id = %d ";
-
- db_query($sql, $xml_content, $analysisfeatureprop->analysisfeatureprop_id);
- fwrite($log, " (Update)\n");
-
- } else {
-
-
-
- $sql = "INSERT INTO {analysisfeature} (feature_id, analysis_id) ".
- "VALUES (%d, %d)";
- db_query ($sql, $feature_id, $analysis_id);
-
- $sql = "SELECT analysisfeature_id FROM {analysisfeature} WHERE feature_id = %d AND analysis_id = %d";
- $analysisfeature_id = db_result(db_query($sql, $feature_id, $analysis_id));
-
-
-
- $sql = "INSERT INTO {analysisfeatureprop} (analysisfeature_id, type_id, value, rank)".
- "VALUES (%d, %d, '%s', %d)";
- db_query($sql, $analysisfeature_id, $type_id, $xml_content, '0');
- fwrite($log, " (Insert)\n");
- }
- }
- }
- }
- }
- }
- }
- }
- tripal_db_set_active ($previous_db);
-
- } else {
- $dir_handle = @opendir($blastfile) or die("Unable to open $blastfile");
- $pattern = sql_regcase($blastfile . "/*.XML");
- $total_files = count(glob($pattern));
- print "$total_files file(s) to be parsed.\n";
- if ($no_parsed == 'all') {
- print "Parsing all hits...\n";
- } else {
- print "Parsing top $no_parsed hits...\n";
- }
- $interval = intval($total_files * 0.01);
- $no_file = 0;
-
-
- $previous_db = tripal_db_set_active('chado');
- $sql = "SELECT CVT.cvterm_id FROM {cvterm} CVT ".
- "INNER JOIN cv ON cv.cv_id = CVT.cv_id ".
- "WHERE CVT.name = 'analysis_blast_output_iteration_hits' ".
- "AND CV.name = 'tripal'";
- $type_id = db_result(db_query($sql));
-
-
- while ($file = readdir($dir_handle)) {
- if(preg_match("/^.*\.XML/i",$file)){
-
- if ($no_file % $interval == 0) {
- $percentage = (int) ($no_file / $total_files * 100);
- tripal_db_set_active($previous_db);
- tripal_job_set_progress($job_id, $percentage);
- $previous_db = tripal_db_set_active('chado');
- print $percentage."% ";
- }
-
- print "Parsing File:".$file.". ";
- fwrite($log, date("D M j G:i:s Y").". Loading $file\n");
-
- $blastoutput = simplexml_load_file($blastfile."/".$file);
- $no_iterations = 0;
- foreach($blastoutput->children() as $tmp) {
- if ($tmp->getName() == 'BlastOutput_iterations') {
- foreach($tmp->children() as $itr) {
- if ($itr->getName() == 'Iteration') {
- $no_iterations ++;
- }
- }
- }
- }
- print "$no_iterations iterations to be processed.\n";
- foreach ($blastoutput->children() as $blastoutput_tags) {
- if ($blastoutput_tags->getName() == 'BlastOutput_iterations') {
- foreach($blastoutput_tags->children() as $iterations) {
- if ($iterations->getName() == 'Iteration') {
-
-
- $feature_id = 0;
- foreach($iterations->children() as $iteration_tags) {
-
-
- $featurenaem_xml = '';
- if($iteration_tags->getName() == 'Iteration_query-def'){
-
-
- if (preg_match("/^(\d+)\|.+/", $iteration_tags, $matches)) {
- $feature_id = $matches[1];
-
- } else {
-
- $first_word = $iteration_tags;
- if (preg_match('/^(.*?)\s.*$/', $iteration_tags, $matches)) {
- $first_word = $matches[1];
- }
-
- $sql = "SELECT count(feature_id) FROM {feature} ".
- "WHERE uniquename = '%s' ";
- $no_features = db_result(db_query($sql, $first_word));
-
- if ($no_features == 1) {
- $sql = "SELECT feature_id FROM {feature} ".
- "WHERE uniquename = '%s' ";
- $feature_id = db_result(db_query($sql, $first_word));
-
- } else if ($no_features > 1) {
- fwrite($log, "Ambiguous: ".$first_word." matches more than one feature and is not processed.\n");
- continue;
-
- } else {
- fwrite($log, "Failed: ".$first_word."\n");
- }
- }
-
- if ($feature_id) {
- fwrite($log, "Succeeded: ".$first_word." => feature id:".$feature_id);
- $featurename_xml = $iteration_tags->asXML();
- }
-
- } else if($iteration_tags->getName() == 'Iteration_hits'){
- if ($feature_id) {
-
- $sql = "SELECT analysisfeatureprop_id FROM {analysisfeatureprop} AFP ".
- "INNER JOIN analysisfeature AF ON AF.analysisfeature_id = AFP.analysisfeature_id ".
- "WHERE feature_id=%d ".
- "AND analysis_id=%d ".
- "AND type_id=%d ";
- $result = db_query($sql, $feature_id, $analysis_id, $type_id);
- $analysisfeatureprop = db_fetch_object($result);
- $xml_content = "<Iteration>\n".$featurename_xml."\n";
-
- if ($no_parsed == 'all') {
- $xml_content .= $iteration_tags->asXML();
-
-
- } else {
- $counter = 0;
- $xml_content .= "<Iteration_hits>\n";
- foreach ($iteration_tags->children() As $hit) {
- if ($counter < $no_parsed) {
- $xml_content .= $hit->asXML();
- } else {
- break;
- }
- $counter ++;
- }
- $xml_content .= "</Iteration_hits>";
- }
- $xml_content .= "\n</Iteration>";
-
- if ($analysisfeatureprop) {
- $sql = "UPDATE {analysisfeatureprop} ".
- "SET value = '%s' ".
- "WHERE analysisfeatureprop_id = %d ";
- db_query($sql, $xml_content, $analysisfeatureprop->analysisfeatureprop_id);
- fwrite($log, " (Update)\n");
-
- } else {
-
-
-
- $sql = "INSERT INTO {analysisfeature} (feature_id, analysis_id) ".
- "VALUES (%d, %d)";
- db_query ($sql, $feature_id, $analysis_id);
-
- $sql = "SELECT analysisfeature_id FROM {analysisfeature} WHERE feature_id = %d AND analysis_id = %d";
- $analysisfeature_id = db_result(db_query($sql, $feature_id, $analysis_id));
-
-
-
- $sql = "INSERT INTO {analysisfeatureprop} (analysisfeature_id, type_id, value, rank)".
- "VALUES (%d, %d, '%s', %d)";
- db_query($sql, $analysisfeature_id, $type_id, $xml_content, '0');
- fwrite($log, " (Insert)\n");
- }
- }
- }
- }
- }
- }
- }
- }
- $no_file ++;
- }
- }
- tripal_db_set_active ($previous_db);
- }
- print "Done.\nSuccessful and failed entries have been saved in the log file:\n $logfile\n";
- fwrite($log, "\n");
- fclose($log);
- return;
- }
- function tripal_get_blast_regex ($db_id) {
- $sql = "SELECT * FROM {tripal_analysis_blast} WHERE db_id = %d";
- $blast_regexs = db_fetch_object(db_query($sql, $db_id));
- drupal_json(array(
- 'name' => $blast_regexs->displayname,
- 'genbank_style' => $blast_regexs->genbank_style,
- 'reg1' => $blast_regexs->regex_hit_id,
- 'reg2' => $blast_regexs->regex_hit_def,
- 'reg3' => $blast_regexs->regex_hit_accession)
- );
- }
- function tripal_analysis_blast_node_info() {
- $nodes = array();
- $nodes['chado_analysis_blast'] = array(
- 'name' => t('Analysis: Blast'),
- 'module' => 'chado_analysis_blast',
- 'description' => t('A blast analysis from the chado database'),
- 'has_title' => FALSE,
- 'title_label' => t('Analysis: Blast'),
- 'has_body' => FALSE,
- 'body_label' => t('Blast Analysis Description'),
- 'locked' => TRUE
- );
- return $nodes;
- }
- function chado_analysis_blast_form ($node){
-
- $type = node_get_types('type', $node);
- $form = array();
- $form['title']= array(
- '#type' => 'hidden',
- '#default_value' => $node->title,
- );
- $form['analysisname']= array(
- '#type' => 'textfield',
- '#title' => t('Analysis Name'),
- '#required' => FALSE,
- '#default_value' => $node->analysisname,
- '#weight' => 1
- );
- $form['program']= array(
- '#type' => 'textfield',
- '#title' => t('Program'),
- '#required' => TRUE,
- '#default_value' => $node->program,
- '#weight' => 2
- );
- $form['programversion']= array(
- '#type' => 'textfield',
- '#title' => t('Program Version'),
- '#required' => TRUE,
- '#default_value' => $node->programversion,
- '#weight' => 3
- );
- $form['algorithm']= array(
- '#type' => 'textfield',
- '#title' => t('Algorithm'),
- '#required' => FALSE,
- '#default_value' => $node->algorithm,
- '#weight' => 4
- );
- $form['sourcename']= array(
- '#type' => 'textfield',
- '#title' => t('Source Name'),
- '#required' => FALSE,
- '#default_value' => $node->sourcename,
- '#weight' => 5
- );
- $form['sourceversion']= array(
- '#type' => 'textfield',
- '#title' => t('Source Version'),
- '#required' => FALSE,
- '#default_value' => $node->sourceversion,
- '#weight' => 6
- );
- $form['sourceuri']= array(
- '#type' => 'textfield',
- '#title' => t('Source URI'),
- '#required' => FALSE,
- '#default_value' => $node->sourceuri,
- '#weight' => 7
- );
-
- $default_time = $node->timeexecuted;
- $year = preg_replace("/^(\d+)-\d+-\d+ .*/", "$1", $default_time);
- $month = preg_replace("/^\d+-0?(\d+)-\d+ .*/", "$1", $default_time);
- $day = preg_replace("/^\d+-\d+-0?(\d+) .*/", "$1", $default_time);
-
- if (!$default_time) {
- $default_time = time();
- $year = format_date($default_time, 'custom', 'Y');
- $month = format_date($default_time, 'custom', 'n');
- $day = format_date($default_time, 'custom', 'j');
- }
- $form['timeexecuted']= array(
- '#type' => 'date',
- '#title' => t('Time Executed'),
- '#required' => TRUE,
- '#default_value' => array(
- 'year' => $year,
- 'month' => $month,
- 'day' => $day,
- ),
- '#weight' => 8
- );
- $form['description']= array(
- '#type' => 'textarea',
- '#rows' => 15,
- '#title' => t('Description and/or Program Settings'),
- '#required' => FALSE,
- '#default_value' => check_plain($node->description),
- '#weight' => 9
- );
-
- if (preg_match("/.*\|.*\|.*/",$node->blastdb)) {
- $prop_values = explode("|", $node->blastdb);
- $node->blastdb = $prop_values[0];
- $node->blastfile = $prop_values[1];
- $node->blastparameters = $prop_values[2];
- }
- $form['blast'] = array(
- '#title' => t('Blast Settings'),
- '#type' => 'fieldset',
- '#description' => t('Specific Settings for Blast Analysis.'),
- '#collapsible' => TRUE,
- '#attributes' => array('id' => 'blast-extra-settings'),
- '#weight' => 11
- );
- $previous_db = tripal_db_set_active('chado');
-
- $sql = 'SELECT db_id, name FROM {db} ORDER BY lower(name)';
- $results = db_query ($sql);
- tripal_db_set_active($previous_db);
- $blastdbs = array();
- while ($db = db_fetch_object($results)){
- $blastdbs[$db->db_id] = $db->name;
- }
- $form['db_options'] = array(
- '#type' => 'value',
- '#value' => $blastdbs
- );
- $form['blast']['blastdb'] = array(
- '#title' => t('Database'),
- '#type' => 'select',
- '#description' => t('The database used for the blast analysis.'),
- '#options' => $form['db_options']['#value'],
- '#default_value' => $node->blastdb,
- );
- $form['blast']['blastfile'] = array(
- '#title' => t('Blast xml File: (if you input a directory without the tailing slash, all xml files in the directory will be loaded)'),
- '#type' => 'textfield',
- '#description' => t('The xml output file generated by blast in full path.'),
- '#default_value' => $node->blastfile,
- );
-
- $form['blast']['no_parsed'] = array(
- '#title' => t('Number of hits to be parsed'),
- '#type' => 'textfield',
- '#description' => t("The number of hits to be parsed. Tripal will parse only top 10 hits if you input '10'' in this field."),
- '#default_value' => 'all',
- );
-
- $form['blast']['blastjob'] = array(
- '#type' => 'checkbox',
- '#title' => t('Submit a job to parse the xml output into analysisfeatureprop table'),
- '#description' => t('Note: features associated with the blast results must '.
- 'exist in chado before parsing the file. Otherwise, blast '.
- 'results that cannot be linked to a feature will be '.
- 'discarded. '),
- '#default_value' => $node->blastjob
- );
-
- $form['blast']['blastbesthit'] = array(
- '#type' => 'checkbox',
- '#title' => t('Submit a job to parse the best hit from the xml output so a best hit report can be created'),
- '#description' => t('Note: the xml parser for this blast database needs to be set first. '.
- 'Otherwise, the hit description may contain undesirable content.'),
- '#default_value' => $node->blastbesthit
- );
-
- $form['blast']['blastparameters'] = array(
- '#title' => t('Parameters'),
- '#type' => 'textfield',
- '#description' => t('The parameters for running the blast analysis.'),
- '#default_value' => $node->blastparameters,
- );
- return $form;
- }
- function chado_analysis_blast_validate($node, &$form){
-
-
-
-
-
-
- if($node->nid){
-
-
-
-
- }
- else{
-
-
- if($node->analysis_id){
-
-
-
- }
- else{
-
-
-
-
-
-
-
- $sql = "SELECT analysis_id ".
- "FROM {analysis} ".
- "WHERE program='%s'".
- "AND programversion='%s'".
- "AND sourcename='%s'";
- $previous_db = tripal_db_set_active('chado');
- $analysis_id = db_result(db_query($sql, $node->program, $node->programversion, $node->sourcename));
- tripal_db_set_active($previous_db);
-
- if($analysis_id){
-
-
-
-
-
- $sql = "SELECT nid FROM {chado_analysis} ".
- "WHERE analysis_id = %d";
- $node_id = db_result(db_query($sql, $analysis_id));
- if($node_id){
-
-
-
-
- $error = 'This analysis already exists in the chado database (analysis id ';
- $error .= $analysis_id.') and has been synchronized ';
- $error .= 'with drupal. See node '.$node_id.' if you wish to update that analysis. ';
- $error .= ' For a new analysis, please select a unique primary key ';
- $error .= '(primary key consists of sourcename, program and programversion).';
- form_set_error('sourcename', t($error));
- }
-
- else{
-
-
-
-
- $error = 'This analysis already exists in the chado database (analysis id ';
- $error .= $analysis_id.') but has not been synchronized ';
- $error .= 'with drupal. See the tripal admin pages to synchronize. ';
- $error .= ' For a new analysis, please select a unique primary key ';
- $error .= '(primary key consists of sourcename, program and programversion).';
- form_set_error('sourcename', t($error));
- }
- }
- }
- }
- }
- function chado_analysis_blast_insert($node){
- global $user;
-
- $time = $node->timeexecuted;
- $month = $time['month'];
- $day = $time['day'];
- $year = $time['year'];
- $timestamp = $month.'/'.$day.'/'.$year;
-
-
-
- $sql = "INSERT INTO {analysis} ".
- " (name, description, program, programversion, algorithm, ".
- " sourcename, sourceversion, sourceuri, timeexecuted) ".
- "VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s')";
- $previous_db = tripal_db_set_active('chado');
- db_query($sql,$node->analysisname, $node->description,
- $node->program,$node->programversion,$node->algorithm,
- $node->sourcename, $node->sourceversion, $node->sourceuri,
- $timestamp);
-
- $sql = "SELECT analysis_id ".
- "FROM {analysis} ".
- "WHERE program='%s'".
- "AND programversion='%s'".
- "AND sourcename='%s'";
- $analysis_id = db_result(db_query($sql, $node->program,
- $node->programversion, $node->sourcename));
-
- $sql = "SELECT CVT.cvterm_id FROM {cvterm} CVT ".
- "INNER JOIN cv ON cv.cv_id = CVT.cv_id ".
- "WHERE CVT.name = 'analysis_blast_settings' ".
- "AND CV.name = 'tripal'";
- $type_id = db_result(db_query($sql));
-
-
-
- $sql = "INSERT INTO {analysisprop} (analysis_id, type_id, value) ".
- "VALUES (%d, %d, '%s')";
- $blastsettings = $node->blastdb."|".$node->blastfile."|".$node->blastparameters;
- db_query($sql, $analysis_id, $type_id, $blastsettings);
-
-
- $sql = "SELECT CVT.cvterm_id FROM {cvterm} CVT ".
- "INNER JOIN cv ON cv.cv_id = CVT.cv_id ".
- "WHERE CVT.name = 'analysis_type' ".
- "AND CV.name = 'tripal'";
- $type_id = db_result(db_query($sql));
-
-
-
-
- $sql = "INSERT INTO {analysisprop} (analysis_id, type_id, value) ".
- "VALUES (%d, %d, '%s')";
- $analysis_type = "tripal_analysis_blast";
- db_query($sql, $analysis_id, $type_id, $analysis_type);
-
- tripal_db_set_active($previous_db);
-
-
-
-
- if($node->blastjob) {
- $job_args[0] = $analysis_id;
- $job_args[1] = $node->blastdb;
- $job_args[2] = $node->blastfile;
- $job_args[3] = $node->no_parsed;
- if (is_readable($node->blastfile)) {
- $fname = preg_replace("/.*\/(.*)/", "$1", $node->blastfile);
- tripal_add_job("Parse blast: $fname",'tripal_analysis_blast',
- 'tripal_analysis_blast_parseXMLFile', $job_args, $user->uid);
- } else {
- drupal_set_message("Can not open blast output file. Job not scheduled.");
- }
- }
- if($node->blastbesthit) {
- $j_args[0] = $analysis_id;
- tripal_add_job("Parse best hit: $node->analysisname",'tripal_analysis_blast',
- 'tripal_analysis_blast_parse_best_hit', $j_args, $user->uid);
- }
-
-
- $sql = "INSERT INTO {chado_analysis} (nid, vid, analysis_id) ".
- "VALUES (%d, %d, %d)";
- db_query($sql,$node->nid,$node->vid,$analysis_id);
-
-
- $record = new stdClass();
-
-
- if ($node->analysisname) {
- $record->title = $node->analysisname;
- } else {
-
- $record->title = "$node->program ($node->programversion)";
- }
- $record->nid = $node->nid;
- drupal_write_record('node',$record,'nid');
- drupal_write_record('node_revisions',$record,'nid');
- }
- function chado_analysis_blast_delete($node){
-
-
- $sql_drupal = "SELECT analysis_id ".
- "FROM {chado_analysis} ".
- "WHERE nid = %d ".
- "AND vid = %d";
- $analysis_id = db_result(db_query($sql_drupal, $node->nid, $node->vid));
-
- $sql_del = "DELETE FROM {chado_analysis} ".
- "WHERE nid = %d ".
- "AND vid = %d";
- db_query($sql_del, $node->nid, $node->vid);
- $sql_del = "DELETE FROM {node} ".
- "WHERE nid = %d ".
- "AND vid = %d";
- db_query($sql_del, $node->nid, $node->vid);
- $sql_del = "DELETE FROM {node_revisions} ".
- "WHERE nid = %d ".
- "AND vid = %d";
- db_query($sql_del, $node->nid, $node->vid);
-
- $previous_db = tripal_db_set_active('chado');
- $sql = "SELECT analysisfeature_id FROM {analysisfeature} WHERE analysis_id=%d";
- $results = db_query($sql, $analysis_id);
- while ($af = db_fetch_object($results)) {
- db_query("DELETE FROM {analysisfeatureprop} WHERE analysisfeature_id = %d", $af->analysisfeature_id);
- }
- db_query("DELETE FROM {analysisfeature} WHERE analysis_id = %d", $analysis_id);
- db_query("DELETE FROM {analysisprop} WHERE analysis_id = %d", $analysis_id);
- db_query("DELETE FROM {analysis} WHERE analysis_id = %d", $analysis_id);
- tripal_db_set_active($previous_db);
- }
- function chado_analysis_blast_update($node){
- global $user;
- if($node->revision){
-
- } else {
-
- $time = $node->timeexecuted;
- $month = $time['month'];
- $day = $time['day'];
- $year = $time['year'];
- $timestamp = $month.'/'.$day.'/'.$year;
-
- $sql = "SELECT analysis_id ".
- "FROM {chado_analysis} ".
- "WHERE vid = %d";
- $analysis_id = db_fetch_object(db_query($sql, $node->vid))->analysis_id;
- $sql = "UPDATE {analysis} ".
- "SET name = '%s', ".
- " description = '%s', ".
- " program = '%s', ".
- " programversion = '%s', ".
- " algorithm = '%s', ".
- " sourcename = '%s', ".
- " sourceversion = '%s', ".
- " sourceuri = '%s', ".
- " timeexecuted = '%s' ".
- "WHERE analysis_id = %d ";
- $previous_db = tripal_db_set_active('chado');
- db_query($sql, $node->analysisname, $node->description, $node->program,
- $node->programversion,$node->algorithm,$node->sourcename,
- $node->sourceversion, $node->sourceuri, $timestamp, $analysis_id);
-
- $sql = "SELECT CVT.cvterm_id FROM {cvterm} CVT ".
- "INNER JOIN cv CV ON CV.cv_id = CVT.cv_id ".
- "WHERE CVT.name = 'analysis_blast_settings' ".
- "AND CV.name = 'tripal'";
- $type_id = db_result(db_query($sql));
-
- $sql = "UPDATE {analysisprop} ".
- "SET value = '%s' ".
- "WHERE analysis_id = %d AND type_id = %d";
- $blastsettings = $node->blastdb."|".$node->blastfile."|".$node->blastparameters;
- db_query($sql, $blastsettings, $analysis_id, $type_id);
- tripal_db_set_active($previous_db);
-
- if($node->blastjob) {
- $job_args[0] = $analysis_id;
- $job_args[1] = $node->blastdb;
- $job_args[2] = $node->blastfile;
- $job_args[3] = $node->no_parsed;
- if (is_readable($node->blastfile)) {
- $fname = preg_replace("/.*\/(.*)/", "$1", $node->blastfile);
- tripal_add_job("Parse blast: $fname",'tripal_analysis_blast',
- 'tripal_analysis_blast_parseXMLFile', $job_args, $user->uid);
- } else {
- drupal_set_message("Can not open blast output file. Job not scheduled.");
- }
- }
- if($node->blastbesthit) {
- $j_args[0] = $analysis_id;
- tripal_add_job("Parse best hit: $node->analysisname",'tripal_analysis_blast',
- 'tripal_analysis_blast_parse_best_hit', $j_args, $user->uid);
- }
-
-
- $record = new stdClass();
-
-
- if ($node->analysisname) {
- $record->title = $node->analysisname;
- } else {
-
- $record->title = "$node->program ($node->programversion)";
- }
- $record->nid = $node->nid;
- drupal_write_record('node',$record,'nid');
- drupal_write_record('node_revisions',$record,'nid');
- }
- }
- function chado_analysis_blast_load($node){
-
- $sql = "SELECT analysis_id FROM {chado_analysis} WHERE nid = %d";
- $ana_node = db_fetch_object(db_query($sql, $node->nid));
- $additions = new stdClass();
- if ($ana_node) {
-
- $sql = "SELECT Analysis_id, name AS analysisname, description, program, ".
- " programversion, algorithm, sourcename, sourceversion, ".
- " sourceuri, timeexecuted ".
- "FROM {Analysis} ".
- "WHERE Analysis_id = $ana_node->analysis_id";
- $previous_db = tripal_db_set_active('chado');
- $additions = db_fetch_object(db_query($sql));
-
- $sql = "SELECT count(feature_id) as featurecount ".
- "FROM {Analysisfeature} ".
- "WHERE Analysis_id = %d";
- $additions->featurecount = db_result(db_query($sql, $ana_node->analysis_id));
-
- $sql = "SELECT CVT.cvterm_id FROM {cvterm} CVT ".
- "INNER JOIN cv ON cv.cv_id = CVT.cv_id ".
- "WHERE CVT.name = 'analysis_blast_settings' ".
- "AND CV.name = 'tripal'";
- $type_id = db_result(db_query($sql));
-
- $sql = "SELECT value FROM {analysisprop} ".
- "WHERE analysis_id = %d ".
- "AND type_id = %d";
- $analysisprop = db_result(db_query($sql, $ana_node->analysis_id, $type_id));
- $prop_values = explode ("|", $analysisprop, 1);
- $additions->blastdb = $prop_values[0];
- $additions->blastfile = $prop_values[1];
- $additions->blastparameters = $prop_values[2];
- tripal_db_set_active($previous_db);
- }
-
-
- if ($additions->analysisname) {
- $additions->title = $additions->analysisname;
- } else {
-
- $additions->title = "$additions->program ($additions->programversion)";
- }
-
- return $additions;
- }
- function chado_analysis_blast_view ($node, $teaser = FALSE, $page = FALSE) {
-
-
- if (!$teaser) {
- $node = node_prepare($node, $teaser);
-
-
- $time = $node->timeexecuted;
- if(is_array($time)){
- $month = $time['month'];
- $day = $time['day'];
- $year = $time['year'];
- $timestamp = $year.'-'.$month.'-'.$day;
- $node->timeexecuted = $timestamp;
- }
-
-
- if (preg_match("/.*\|.*\|.*/",$node->blastdb)) {
- $prop_values = explode("|", $node->blastdb);
- $node->blastdb = $prop_values[0];
- $node->blastfile = $prop_values[1];
- $node->blastparameters = $prop_values[2];
- }
- }
- return $node;
- }
- function tripal_analysis_blast_perm(){
- return array(
- 'access chado_analysis_blast content',
- 'create chado_analysis_blast content',
- 'delete chado_analysis_blast content',
- 'edit chado_analysis_blast content',
- );
- }
- function chado_analysis_blast_access($op, $node, $account){
- if ($op == 'create') {
- return user_access('create chado_analysis_blast content', $account);
- }
- if ($op == 'update') {
- if (user_access('edit chado_analysis_blast content', $account)) {
- return TRUE;
- }
- }
- if ($op == 'delete') {
- if (user_access('delete chado_analysis_blast content', $account)) {
- return TRUE;
- }
- }
- if ($op == 'view') {
- if (user_access('access chado_analysis_blast content', $account)) {
- return TRUE;
- }
- }
- return FALSE;
- }
- function tripal_analysis_blast_get_result_object($xml_string,$db,$max,$feature_id, $analysis) {
- $blast_object = new stdClass();
-
- $sql = "SELECT * FROM {tripal_analysis_blast} WHERE db_id = %d";
- $parser = db_fetch_object(db_query($sql, $db->db_id));
- $db_name = $parser->displayname;
- $is_genbank = $parser->genbank_style;
- $regex_hit_id = $parser->regex_hit_id;
- $regex_hit_def = $parser->regex_hit_def;
- $regex_hit_accession = $parser->regex_hit_accession;
-
- if(!$regex_hit_id){
- $regex_hit_id = '/^(.*?)\s.*$/';
- } else {
- $regex_hit_id = '/'.$regex_hit_id.'/';
- }
- if(!$regex_hit_def){
- $regex_hit_def = '/^.*?\s(.*)$/';
- } else {
- $regex_hit_def = '/'.$regex_hit_def.'/';
- }
- if(!$regex_hit_accession){
- $regex_hit_accession = '/^(.*?)\s.*$/';
- } else {
- $regex_hit_accession = '/'.$regex_hit_accession.'/';
- }
-
- $blast_object->analysis = $analysis;
- $blast_object->db = $db;
- if (!$db_name) {
- $blast_object->title = $analysis->name;
- } else {
- $blast_object->title = $db_name;
- }
-
- $ana_nid = db_result(db_query("SELECT nid FROM {chado_analysis} WHERE analysis_id = %d", $analysis->aid));
- $blast_object->ana_nid = $ana_nid;
- $blast_object->ana_time = $analysis->time;
- $blast_object->ana_name = $analysis->name;
-
-
-
-
- $xml_output = simplexml_load_string($xml_string);
- $iteration = '';
-
- if ($xml_output->getName() == 'Iteration') {
- foreach ($xml_output->children() as $xml_tag) {
- if ($xml_tag->getName() == 'Iteration_query-def') {
-
- $blast_object->xml_tag = $xml_tag;
- } else if ($xml_tag->getName() == 'Iteration_hits') {
- $iteration = $xml_tag;
- }
- }
-
- } else {
- $iteration = $xml_output;
- }
- $number_hits = 0;
- foreach($iteration->children() as $hits){
- $number_hits ++;
- }
-
- $blast_object->max = $max;
- $blast_object->number_hits = $number_hits;
- $blast_object->feature_id = $feature_id;
-
- $hits_array = array();
- $hit_count = 0;
- foreach($iteration->children() as $hits){
- $hsp_array = array();
- $counter = 0;
- foreach($hits->children() as $hit){
- $best_evalue = 0;
- $best_identity = 0;
- $best_len = 0;
- $element_name = $hit->getName();
- if($element_name == 'Hit_id'){
-
- if ($is_genbank) {
- $hit_name = $hit;
- }
- } else if($element_name == 'Hit_def'){
- if($is_genbank){
- $description = $hit;
- } else {
- $accession = preg_replace($regex_hit_accession,"$1",$hit);
- $hit_name = preg_replace($regex_hit_id,"$1",$hit);
- $description = preg_replace($regex_hit_def,"$1",$hit);
- }
- } else if($element_name == 'Hit_accession'){
-
- if ($is_genbank){
- $accession = $hit;
- }
-
- } else if($element_name == 'Hit_hsps'){
- foreach($hit->children() as $hsp){
- foreach($hsp->children() as $hsp_info){
- $element_name = $hsp_info->getName();
- if($element_name == 'Hsp_num'){
- $hsp_num = $hsp_info;
- }
- if($element_name == 'Hsp_bit-score'){
- $hsp_bit_score = $hsp_info;
- }
- if($element_name == 'Hsp_score'){
- $hsp_score = $hsp_info;
- }
- if($element_name == 'Hsp_evalue'){
- $hsp_evalue = $hsp_info;
-
-
-
- if(!$best_evalue){
- $best_evalue = $hsp_evalue;
- }
- }
- if($element_name == 'Hsp_query-from'){
- $hsp_query_from = $hsp_info;
- }
- if($element_name == 'Hsp_query-to'){
- $hsp_query_to = $hsp_info;
- }
- if($element_name == 'Hsp_hit-from'){
- $hsp_hit_from = $hsp_info;
- }
- if($element_name == 'Hsp_hit-to'){
- $hsp_hit_to = $hsp_info;
- }
- if($element_name == 'Hsp_query-frame'){
- $hsp_query_frame = $hsp_info;
- }
- if($element_name == 'Hsp_identity'){
- $hsp_identity = $hsp_info;
-
-
-
- if(!$best_identity){
- $best_identity = $hsp_identity;
- }
- }
- if($element_name == 'Hsp_positive'){
- $hsp_positive = $hsp_info;
- }
- if($element_name == 'Hsp_align-len'){
- $hsp_align_len = $hsp_info;
-
-
-
- if(!$best_len){
- $best_len = $hsp_align_len;
- }
- }
- if($element_name == 'Hsp_qseq'){
- $hsp_qseq = $hsp_info;
- }
- if($element_name == 'Hsp_hseq'){
- $hsp_hseq = $hsp_info;
- }
- if($element_name == 'Hsp_midline'){
- $hsp_midline = $hsp_info;
- }
- }
- $hsp_content = array();
- $hsp_content['hsp_num'] = $hsp_num;
- $hsp_content['bit_score'] = $hsp_bit_score;
- $hsp_content['score'] = $hsp_score;
- $hsp_content['evalue'] = $hsp_evalue;
- $hsp_content['query_frame'] = $hsp_query_frame;
- $hsp_content['qseq'] = $hsp_qseq;
- $hsp_content['midline'] = $hsp_midline;
- $hsp_content['hseq'] = $hsp_hseq;
- $hsp_content['hit_from'] = $hsp_hit_from;
- $hsp_content['hit_to'] = $hsp_hit_to;
- $hsp_content['identity'] = $hsp_identity;
- $hsp_content['align_len'] = $hsp_align_len;
- $hsp_content['positive'] = $hsp_positive;
- $hsp_content['query_from'] = $hsp_query_from;
- $hsp_content['query_to'] = $hsp_query_to;
- $hsp_array[$counter] = $hsp_content;
- $counter ++;
- }
- }
- }
- $arrowr_url = url(drupal_get_path('theme', 'tripal')."/images/arrow_r.png");
- $hits_array[$hit_count]['arrowr_url'] = $arrowr_url;
- $hits_array[$hit_count]['accession'] = $accession;
- $hits_array[$hit_count]['hit_name'] = $hit_name;
-
- if($accession && $db->urlprefix){
- $hits_array[$hit_count]['hit_url'] = "$db->urlprefix$accession";
- } else {
-
- $sql = "SELECT feature_id FROM {feature} WHERE uniquename = '%s'";
- $previous_db = db_set_active('chado');
- $hit_feature_id = db_result(db_query($sql, $hit_name));
- db_set_active($previous_db);
-
- if ($hit_feature_id) {
- $hits_array[$hit_count]['hit_url'] = "ID$hit_feature_id";
- }
- }
-
- $hits_array[$hit_count]['best_evalue'] = $best_evalue;
- $percent_identity = number_format($best_identity/$best_len*100, 2);
- $hits_array[$hit_count]['percent_identity'] = $percent_identity;
- $hits_array[$hit_count]['description'] = $description;
- $hits_array[$hit_count]['hsp'] = $hsp_array;
- $hit_count ++;
-
-
- if($max > 0 && $hit_count >= $max){
- break;
- }
- }
- $blast_object->hits_array = $hits_array;
- return $blast_object;
- }
- function tripal_analysis_blast_parse_best_hit ($analysis_id) {
-
- $sql = "SELECT feature_id
- FROM {analysisfeature} AF
- WHERE analysis_id = %d";
- $previous_db = tripal_db_set_active('chado');
- $result = db_query($sql, $analysis_id);
- $featureSet = array ();
- $counter = 0;
- while ($feature = db_fetch_object($result)) {
- $featureSet [$counter] = $feature->feature_id;
- $counter ++;
- }
-
- $sql = "SELECT value, name, to_char(timeexecuted, 'MM-DD-YYYY') AS time
- FROM {analysis} A
- INNER JOIN {analysisprop} AP ON A.analysis_id = AP.analysis_id
- WHERE A.analysis_id = %d
- AND type_id= (SELECT cvterm_id
- FROM {cvterm}
- WHERE name = 'analysis_blast_settings')";
- $analysis = db_fetch_object(db_query($sql, $analysis_id));
-
-
- $blastsettings = explode("|", $analysis->value);
- $db_id = $blastsettings [0];
-
-
- tripal_db_set_active($previous_db);
- $sql = "SELECT * FROM {tripal_analysis_blast} WHERE db_id = %d";
- $parser = db_fetch_object(db_query($sql, $db_id));
- $db_name = $parser->displayname;
- $is_genbank = $parser->genbank_style;
- $regex_hit_id = $parser->regex_hit_id;
- $regex_hit_def = $parser->regex_hit_def;
- $regex_hit_accession = $parser->regex_hit_accession;
-
- if(!$regex_hit_id){
- $regex_hit_id = '/^(.*?)\s.*$/';
- } else {
- $regex_hit_id = '/'.$regex_hit_id.'/';
- }
- if(!$regex_hit_def){
- $regex_hit_def = '/^.*?\s(.*)$/';
- } else {
- $regex_hit_def = '/'.$regex_hit_def.'/';
- }
- if(!$regex_hit_accession){
- $regex_hit_accession = '/^(.*?)\s.*$/';
- } else {
- $regex_hit_accession = '/'.$regex_hit_accession.'/';
- }
-
- $interval = intval($counter * 0.01);
- for ($i = 0; $i < $counter; $i ++) {
-
- if ($i !=0 && $i % $interval == 0) {
- $percentage = (int) ($i / $counter * 100);
- tripal_job_set_progress($job_id, $percentage);
- print $percentage."% ";
- }
- $sql = "SELECT value
- FROM {analysisfeatureprop} AFP
- INNER JOIN {analysisfeature} AF ON AFP.analysisfeature_id = AF.analysisfeature_id
- WHERE analysis_id = %d
- AND feature_id = %d
- AND type_id = (SELECT cvterm_id FROM cvterm WHERE name='analysis_blast_output_iteration_hits' AND cv_id = (SELECT cv_id FROM cv WHERE name='tripal'))";
- $previous_db = tripal_db_set_active('chado');
- $xml_output = simplexml_load_string(db_result(db_query($sql, $analysis_id, $featureSet[$i])));
-
- $iteration = '';
-
- if ($xml_output->getName() == 'Iteration') {
- $query = "";
- foreach ($xml_output->children() as $xml_tag) {
- if ($xml_tag->getName() == 'Iteration_query-def') {
-
- $query = $xml_tag;
- } else if ($xml_tag->getName() == 'Iteration_hits') {
- $iteration = $xml_tag;
- }
- }
-
- } else {
- $iteration = $xml_output;
- }
- $number_hits = 0;
- foreach($iteration->children() as $hits){
- $number_hits ++;
- }
- $query = explode(" ", $query) ;
- $query = $query [0];
- if ($number_hits == 0) {
- continue;
- }
-
-
- foreach($iteration->children() as $hits){
- $hit_count++;
- foreach($hits->children() as $hit){
- $best_evalue = 0;
- $best_identity = 0;
- $best_len = 0;
- $element_name = $hit->getName();
- if($element_name == 'Hit_id'){
-
- if ($is_genbank) {
- $hit_name = $hit;
- }
- } else if($element_name == 'Hit_def'){
- if($is_genbank){
- $description = $hit;
- } else {
- $accession = preg_replace($regex_hit_accession,"$1",$hit);
- $hit_name = preg_replace($regex_hit_id,"$1",$hit);
- $description = preg_replace($regex_hit_def,"$1",$hit);
- }
- } else if($element_name == 'Hit_accession'){
-
- if ($is_genbank){
- $accession = $hit;
- }
-
- } else if($element_name == 'Hit_hsps'){
- foreach($hit->children() as $hsp){
- foreach($hsp->children() as $hsp_info){
- $element_name = $hsp_info->getName();
- if($element_name == 'Hsp_num'){
- $hsp_num = $hsp_info;
- }
- if($element_name == 'Hsp_bit-score'){
- $hsp_bit_score = $hsp_info;
- }
- if($element_name == 'Hsp_score'){
- $hsp_score = $hsp_info;
- }
- if($element_name == 'Hsp_evalue'){
- $hsp_evalue = $hsp_info;
-
-
-
- if(!$best_evalue){
- $best_evalue = $hsp_evalue;
- }
- }
- if($element_name == 'Hsp_query-from'){
- $hsp_query_from = $hsp_info;
- }
- if($element_name == 'Hsp_query-to'){
- $hsp_query_to = $hsp_info;
- }
- if($element_name == 'Hsp_hit-from'){
- $hsp_hit_from = $hsp_info;
- }
- if($element_name == 'Hsp_hit-to'){
- $hsp_hit_to = $hsp_info;
- }
- if($element_name == 'Hsp_query-frame'){
- $hsp_query_frame = $hsp_info;
- }
- if($element_name == 'Hsp_identity'){
- $hsp_identity = $hsp_info;
-
-
-
- if(!$best_identity){
- $best_identity = $hsp_identity;
- }
- }
- if($element_name == 'Hsp_positive'){
- $hsp_positive = $hsp_info;
- }
- if($element_name == 'Hsp_align-len'){
- $hsp_align_len = $hsp_info;
-
-
-
- if(!$best_len){
- $best_len = $hsp_align_len;
- }
- }
- if($element_name == 'Hsp_qseq'){
- $hsp_qseq = $hsp_info;
- }
- if($element_name == 'Hsp_hseq'){
- $hsp_hseq = $hsp_info;
- }
- if($element_name == 'Hsp_midline'){
- $hsp_midline = $hsp_info;
- }
- }
- }
- }
- }
-
-
- $sql = "SELECT analysisfeature_id FROM {analysisfeature} WHERE analysis_id = %d AND feature_id = %d";
- $af_id = db_result(db_query($sql, $analysis_id, $featureSet[$i]));
-
-
- $sql = "SELECT cvterm_id FROM {cvterm} WHERE name = '%s' AND cv_id = (SELECT cv_id FROM {cv} WHERE name = 'tripal')";
- $type_id = db_result(db_query($sql, 'analysis_blast_besthit_query'));
-
- $sql_test ="SELECT analysisfeatureprop_id FROM {analysisfeatureprop} WHERE analysisfeature_id = $af_id AND type_id = %d";
- $test_afpid = db_result(db_query($sql_test, $type_id));
-
-
- if (!$test_afpid) {
- $afp_sql = "INSERT INTO {analysisfeatureprop} (analysisfeature_id, type_id, value, rank) VALUES (%d, %d, '%s', 0)";
-
-
- db_query($afp_sql, $af_id, $type_id, $query);
-
- $type_id = db_result(db_query($sql, 'analysis_blast_besthit_match'));
- db_query($afp_sql, $af_id, $type_id, $hit_name);
-
-
- $type_id = db_result(db_query($sql, 'analysis_blast_besthit_description'));
- db_query($afp_sql, $af_id, $type_id, $description);
-
-
- $type_id = db_result(db_query($sql, 'analysis_blast_besthit_evalue'));
- $e_digit = explode("e-", $best_evalue);
- if (count($e_digit) == 2) {
- $evalue_shown = number_format($e_digit [0],1);
- $best_evalue = $evalue_shown."e-".$e_digit[1];
- }
- db_query($afp_sql, $af_id, $type_id, $best_evalue);
-
-
- $type_id = db_result(db_query($sql, 'analysis_blast_besthit_identity'));
- $percent_identity = number_format($best_identity/$best_len*100, 1);
- db_query($afp_sql, $af_id, $type_id, $percent_identity);
-
-
- $type_id = db_result(db_query($sql, 'analysis_blast_besthit_length'));
- db_query($afp_sql, $af_id, $type_id, $best_len);
-
-
- } else {
- $afp_sql = "UPDATE {analysisfeatureprop} SET analysisfeature_id = %d, type_id = %d, value = '%s', rank = 0 WHERE analysisfeatureprop_id = %d";
-
- db_query($afp_sql, $af_id, $type_id, $query, $test_afpid);
-
-
- $type_id = db_result(db_query($sql, 'analysis_blast_besthit_match'));
- $test_afpid = db_result(db_query($sql_test, $type_id));
- db_query($afp_sql, $af_id, $type_id, $hit_name, $test_afpid);
-
- $type_id = db_result(db_query($sql, 'analysis_blast_besthit_description'));
- $test_afpid = db_result(db_query($sql_test, $type_id));
- db_query($afp_sql, $af_id, $type_id, $description, $test_afpid);
-
-
- $type_id = db_result(db_query($sql, 'analysis_blast_besthit_evalue'));
- $test_afpid = db_result(db_query($sql_test, $type_id));
- $e_digit = explode("e-", $best_evalue);
- if (count($e_digit) == 2) {
- $evalue_shown = number_format($e_digit [0],1);
- $best_evalue = $evalue_shown."e-".$e_digit[1];
- }
- db_query($afp_sql, $af_id, $type_id, $best_evalue, $test_afpid);
-
-
- $type_id = db_result(db_query($sql, 'analysis_blast_besthit_identity'));
- $test_afpid = db_result(db_query($sql_test, $type_id));
- $percent_identity = number_format($best_identity/$best_len*100, 1);
- db_query($afp_sql, $af_id, $type_id, $percent_identity, $test_afpid);
-
-
- $type_id = db_result(db_query($sql, 'analysis_blast_besthit_length'));
- $test_afpid = db_result(db_query($sql_test, $type_id));
- db_query($afp_sql, $af_id, $type_id, $best_len, $test_afpid);
-
- }
- tripal_db_set_active($previous_db);
-
- break;
- }
- }
- print "100%\n";
- return;
- }
|