예제 #1
0
def add_mapping_to_db():
    f = FileHandler('../../data/mapping.txt')
    for r in f:
        tokens = r.split('\t')
        rsname, gn, mn, version = tokens[0:4]
        mapped_count, unique_count, pos_unique_count, neg_unique_count, s_name = tokens[4:9]
        params = tokens[9:]
        rs = Raw_sample.objects.get(name=rsname)
        genome = Genome.objects.get(assembly_name=gn)
        method = Method.objects.get(method_name=mn, method_version=version)
        
        sid = transaction.savepoint()
        try:
            '''
            param_set = Parameter_set(method_obj = method)
            param_set.save()
            for p in params:
                t = p.split(':')
                if len(t) == 1:
                    t.append('')
                param = Parameter(param_set = param_set, param_name = t[0], param_value = t[1])
                param.save()
            '''
            param_set = add_params_to_db(params, method)
            mapping = Mapping(method_obj=method, genome_obj=genome, params=param_set, sample_obj=rs)
            mapping.save()
            sample = Sample(name = s_name, mapped_tags = mapped_count, unique_tags = unique_count, positive_unique_count = pos_unique_count, negative_unique_count = neg_unique_count, mapping_obj = mapping)
            sample.save()
            transaction.savepoint_commit(sid)
        except IntegrityError:
            print 'error'
            transaction.savepoint_rollback(sid)
    f.close()
예제 #2
0
def add_peakcalling_to_db():
    f = FileHandler("../../data/peakcalling.txt")
    
    
    for r in f:
        tokens = r.split('\t')
        s_name, method_name, method_version = tokens[0:3]
        peak_file = tokens[3]
        params = tokens[4:]
        print "a"
        sample = Sample.objects.get(name=s_name)
        method = Method.objects.get(method_name = method_name, method_version = method_version)
        print 'b'
        '''
        run = models.ForeignKey(Call_peak)
        chrom = models.CharField(max_length=40)
        start = models.PositiveIntegerField(null=False) #0-based
        end = models.PositiveIntegerField(null=False) #1-based
        size = models.PositiveIntegerField(null=False)
        strand = models.CharField(max_length=1, choices=(('+', 'Positive'), ('-', 'Negative'), ('.', 'Unspecified')))
        tag_count = models.FloatField(null=False)
        summit_pos_pileup = models.PositiveSmallIntegerField(null=False) #Origin summit location by peak calling method
        summit_val_pileup = models.FloatField(null=False)
        summit_pos_5 = models.PositiveSmallIntegerField()
        summit_val_5 = models.FloatField()
        p_value = models.FloatField()
        q_value = models.FloatField()
        fold = models.FloatField()
        '''
        
        
        sid = transaction.savepoint()
        
        try:
            param_set = add_params_to_db(params, method)
            callpeak = Call_peak(sample_obj = sample, method_obj = method, params = param_set)
            callpeak.save()
            sid = transaction.savepoint()
            ph = FileHandler(peak_file)
            for p in ph:
                ptokens = p.split('\t')
                chrom, start, end, length, strand, abs_summit, pileup, pileup_5, p_val, fold, q_val, _, tc, rpkm = ptokens
                start = int(start)
                abs_summit = int(abs_summit)
                tc = float(tc)
                #print p
                newp = Peak(run=callpeak, chrom=chrom, start=start-1, end=end,
                            size=length, strand=strand, tag_count=tc, summit_pos_pileup=abs_summit-1-start,
                            summit_val_pileup = pileup, summit_pos_5 = abs_summit-1-start, summit_val_5=pileup_5,
                            p_value = p_val, q_value = q_val, fold = fold)
                newp.save()
            transaction.savepoint_commit(sid)
            ph.close()
        except IntegrityError, msg:
            print msg
            transaction.savepoint_rollback(sid)
예제 #3
0
def add_method_to_db():
    f = FileHandler("../../data/methods.txt")
    sid = transaction.savepoint()
    for r in f:
        tokens = r.split('\t')
        m = Method(method_name=tokens[0],
                   method_type=tokens[1],
                   method_version=tokens[2])
        sid = transaction.savepoint()
        try:
            m.save()
            transaction.savepoint_commit(sid)
        except DatabaseError:
            transaction.savepoint_rollback(sid)
    f.close()
예제 #4
0
def add_genome_to_db():
    f = FileHandler("../../data/genomes.txt")
    #sid = transaction.savepoint()
    for r in f:
        tokens = r.split('\t')
        g = Genome(assembly_name = tokens[0],
                   alternative_assembly_name = tokens[1],
                   species = tokens[2],
                   size = tokens[3])
        sid = transaction.savepoint()    #create save points before transaction for rollback if error occurs.
        try:
            g.save()
            transaction.savepoint_commit(sid)
        except IntegrityError:
            print "error"
            transaction.savepoint_rollback(sid)
    f.close()
예제 #5
0
def add_chromosomes_to_db():
    genomes = Genome.objects.all()
    if len(genomes) <= 0:
        print "No genomes. Abort."
    else:
        for g in genomes:
            f = FileHandler('../../data/%s.chrom.sizes'%g.assembly_name)
            #sid = transaction.savepoint()
            for r in f:
                cname, csize = r.split()
                chrom = Chromosome(name=cname, length=csize, genome_obj=g)
                sid = transaction.savepoint()
                try:
                    chrom.save()
                    transaction.savepoint_commit(sid)
                except IntegrityError:
                    transaction.savepoint_rollback(sid)
                    print 'error'
            
            f.close()
예제 #6
0
    '''
    dw = DownloadFromWeb("ftp.fu-berlin.de","pub/misc/movies/database" )
    download = False
    option = 0
    try:
        option = int(input("Do you want to download the new version of the databases if available? (1=yes, 0= no)"))
        if option != 0 and option != 1:
            raise ValueError()
    except ValueError:
        print("Option not found, assuming default value: no")
        
    if option == 1:
        download = True
    result = dw.fetch("actresses.list.gz", download)
    print(result)
    result = dw.fetch("actors.list.gz", download)
    print(result)

    print("All fetched")
    print("Loading actors")
    actorHandler = FileHandler()
    actorHandler.open("actors.list.gz")
    actorHandler.loadData()
    print("Actors loaded, closing files")
    actorHandler.close()    
    print("Loading actresses")
    actressHandler = FileHandler()
    actressHandler.open("actresses.list.gz")
    actressHandler.loadData()
    print("Actresses loaded, closing files")
    actressHandler.close()