__author__ = 'weizhisong'


#################################################################################
#                                                                               #
#       This script was writen to format the NCBI blast results and put each    #
#                          orthology in one line                                #
#                                                                               #
#################################################################################

# The input file was generated by an all versus all blast step with the following parameter:
# /share/apps/blast+/2.2.31/bin/blastp -query -db -out -evalue 1e-5 -outfmt 6
blast_result = open('/Users/weizhisong/Desktop/HGT_temp/blast_results/all_vs_all_91bins_ffn.tab')
output = open('/Users/weizhisong/Desktop/HGT_temp/blast_results/orthology.txt', 'w')


gene_group = ''
subject_names = []

for each_line in blast_result:
        line_split = each_line.split("\t")
        query_name = line_split[0].strip('\n')
        subject_name = line_split[1].strip('\n')
        identity = float(line_split[2].strip('\n'))
        if query_name == gene_group and identity >= 50.00:
            subject_names.append(subject_name)
        else:
            if gene_group != '' and identity >= 50.00:
                output.write("%s\t%s" % (gene_group, '\t'.join(subject_names)) + '\n')
            gene_group = query_name
            subject_names = []
print gene_group

output.close()
