Example #1
0
def _get_new_or_db_barcode(name, index):
    if (name and index):
        barcodes = dnaBarcode.objects.filter(name=name, index=index)
        if barcodes:
            print("db barcode FOUND for name=%s; index=%s" % (name, index))
            return barcodes[0]
    return dnaBarcode()
Example #2
0
def add_or_update_dnaBarcode(fields):
    name = fields.get("name")
    sequence = fields.get("sequence")
    if sequence == "ALL":
        updateObjs = dnaBarcode.objects.filter(name=name)
        if updateObjs.count() == 0:
            raise Exception("Barcode set %s is not installed" % name)
        else:
            fields.pop("sequence")
            updateObjs.update(**fields) 
    else:
        try:
            barcodeObj = dnaBarcode.objects.get(name=name, sequence=sequence)
        except dnaBarcode.DoesNotExist:
            barcodeObj = dnaBarcode()

        for key, value in fields.items():
            setattr(barcodeObj, key, value)
        barcodeObj.save()
Example #3
0
def add_or_update_dnaBarcode(fields):
    name = fields.get("name")
    sequence = fields.get("sequence")
    if sequence == "ALL":
        updateObjs = dnaBarcode.objects.filter(name=name)
        if updateObjs.count() == 0:
            raise Exception("Barcode set %s is not installed" % name)
        else:
            fields.pop("sequence")
            updateObjs.update(**fields) 
    else:
        try:
            barcodeObj = dnaBarcode.objects.get(name=name, sequence=sequence)
        except dnaBarcode.DoesNotExist:
            barcodeObj = dnaBarcode()

        for key, value in fields.items():
            setattr(barcodeObj, key, value)
        barcodeObj.save()
Example #4
0
def add_or_update_barcode_set2(blist,btype,name,adapter, scoreMode, scoreCutoff, alt_barcode_prefix = None, skip_barcode_leading_zero = False):
    print("add_or_update_barcode_set2... name=%s; alt_barcode_prefix=%s" %(name, alt_barcode_prefix))
    
# Attempt to read dnabarcode set named 'IonXpress' from dbase
    dnabcs = models.dnaBarcode.objects.filter(name=name)
    if len(dnabcs) > 0:
        #print '%s dnaBarcode Set exists in database' % name
        # make sure we have all the sequences we expect
        for index,sequence in enumerate(blist,start=1):
            # Search for this sequence in the list of barcode records
            bc_found = dnabcs.filter(sequence=sequence)
            if len(bc_found) > 1:
                print "ERROR: More than one entry with sequence %s" % sequence
                print "TODO: Fix this situation, Mr. Programmer!"

            # Make sure id string has zero padded index field if skip_barcode_leading_zero is not true
            if alt_barcode_prefix:
                if skip_barcode_leading_zero:
                    barcode_name = '%s%d' % (alt_barcode_prefix,index)
                else:
                    barcode_name = '%s%02d' % (alt_barcode_prefix,index)
            else:
                if skip_barcode_leading_zero:
                    barcode_name = '%s_%d' % (name,index) 
                else:     
                    barcode_name = '%s_%02d' % (name,index)
                            
            if len(bc_found) == 1:

                #print "%s dnaBarcode sequence %s already in the database" % (name, sequence)  
                              
                # Make sure floworder field is not 'none'
                if bc_found[0].floworder == 'none':
                    bc_found[0].floworder = ''
             
                bc_found[0].id_str = barcode_name
                
                # update type
                bc_found[0].type = btype
                                
                # Save changes to database
                bc_found[0].save()
                
            else:   # array length is zero
                #print "Adding entry for %s" % sequence
                kwargs = {
                    'name':name,
                    'id_str': barcode_name,
                    'sequence':sequence,
                    'type':btype,
                    'length':len(sequence),
                    'floworder':'',
                    'index':index,
                    'annotation':'',
                    'adapter':adapter,
                    'score_mode':scoreMode,
                    'score_cutoff':scoreCutoff,
                }
                ret = models.dnaBarcode(**kwargs)
                ret.save()
    else:
        # Add the barcodes because they do not exist.
        # NOTE: name for id_str
        for index,sequence in enumerate(blist,start=1):
            if alt_barcode_prefix:
                if skip_barcode_leading_zero:
                    barcode_name = '%s%d' % (alt_barcode_prefix,index)
                else:
                    barcode_name = '%s%02d' % (alt_barcode_prefix,index)
            else:
                if skip_barcode_leading_zero:
                    barcode_name = '%s_%d' % (name,index) 
                else:     
                    barcode_name = '%s_%02d' % (name,index)
            
            kwargs = {
                'name':name,
                'id_str': barcode_name,
                'sequence':sequence,
                'type':btype,
                'length':len(sequence),
                'floworder':'',
                'index':index,
                'annotation':'',
                'adapter':adapter,
                'score_mode':scoreMode,
                'score_cutoff':scoreCutoff,
            }
            ret = models.dnaBarcode(**kwargs)
            ret.save()
        print '%s dnaBarcode Set added to database' % name
