Exemple #1
0
def getElementPath(element):
  """ return a qualified name for the element if it is possible to compute one
      by concatenating the "name" of parent elements.
      The path is computed according to some appropriate
      "parentship" association depending on the type of elements.
      If it is not possible to get the path, then return the id of the element.
  """
  try:
    names = map( lambda x:x.getName(), 
                 getElementParents(element,inclusive=True,reverse=True))
    if exists(isEmpty,names):
      return unicode(getElementId(element))
    else:
      return unicode(".".join(names))
  except:
    return unicode(getElementId(element))  
Exemple #2
0
import os
import sys
from misc import args, sep, exists, pd, run, err

if len(args) < 4:
    err("python3 raster_stack.py [input raster 1] [input raster 2] .." +
        " [input raster n] [output raster name]")

rasters = args[1:-1]  # raster files to cat together
outf = args[-1]  # output filename
ofhn = args[-1][:-4] + '.hdr'  # output header file name
cmd = ['cat'] + rasters + ['>', outf]  # cat command
cmd = ' '.join(cmd)
print(cmd)
print('')
if not exists(outf):
    a = os.system(cmd)

for r in rasters:
    print(" ", r)

cmd = [
    'python3',  # envi_header_cat.py is almost like a reverse-polish notation. Have to put the "first thing" on the back..
    pd + 'envi_header_cat.py',
    rasters[1][:-4] + '.hdr',
    rasters[0][:-4] + '.hdr',
    ofhn
]  # 'raster.hdr']
cmd = ' '.join(cmd)
run(cmd)
Exemple #3
0
fn, s = args[1], args[2]
cd = pd + '..' + sep + 'cpp' + sep

hfn = hdr_fn(fn)
samples, lines, bands = read_hdr(hfn)
nrow, ncol = lines, samples

bi = []
bn = band_names(hfn)
for i in range(len(bn)):
    if len(bn[i].split(s)) > 1:
        bi.append(i)
        print('  ', str(i + 1), '  ', bn[i])

if not exists(fn + '_001.hdr'):
    run(cd + 'unstack.exe ' + fn)

for i in bi:
    f = fn + '_' + str(i + 1).zfill(3) + '.bin'
    print(f)
fni = [fn + '_' + str(i + 1).zfill(3) + '.bin' for i in bi]
bands = str(len(bi))  # number of bands to write
ofn = fn + '_' + s + '.bin'
ohn = fn + '_' + s + '.hdr'
run('cp ' + hfn + ' ' + ohn)

c = ['python3', pd + 'envi_header_modify.py', ohn, nrow, ncol, bands]
c += [('"' + bn[bi[i]] + '"') for i in range(len(bi))]
c = ' '.join(c)
run(c)
Exemple #4
0
import sys
from misc import err, run, pd, sep, exists, args

cd = pd + '..' + sep + 'cpp' + sep

if len(args) < 2:
    err('sentinel2_trace_active.py [input file, binary mask envi type 4] [optional arg: class index]'
        )

class_select = None
if len(args) > 2:
    class_select = int(args[2])
print(class_select)

fn = args[1]
if not exists(fn):
    err('please check input file')

for i in [150]:  # [10, 20, 60]: # 90
    if not exists(fn + '_flood4.bin'):
        run('ulimit -s 1000000')
        run(cd + 'flood.exe ' + fn)
    if not exists(fn + '_flood4.bin_link.bin'):
        run(cd + 'class_link.exe ' + fn + '_flood4.bin  ' + str(i))  # 40')
    if not exists(fn + '_flood4.bin_link.bin_recode.bin'):
        run(cd + 'class_recode.exe ' + fn + '_flood4.bin_link.bin 1')
    if not exists(fn + '_flood4.bin_link.bin_recode.bin_wheel.bin'):
        run(cd + 'class_wheel.exe ' + fn + '_flood4.bin_link.bin_recode.bin')
        run('python3 ' + pd + 'raster_plot.py ' + fn +
            '_flood4.bin_link.bin_recode.bin_wheel.bin  1 2 3 1 1')
    print('class onehot..')
# 2) run the fire detection!
cmds = []
dets = []
for x in L:
    # print(x)
    df = 'SENTINEL2_L2A_EPSG*10m.bin'
    cmd = 'find ' + x[1] + ' -name ' + df
    y = os.popen(cmd).read().strip()
    if y == '':
        print('x warning: no data ' + x[1])
    else:
        # run detector
        fn = y + '_active.bin'
        print('* ' + fn)
        dets.append([x[0], x[1], y, fn])
        if not exists(fn):
            cmd = pd + '../cpp/sentinel2_active.exe ' + y
            print(cmd)
            cmds.append(cmd)


def r(x):
    return os.popen(x).read()


