Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.48 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. from Bio.Blast import NCBIXML
  4. import time, datetime
  5.  
  6.  
  7. class BlastResult:
  8.     def __init__(self):
  9.         self.all_found_querys = set()
  10.         self.all_found_accessions = set()
  11.         self.all_found_pairs = {}
  12.         self.all_hps_per_hit = {}
  13.         self.all_hsp_per_sbjct = {}
  14.         self.all_sbjcts = set()
  15.         #self.records = []
  16.  
  17. class BlastRecord: #TODO is wel nodig voor hps's visualiseren / of deprecated
  18.     def __init__(self, query_name, query_length):
  19.         self.name = query_name
  20.         self.length = query_length
  21.         self.hits = []
  22.  
  23. class BlastHit:
  24.     def __init__(self, accession, title, length):
  25.         self.accession = accession
  26.         self.title = title
  27.         self.length = length
  28.         self.hsps = []
  29.  
  30. class BlastHsp:
  31.     def __init__(self, hsp_id, acc_id, expect, identities, query, match, subject, score):
  32.         self.hsp_id = hsp_id
  33.         self.acc_id = acc_id
  34.         self.expect = float(expect)
  35.         self.identities = identities
  36.         self.query = query
  37.         self.match = match
  38.         self.subject = subject
  39.         self.score = float(score)
  40.  
  41. """ origineel
  42. class BlastHandle:
  43.  
  44.    @staticmethod
  45.    def obj_to_dict(obj):
  46.        return obj.__dict__
  47.  
  48.    @staticmethod
  49.    def parse_blast_outfile(outfile="outfile.xml") -> BlastResult:
  50.        hsp_id = 1;
  51.        acc_id = 1;
  52.        blast_result_object = BlastResult()
  53.        with open(outfile) as file:
  54.            records = NCBIXML.parse(file)
  55.            for record in records:
  56.                blast_result_object.all_found_querys.add(record.query)
  57.                for hit in record.alignments:
  58.                    all_hsp_per_hit = []
  59.                    if record.query != hit.title[hit.title.find(" ") + 1:]:  # filter results where query == subject
  60.                        evalue = 1
  61.                        blast_hit = BlastHit(hit.accession,
  62.                                             hit.title,
  63.                                             hit.length)
  64.                        blast_result_object.all_found_accessions.add(hit.title)
  65.                        for hsp in hit.hsps:
  66.                            blast_hsp = BlastHsp(hsp_id,
  67.                                                 acc_id,
  68.                                                 float(hsp.expect),
  69.                                                 hsp.identities,
  70.                                                 hsp.query,
  71.                                                 hsp.match,
  72.                                                 hsp.sbjct,
  73.                                                 float(hsp.score))
  74.                            if hsp.expect < evalue:
  75.                                #data structuur: key als basis naam query + naam hit.
  76.                                #in principe de basis voor opbouw van tabel key=row+header, value=cel
  77.                                blast_result_object.all_found_pairs[record.query + "-" + hit.title] = [blast_hsp, blast_hit]
  78.                                evalue = hsp.expect
  79.                            all_hsp_per_hit.append(BlastHandle.obj_to_dict(blast_hsp))
  80.                            hsp_id += 1
  81.                    blast_result_object.all_hps_per_hit[acc_id] = all_hsp_per_hit
  82.                    acc_id += 1
  83.        return blast_result_object
  84. """
  85. class BlastHandle:
  86.  
  87.     @staticmethod
  88.     def obj_to_dict(obj):
  89.         return obj.__dict__
  90.  
  91.     @staticmethod
  92.     def parse_blast_outfile(outfile="testdata/GCF_000313135.1_Acastellanii.strNEFF_v1_genomic.xml") -> BlastResult:
  93.         hsp_id = 1;
  94.         acc_id = 1;
  95.         blast_result_object = BlastResult()
  96.         with open(outfile) as file:
  97.             records = NCBIXML.parse(file)
  98.             for record in records:
  99.                 blast_result_object.all_found_querys.add(record.query)
  100.                 for hit in record.alignments:
  101.                     all_hsp_per_hit = []
  102.                     if record.query != hit.title[hit.title.find(" ") + 1:]:  # filter results where query == subject
  103.                         evalue = 1
  104.                         blast_hit = BlastHit(hit.accession,
  105.                                              hit.title,
  106.                                              hit.length)
  107.                         blast_result_object.all_found_accessions.add(hit.title)
  108.                         for hsp in hit.hsps:
  109.                             blast_hsp = BlastHsp(hsp_id,
  110.                                                  acc_id,
  111.                                                  float(hsp.expect),
  112.                                                  hsp.identities,
  113.                                                  hsp.query,
  114.                                                  hsp.match,
  115.                                                  hsp.sbjct,
  116.                                                  float(hsp.score))
  117.  
  118.                             #extra logic
  119.                             blast_result_object.all_sbjcts.add(blast_hsp.subject)
  120.                             h = hash(record.query+"-"+hit.title+"-"+blast_hsp.subject)
  121.                             blast_result_object.all_hsp_per_sbjct[h] = blast_hsp
  122.                             #end extra logic
  123.  
  124.                             """
  125.                            if hsp.expect < evalue:
  126.                                #data structuur: key als basis naam query + naam hit.
  127.                                #in principe de basis voor opbouw van tabel key=row+header, value=cel
  128.                                blast_result_object.all_found_pairs[record.query + "-" + hit.title] = [blast_hsp, blast_hit]
  129.                                evalue = hsp.expect
  130.                            all_hsp_per_hit.append(BlastHandle.obj_to_dict(blast_hsp))
  131.                            """
  132.                             hsp_id += 1
  133.                     #blast_result_object.all_hps_per_hit[acc_id] = all_hsp_per_hit
  134.                     acc_id += 1
  135.         return blast_result_object
  136.  
  137.  
  138. print("start")
  139. print(datetime.datetime.now())
  140. start = time.time()
  141.  
  142. test = BlastHandle.parse_blast_outfile()
  143.  
  144. stop = time.time()
  145.  
  146. print("done reading: " + str(int(stop - start)) + " - seconden")
  147.  
  148. print("lengte querys: "+ str(len(test.all_found_querys)))
  149. print("lengte subjects: "+ str(len(test.all_found_accessions)))
  150. print("lengte unieke hsps: "+ str(len(test.all_sbjcts)))
  151.  
  152. for row in test.all_sbjcts:
  153.     for column in test.all_sbjcts:
  154.         x = 1
  155.  
  156.  
  157. stop2 = time.time()
  158.  
  159. print("done creating table: " + str(int(stop2 - stop)) + " - seconden")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement