def load_genome(genome_path, res=None, verbose=False): """ Search, at a given path, for chromosome folders containing TADbit chromosome objects saved as files. :param genome_path: Path where to search for TADbit chomosomes :param None res: Resolution at which should be saved chromosomes :param False verbose: :returns: a dictionary with all TADbit chromosomes found """ ref_genome = {} for crm in listdir(genome_path): crm_path = os.path.join(genome_path, crm) if not isdir(crm_path): continue for crm_fh in listdir(crm_path): crm_pik = os.path.join(crm_path, crm_fh) if not check_pik(crm_pik): continue if crm in ref_genome: raise Exception('More than 1 TADbit chromosome file found\n') if verbose: print ' Chromosome:', crm ref_genome[crm] = load_chromosome(crm_pik) # check resolution resolutions = [] if not res else [res] for crm in ref_genome: for exp in ref_genome[crm].experiments: resolutions.append(exp.resolution) if not all([r == resolutions[0] for r in resolutions]): raise AssertionError('Not all Experiments have the ' + 'same resolution\n') res = resolutions[0] return ref_genome
def load_genome(genome_path, res=None, verbose=False): """ Search, at a given path, for chromosome folders containing TADbit chromosome objects saved as files. :param genome_path: Path where to search for TADbit chomosomes :param None res: Resolution at which should be saved chromosomes :param False verbose: :returns: a dictionary with all TADbit chromosomes found """ ref_genome = {} for crm in listdir(genome_path): crm_path = os.path.join(genome_path, crm) if not isdir(crm_path): continue for crm_fh in listdir(crm_path): crm_pik = os.path.join(crm_path, crm_fh) if not check_pik(crm_pik): continue if crm in ref_genome: raise Exception('More than 1 TADbit chromosome file found\n') if verbose: print ' Chromosome:', crm ref_genome[crm] = load_chromosome(crm_pik) # check resolution resolutions = [] if not res else [res] for crm in ref_genome: for exp in ref_genome[crm].experiments: resolutions.append(exp.resolution) if not all([r == resolutions[0] for r in resolutions]): raise AssertionError('Not all Experiments have the ' + 'same resolution\n') res = resolutions[0] return ref_genome
def test_05_save_load(self): test_chr = Chromosome(name='Test Chromosome', experiment_tads=[exp1, exp2], experiment_names=['exp1', 'exp2'], experiment_resolutions=[20000,20000]) test_chr.save_chromosome('lolo', force=True) test_chr = load_chromosome('lolo') system('rm -f lolo') system('rm -f lolo_hic')
def loadData(tdbfile, samples): my_chrom = load_chromosome(tdbfile) for i, exp in enumerate(my_chrom.experiments): try: label, path = samples[i].split(",") print(path) print(exp.name) exp.load_hic_data(path) exp.normalize_hic(factor=1) exp.filter_columns() except IOError: print 'file not found for experiment: ' + exp.name continue return my_chrom
def test_05_save_load(self): if CHKTIME: t0 = time() test_chr1 = Chromosome(name='Test Chromosome', experiment_tads=[exp1, exp2], experiment_names=['exp1', 'exp2'], experiment_resolutions=[20000, 20000], silent=True) test_chr1.save_chromosome('lolo', force=True) test_chr2 = load_chromosome('lolo') system('rm -f lolo') system('rm -f lolo_hic') self.assertEqual(str(test_chr1.__dict__), str(test_chr2.__dict__)) if CHKTIME: print '5', time() - t0
def test_05_save_load(self): if CHKTIME: t0 = time() test_chr1 = Chromosome(name='Test Chromosome', experiment_tads=[exp1, exp2], experiment_names=['exp1', 'exp2'], experiment_resolutions=[20000,20000], silent=True) test_chr1.save_chromosome('lolo', force=True) test_chr2 = load_chromosome('lolo') system('rm -f lolo') system('rm -f lolo_hic') self.assertEqual(str(test_chr1.__dict__), str(test_chr2.__dict__)) if CHKTIME: print '5', time() - t0
def test_05_save_load(self): if ONLY and not "05" in ONLY: return if CHKTIME: t0 = time() test_chr1 = Chromosome(name="Test Chromosome", experiment_tads=[exp1, exp2], experiment_names=["exp1", "exp2"], experiment_resolutions=[20000, 20000], silent=True) test_chr1.save_chromosome("lolo", force=True) test_chr2 = load_chromosome("lolo") system("rm -f lolo") system("rm -f lolo_hic") self.assertEqual(str(test_chr1.__dict__), str(test_chr2.__dict__)) if CHKTIME: print "5", time() - t0
def test_05_save_load(self): if ONLY and ONLY != "05": return if CHKTIME: t0 = time() test_chr1 = Chromosome( name="Test Chromosome", experiment_tads=[exp1, exp2], experiment_names=["exp1", "exp2"], experiment_resolutions=[20000, 20000], silent=True, ) test_chr1.save_chromosome("lolo", force=True) test_chr2 = load_chromosome("lolo") system("rm -f lolo") system("rm -f lolo_hic") self.assertEqual(str(test_chr1.__dict__), str(test_chr2.__dict__)) if CHKTIME: print "5", time() - t0
def main(): """ main function """ opts = get_options() res = opts.res if opts.genomes: # load all chromosomes of reference genomes ref_genome = {} for crm in listdir(opts.ref_genome): crm_path = opts.ref_genome + crm + '/' if not isdir(crm_path): continue for crm_fh in listdir(crm_path): crm_pik = crm_path + crm_fh if not check_pik(crm_pik): continue ref_genome[crm] = load_chromosome(crm_pik) if not opts.res: resolutions = [] for crm in ref_genome: for exp in ref_genome[crm].experiments: resolutions.append(exp.resolution) if not all([r == resolutions[0] for r in resolutions]): raise AssertionError('Not all Experiments have the ' + 'same resolution\n') res = resolutions[0] alt_genomes = {} for i, genome in enumerate(opts.genomes): alt_genomes[i] = {} for crm in listdir(genome): crm_path = genome + crm + '/' if not isdir(crm_path): continue for crm_fh in listdir(crm_path): crm_pik = crm_path + crm_fh if not check_pik(crm_pik): continue try: alt_genomes[i][crm] = load_chromosome(crm_pik) except: print('SKIPPING: {} \n not a valid ' + 'chromosome').format(crm_pik) genome = {} for crm in alt_genomes[i]: genome = remap_chr(alt_genomes[i][crm], crm, '/tmp/', opts.lft_path, opts.chain_path, genome=genome) reorder(genome) for exp in genome: for crm in genome[exp]: try: ref_genome[crm].add_experiment( exp, res, tad_handler=genome[exp][crm]) except KeyError: print('Chromosome {} skipped, not in reference ' + 'genome').format(crm) system('mkdir -p ' + opts.out_path) for crm in ref_genome: system('mkdir -p ' + opts.out_path + '/' + crm) out_f = opts.out_path + '/' + crm + '/chr' + crm + '.tdb' ref_genome[crm].save_chromosome(out_f, force=True)
def main(): """ main function """ opts = get_options() res = opts.res if opts.genomes: # load all chromosomes of reference genomes ref_genome = {} for crm in listdir(opts.ref_genome): crm_path = opts.ref_genome + crm + '/' if not isdir(crm_path): continue for crm_fh in listdir(crm_path): crm_pik = crm_path + crm_fh if not check_pik(crm_pik): continue ref_genome[crm] = load_chromosome(crm_pik) if not opts.res: resolutions = [] for crm in ref_genome: for exp in ref_genome[crm].experiments: resolutions.append(exp.resolution) if not all([r == resolutions[0] for r in resolutions]): raise AssertionError('Not all Experiments have the ' + 'same resolution\n') res = resolutions[0] alt_genomes = {} for i, genome in enumerate(opts.genomes): alt_genomes[i] = {} for crm in listdir(genome): crm_path = genome + crm + '/' if not isdir(crm_path): continue for crm_fh in listdir(crm_path): crm_pik = crm_path + crm_fh if not check_pik(crm_pik): continue try: alt_genomes[i][crm] = load_chromosome(crm_pik) except: print ('SKIPPING: {} \n not a valid ' + 'chromosome').format(crm_pik) genome = {} for crm in alt_genomes[i]: genome = remap_chr(alt_genomes[i][crm], crm, '/tmp/', opts.lft_path, opts.chain_path, genome=genome) reorder(genome) for exp in genome: for crm in genome[exp]: try: ref_genome[crm].add_experiment( exp, res, tad_handler=genome[exp][crm]) except KeyError: print ('Chromosome {} skipped, not in reference ' + 'genome').format(crm) system('mkdir -p ' + opts.out_path) for crm in ref_genome: system('mkdir -p ' + opts.out_path + '/' + crm) out_f = opts.out_path + '/' + crm + '/chr' + crm + '.tdb' ref_genome[crm].save_chromosome(out_f, force=True)