tankdthedruid

biopy_multi_class.py

Jan 25th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.53 KB | None | 0 0
  1. # ~/devel/pyDev/biopy_multi_class.py
  2. # pastebin.com/wNRGXJeM
  3.  
  4. # renamed the file: "HPA88HDJ016-Alignment.xml" to "blast_output.xml" and used it as the database being parsed.
  5. FILE_INPUT = open("blast_output.xml")   #   // set FILE_INPUT equal to the contents of blast_output.xml.
  6. MINIMUM_INT = 20    #   // set MINIMUM_INT to 20 (threshold integer)
  7. from Bio.Blast import NCBIXML   #   // import the specialty module NCBXML from Bio.Blast.
  8. PARSE_FILE = NCBIXML.parse(FILE_INPUT)  # // use the parse() function, from the NCBIXML module, on FILE_INPUT and set PARSE_FILE as the result. we now will use PARSE_FILE as our iterator.
  9. READ_LINE = PARSE_FILE.next()   #   // tells the PARSE_FILE iterator to use the next() method to parse each line into the READ_LINE variable.
  10.  
  11. # though commented heavily, this script is fully executable. the intention of writing the script in this way was
  12. # so that various sections of the code could be commented and uncommented to show various results when executed.
  13. # it was my hope that writing a script in this fashion would help you determine the various data sets and how they
  14. # can be displayed and manipulated.
  15.  
  16. """ [UNCOMMENT FOR EXECUTION]
  17. for READ_LINE in PARSE_FILE:    # for each line in PARSE_FILE, begin to loop.
  18.     for alignment in READ_LINE.alignments:  #   // for each alignment value in the alignments class, begin nested loop.
  19.         for hsp in alignment.hsps:  #   // for each hsp value in the hsps class, begin the second nested loop.
  20.             if hsp.identities > MINIMUM_INT:    #   // if the value of the each record's identities field is greater than MINIMUM_INT execute the following.
  21.                 print '\n***Seperator*** \n\n ALIGNMENT_TITLE: ', alignment.title
  22.                 print 'ALIGNMENT_LENGTH: ', alignment.length
  23.                 print 'HSP_SCORE: ', hsp.score
  24.                 print 'HSP_BITS: ', hsp.bits
  25.                 print 'HSP_EXPECT: ', hsp.expect
  26.                 print 'HSP_NUM_ALIGNMENTS: ', hsp.num_alignments
  27.                 print 'HSP_IDENTITIES: ', hsp.identities
  28.                 print 'HSP_POSITIVES: ', hsp.positives
  29.                 print 'HSP_GAPS: ', hsp.gaps
  30.                 print 'HSP_STRAND: ', hsp.strand
  31.                 print 'HSP_FRAME: ', hsp.frame
  32.                 print 'HSP_QUERY: ', hsp.query[0:75] + '...'
  33.                 print 'HSP_QUERY_START: ', hsp.query_start
  34.                 print 'HSP_MATCH: ', hsp.match[0:75] + '...'
  35.                 print 'HSP_SBJCT: ', hsp.sbjct[0:75] + '...'
  36.                 print 'HSP_SBJCT_START: ', hsp.sbjct_start
  37. [UNCOMMENT FOR EXECUTION] """
  38.  
  39. """ [UNCOMMENT FOR EXECUTION]
  40. above is an example of how a parser could be written to parse and print each value contained in the HSP and Alignment classes.
  41. the snippet of code written below is one way to set 're-usable' variables for eash record parsed.
  42. while this is not necessary it may be helpful if trying to format the output.
  43.  
  44.                 ALIGNMENT_TITLE  = alignment.title
  45.                 ALIGNMENT_LENGTH  = alignment.length
  46.                 HSP_SCORE  = hsp.score
  47.                 HSP_BITS  = hsp.bits
  48.                 HSP_EXPECT  = hsp.expect
  49.                 HSP_NUM_ALIGNMENTS  = hsp.num_alignments
  50.                 HSP_IDENTITIES  = hsp.identities
  51.                 HSP_POSITIVES  = hsp.positives
  52.                 HSP_GAPS  = hsp.gaps
  53.                 HSP_STRAND  = hsp.strand
  54.                 HSP_FRAME  = hsp.frame
  55.                 HSP_QUERY  = hsp.query
  56.                 HSP_QUERY_START  = hsp.query_start
  57.                 HSP_MATCH  = hsp.match
  58.                 HSP_SBJCT  = hsp.sbjct
  59.                 HSP_SBJCT_START  = hsp.sbjct_start
  60. [UNCOMMENT FOR EXECUTION] """
  61.  
  62. """ [UNCOMMENT FOR EXECUTION]
  63. if you wanted to look inside another class contained in the database such as the description class,
  64. you could something like the function below.
  65.  
  66. FILE_INPUT = open("blast_output.xml")
  67. MINIMUM_INT = 20
  68. from Bio.Blast import NCBIXML
  69. PARSE_FILE = NCBIXML.parse(FILE_INPUT)
  70. READ_LINE = PARSE_FILE.next()
  71. for READ_LINE in PARSE_FILE:
  72.     for description in READ_LINE.descriptions:  #   // for loop through each line contained in the Description class.
  73.         print '\n***Seperator***\n\nDESCRIPTION_TITLE: ', description.title #   // seperator to show loop begining and end points, also print each description title.
  74.         print 'DESCRIPTION_SCORE: ', description.score #    // print the description's score.
  75.         print 'DESCRIPTION_E: ', description.e  #   // print the description's E. Whatever that might be.
  76.         print 'DESCRIPTION_NUM_ALIGNMENTS: ', description.num_alignments    # // print description's num_alignments.
  77.        
  78.         DESC_TITLE  = str(description.title)    #   // set DESC_TITLE equal to the description's title.
  79.         DESC_SCORE  = str(description.score)    #   // set DESC_SCORE equal to the description's score.
  80.         DESC_E  = str(description.e)    #   // set DESC_E equal to the description's E...
  81.         DESC_NUM_ALIGN  = str(description.num_alignments)   #   // set DESC_NUM_ALIGN equal to the description's num_alignments.
  82. [UNCOMMENT FOR EXECUTION]"""
Advertisement
Add Comment
Please, Sign In to add comment