parfor(r, cmds, 4)  # limited by disk I/O

# 3) cat the results together?
nrow, ncol, nband = 0, 0, 0
cmd = 'cat '
for d in dets:
Exemple #6
0
def make_hist_se(infile, outfile, hashing_val, l10p_cutoff, l2fc_cutoff, all_exons, exon_overhang, intron_overhang):
    try:
        region_types = ["upstream_region_skipped_exon",
                        "upstream_region_downstream_exon",
                        "downstream_region_skipped_exon",
                        "downstream_region_upstream_exon"]
        position_sum = {}
        count = []
        event_dict = defaultdict(dict)

        event_dict['downstream_region_upstream_exon'] = {}
        event_dict['upstream_region_skipped_exon'] = {}
        event_dict['downstream_region_skipped_exon'] = {}
        event_dict['upstream_region_downstream_exon'] = {}

        intersect = 0
        no_intersect = 0

        with open(infile, 'r') as f:
            for line in f:
                intersects_any_region = False # if this flag is true, then a peak intersects a region.
                line = line.split('\t')
                chrom = line[0]
                pstart = int(line[1])
                pstop = int(line[2])
                l10p = float(line[3])
                l2fc = float(line[4])
                stra = line[5].strip()

                # correct bed files being 0-based, open ended
                pstart = pstart + 1

                if l10p < l10p_cutoff:
                    continue
                if l2fc < l2fc_cutoff:
                    continue

                x = int(pstart / hashing_val)
                y = int(pstop / hashing_val)

                p_str = '{}:{}-{}:{}'.format(chrom, pstart, pstop, stra)
                # for each peak, find ALL regions that intersect it
                for region_type in region_types:  # within a region
                    tmphash = {}
                    for i in range(x, y + 1):  # within a bin
                        for event in all_exons[chrom, stra, i, region_type]:
                            exchr, exregstart, exstart, exregstop, exstr = event.split(':')
                            if pstop < int(exregstart):
                                continue
                            if pstart > int(exregstop):  # pass if peak stop occurs before exon region start
                                continue
                            tmphash[event] = 1  # otherwise peak falls within event region
                            intersects_any_region = True
                    # event_dict[region_type][p_str] = tmphash
                    for event in tmphash:
                        if stra == "+":
                            exchr, exregstart, exstart, exregstop, exstr = event.split(':')
                            start_val = max(int(pstart), int(exregstart))  # peak start OR region start
                            end_val = min(int(pstop), int(exregstop))  # peak stop OR region stop
                            for j in range(start_val,
                                           end_val + 1):  # count intersecting positions between peak and region
                                relative_pos = j - int(exstart)  # calculate relative position
                                position_sum[region_type, relative_pos] = misc.ini(position_sum,
                                                                                   region_type,
                                                                                   relative_pos)  # count + 1 for the region
                        elif stra == '-':
                            exchr, exregstart, exstart, exregstop, exstr = event.split(':')
                            start_val = max(int(pstart), int(exregstart))
                            end_val = min(int(pstop), int(exregstop))
                            for j in range(start_val, end_val + 1):
                                relative_pos = -1 * (j - int(exstart))
                                position_sum[region_type, relative_pos] = misc.ini(position_sum,
                                                                                   region_type,
                                                                                   relative_pos)
                        else:
                            print("strand error\n")
                if intersects_any_region:
                    intersect+=1
                else:
                    no_intersect+=1
        o = open(outfile + '.overlapping_peaks', 'w')
        o.write('infile\tintersect\tno_intersect\n')
        o.write('{}\t{}\t{}'.format(infile, intersect, no_intersect))
        o.close()
        # print(event_dict['downstream_region_skipped_exon']['chr11:47237411-47237505:+'])

        # count from 0 to max
        current_pos = 0
        o = open(outfile, 'w')
        for j in range(-exon_overhang, intron_overhang + 1):
            if misc.exists(position_sum, "downstream_region_upstream_exon", j):
                o.write("{}\n".format(position_sum["downstream_region_upstream_exon", j]))
                count.append(position_sum["downstream_region_upstream_exon", j])
            else:
                o.write("{}\n".format(0))
                count.append(0)
            current_pos = current_pos + 1
        for j in range(-intron_overhang, exon_overhang + 1):
            if misc.exists(position_sum, "upstream_region_skipped_exon", j):
                o.write("{}\n".format(position_sum["upstream_region_skipped_exon", j]))
                count.append(position_sum["upstream_region_skipped_exon", j])
            else:
                o.write("{}\n".format(0))
                count.append(0)
            current_pos = current_pos + 1
        for j in range(-exon_overhang, intron_overhang + 1):
            if misc.exists(position_sum, "downstream_region_skipped_exon", j):
                o.write("{}\n".format(position_sum["downstream_region_skipped_exon", j]))
                count.append(position_sum["downstream_region_skipped_exon", j])
            else:
                o.write("{}\n".format(0))
                count.append(0)
            current_pos = current_pos + 1
        for j in range(-intron_overhang, exon_overhang + 1):
            if misc.exists(position_sum, "upstream_region_downstream_exon", j):
                o.write("{}\n".format(position_sum["upstream_region_downstream_exon", j]))
                count.append(position_sum["upstream_region_downstream_exon", j])
            else:
                o.write("{}\n".format(0))
                count.append(0)
            current_pos = current_pos + 1
        o.close()
        return count
    except Exception as e:
        print(e)
