From 866bbfa294bd543b9051d00b58e2a9f3a3e32d5b Mon Sep 17 00:00:00 2001 From: davidshumway Date: Thu, 10 Jun 2021 22:54:18 -0500 Subject: [PATCH] Test bin peaks --- .../migrations/0008_auto_20210611_0041.py | 21 ++++++++++++ mdb/spectra/models.py | 6 ++-- mdb/spectra_search/views.py | 33 +++++++++++++++--- rplumber/plumber.R | 34 ++++++++++++++++--- 4 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 mdb/spectra/migrations/0008_auto_20210611_0041.py diff --git a/mdb/spectra/migrations/0008_auto_20210611_0041.py b/mdb/spectra/migrations/0008_auto_20210611_0041.py new file mode 100644 index 0000000..2b7a6d0 --- /dev/null +++ b/mdb/spectra/migrations/0008_auto_20210611_0041.py @@ -0,0 +1,21 @@ +# Generated by Django 3.1 on 2021-06-11 00:41 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('spectra', '0007_auto_20210609_2238'), + ] + + operations = [ + migrations.RemoveField( + model_name='binnedpeaks', + name='peak_intensity', + ), + migrations.RemoveField( + model_name='collapsedcosinescore', + name='binned_peaks', + ), + ] diff --git a/mdb/spectra/models.py b/mdb/spectra/models.py index f1ce8b4..843b0c9 100644 --- a/mdb/spectra/models.py +++ b/mdb/spectra/models.py @@ -239,11 +239,9 @@ class BinnedPeaks(models.Model): Binned peaks version of a spectra when matched against set of spectra. ''' peak_mass = models.TextField(blank = True) - peak_intensity = models.TextField(blank = True) peak_snr = models.TextField(blank = True) spectra = models.ForeignKey( 'CollapsedSpectra', - # ~ related_name = 'spectra', on_delete = models.CASCADE) # ~ class CollapsedCosineScore(AbstractCosineScore): @@ -253,14 +251,14 @@ class CollapsedCosineScore(models.Model): ''' spectra = models.ForeignKey( 'CollapsedSpectra', - # ~ related_name = 'spectra', on_delete = models.CASCADE) library = models.ForeignKey( 'chat.Library', on_delete = models.CASCADE) scores = models.TextField() spectra_ids = models.TextField() - binned_peaks = models.ManyToManyField('BinnedPeaks') + #intensities = models.TextField() + #binned_peaks = models.ManyToManyField('BinnedPeaks') # ~ class CollapsedCosineScore(AbstractCosineScore): # ~ spectra1 = models.ForeignKey( diff --git a/mdb/spectra_search/views.py b/mdb/spectra_search/views.py index 8c9d816..0e5010b 100644 --- a/mdb/spectra_search/views.py +++ b/mdb/spectra_search/views.py @@ -158,8 +158,7 @@ def process_file(request, file, form, owner): n2 = CollapsedSpectra.objects.filter( library__exact = form.cleaned_data['search_library'].id, spectra_content__exact = 'PR' - #max_mass__gt = 6000 - ).values('id') + ).order_by('id').values('id')#, 'strain_id') data = { 'ids': [n1.id] + [s['id'] for s in list(n2)] } @@ -168,7 +167,27 @@ def process_file(request, file, form, owner): params = data ) - print('bp', r.json()['binnedPeaks'][1][0]) + print('bp', r.json()['binnedPeaks']) + # ~ print('bp', r.json()['binnedPeaks']['intensity.233']) + # ~ print('bp', r.json()['binnedPeaks']['intensity.233'][0]) + # ~ print('bp', r.json()['colnames']) + tmp = r.json()['binnedPeaks'] + binned_peaks = {} + # ~ count = 0 + for v in tmp: # Dictionary of binned peaks + binned_peaks[str(v['csId'][0])] = { + 'mass': v['mass'], + 'intensity': v['intensity'], + 'snr': v['snr'], + } + # ~ else: + # ~ c = str(count) + # ~ binned_peaks[v['csId.' + c][0]] = { + # ~ 'mass': v['mass.' + c], + # ~ 'intensity': v['intensity.' + c], + # ~ 'snr': v['snr.' + c], + # ~ } + # ~ count += 1 # ~ # test # ~ from spectra.models import * @@ -188,7 +207,7 @@ def process_file(request, file, form, owner): # ~ key = lambda x: (x[1], x[0]), reverse = True) # ~ ) - # create a dictionary and sort by its values + # Dictionary sorted by its values from collections import OrderedDict k = [str(s['id']) for s in list(n2)] # one less v = r.json()['similarity'][1:] # one more remove first??? @@ -209,6 +228,9 @@ def process_file(request, file, form, owner): 'original': { 'peak_mass': n1.peak_mass, 'peak_intensity': n1.peak_intensity, + 'binned_mass': binned_peaks[str(n1.id)]['mass'], + 'binned_intensity': binned_peaks[str(n1.id)]['intensity'], + 'binned_snr': binned_peaks[str(n1.id)]['snr'], } } @@ -233,6 +255,9 @@ def process_file(request, file, form, owner): 'rowcount': rowcount, 'peak_mass': cs.peak_mass, 'peak_intensity': cs.peak_intensity, + 'binned_mass': binned_peaks[str(cs.id)]['mass'], + 'binned_intensity': binned_peaks[str(cs.id)]['intensity'], + 'binned_snr': binned_peaks[str(cs.id)]['snr'], }) rowcount += 1 ws.send(json.dumps(l)) diff --git a/rplumber/plumber.R b/rplumber/plumber.R index 3a5ff5d..17cd725 100644 --- a/rplumber/plumber.R +++ b/rplumber/plumber.R @@ -81,18 +81,38 @@ function(req, ids) { binnedPeaks <- MALDIquant::binPeaks(allPeaks, tolerance = 0.002) featureMatrix <- MALDIquant::intensityMatrix(binnedPeaks, allSpectra) - # manual inspection... - print(toString(head(binnedPeaks, 2))) + + b <- list() +#~ allSpectra = list() +#~ allPeaks = list() + for(i in 1:length(binnedPeaks)) { + s <- binnedPeaks[[i]] + b <- append(b, list(list( + 'mass' = MALDIquant::mass(s), + 'intensity' = MALDIquant::intensity(s), + 'snr' = MALDIquant::snr(s), + 'csId' = ids[[i]] + ))) + } +#~ print(mass(binnedPeaks[[1]])) #~ print(toString(binnedPeaks)) +#~ print(colnames(featureMatrix)) +#~ print(capture.output(featureMatrix)) #print(toString(featureMatrix[1,])) #print(toString(featureMatrix[2,])) #print(coop::cosine(featureMatrix[1,], featureMatrix[2,])) d <- coop::cosine(t(featureMatrix)) d <- round(d, 3) - return(list('similarity' = d[1,], 'binnedPeaks' = capture.output(binnedPeaks))) + return(list( + 'similarity' = d[1,], + 'binnedPeaks' = b +#~ 'binnedPeaks' = capture.output(toString(binnedPeaks)) +#~ 'featureMatrix' = featureMatrix, +#~ 'colnames' = colnames(featureMatrix) + )) - return(d[1,]) + #return(d[1,]) emptyProtein <- unlist( lapply(allPeaks, MALDIquant::isEmpty) @@ -269,8 +289,14 @@ dbSpectra <- function(ids) { allSpectra = list() allPeaks = list() + # q&d initial solution: require monotonically increasing ids + id <- 0 + for(i in 1:nrow(q)) { row <- q[i,] + if (row$id < id) { + stop('results not monotonic') + } allPeaks <- append(allPeaks, MALDIquant::createMassPeaks( mass = as.numeric(strsplit(row$peak_mass, ",")[[1]]),