def test_get_genome_data(self): """hg19""" result = GenomicRegionSet("hg19") result.get_genome_data(organism="hg19") self.assertEqual(len(result), 23) """hg19, with Mitochondria chromosome""" result = GenomicRegionSet("hg19") result.get_genome_data(organism="hg19", chrom_M=True) self.assertEqual(len(result), 24)
def sort_dbs_by_regions(self, regionset): """Sort the DBS by given GenomicRegionSet""" dbss = self.get_dbs(sort=True) result = {} if not regionset.sorted: regionset.sort() iter_dbs = iter(dbss) dbs = iter_dbs.next() last_j = len(regionset) - 1 j = 0 cont_loop = True pre_inter = 0 cont_overlap = False while cont_loop: # When the regions overlap if dbs.overlap(regionset[j]): result[regionset[j].toString()].add(dbs) if cont_overlap == False: pre_inter = j if j == last_j: try: dbs = iter_dbs.next() except: cont_loop = False else: j = j + 1 result[regionset[j].toString()] = GenomicRegionSet( "RBS_" + regionset[j].toString()) cont_overlap = True elif dbs < regionset[j]: try: dbs = iter_dbs.next() j = pre_inter cont_overlap = False except: cont_loop = False elif dbs > regionset[j]: if j == last_j: cont_loop = False else: j = j + 1 result[regionset[j].toString()] = GenomicRegionSet( "RBS_" + regionset[j].toString()) cont_overlap = False return result
def region_sets(self, listA, listB): """ Setting two GenomicRegionSets as self.setA and self.setB for each case test. """ self.setA = GenomicRegionSet('for Unit Test') for i in range(len(listA)): self.setA.add( GenomicRegion(chrom=listA[i][0], initial=listA[i][1], final=listA[i][2])) self.setB = GenomicRegionSet('for Unit Test') for i in range(len(listB)): self.setB.add( GenomicRegion(chrom=listB[i][0], initial=listB[i][1], final=listB[i][2]))
def mode_3(exp_matrix): #remember value of bedgraph, ugly way score = {} for regions in exp_matrix.get_regionsets(): for region in regions: score[(region.chrom + ':' + str(region.initial) + '-' + str(region.final))] = region.data for region in exp_matrix.get_regionsets(): f = open("region_" + str(region.name) + ".data", 'w') region_set = GenomicRegionSet("") _, _, mappedGenes, _, gene_peaks_mapping = region_set.filter_by_gene_association(region.fileName, None, gene_file, genome_file, thresh_dist=2000) avg_score = {} #score per peak genes = {} for peak, gene_list in list(gene_peaks_mapping.items()): for gen in gene_list: #reverse mapping peak -> gene to gene -> peak if not gen: continue genes[gen] = genes.get(gen, set()) genes[gen].add(peak) avg_score[gen] = avg_score.get(gen, []) avg_score[gen].append(score[peak]) #join all scores of peaks assigned to a gen for gen in list(genes.keys()): avg = sum([float(x) for x in avg_score[gen]])/ float(len(avg_score[gen])) print(gen, avg, ", ".join(str(t) for t in genes[gen]), sep='\t', file = f) f.close()
def mode_2(exp_matrix): #remember value of bedgraph, ugly way value = {} for regions in exp_matrix.get_regionsets(): for region in regions: value[(region.chrom, region.initial, region.final)] = region.data for region in exp_matrix.get_regionsets(): f = open("region_" + str(region.name) + ".data", 'w') region_set = GenomicRegionSet("") _, _, mappedGenes, _, gene_peaks_mapping = region_set.filter_by_gene_association(region.fileName, None, gene_file, genome_file, thresh_dist=2000) for k in list(gene_peaks_mapping.keys()): chr, raw_positions = k.split(':') start, end = [int(x) for x in raw_positions.split('-')] #if peak is not assigned, an empty string occurs if "" in gene_peaks_mapping[k]: gene_peaks_mapping[k].remove("") list = 'NA' if not gene_peaks_mapping[k] else ','.join(gene_peaks_mapping[k]) print(chr, start, end, value[(chr, start, end)], list, sep='\t', file = f) f.close()
def mode_1(exp_matrix): for region in exp_matrix.get_regionsets(): region_set = GenomicRegionSet("") _, _, mappedGenes, _, _ = region_set.filter_by_gene_association( region.fileName, None, gene_file, genome_file, thresh_dist=50000) print('#number of mapped genes:', mappedGenes) print(region.name + "\t" + ("\t".join(region_set.genes)))
def merge_rbs(self, rm_duplicate=False, asgene_organism=None, cutoff=0): """Merge the RNA binding regions which have overlap to each other and combine their corresponding DNA binding regions. extend -> Define the extending length in basepair of each RNA binding regions perfect_match -> Merge only the exactly same RNA binding regions """ # Merge RBS rna_merged = self.get_rbs() rna_merged.merge() # A dict: RBS as key, and GenomicRegionSet as its value new_dict = OrderedDict() for rbsm in rna_merged: regions = GenomicRegionSet(rbsm.toString()) for rd in self: if rbsm.overlap(rd.rna): regions.add(rd.dna) if rm_duplicate: regions.remove_duplicates() if len(regions) > cutoff: new_dict[rbsm] = regions if asgene_organism: try: new_dict[rbsm] = new_dict[rbsm].gene_association(organism=asgene_organism) except: print("* No annotation file for mapping associated genes.") else: continue self.merged_dict = new_dict
def get_biotypes(self, gene_set=None): """Get the region sets of different Biotypes. *Keyword arguments:* *Return:* - result_grs -- A list of GenomicRegionSets containing the regions for each Biotype. """ # Fetching gene names mapped_gene_list = None unmapped_gene_list = None # Fetching exons query_dictionary = {self.GeneField.FEATURE_TYPE: "exon"} query_annset = self.get(query_dictionary) # Creating GenomicRegionSet result_grs = GenomicRegionSet("exon") for e in query_annset.gene_list: gr = e[self.GeneField.GENOMIC_REGION] gr.name = e[self.GeneField.TRANSCRIPT_ID] result_grs.add(gr) if gene_set: return result_grs, unmapped_gene_list else: return result_grs
def get_dbs(self, sort=False, orientation=None, rm_duplicate=False, dbd_tag=False): """Return GenomicRegionSet which contains all DNA binding sites""" dna_set = GenomicRegionSet(name="DNA_binding_sites") if len(self) == 0: return dna_set for rd in self.sequences: if dbd_tag: dbs = GenomicRegion(chrom=rd.dna.chrom, initial=rd.dna.initial, final=rd.dna.final, name=rd.rna.str_rna(), orientation=rd.dna.orientation, data=rd.score) else: dbs = GenomicRegion(chrom=rd.dna.chrom, initial=rd.dna.initial, final=rd.dna.final, name=rd.dna.name, orientation=rd.dna.orientation, data=rd.score) if not orientation: dna_set.add(dbs) else: if orientation == rd.orient: dna_set.add(dbs) else: pass if sort: dna_set.sort() if rm_duplicate: dna_set.remove_duplicates() return dna_set
def load_objects(self, is_bedgraph, verbose=False, test=False): """Load files and initialize object. *Keyword arguments:* - is_bedgraph -- Whether regions are in bedgraph format (default = False). - verbose -- Verbose output (default = False). - test -- Fetch only 10 regions form each BED files for test. """ for i, t in enumerate(self.types): if verbose: print("Loading file ", self.files[self.names[i]], file=sys.stderr) if t not in ["regions", "genes"] and verbose: print("Cannot load objects", file=sys.stderr) if t == "regions": regions = GenomicRegionSet(self.names[i]) if is_bedgraph: regions.read_bedgraph( os.path.abspath(self.files[self.names[i]])) else: regions.read_bed(os.path.abspath( self.files[self.names[i]])) if test: regions.sequences = regions.sequences[0:11] self.objectsDict[self.names[i]] = regions elif t == "genes": genes = GeneSet(self.names[i]) genes.read( os.path.abspath(self.files[self.names[i]]) ) # Here change the relative path into absolute path self.objectsDict[self.names[i]] = genes
def load_objects(self, is_bedgraph, verbose=False): """Load files and initialize object""" for i, t in enumerate(self.types): if verbose: print("Loading file ", self.files[self.names[i]], file=sys.stderr) if t not in ["regions", "genes"] and verbose: print("Cannot load objects", file=sys.stderr) if t == "regions": regions = GenomicRegionSet(self.names[i]) if is_bedgraph: regions.read_bedgraph( os.path.abspath(self.files[self.names[i]])) else: regions.read_bed( os.path.abspath(self.files[self.names[i]]) ) # Here change the relative path into absolute path self.objectsDict[self.names[i]] = regions elif t == "genes": genes = GeneSet(self.names[i]) genes.read( os.path.abspath(self.files[self.names[i]]) ) # Here change the relative path into absolute path self.objectsDict[self.names[i]] = genes
def get_transcripts(self, gene_set = None): """Gets transcripts of genes. It returns a GenomicRegionSet with such transcripts. The id of each gene will be put in the NAME field of each GenomicRegion. *Keyword arguments:* - gene_set -- A set of genes to narrow the search. *Return:* - result_grs -- A GenomicRegionSet containing the exons. - unmapped_gene_list -- A list of genes that could not be mapped to an ENSEMBL ID. """ # Fetching gene names mapped_gene_list = None unmapped_gene_list = None if gene_set: mapped_gene_list, unmapped_gene_list = self.fix_gene_names(gene_set) # Fetching exons if gene_set: query_dictionary = {self.GeneField.FEATURE_TYPE:"exon", self.GeneField.GENE_ID:mapped_gene_list} else: query_dictionary = {self.GeneField.FEATURE_TYPE:"exon"} query_annset = self.get(query_dictionary) # Creating GenomicRegionSet result_grs = GenomicRegionSet("exon") for e in query_annset.gene_list: gr = e[self.GeneField.GENOMIC_REGION] gr.name = e[self.GeneField.TRANSCRIPT_ID] result_grs.add(gr) if gene_set: return result_grs, unmapped_gene_list else: return result_grs
def get_exons(self, start_site=False, end_site=False, gene_set=None, merge=True): """Gets exons of genes. It returns a GenomicRegionSet with such exons. The id of each gene will be put in the NAME field of each GenomicRegion. *Keyword arguments:* - start_site -- Whether to relocate the start sites. - end_site -- Whether to relocate the end sites. - gene_set -- A set of genes to narrow the search. *Return:* - result_grs -- A GenomicRegionSet containing the exons. - unmapped_gene_list -- A list of genes that could not be mapped to an ENSEMBL ID. """ # Fetching gene names mapped_gene_list = None unmapped_gene_list = None if gene_set: mapped_gene_list, unmapped_gene_list = self.fix_gene_names( gene_set) # Fetching exons if gene_set: query_dictionary = { self.GeneField.FEATURE_TYPE: "exon", self.GeneField.GENE_ID: mapped_gene_list } else: query_dictionary = {self.GeneField.FEATURE_TYPE: "exon"} query_annset = self.get(query_dictionary) # Creating GenomicRegionSet result_grs = GenomicRegionSet("exon") for e in query_annset.gene_list: gr = e[self.GeneField.GENOMIC_REGION] # gr.name = e[self.GeneField.GENE_ID] gr.name = e[self.GeneField.TRANSCRIPT_ID] result_grs.add(gr) if start_site: result_grs.relocate_regions("leftend", left_length=1, right_length=1) elif end_site: result_grs.relocate_regions("rightend", left_length=1, right_length=1) if merge: result_grs.merge() if gene_set: return result_grs, unmapped_gene_list else: return result_grs
def get_promoters(self, promoterLength=1000, gene_set=None, unmaplist=False): """ Gets promoters of genes given a specific promoter length. It returns a GenomicRegionSet with such promoters. The ID of each gene will be put in the NAME field of each GenomicRegion. Each promoter includes also the coordinate of the 5' base pair, therefore each promoter actual length is promoterLength+1. Keyword arguments: promoterLength -- The length of the promoter region. gene_set -- A set of genes to narrow the search. Return: result_grs -- A GenomicRegionSet containing the promoters. unmapped_gene_list -- A list of genes that could not be mapped to an ENSEMBL ID. """ # Fetching gene names mapped_gene_list = None unmapped_gene_list = None if (gene_set): mapped_gene_list, unmapped_gene_list = self.fix_gene_names( gene_set) # Fetching genes #if(gene_set): query_dictionary = {self.GeneField.FEATURE_TYPE:"gene", self.GeneField.GENE_ID:mapped_gene_list} #else: query_dictionary = {self.GeneField.FEATURE_TYPE:"gene"} if (gene_set): query_dictionary = { self.GeneField.FEATURE_TYPE: "transcript", self.GeneField.GENE_ID: mapped_gene_list } else: query_dictionary = {self.GeneField.FEATURE_TYPE: "transcript"} query_annset = self.get(query_dictionary) # Creating GenomicRegionSet result_grs = GenomicRegionSet("promoters") for e in query_annset.gene_list: gr = e[self.GeneField.GENOMIC_REGION] if (gr.orientation == "+"): gr.final = gr.initial + 1 gr.initial = gr.initial - promoterLength else: gr.initial = gr.final - 1 gr.final = gr.initial + promoterLength + 1 gr.name = e[self.GeneField.GENE_ID] result_grs.add(gr) if unmaplist: return result_grs, unmapped_gene_list else: return result_grs
def get_dbs(self, sort=False, orientation=None, rm_duplicate=False): """Return GenomicRegionSet which contains all DNA binding sites""" dna_set = GenomicRegionSet(name="DNA_binding_sites") for rd in self.sequences: if not orientation: dna_set.add(rd.dna) else: if orientation == rd.orient: dna_set.add(rd.dna) else: pass if sort: dna_set.sort() if rm_duplicate: dna_set.remove_duplicates() return dna_set
def match_ms_tags(self, field, test=False): """Add more entries to match the missing tags of the given field. For example, there are tags for cell like 'cell_A' and 'cell_B' for reads, but no these tag for regions. Then the regions are repeated for each tags from reads to match all reads. *Keyword arguments:* - field -- Field to add extra entries. """ # check regions or reads have empty tag altypes = self.fieldsDict[field].keys() if "ALL" in altypes: altypes.remove("ALL") for name in self.fieldsDict[field]["ALL"]: i = self.names.index(name) for t in altypes: # print("\t"+t) n = name + "_" + t # print("\t\t"+n) self.names.append(n) self.types.append(self.types[i]) self.files[n] = self.files[name] # types = self.get_types(name,skip_all=True) # print("************") # print(types) for f in self.fields[3:]: if f == field: try: self.fieldsDict[f][t].append(n) except: self.fieldsDict[f][t] = [n] else: try: self.fieldsDict[f][self.get_type( name=name, field=f)].append(n) except: self.fieldsDict[f][self.get_type( name=name, field=f)] = [n] # for f in self.fieldsDict.keys(): # for ty in types: # try: self.fieldsDict[f][ty].append(n) # except: pass if self.types[i] == "regions": g = GenomicRegionSet(n) g.read_bed(self.files[name]) if test: g.sequences = g.sequences[0:11] self.objectsDict[n] = g self.trash.append(name)
def get_tts(self, gene_set=None): """Gets TTS(Transcription termination site) of genes. It returns a GenomicRegionSet with such TTS. The ID of each gene will be put in the NAME field of each GenomicRegion. *Keyword arguments:* - gene_set -- A set of genes to narrow the search. *Return:* - result_grs -- A GenomicRegionSet containing TTS. - unmapped_gene_list -- A list of genes that could not be mapped to an ENSEMBL ID. """ # Fetching gene names mapped_gene_list = None unmapped_gene_list = None if gene_set: mapped_gene_list, unmapped_gene_list = self.fix_gene_names( gene_set) # Fetching genes if gene_set: query_dictionary = { self.GeneField.FEATURE_TYPE: "gene", self.GeneField.GENE_ID: mapped_gene_list } else: query_dictionary = {self.GeneField.FEATURE_TYPE: "gene"} query_annset = self.get(query_dictionary) # Creating GenomicRegionSet result_grs = GenomicRegionSet("TTS") for e in query_annset.gene_list: gr = e[self.GeneField.GENOMIC_REGION] if gr.orientation == "+": gr.initial = gr.initial gr.final = gr.initial + 1 else: gr.initial = gr.final - 1 gr.final = gr.final gr.name = e[self.GeneField.GENE_ID] result_grs.add(gr) result_grs.merge() if gene_set: return result_grs, unmapped_gene_list else: return result_grs
def merge_by(self, rbss, rm_duplicate=False, asgene_organism=False): """Merge the RNA Binding Sites by the given list of Binding sites""" new_dict = OrderedDict() for rbsm in rbss: new_dict[rbsm] = GenomicRegionSet(rbsm.toString()) for rd in self: if rbsm.overlap(rd.rna): new_dict[rbsm].add(rd.dna) if rm_duplicate: new_dict[rbsm].remove_duplicates() if asgene_organism: try: new_dict[rbsm] = new_dict[rbsm].gene_association(organism=asgene_organism) except: print("* No annotation file for mapping associated genes.") self.merged_dict = new_dict
def get_genes(self, gene_set=None): """ Gets regions of genes. It returns a GenomicRegionSet with such genes. The id of each gene will be put in the NAME field of each GenomicRegion. Keyword arguments: gene_set -- A set of genes to narrow the search. Return: result_grs -- A GenomicRegionSet containing the genes. unmapped_gene_list -- A list of genes that could not be mapped to an ENSEMBL ID. """ # Fetching gene names mapped_gene_list = None unmapped_gene_list = None if (gene_set): mapped_gene_list, unmapped_gene_list = self.fix_gene_names( gene_set) # Fetching genes if (gene_set): query_dictionary = { self.GeneField.FEATURE_TYPE: "gene", self.GeneField.GENE_ID: mapped_gene_list } else: query_dictionary = {self.GeneField.FEATURE_TYPE: "gene"} query_annset = self.get(query_dictionary) # Creating GenomicRegionSet result_grs = GenomicRegionSet("genes") for e in query_annset.gene_list: gr = e[self.GeneField.GENOMIC_REGION] gr.name = e[self.GeneField.GENE_ID] result_grs.add(gr) result_grs.merge() if (gene_set): return result_grs, unmapped_gene_list else: return result_grs
def load_exon_sequence(bed, directory, genome_path): """Load the exon sequence from the the transcripts. Input BED format should contain: blockCount - The number of blocks (exons) in the BED line. blockSizes - A comma-separated list of the block sizes. blockStarts - A comma-separated list of block starts. see details: http://genome.ucsc.edu/FAQ/FAQformat#format1 Output: Each FASTA file represants a transcript and contains all the exons within the file. """ regionset = GenomicRegionSet("bed") regionset.read(bed) regionset.sort() genome = pysam.Fastafile(genome_path) try: if len(regionset.sequences[0].data.split("\t")) == 7: blockinfor = True no_exon = False except: blockinfor = False regionset.sequences.sort(key=lambda g: g.name) no_exon = True if blockinfor: for gr in regionset: if not gr.name: print("Error: For fetching exon sequences, please define the transcript name.") sys.exit() else: if not os.path.exists(directory): os.makedirs(directory) f = open(os.path.join(directory, gr.name+".fa"), "w") data = gr.data.split("\t") #print(len(data)) if len(data) == 7: #print(data) n = int(data[4]) blocks = [ int(b) for b in filter(None, data[5].split(",")) ] starts = [ int(s) for s in filter(None, data[6].split(",")) ] printstr = [] for i in range(n): start = gr.initial + starts[i] end = start + blocks[i] if no_exon and i == 0: ex = "" elif gr.orientation == "-": ex = "exon:"+str(n-i) else: ex = "exon:"+str(i+1) if gr.orientation == "-": seq = Seq(genome.fetch(gr.chrom, start-1, end-1), IUPAC.unambiguous_dna) seq = seq.reverse_complement() p = [ ">"+ " ".join([ gr.name, ex, "_".join(["REGION",gr.chrom, str(start),str(end), gr.orientation]) ]), seq ] printstr.append(p) else: p = [ ">"+ " ".join([gr.name, ex, "_".join(["REGION",gr.chrom,str(start),str(end), gr.orientation]) ]), genome.fetch(gr.chrom, start-1, end-1) ] printstr.append(p) if gr.orientation == "-": printstr = printstr[::-1] for i in range(n): print(printstr[i][0], file=f) print(printstr[i][1], file=f) else: print("Warning: The given regions have no block information, please try write_bed_blocks") f.close() else: pre_id = "" for gr in regionset: if not gr.name: gr.name = gr.toString() if pre_id == "": pre_id = gr.name z = GenomicRegionSet(gr.name) z.add(gr) elif gr.name == pre_id: z.add(gr) else: f = open(os.path.join(directory, pre_id+".fa"), "w") for i, g in enumerate(z): try: regiontag = "_".join(["REGION", g.chrom, str(g.initial),str(g.final), gr.orientation]) except: regiontag = "_".join(["REGION", g.chrom, str(g.initial),str(g.final)] ) print( ">"+ " ".join([g.name, regiontag ]), file=f) print(genome.fetch(g.chrom, g.initial, g.final), file=f) f.close() pre_id = gr.name z = GenomicRegionSet(gr.name) z.add(gr) # Last TX f = open(os.path.join(directory, pre_id+".fa"), "w") for i, g in enumerate(z): try: regiontag = "_".join(["REGION", g.chrom, str(g.initial),str(g.final), gr.orientation]) except: regiontag = "_".join(["REGION", g.chrom, str(g.initial),str(g.final)] ) print( ">"+ " ".join([g.name, regiontag ]), file=f) print(genome.fetch(g.chrom, g.initial, g.final), file=f) f.close()
def get_promoters(self, promoter_length=1000, tss=0, gene_set=None, unmaplist=False, variants=False, gene_id=False, regiondata=False): """ Gets promoters of genes given a specific promoter length. It returns a GenomicRegionSet with such promoters. The ID of each gene will be put in the NAME field of each GenomicRegion. Each promoter includes also the coordinate of the 5' base pair, therefore each promoter actual length is promoter_length+1. *Keyword arguments:* - promoter_length -- The length of the promoter region. - gene_set -- A set of genes to narrow the search. - unmaplist -- If True than also return the unmappable genes list (default = False). *Return:* - result_grs -- A GenomicRegionSet containing the promoters. - unmapped_gene_list -- A list of genes that could not be mapped to an ENSEMBL ID. """ # Fetching gene names mapped_gene_list = None unmapped_gene_list = None if gene_set: mapped_gene_list, unmapped_gene_list, mapping_dict = self.fix_gene_names( gene_set, output_dict=True) # Fetching genes if not variants: target = "gene" else: target = "transcript" if gene_set: query_dictionary = { self.GeneField.FEATURE_TYPE: target, self.GeneField.GENE_ID: mapped_gene_list } else: query_dictionary = {self.GeneField.FEATURE_TYPE: target} query_annset = self.get(query_dictionary) # Creating GenomicRegionSet result_grs = GenomicRegionSet("promoters") for e in query_annset.gene_list: gr = e[self.GeneField.GENOMIC_REGION] if gr.orientation == "+": gr.final = gr.initial + 1 + tss gr.initial = gr.initial - promoter_length else: gr.initial = gr.final - 1 - tss gr.final = gr.initial + promoter_length + 1 if gene_set: try: gr.name = mapping_dict[e[self.GeneField.GENE_ID]] except: gr.name = e[self.GeneField.GENE_ID] elif gene_id: gr.name = e[self.GeneField.GENE_ID] else: gr.name = e[self.GeneField.GENE_NAMES] if gene_set and regiondata: gr.data = gene_set.values[gr.name] result_grs.add(gr) if unmaplist: return result_grs, unmapped_gene_list else: return result_grs
""" Updated on 22 May 2014 by Joseph """ ############################# Parameters ############################## parser = argparse.ArgumentParser(description='Return the random sequences according to the given parameters.') parser.add_argument('-o','-organism', default= "hg19", help='Define the organism. Default: hg19') parser.add_argument('-l','-length', type=int, help='Define the length of each sequence.') parser.add_argument('-n','-number', type=int, help='Define the number of random regions.') parser.add_argument('-f','-filter', default=None, help='Given the path to the BED file as the filter.') args = parser.parse_args() # Setup the entries region = GenomicRegion("sample", initial=0, final=args.l) template = GenomicRegionSet("tamplate") template.add(region) if not os.path.exists(bed_dir): os.makedirs(bed_dir) # Random region result = template.random_regions(organism= "hg19", total_size=args.n, multiply_factor=0, overlap_result=True, overlap_input=True, chrom_X=False, chrom_M=False, filter_path=args.f) result.write(os.path.join(bed_dir, "00total.bed")) chrom = GenomicRegionSet("chrom") chrom.get_genome_data(organism=args.o, chrom_X=False, chrom_M=False) chrom_list = [] for r in chrom.sequences: chrom_list.append(r.chrom)
################################################################################## parser = argparse.ArgumentParser(description='Replace TCONs in BED file by assoicated gene names', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-bed', type=str, help="BED file or a directory containing BED files") parser.add_argument('-output', type=str, help="Define the output directory") parser.add_argument('-organism', type=str, help="Define the organism") args = parser.parse_args() genome = GenomeData(args.organism) if os.path.isfile(args.bed): regionset = GenomicRegionSet("bed") regionset.read_bed(args.bed) gr = regionset.gene_association(organism=args.organism, promoterLength=1000, threshDist=500000, show_dis=True) regionset.replace_region_name(gr,combine=True) regionset.write_bed(args.output) elif os.path.isdir(args.bed): if not os.path.exists(args.output): os.makedirs(args.output) for root, dirnames, filenames in os.walk(args.bed): for filename in filenames: if ".bed" in filename: print(filename)
from rgt.GenomicRegionSet import * from rgt.ExperimentalMatrix import * #from fisher import pvalue import scipy.stats outdir = "" back = False designFile = sys.argv[1] genomeName = sys.argv[2] geneFile = sys.argv[3] randomize = int(sys.argv[4]) backGroundPeaks = False if len(sys.argv) > 5: backGroundPeaksName = sys.argv[6] backBed = GenomicRegionSet("BACK") backBed.read_bed(backGroundPeaksName) backGroundPeaks = True distance = 50000 if len(sys.argv) > 6: distance = len(sys.argv[6]) if len(sys.argv) > 7: outdir = sys.argv[7] #genomeFile=anotationPath+"chrom.sizes" #geneFile=anotationPath+"association_file.bed" exps = ExperimentalMatrix() exps.read(designFile)
def merge_rbs(self, rbss=None, rm_duplicate=False, asgene_organism=None, region_set=None, name_replace=None, cutoff=0): """Merge the RNA binding regions which have overlap to each other and combine their corresponding DNA binding regions. extend -> Define the extending length in basepair of each RNA binding regions perfect_match -> Merge only the exactly same RNA binding regions """ # A dict: RBS as key, and GenomicRegionSet as its value self.merged_dict = OrderedDict() # reg = copy.deepcopy(region_set) # res = copy.deepcopy(name_replace) # reg = region_set res = name_replace if not rbss: # Merge RBS rna_merged = self.get_rbs() rna_merged.merge() else: rna_merged = rbss for r in rna_merged: self.merged_dict[r] = GenomicRegionSet(r.toString()) rbsm = iter(rna_merged) try: r = rbsm.next() except: return if self.sorted_rna: pass else: self.sort_rbs() con = iter(self) try: rd = con.next() except: return con_loop = True while con_loop: #print(".", end="") if r.overlap(rd.rna): self.merged_dict[r].add(rd.dna) try: rd = con.next() except: try: r = rbsm.next() # self.merged_dict[r] = GenomicRegionSet(r.toString()) except: if rm_duplicate: self.merged_dict[r].remove_duplicates() con_loop = False elif rd.rna < r: try: rd = con.next() except: try: r = rbsm.next() # self.merged_dict[r] = GenomicRegionSet(r.toString()) except: if rm_duplicate: self.merged_dict[r].remove_duplicates() con_loop = False elif rd.rna > r: if rm_duplicate: self.merged_dict[r].remove_duplicates() #print(self.merged_dict[r].sequences[0].name) try: r = rbsm.next() # self.merged_dict[r] = GenomicRegionSet(r.toString()) except: try: rd = con.next() except: con_loop = False if region_set: for r in self.merged_dict.keys(): s = region_set.intersect(self.merged_dict[r], mode=OverlapType.ORIGINAL, rm_duplicates=rm_duplicate) self.merged_dict[r] = s if not region_set and rm_duplicate: for r in self.merged_dict.keys(): self.merged_dict[r].remove_duplicates() if cutoff: for r in self.merged_dict.keys(): if len(self.merged_dict[r]) < cutoff: n = self.merged_dict.pop(r, None) if res: for r in self.merged_dict.keys(): self.merged_dict[r].replace_region_name(regions=res) #self.merged_dict = new_dict elif asgene_organism: for r in self.merged_dict.keys(): try: self.merged_dict[r] = self.merged_dict[r].gene_association( organism=asgene_organism) except: pass
# experimental_matrix_file = "/home/manuel/workspace/cluster_p/THOR/exp/exp23_macs2_payton/1" # gene_exp = "/home/manuel/workspace/cluster_p/allhoff/project_THOR/data/payton/gene_expression/CCmean.data" # annotation_path = "/home/manuel/workspace/cluster_h/rgtdata/hg19/" # outputdir = "/home/manuel/test/" exps = ExperimentalMatrix() exps.read(experimental_matrix_file) regionsets = exps.get_regionsets() genome_file = annotation_path + "/chrom.sizes" gene_file = annotation_path + "/association_file.bed" genes = GeneSet("Expression") genes.read_expression(gene_exp) for region in regionsets: bedNew = GenomicRegionSet("") [degenes, de_peak_genes, mappedGenes, totalPeaks, regionsToGenes] \ = bedNew.filter_by_gene_association_old(region.fileName, genes.genes, gene_file, genome_file) [ct, labels] = averageExpression(region, genes, regionsToGenes) aux = region.fileName.split("/") fileName = aux[-1] fileName = fileName.split(".") output(genes.cond, labels, ct, outputdir + "/" + fileName[0] + ".txt")
def test_filter_tts(self): txp = RNADNAInteractionSet(organism="hg19", filename=sample_txp) g = GenomicRegionSet("g") s = GenomicRegion(chrom="chr2", initial=74000000, final=75000000) g.add(s) result = txp.count_tts(g)
import unittest from rgt.GenomicRegionSet import * from rgt.CoverageSet import CoverageSet regions = GenomicRegionSet("test") regions.add(GenomicRegion("chr1", 10000, 11000, "+")) regions.add(GenomicRegion("chr1", 20000, 21000, "-")) cov = CoverageSet("coverage", regions) bamfile = "/projects/lncRNA/local/cardio/total_rna/bam/d4_1.bam" bedfile = "~/rgtdata/hg38/genes_hg38.bed" class CoverageSet_Test(unittest.TestCase): def coverage_from_genomicset(self): cov.coverage_from_genomicset(bamfile) print(cov.coverage) self.assertEqual(cov.coverage, 4)
from fisher import pvalue back = False designFile = sys.argv[1] anotationPath = sys.argv[2] genomeFile = anotationPath + "chrom.sizes" geneFile = anotationPath + "association_file.bed" exps = ExperimentalMatrix() exps.read(designFile) beds = [] geneLists = [] #this should be improved bedGenes = GenomicRegionSet(geneFile) bedGenes.read_bed(geneFile) allgenes = [] for r in bedGenes: allgenes.append(r.name) allgenes = list(set(allgenes)) genesets = exps.get_genesets() if len(sys.argv) > 3: back = True backGroundPeaks = sys.argv[3] backBed = GenomicRegionSet("BACK") backBed.read_bed(backGroundPeaks) backBed = GenomicRegionSet("BACK")