Exemple #7
0
def make_hist_a5ss(infile, outfile, hashing_val, l10p_cutoff, l2fc_cutoff, all_exons, exon_overhang, intron_overhang):
    try:
        # region_types = ["ex_up","dnex","ex_dn","upex"] # former annotation
        region_types = ["upstream_region_skipped_exon",
                        "downstream_region_skipped_exon",
                        "upstream_region_downstream_exon",
                        "downstream_region_upstream_exon"]
        position_sum = {}
        count = 0
        with open(infile, 'r') as f:
            for line in f:
                line = line.split('\t')
                chrom = line[0]
                pstart = int(line[1])
                pstop = int(line[2])
                l10p = float(line[3])
                l2fc = float(line[4])
                stra = line[5].strip()

                # correct bed files being 0-based, open ended
                pstart = pstart + 1

                if l10p < l10p_cutoff:
                    continue
                if l2fc < l2fc_cutoff:
                    continue

                x = int(pstart / hashing_val)
                y = int(pstop / hashing_val)

                # for each peak, find ALL regions that intersect it
                for region_type in region_types:  # within a region
                    tmphash = {}
                    for i in range(x, y + 1):  # within a bin
                        for event in all_exons[chrom, stra, i, region_type]:
                            exchr, exregstart, exstart, exregstop, exstr = event.split(':')
                            if pstop < int(exregstart):  # pass if peak stop occurs before exon region start
                                continue
                            if pstart > int(exregstop):  # pass if peak start occurs after exon region end
                                continue
                            tmphash[event] = 1  # otherwise peak falls within event region
                    for event in tmphash:
                        if stra == "+":
                            exchr, exregstart, exstart, exregstop, exstr = event.split(':')
                            start_val = max(int(pstart), int(exregstart))  # peak start OR region start
                            end_val = min(int(pstop), int(exregstop))  # peak stop OR region stop
                            for j in range(start_val,
                                           end_val + 1):  # count intersecting positions between peak and region
                                relative_pos = j - int(exstart)  # calculate relative position
                                position_sum[region_type, relative_pos] = misc.ini(position_sum,
                                                                                   region_type,
                                                                                   relative_pos)  # count + 1 for the region
                        elif stra == '-':
                            exchr, exregstart, exstart, exregstop, exstr = event.split(':')
                            start_val = max(int(pstart), int(exregstart))
                            end_val = min(int(pstop), int(exregstop))
                            for j in range(start_val, end_val + 1):
                                relative_pos = -1 * (j - int(exstart))
                                position_sum[region_type, relative_pos] = misc.ini(position_sum,
                                                                                   region_type,
                                                                                   relative_pos)
                        else:
                            print("strand error\n")

        # count from 0 to max
        current_pos = 0
        o = open(outfile, 'w')
        for j in range(-exon_overhang, 1):
            if misc.exists(position_sum, "downstream_region_upstream_exon", j):
                o.write("{}\n".format(position_sum["downstream_region_upstream_exon", j]))
            else:
                o.write("{}\n".format(0))
            current_pos = current_pos + 1
        # o.write("STOP")
        for j in range(0, exon_overhang + 1):
            if misc.exists(position_sum, "upstream_region_skipped_exon", j):
                o.write("{}\n".format(position_sum["upstream_region_skipped_exon", j]))
            else:
                o.write("{}\n".format(0))
            current_pos = current_pos + 1
        # o.write("STOP")
        for j in range(-exon_overhang, intron_overhang + 1):
            if misc.exists(position_sum, "downstream_region_skipped_exon", j):
                o.write("{}\n".format(position_sum["downstream_region_skipped_exon", j]))
            else:
                o.write("{}\n".format(0))
            current_pos = current_pos + 1
        for j in range(-intron_overhang, exon_overhang + 1):
            if misc.exists(position_sum, "upstream_region_downstream_exon", j):
                o.write("{}\n".format(position_sum["upstream_region_downstream_exon", j]))
            else:
                o.write("{}\n".format(0))
            current_pos = current_pos + 1
        o.close()
    except Exception as e:
        print(e)