PowerShell Remoting Project Home

Sunday, January 08, 2006

GUI version of NCBI Blastn(nr) MSH script

An NCBI Blastn (using nr database) client MSH script with winform GUI. See NCBI Blastn under MSH command line for a introduction of NCBI Blastn program.

Although personally I like command line interface, there are always some users prefer GUI. Well, since monad can use System.Windows.Forms.form, it can create comman winform GUI which is similar to what tk/tcl can do.

One of the big advantage of monad is that script block can be used a event handler.

Another thing need to mention is that monad use grave-accent(`) but not "\" for escape sequences. To get more help on escape character using command:
help about_escape_character
Because NCBI Blast server running unix like OS, so the returning results using \n but not \r\n for a new line. We have to replace those \n with \r\n to get a proper view in textbox control.



# Blastn-GUI.msh
# ===========================================================================
#
# This code is for test purposes only. Use it at your own risk.
#
# Please do not submit or retrieve more than one request every two seconds.
#
# Results will be kept at NCBI for 24 hours. For best batch performance,
# It is recommend that you submit requests after 2000 EST (0100 GMT) and
# retrieve results before 0500 EST (1000 GMT).
#
# by tony 2006 http://mshforfun.blogspot.com/
# ===========================================================================
[void] [Reflection.Assembly]::Load( `
"System.Windows.Forms, Version=2.0.0.0, Culture=neutral, `
PublicKeyToken=b77a5c561934e089")
$script:form = new-object System.Windows.Forms.form
$Panel = new-object System.Windows.Forms.Panel
$Label = new-object System.Windows.Forms.Label
$Query = new-object System.Windows.Forms.TextBox
$Submit = new-object System.Windows.Forms.Button
$script:Results = new-object System.Windows.Forms.TextBox
$script:Status = new-object System.Windows.Forms.TextBox

function SubmitQuery([String]$Sequence)
{
 if ($Sequence -match "[^ATGCatgcNn]")
 {
    $Status.Text += "Error 1: Invalid charactors in query sequence! Only A/T/G/C/N can be used!`r`n`r`n"
    $Status.Refresh()
    $Status.ScrollToCaret()
    return
 }
 
 $Status.Text += "`r`n==================================================`r`n"
 $Status.Text += (get-date).ToString("MM-dd-yyyy HH:mm") + ": Submiting sequence...`r`n"
 $Status.Refresh()
 $Status.ScrollToCaret()

 $uri="http://www.ncbi.nlm.nih.gov/blast/Blast.cgi?CMD=Put&PROGRAM=blastn&DATABASE=nr&QUERY=" + $Sequence
 $BlastClient = new-object System.Net.WebClient
 $pagecontent = $BlastClient.DownloadString($uri);
 if ($pagecontent -match "   RID = (.*)")
 {
   $RID=$Matches[1]
   $Status.Text += "RID =" + $RID + "`r`n"
   $Status.Refresh()
   $Status.ScrollToCaret()
  }

 if ($pagecontent -match "   RTOE = (.*)")
 {
  $TimeToComplete= $Matches[1]
  $Status.Text += "Estimated Time to finish searching: " + $TimeToComplete + " seconds`r`nWaiting...`r`n`r`n"
  $Status.Refresh()
  $Status.ScrollToCaret()
 }

 Start-sleep $TimeToComplete

 While ($true)
 {
  
   $uri= "http://www.ncbi.nlm.nih.gov/blast/Blast.cgi?CMD=Get&FORMAT_OBJECT=SearchInfo&RID=" + $RID   
   $pagecontent = $BlastClient.DownloadString($uri);
   if ($pagecontent -match "Status=WAITING")
   {
    $Status.Text += (get-date).ToString("MM-dd-yyyy HH:mm") + ": Search not finished yet, waiting... `r`n"
    $Status.Refresh()
    $Status.ScrollToCaret()
    Start-sleep 5
    continue
   }
   if ($pagecontent -match "Status=FAILED")
   {
     $Status.Text += (get-date).ToString("MM-dd-yyyy HH:mm") + ": Error 2 -- Search failed!`r`n`r`n"
     $Status.Refresh()
     return
   }
   if ($pagecontent -match "Status=UNKNOWN")
   {
     $Status.Text += (get-date).ToString("MM-dd-yyyy HH:mm") + ": Error 3-- Search expired!`r`n`r`n"
     $Status.Refresh()
     return
   }
   if ($pagecontent -match "Status=READY")
   { 
      if ($pagecontent -match "ThereAreHits=yes")
      {
         $Status.Text += (get-date).ToString("MM-dd-yyyy HH:mm") + ": Search complete, retrieving results...`r`n`r`n"
     $Status.Refresh()
     $Status.ScrollToCaret()
     break
      }
      else
      {
        $Status.Text += (get-date).ToString("MM-dd-yyyy HH:mm") + ": Error 4-- No hits found!`r`n`r`n"
    $Status.Refresh()
        return
      }
   }
   $Status.Text += (get-date).ToString("MM-dd-yyyy HH:mm") + ": Error 5-- Unknown error! `r`n`r`n"
   $Status.Refresh()
   return
}
$uri= "http://www.ncbi.nlm.nih.gov/blast/Blast.cgi?CMD=Get&RID=" + $RID + "&ALIGNMENTS=500&ALIGNMENT_VIEW=QueryAnchored&FORMATOBJECT=Alignment&FORMAT_TYPE=TEXT"
$Results.Text = $BlastClient.DownloadString($uri).Replace("`n","`r`n");
return
}

$form.text = "NCBI Blastn(nr) GUI"
$form.Size =  "800,600"

$Panel.Size = "794, 32"
$Panel.Location = "0, 0"

$Label.Text = "Query:"
$Label.Location = "10, 8"
$Label.Size = "80, 32"
$Panel.Controls.Add($Label)

$Query.Location = "100, 8"
$Query.Size = "500, 20"
$Panel.Controls.Add($Query)

$Submit.Location = "710, 8"
$Submit.Text = "Submit"
$Submit.Add_Click({SubmitQuery($Query.Text)})
$Panel.Controls.Add($Submit)

$form.Controls.Add($Panel)

$Results.Location = "0, 40"
$Results.Multiline = $True
$Results.Size = "790, 400"
$Results.ScrollBars = "Vertical"
$Results.ReadOnly = $true
$form.Controls.Add($Results)

$Status.Location = "0, 440"
$Status.Multiline = $True
$Status.Size = "790, 100"
$Status.ScrollBars = "Vertical"
$Status.ReadOnly = $true
$form.Controls.Add($Status)

$form.showdialog() 


[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

Tags:       


Comments:

Post a Comment





<< Home