Example #1
0
def CF_export(args):
    try:
        h5file_in_fn = str(args.input)
        h5file_in = openFile(h5file_in_fn, mode='r')
    except IOError as e:
        print '[ERROR] Cannot open CoNIFER input file for reading: ', h5file_in_fn
        sys.exit(0)

    # read probes
    probes = {}
    for probes_chr in h5file_in.root.probes:
        probes[probes_chr.title] = probes_chr.read()

    if args.sample == 'all':
        all_samples = list(
            h5file_in.root.samples.samples.read(field="sampleID"))

        out_path = os.path.abspath(args.output)

        print "[INIT] Preparing to export all samples (%d samples) to %s" % (
            len(all_samples), out_path)
        for sample in all_samples:
            try:
                outfile_fn = out_path + "/" + sample + ".bed"
                outfile_f = open(outfile_fn, 'w')
            except IOError as e:
                print '[ERROR] Cannot open output file for writing: ', outfile_fn
                sys.exit(0)
            print "[RUNNING] Exporting %s" % sample

            cf.export_sample(h5file_in, sample, probes, outfile_f)
            outfile_f.close()

    elif len(args.sample) == 1:
        out_path = os.path.abspath(args.output)
        sample = args.sample[0]
        print "[INIT] Preparing to export sampleID %s to %s" % (args.sample[0],
                                                                out_path)
        try:
            if os.path.isdir(out_path):
                outfile_fn = out_path + "/" + sample + ".bed"
            else:
                outfile_fn = out_path
            outfile_f = open(outfile_fn, 'w')
        except IOError as e:
            print '[ERROR] Cannot open output file for writing: ', outfile_fn
            sys.exit(0)
        print "[RUNNING] Exporting %s to %s" % (sample, outfile_fn)

        cf.export_sample(h5file_in, sample, probes, outfile_f)
        outfile_f.close()

    else:
        out_path = os.path.abspath(args.output)
        print "[INIT] Preparing to export %d samples to %s" % (len(
            args.sample), out_path)
        for sample in args.sample:
            try:
                if os.path.isdir(out_path):
                    outfile_fn = out_path + "/" + sample + ".bed"
                else:
                    outfile_fn = out_path
                outfile_f = open(outfile_fn, 'w')
            except IOError as e:
                print '[ERROR] Cannot open output file for writing: ', outfile_fn
                sys.exit(0)
            print "[RUNNING] Exporting %s to %s" % (sample, outfile_fn)

            cf.export_sample(h5file_in, sample, probes, outfile_f)
            outfile_f.close()
    sys.exit(0)
Example #2
0
def CF_export(args):
	try: 
		h5file_in_fn = str(args.input)
		h5file_in = openFile(h5file_in_fn, mode='r')
	except IOError as e: 
		print '[ERROR] Cannot open CoNIFER input file for reading: ', h5file_in_fn
		sys.exit(0)	
	
	# read probes
	probes = {}
	for probes_chr in h5file_in.root.probes:
		probes[probes_chr.title] = probes_chr.read()
	
	if args.sample =='all':
		all_samples = list(h5file_in.root.samples.samples.read(field="sampleID"))
		
		out_path = os.path.abspath(args.output)
		
		print "[INIT] Preparing to export all samples (%d samples) to %s" % (len(all_samples), out_path)
		for sample in all_samples:
			try:
				outfile_fn = out_path + "/" + sample + ".bed"
				outfile_f = open(outfile_fn,'w')
			except IOError as e:
				print '[ERROR] Cannot open output file for writing: ', outfile_fn
				sys.exit(0)
			print "[RUNNING] Exporting %s" % sample
			
			cf.export_sample(h5file_in,sample,probes,outfile_f)
			outfile_f.close()
	
	elif len(args.sample) == 1:
		out_path = os.path.abspath(args.output)
		sample = args.sample[0]
		print "[INIT] Preparing to export sampleID %s to %s" % (args.sample[0], out_path)
		try:
			if os.path.isdir(out_path):
				outfile_fn = out_path + "/" + sample + ".bed"
			else:
				outfile_fn = out_path
			outfile_f = open(outfile_fn,'w')
		except IOError as e:
			print '[ERROR] Cannot open output file for writing: ', outfile_fn
			sys.exit(0)
		print "[RUNNING] Exporting %s to %s" % (sample, outfile_fn)
		
		cf.export_sample(h5file_in,sample,probes,outfile_f)
		outfile_f.close()
	
	else:
		out_path = os.path.abspath(args.output)
		print "[INIT] Preparing to export %d samples to %s" % (len(args.sample), out_path)
		for sample in args.sample:
			try:
				if os.path.isdir(out_path):
					outfile_fn = out_path + "/" + sample + ".bed"
				else:
					outfile_fn = out_path
				outfile_f = open(outfile_fn,'w')
			except IOError as e:
				print '[ERROR] Cannot open output file for writing: ', outfile_fn
				sys.exit(0)
			print "[RUNNING] Exporting %s to %s" % (sample, outfile_fn)
			
			cf.export_sample(h5file_in,sample,probes,outfile_f)
			outfile_f.close()		
	sys.exit(0)