Example #1
0
    def execute(self, request) -> Response:
        component_repository = Repository()
        factory = Factory()

        for f in request.files:
            file = request.files.get(f)
            try:
                xlsx_file = XLSXFile(file)
            except InvalidArgument:
                return Response(
                    False,
                    'An invalid file was provided, please make sure you are uploading a .xlsx file'
                )

        for row in xlsx_file.read(sheet='', userows=True):
            if not search(row[1], row[2]):
                continue

            baseprotocol = 0
            if len(row) > 2:
                baseprotocol = int(row[3])

            model = factory.create({
                'type': row[0],
                'name': row[1],
                'unit': row[2],
                'base_protocol': baseprotocol
            })
            component_repository.save(model)

        xlsx_file.delete()

        return Response(True, 'File imported successfully')
Example #2
0
    def execute(self) -> Response:
        timestart = time.time()
        self.measurement_manager = MeasurementManager()

        if self.form is None:
            get_existing_metadata(self)
        else:
            update_metadata(self)

        build_swap_inputs(self)
        build_group_inputs(self)
        validate_errors(self)

        wellindex = 0
        for wellindex, well in enumerate(get_collection(self)):

            well = add_custom_group_label(self, well, wellindex)

            # swap wells and shift RFUs to account for a cut time
            if len(self.swaps) > 0 and self.swaps.get(well.get_excelheader()) is not None:
                well = swap_wells(self, well)
                self.measurement_manager.update(well)
                try:
                    self.swaps.pop(well.get_excelheader())
                except KeyError:
                    current_app.logger.error('Error deleting swap from swap dictionary')

            # set well status to invalid if reported
            if well.get_excelheader() in self.errorwells:
                well['is_valid'] = False

            # build time list from first well
            if wellindex < 2:
                self.time = [n for n in range(len(well.get_rfus()))]

            self.measurement_manager.update(well)
            response = self.processData(well)

            if not response.is_success():
                return Response(False, response.get_message())

        if len(self.swaps) > 0:
            missing_swaps = ", ".join([item for item in self.swaps.keys()])
            flash('Swap(s) from the following well(s) %s could not occur. Please verify the entered values.' % missing_swaps, 'error')
        if len(self.errorwells) > 0 and self.errorwells[0] != '':
            flash('Peaks were not found in wells %s' % str(', '.join(self.errorwells)), 'error')

        self.getStatistics()

        welltotal = sum([int(self.groupings[g]['Wells']) for g in self.groupings.keys()])
        if welltotal == 0:
            welltotal = wellindex + 1
        if welltotal != wellindex+1:
            flash('Number of Wells in Custom Groups Not Equal to Total Number of Wells', 'error')
            flash('Please Edit Inputs', 'error')

        return Response(True, str(round(time.time() - timestart, 2)))
    def validate_target(self, target):
        if re.match(r'^\d+\s*[a-z]+\/*[a-zA-Z]+?\s+\w?', target) is not None:
            quantityRe = re.match(r'^\d+', target)
            unitRe = reg_conc(target)
            if quantityRe is None or unitRe is None:
                Response(False, 'Target units and name could not be identified')
            name = target[unitRe.end():]
            unit = target[quantityRe.end():unitRe.end()]

            component = self.component_repository.search_by_name_and_unit(name, unit)
            if component is not None:
                return Response(True, component['_id'])
            return Response(False, 'Target does not exist in the component library')

        return Response(False, 'Target units and name could not be identified')
    def execute(self, request, name) -> Response:
        self.measurement_factory = MeasurementFactory()
        self.measurement_manager = MeasurementManager()

        dataset_repository = Repository()
        if self.dataset is None:
            factory = Factory()
            model = factory.create({'name': name})
            dataset_repository.save(model)
            self.dataset = model

        infofile = None
        rfufile = None

        for f in request.files:
            file = request.files.get(f)
            try:
                xlsx_file = XLSXFile(file)
            except InvalidArgument:
                dataset_repository.delete(self.dataset)
                return Response(False, 'An invalid file was provided, please make sure you are uploading a .xlsx file')

            if file.filename.endswith('INFO.xlsx'):
                infofile = xlsx_file
            elif file.filename.endswith('RFU.xlsx'):
                rfufile = xlsx_file
                name = buildname(file.filename)

        self.getexperimentlength(infofile)
        for info, rfu in zip(infofile.read(sheet='0', userows=True), rfufile.read(sheet='SYBR', usecolumns=True)):
            self.add_measurement(info, rfu)

            self.measurement_manager.save()

        infofile.delete()
        rfufile.delete()

        self.dataset['measure_count'] = self.dataset.get_well_collection().get_size()
        self.dataset['version'] = float(current_app.config['VERSION'])
        dataset_repository.save(self.dataset)

        flash('File imported successfully', 'success')
        flash('Calculated cycle length was %s' % round(self.cyclelength, 3), 'success')

        return Response(
            True,
            self.dataset_id
        )
