Mercurial > repos > abims-sbr > pairwise
comparison scripts/S05_find_rbh.py @ 0:90b57ab0bd1d draft default tip
planemo upload for repository https://github.com/abims-sbr/adaptsearch commit 3c7982d775b6f3b472f6514d791edcb43cd258a1-dirty
| author | abims-sbr |
|---|---|
| date | Fri, 01 Feb 2019 10:23:16 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:90b57ab0bd1d |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 # coding: utf-8 | |
| 3 # Author : Victor Mataigne | |
| 4 | |
| 5 import argparse, pickle, itertools | |
| 6 | |
| 7 def main(): | |
| 8 parser = argparse.ArgumentParser() | |
| 9 parser.add_argument('besthits_file1', help='') | |
| 10 parser.add_argument('besthits_file2', help='') | |
| 11 args = parser.parse_args() | |
| 12 | |
| 13 # Open dict of best hits | |
| 14 file_best_hit_dict_q = open('dict_best_hits_from_blast_1') | |
| 15 file_best_hit_dict_db = open('dict_best_hits_from_blast_2') | |
| 16 best_hit_dict_q = pickle.load(file_best_hit_dict_q) | |
| 17 best_hit_dict_db = pickle.load(file_best_hit_dict_db) | |
| 18 file_best_hit_dict_q.close() | |
| 19 file_best_hit_dict_db.close() | |
| 20 | |
| 21 best_h1 = {} | |
| 22 with open(args.besthits_file1, 'r') as bh1 : | |
| 23 for h, s in itertools.izip_longest(*[bh1]*2): | |
| 24 header = h.strip('>\n') | |
| 25 sequence = s.strip('\n') | |
| 26 best_h1[header] = sequence | |
| 27 | |
| 28 best_h2 = {} | |
| 29 with open(args.besthits_file2, 'r') as bh2 : | |
| 30 for h, s in itertools.izip_longest(*[bh2]*2): | |
| 31 header = h.strip('>\n') | |
| 32 sequence = s.strip('\n') | |
| 33 best_h2[header] = sequence | |
| 34 | |
| 35 # Find RBH: | |
| 36 reverse_best_hit_dict_db = dict((v,k) for k,v in best_hit_dict_db.iteritems()) | |
| 37 | |
| 38 rbh = set(best_hit_dict_q.items()).intersection(set(reverse_best_hit_dict_db.items())) | |
| 39 | |
| 40 s = args.besthits_file1.split('_') | |
| 41 suffix = s[4] + '_' + s[5] | |
| 42 out_name = 'RBH_{}_dna.fasta'.format(suffix) | |
| 43 output = open(out_name, 'w') | |
| 44 | |
| 45 for pairwise_couple in rbh : | |
| 46 output.write('>'+pairwise_couple[0]+'\n') | |
| 47 output.write(best_h1[pairwise_couple[0]]+'\n') | |
| 48 output.write('>'+pairwise_couple[1]+'\n') | |
| 49 output.write(best_h2[pairwise_couple[1]]+'\n') | |
| 50 output.close() | |
| 51 | |
| 52 if __name__ == "__main__": | |
| 53 main() |
