def load_protein_configuration(protein_id, ref_species_dict = None): ''' Loads the data from a description file, and calls the containers generating functions to create basic objects. @param protein_id: id of a single protein ''' if not check_status_file_no_alignment(protein_id): return False if ref_species_dict is None: ref_species_dict = FileUtilities.get_reference_species_dictionary() logger = Logger.Instance() containers_logger = logger.get_logger('containers') data_map_container = DataMapContainer.Instance() protein_container = ProteinContainer.Instance() gene_container = GeneContainer.Instance() transcript_container = TranscriptContainer.Instance() ens_exon_container = EnsemblExonContainer.Instance() (known_proteins, abinitio_proteins) = DescriptionParser().parse_description_file_general_info(protein_id) for species_data in known_proteins: (species_name, spec_protein_id, spec_gene_id, spec_transcript_id, location_type, assembly, location_id, seq_begin, seq_end, strand) = species_data ab_initio = False # data map data_map_key = (protein_id, species_name) data_map = DataMap(spec_protein_id, spec_transcript_id, spec_gene_id, data_map_key, location_type, assembly, location_id, strand, seq_begin, seq_end, ab_initio) try: data_map_container.add(data_map_key, data_map) except (KeyError, TypeError), e: containers_logger.error("{0}, {1}, {2}, error adding to datamap".format(protein_id, species_name, e.args[0])) # everything else - protein, transcript, gene, ensembl exons protein = Protein(spec_protein_id, data_map_key, ref_species_dict[species_name]) gene = Gene(spec_gene_id, data_map_key, ref_species_dict[species_name]) transcript = Transcript(spec_transcript_id, data_map_key, ref_species_dict[species_name]) ens_exons = EnsemblExons(data_map_key, ref_species_dict[species_name]) try: ens_exons.load_exons() except (Exception), e: containers_logger.error("{0}, {1}, {2}, error loading exons".format(protein_id, species_name, e.args[0]))
def produce_statistics_for_alignment (exons_key, alignment_type, reference_exons): ''' Produces the straight-forward statistics for the alignment. For each alignment, it only calculates the coverage percentage. Where there are multiple alignments for a particular exon, percentages are summed. @param exons_key: (reference protein id, species) @param alignment_type: blastn, tblastn, sw_gene, sw_exon @return: list of similarity percentages in correct order starting from first reference exon to thel last ''' (ref_protein_id, species) = exons_key # if the alignment type is tblastn, we have to multiply # the coverage by 3 because the length is expressed in AAs, not in NBs if alignment_type == "tblastn": coverage_constant = 1. else: coverage_constant = 1. exon_container = ExonContainer.Instance() reference_species_dict = FileUtilities.get_reference_species_dictionary() logger = Logger.Instance() containers_logger = logger.get_logger("containers") #reference_exons = exon_container.get((ref_protein_id, reference_species_dict[species], "ensembl")) try: alignment_exons = exon_container.get((ref_protein_id, species, alignment_type)) except KeyError: containers_logger.error ("{0},{1},{2}".format(ref_protein_id, species, alignment_type)) return None perc_list = [] # TODO #remove_overlapping_alignments((ref_protein_id, species, alignment_type)) for ref_exon in reference_exons: ref_exon_id = ref_exon.exon_id if ref_exon_id not in alignment_exons.alignment_exons: perc_list.append(0) #print "%s length: %d\n\tnot present in alignment" % (ref_exon_id, len(ref_exon.sequence)) else: al_exons = alignment_exons.alignment_exons[ref_exon_id] internal_stat = 0. for al_exon in al_exons: if al_exon.viability: #print al_exon.alignment_info["sbjct_start"], al_exon.alignment_info["sbjct_end"] internal_stat += coverage_constant * float(al_exon.alignment_info["identities"]) / len(ref_exon.sequence) if internal_stat > 1: print "Coverage cannot be larger than 1 (%s,%s,%s)" % (ref_protein_id, species, alignment_type) print len(al_exon.alignment_info["sbjct_seq"]), len(ref_exon.sequence) raise ValueError ("Coverage cannot be larger than 1 (%s,%s,%s,%s,%f)" % (ref_protein_id, species, alignment_type,ref_exon_id, internal_stat)) #print "\t%1.2f" % ( float(al_exon.alignment_info["length"] - al_exon.alignment_info["gaps"]) / len(ref_exon.sequence)) perc_list.append(internal_stat) return perc_list
def load_exon_configuration_batch(protein_id_list, alignment_type): ''' Loads exons for all the proteins in the protein list for a particular alignment type ''' ref_species_dict = FileUtilities.get_reference_species_dictionary() folders_loaded_cnt = 0 for protein_id in protein_id_list: if load_exon_configuration(protein_id, ref_species_dict, alignment_type) == True: folders_loaded_cnt += 1 return folders_loaded_cnt
def load_protein_configuration_batch(protein_id_list): ''' Loads data from .descr files from all the proteins in the protein list @param protein_id_list: list of protein id's ''' ref_species_dict = FileUtilities.get_reference_species_dictionary() folders_loaded_cnt = 0 for protein_id in protein_id_list: if load_protein_configuration(protein_id, ref_species_dict) == True: folders_loaded_cnt += 1 return folders_loaded_cnt
def load_exon_configuration (ref_protein_id, ref_species_dict, exon_type): ''' Load exons of a particular type for all available species @param ref_protein_id: referent protein id @param exon_type: exon_type: ensembl, genewise, blatn, tblastn, sw_gene, sw_exon ''' dc = DescriptionParser() exon_container = ExonContainer.Instance() ens_exon_container = EnsemblExonContainer.Instance() logger = Logger.Instance() containers_logger = logger.get_logger('containers') if exon_type == "ensembl" or exon_type == "genewise": if not check_status_file_no_alignment(ref_protein_id): containers_logger.info ("{0},exon_type:{1},check status file -> failed".format(ref_protein_id, exon_type)) return False else: if not check_status_file(ref_protein_id): containers_logger.info ("{0},exon_type:{1},check status file -> failed".format(ref_protein_id, exon_type)) return False if not ref_species_dict: ref_species_dict = FileUtilities.get_reference_species_dictionary() (known_species, abinitio_species) = dc.get_separated_species(ref_protein_id) for species in known_species: ref_species = ref_species_dict[species] if exon_type != "genewise": if exon_type == "ensembl": exons = EnsemblExons ((ref_protein_id, species), ref_species) try: exon_dict = exons.load_exons() except Exception, e: containers_logger.error("{0},{1},{2},error loading exons".format(ref_protein_id, species, exon_type)) continue else: exons = Exons((ref_protein_id, species), ref_species, exon_type) try: exon_dict = exons.load_exons() except Exception, e: containers_logger.error("{0},{1},{2},error loading exons".format(ref_protein_id, species, exon_type)) continue if not exon_dict: continue if (exon_type != "ensembl"): exons.set_exon_ordinals() data_map_key = [ref_protein_id, species] exon_container.add(exon_type, data_map_key, exons)
def annotate_spurious_alignments(exons_key): ''' Annotates all the alignments which are not in the correct order. Annotation means their viability variable will be set to False. (Supporting the assumption that all exons are in the correct, sequential order) @param exons_key: (reference protein id, species) @param alignment_type: blastn, tblastn, sw_gene, sw_exon @return: updated alignment exons, None if something is wrong with the protein (meaning in the .status file) ''' (ref_protein_id, species, alignment_type) = exons_key print "Annotating spurious alignments %s,%s,%s" % (ref_protein_id, species, alignment_type) # if something is wrong with the protein, return if not check_status_file(ref_protein_id): return None exon_container = ExonContainer.Instance() reference_species_dict = FileUtilities.get_reference_species_dictionary() # load logging utilities logger = Logger.Instance() containers_logger = logger.get_logger("containers") # get the reference exons: (ref_prot_id, ref_species, ensembl) reference_exons = exon_container.get((ref_protein_id, reference_species_dict[species], "ensembl")) # try to get the exons which are the product of specified alignment try: alignment_exons = exon_container.get((ref_protein_id, species, alignment_type)) except KeyError: containers_logger.error ("{0},{1},{2},No exons available for alignment".format(ref_protein_id, species, alignment_type)) return None correct_order_exons = _find_best_orderred_subset (alignment_exons, reference_exons) updated_alignment_exons = _set_viabilities (alignment_exons, correct_order_exons) # update the exon container to hold the new alignment exons exon_container.update(exons_key, updated_alignment_exons) return updated_alignment_exons
def remove_overlapping_alignments (exons_key): (ref_protein_id, species, alignment_type) = exons_key printin = False if printin: print "Removing blastn overlaps (%s,%s,%s)..." % (ref_protein_id, species, alignment_type) if not check_status_file(ref_protein_id): return None exon_container = ExonContainer.Instance() reference_species_dict = FileUtilities.get_reference_species_dictionary() # load logging utilities logger = Logger.Instance() containers_logger = logger.get_logger("containers") # get the reference exons: (ref_prot_id, ref_species, ensembl) reference_exons = exon_container.get((ref_protein_id, reference_species_dict[species], "ensembl")) # try to get the exons which are the product of specified alignment try: alignment_exons = exon_container.get(exons_key) except KeyError: containers_logger.error ("{0},{1},{2}".format(ref_protein_id, species, alignment_type)) return None for ref_exon_id in alignment_exons.alignment_exons: al_exons = alignment_exons.alignment_exons[ref_exon_id] if printin: print ref_exon_id toplevel_start = 0 toplevel_stop = 0 #for al_exon in sorted(al_exons, key = lambda al_exon: al_exon.get_fitness(), reverse = True): for al_exon in al_exons: exon_start = al_exon.alignment_info["sbjct_start"] exon_stop = exon_start + al_exon.alignment_info["length"] # if exon is already marked as not viable, just discard it if hasattr(al_exon, "viability"): if not al_exon.viability: continue if not toplevel_start: # if toplevel locations haven't been set, set them toplevel_start = exon_start toplevel_stop = exon_stop toplevel_exon = al_exon al_exon.set_viability(True) if printin: print "First exon: %d - %d" % (exon_start, exon_stop) elif exon_start < toplevel_start and exon_stop > toplevel_stop: toplevel_exon.set_viability(False) toplevel_exon = al_exon toplevel_start = exon_start toplevel_stop = exon_stop al_exon.set_viability(True) if printin: print " New toplevel: %d - %d" % (exon_start, exon_stop) else: # what this wonderful if checks if one of the following cases: # if the exon is contained within the toplevel exon # |----------------------| # |------| # or the start is to the left of the toplevel, but they are still overlapping # |----------| # or the end is to the right of the toplevel, but they are still overlapping # |--------------| if (exon_start >=toplevel_start and exon_stop <= toplevel_stop) or \ (exon_start <= toplevel_start and (exon_stop >= toplevel_start and exon_stop <= toplevel_stop)) or \ ((exon_start >= toplevel_start and exon_start <= toplevel_stop) and exon_stop >= toplevel_stop): if printin: print " Bad exon: %d - %d" % (exon_start, exon_stop) al_exon.set_viability(False) else: if exon_start < toplevel_start: toplevel_start = exon_start if exon_stop > toplevel_stop: toplevel_stop = exon_stop if printin: print " Good exon: %d - %d" % (exon_start, exon_stop) exon_container.update(exons_key, alignment_exons)