Example #5
0
    def execute(self, request) -> Response:
        if request.form['select'] != '':
            return Response(True, '')

        if request.form.get('delete') and request.form['select'] == '':
            return Response(False, 'No dataset selected')

        if request.files is None:
            return Response(False, 'Files are required')

        for file in request.files.values():
            if file is None or file.filename == '':
                return Response(False, 'A file is required')

            if not file.filename.endswith('.xlsx'):
                return Response(False, 'The file has an incorrect filetype')

        return Response(True, '')
Example #6
0
    def writebook(self):
        dataset_repository = Repository()
        dataset = dataset_repository.get_by_id(self.dataset_id)
        df = dataset.get_pd_well_collection()
        df = self.build_dataframe(df)

        # write individual variables of interest
        startindex = int(
            np.where(df.columns.str.startswith('Inflection '))[0][0])
        variablesofinterest = 4 * 3
        variablecolumns = [startindex + n for n in range(variablesofinterest)]
        variablecolumns.insert(0, 6)
        for group in range(1, int(df['group'].max()) + 1):
            self.write_to_sheet('Inflections', df[(df['group'] == group)],
                                variablecolumns)
            self.rowshift += df[(df['group'] == group)].shape[0] + 4

        # write averages of variables of interest
        self.rowshift = 0
        adf = self.build_averages(df)
        variablecolumns.pop(0)
        startindex = variablesofinterest + list(
            np.where(df.columns.str.startswith('Inflection ')))[0][0]
        for group in range(1, int(adf['group'].max()) + 1):
            columns = [n for n in variablecolumns]
            gdf = adf[(adf['group'] == group)]
            # TODO: use this when control work is finished
            # control = gdf[gdf['is_control'] == 'True']
            control = gdf[gdf['triplicate'] == gdf['triplicate'].min()]
            for inf in range(4):
                columns.append(startindex + inf)
                inf_label = 'Inflection ' + str(inf + 1)
                gdf.insert(int(startindex + inf),
                           'Difference from control ' + str(inf + 1),
                           gdf[inf_label] - float(control[inf_label]))
            gdf = gdf.iloc[:, columns]

            gdf.to_excel(self.excelwriter,
                         sheet_name='Averages',
                         startrow=self.rowshift)
            self.excel_formatting('Averages', gdf, 0)
            self.rowshift += gdf.shape[0] + 4

        # write inflection and percent differences in matrices
        self.rowshift = 0
        for group in range(1, int(adf['group'].max()) + 1):
            gdf = adf[(adf['group'] == group)]
            pdf = adf[(adf['group'] == group)]
            for inf in range(4):
                columns = []
                inf_label = 'Inflection ' + str(inf + 1)
                pcnt_label = 'Percent Diff ' + str(inf + 1)
                columns.append(7)
                for triplicateA in gdf['triplicate'].unique():
                    columns.append(len(gdf.columns))
                    rowA = gdf[gdf['triplicate'] == triplicateA]
                    gdf.insert(len(gdf.columns),
                               str(len(columns) - 2) + ' ' + inf_label, [
                                   label - float(rowA[inf_label])
                                   if triplicateB <= triplicateA else 'nan'
                                   for label, triplicateB in zip(
                                       gdf[inf_label], gdf['triplicate'])
                               ])
                    pdf.insert(len(pdf.columns),
                               str(len(columns) - 2) + ' ' + pcnt_label, [
                                   label - float(rowA[pcnt_label])
                                   if triplicateB >= triplicateA else 'nan'
                                   for label, triplicateB in zip(
                                       pdf[pcnt_label], pdf['triplicate'])
                               ])
                spacedifferencematrices = (len(columns) + 4) * inf
                self.write_to_sheet('Inf Differences', gdf, columns,
                                    spacedifferencematrices)
                self.write_to_sheet('Percent Differences', pdf, columns,
                                    spacedifferencematrices)
            self.rowshift += gdf.shape[0] + 4

        # write individual ct values
        self.rowshift = 0
        df.insert(0, 'Ct threshold', [x[1] for x in df['deltaCt']])
        df.insert(0, 'delta Ct', [x[0] for x in df['deltaCt']])
        variablecolumns = []
        for item in ['label', 'group', 'sample', 'Ct threshold', 'delta Ct']:
            variablecolumns.append(int(np.where(df.columns == item)[0]))
        for group in range(1, int(df['group'].max()) + 1):
            self.write_to_sheet('Ct Thresholds', df[(df['group'] == group)],
                                variablecolumns)
            self.rowshift += df[(df['group'] == group)].shape[0] + 4

        return Response(True, '')