Example #5
0
def add_or_update_barcode_set(blist,btype,name,adapter):
# Attempt to read dnabarcode set named 'IonXpress' from dbase
    dnabcs = models.dnaBarcode.objects.filter(name=name)
    if len(dnabcs) > 0:
        #print '%s dnaBarcode Set exists in database' % name
        # make sure we have all the sequences we expect
        for index,sequence in enumerate(blist,start=1):
            # Search for this sequence in the list of barcode records
            bc_found = dnabcs.filter(sequence=sequence)
            if len(bc_found) > 1:
                print "ERROR: More than one entry with sequence %s" % sequence
                print "TODO: Fix this situation, Mr. Programmer!"
            if len(bc_found) == 1:
                
                # Make sure floworder field is not 'none'
                if bc_found[0].floworder == 'none':
                    bc_found[0].floworder = ''
                    
                # Make sure id string has zero padded index field
                bc_found[0].id_str = '%s_%03d' % (name,index)
                
                # update type
                bc_found[0].type = btype
                
                # Save changes to database
                bc_found[0].save()
                
            else:   # array length is zero
                #print "Adding entry for %s" % sequence
                kwargs = {
                    'name':name,
                    'id_str':'%s_%03d' % (name,index),
                    'sequence':sequence,
                    'type':btype,
                    'length':len(sequence),
                    'floworder':'',
                    'index':index,
                    'annotation':'',
                    'adapter':adapter,
                    'score_mode':1,
                    'score_cutoff':2.0,
                }
                ret = models.dnaBarcode(**kwargs)
                ret.save()
    else:
        # Add the barcodes because they do not exist.
        # NOTE: name for id_str
        for index,sequence in enumerate(blist,start=1):
            kwargs = {
                'name':name,
                'id_str':'%s_%03d' % (name,index),
                'sequence':sequence,
                'type':btype,
                'length':len(sequence),
                'floworder':'',
                'index':index,
                'annotation':'',
                'adapter':adapter,
                'score_mode':1,
                'score_cutoff':2.0,
            }
            ret = models.dnaBarcode(**kwargs)
            ret.save()
        print '%s dnaBarcode Set added to database' % name