def search_components(self, name, unit):
    component = self.component_repository.search_by_name_and_unit(name, unit)
    if component is not None:
        return Response(True, component['_id'])
    return Response(False, 'Target does not exist in the component library.')
Example #8
0
    def processData(self, well):
        if well['excelheader'] in self.errorwells:
            well['is_valid'] = False

        else:
            well['is_valid'] = True
            percentdiffs = [0 for x in range(4)]
            deltact = [0 for x in range(3)]
            inflectiondict = {}
            derivatives = get_derivatives(well)

            for dIndex in derivatives.keys():
                inflectiondict = get_peaks(self,
                                           well=well,
                                           derivativenumber=dIndex,
                                           derivative=derivatives[dIndex],
                                           allpeaks=inflectiondict)
            inflectiondict = dict(sorted(inflectiondict.items()))

            well['inflections'] = [(key, inflectiondict[key]['inflection']) for key in inflectiondict.keys()]
            well['inflectionRFUs'] = [(key, inflectiondict[key]['rfu']) for key in inflectiondict.keys()]

            if len(inflectiondict.keys()) < 4:
                flash('%s of 4 inflections were found in well: %s' % (str(len(inflectiondict)),
                                                                      well.get_excelheader()), 'error')

            if self.groupings.get(str(well.get_group())):
                groupcontrols = self.groupings[str(well.get_group())]['Controls'].split(',')
                if well.get_excelheader() in groupcontrols:
                    well['is_control'] = True
                    # TODO: will need to loop through all wells, set control property
                    # before making the following calculations

            if self.control is None or well.get_group() != self.control.get_group():
                self.control = well
                self.controllist = []
                self.ctthreshold = {'Ct RFU': [], 'Ct Cycle': []}

            #for all samples that match the control sample, collect controls
            if self.control.get_sample() == well.get_sample():
                self.controllist.append([x for x in well.get_inflections()])
                if not self.form.get('gPCR'):
                    controlCt = self.getCtThreshold(well, derivatives[1], inflectiondict)
                    self.ctlist.append(controlCt)
                    deltact = [0, controlCt['Ct Cycle'], controlCt['Ct RFU']]

                #average the control inflections
                #TODO: what if the first control has only 2 inflections and the others have 4? or vice versa?

                for idx, x in enumerate(self.control.get_inflections()):
                    i = int(x[0])
                    self.control['inflections'][idx] = (str(i), np.mean([controlinflection[idx][1]
                                                                         for controlinflection in self.controllist
                                                                         if len(controlinflection) > idx]))

                #average the ct threshold
                self.ctthreshold['Ct Cycle'] = np.mean([x['Ct Cycle'] for x in self.ctlist])
                self.ctthreshold['Ct RFU'] = np.mean([x['Ct RFU'] for x in self.ctlist])

            # get percent differences and delta ct values
            elif self.control.get_sample() != well.get_sample():
                percentdiffs = get_percent_difference(self, well['inflections'])
                if not self.form.get('gPCR'):
                    deltact = self.getDeltaCt(well)

            # calculate delta ct and percent diffs
            well['deltaCt'] = deltact
            well['percentdiffs'] = percentdiffs

            #TODO: check validity? These should be able to be corrected on another run
            if len(inflectiondict.keys()) == 4:
                self.statistics = self.statistics.append([{'group': well.get_group(),
                                                           'sample': well.get_sample(),
                                                           '1': inflectiondict.get('1')['inflection'],
                                                           '2': inflectiondict.get('2')['inflection'],
                                                           '3': inflectiondict.get('3')['inflection'],
                                                           '4': inflectiondict.get('4')['inflection']}],
                                                         ignore_index=True)

        self.measurement_manager.update(well)

        return Response(True, '')