Example #6
0
def add_or_update_barcode_set2(blist, btype, name, adapter, scoreMode,
                               scoreCutoff):
    # Attempt to read dnabarcode set named 'IonXpress' from dbase
    dnabcs = models.dnaBarcode.objects.filter(name=name)
    if len(dnabcs) > 0:
        #print '%s dnaBarcode Set exists in database' % name
        # make sure we have all the sequences we expect
        for index, sequence in enumerate(blist, start=1):
            # Search for this sequence in the list of barcode records
            bc_found = dnabcs.filter(sequence=sequence)
            if len(bc_found) > 1:
                print "ERROR: More than one entry with sequence %s" % sequence
                print "TODO: Fix this situation, Mr. Programmer!"
            if len(bc_found) == 1:

                #print "%s dnaBarcode sequence %s already in the database" % (name, sequence)

                # Make sure floworder field is not 'none'
                if bc_found[0].floworder == 'none':
                    bc_found[0].floworder = ''

                # Make sure id string has zero padded index field
                bc_found[0].id_str = '%s_%02d' % (name, index)

                # update type
                bc_found[0].type = btype

                # Save changes to database
                bc_found[0].save()

            else:  # array length is zero
                #print "Adding entry for %s" % sequence
                kwargs = {
                    'name': name,
                    'id_str': '%s_%02d' % (name, index),
                    'sequence': sequence,
                    'type': btype,
                    'length': len(sequence),
                    'floworder': '',
                    'index': index,
                    'annotation': '',
                    'adapter': adapter,
                    'score_mode': scoreMode,
                    'score_cutoff': scoreCutoff,
                }
                ret = models.dnaBarcode(**kwargs)
                ret.save()
    else:
        # Add the barcodes because they do not exist.
        # NOTE: name for id_str
        for index, sequence in enumerate(blist, start=1):
            kwargs = {
                'name': name,
                'id_str': '%s_%02d' % (name, index),
                'sequence': sequence,
                'type': btype,
                'length': len(sequence),
                'floworder': '',
                'index': index,
                'annotation': '',
                'adapter': adapter,
                'score_mode': scoreMode,
                'score_cutoff': scoreCutoff,
            }
            ret = models.dnaBarcode(**kwargs)
            ret.save()
        print '%s dnaBarcode Set added to database' % name
Example #7
0
def _add_barcode(request):
    """add the barcodes, with CSV validation"""

    if request.method == 'POST':
        name = request.POST.get('name', '')
        postedfile = request.FILES['postedfile']

        barCodeSet = dnaBarcode.objects.filter(name=name)
        if barCodeSet:
            return HttpResponse(json.dumps({"status": "Error: Barcode set with the same name already exists"}), mimetype="text/html")

        expectedHeader = ["id_str", "type", "sequence", "floworder", "index", "annotation", "adapter"]

        barCodes = []
        failed = {}
        nucs = ["sequence", "floworder", "adapter"]  # fields that have to be uppercase
        reader = csv.DictReader(postedfile.read().splitlines())
        for index, row in enumerate(reader, start=1):
            invalid = _validate_barcode(row)
            if invalid:  # don't make dna object or add it to the list
                failed[index] = invalid
                continue
            newBarcode = dnaBarcode(name=name, index=index)
            for key in expectedHeader:  # set the values for the objects
                value = row.get(key, None)
                if value:
                    value = value.strip()  # strip the strings
                    if key in nucs:  # uppercase if a nuc
                        value = value.upper()
                    setattr(newBarcode, key, value)
            if not newBarcode.id_str:  # make a id_str if one is not provided
                newBarcode.id_str = str(name) + "_" + str(index)
            newBarcode.length = len(newBarcode.sequence)  # now set a default
            barCodes.append(newBarcode)  # append to our list for later saving

        if failed:
            r = {"status": "Barcodes validation failed. The barcode set has not been saved.", "failed": failed}
            return HttpResponse(json.dumps(r), mimetype="text/html")
        if not barCodes:
            return HttpResponse(json.dumps({"status": "Error: There must be at least one barcode! Please reload the page and try again with more barcodes."}), mimetype="text/html")
        usedID = []
        for barCode in barCodes:
            if barCode.id_str not in usedID:
                usedID.append(barCode.id_str)
            else:
                error = {"status": "Duplicate id_str for barcodes named: " + str(barCode.id_str) + "."}
                return HttpResponse(json.dumps(error), mimetype="text/html")
        usedIndex = []
        for barCode in barCodes:
            if barCode.index not in usedIndex:
                usedIndex.append(barCode.index)
            else:
                error = {"status": "Duplicate index: " + barCode.index + "."}
                return HttpResponse(json.dumps(error), mimetype="text/html")

        #saving to db needs to be the last thing to happen
        for barCode in barCodes:
            try:
                barCode.save()
            except:
                logger.exception("Error saving barcode to database")
                return HttpResponse(json.dumps({"status": "Error saving barcode to database!"}), mimetype="text/html")
        r = {"status": "Barcodes Uploaded! The barcode set will be listed on the references page.", "failed": failed, 'success': True}
        return HttpResponse(json.dumps(r), mimetype="text/html")
Example #8
0
def add_or_update_barcode_set2(blist,btype,name,adapter, scoreMode, scoreCutoff, alt_barcode_prefix = None, skip_barcode_leading_zero = False, barcode_num_digits = 2, start_id_str_value = 1, index = 0):
    print("add_or_update_barcode_set2... name=%s; alt_barcode_prefix=%s" %(name, alt_barcode_prefix))
    
    digitCount = str(barcode_num_digits)
    
# Attempt to read dnabarcode set named 'IonXpress' from dbase
    dnabcs = models.dnaBarcode.objects.filter(name=name)
    if len(dnabcs) > 0:
        #print '%s dnaBarcode Set exists in database' % name
        # make sure we have all the sequences we expect
        for id_index,sequence in enumerate(blist,start= start_id_str_value):
            # Search for this sequence in the list of barcode records
            bc_found = dnabcs.filter(sequence=sequence)
            if len(bc_found) > 1:
                print "ERROR: More than one entry with sequence %s" % sequence
                print "TODO: Fix this situation, Mr. Programmer!"

            index += 1
            # Make sure id string has zero padded index field if skip_barcode_leading_zero is not true
            if alt_barcode_prefix:
                if skip_barcode_leading_zero:
                    barcode_name = '%s%d' % (alt_barcode_prefix,id_index)
                else:
                    format_string = '%s%0'+digitCount+ 'd'
                    barcode_name = format_string % (alt_barcode_prefix,id_index)
            else:
                if skip_barcode_leading_zero:
                    barcode_name = '%s_%d' % (name,id_index) 
                else:     
                    format_string = '%s_%0'+digitCount+'d'
                    barcode_name = format_string % (name,id_index)
                            
            if len(bc_found) == 1:

                #print "%s dnaBarcode sequence %s already in the database" % (name, sequence)  
                              
                # Make sure floworder field is not 'none'
                if bc_found[0].floworder == 'none':
                    bc_found[0].floworder = ''
             
                bc_found[0].id_str = barcode_name
                
                # update type
                bc_found[0].type = btype

                # update index
                bc_found[0].index = index
                                
                # Save changes to database
                bc_found[0].save()
                
            else:   # array length is zero
                #print "Adding entry for %s" % sequence
                kwargs = {
                    'name':name,
                    'id_str': barcode_name,
                    'sequence':sequence,
                    'type':btype,
                    'length':len(sequence),
                    'floworder':'',
                    'index':index,
                    'annotation':'',
                    'adapter':adapter,
                    'score_mode':scoreMode,
                    'score_cutoff':scoreCutoff,
                }
                ret = models.dnaBarcode(**kwargs)
                ret.save()
    else:
        # Add the barcodes because they do not exist.
        # NOTE: name for id_str
          
        format_string = '%s%0'+digitCount+'d'
                    
        for id_index,sequence in enumerate(blist,start= start_id_str_value):
            index += 1
            if alt_barcode_prefix:
                if skip_barcode_leading_zero:
                    barcode_name = '%s%d' % (alt_barcode_prefix,id_index)
                else:
                    barcode_name = format_string % (alt_barcode_prefix,id_index)
            else:
                if skip_barcode_leading_zero:
                    barcode_name = '%s_%d' % (name,id_index) 
                else:     
                    barcode_name = format_string % (name,id_index)
            
            kwargs = {
                'name':name,
                'id_str': barcode_name,
                'sequence':sequence,
                'type':btype,
                'length':len(sequence),
                'floworder':'',
                'index':index,
                'annotation':'',
                'adapter':adapter,
                'score_mode':scoreMode,
                'score_cutoff':scoreCutoff,
            }
            ret = models.dnaBarcode(**kwargs)
            ret.save()
        print '%s dnaBarcode Set added to database' % name

    return index