Skip to content
This repository has been archived by the owner on Oct 7, 2022. It is now read-only.

Commit

Permalink
Test bin peaks
Browse files Browse the repository at this point in the history
  • Loading branch information
davidshumway committed Jun 11, 2021
1 parent d85a592 commit 866bbfa
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 12 deletions.
21 changes: 21 additions & 0 deletions mdb/spectra/migrations/0008_auto_20210611_0041.py
Original file line number Diff line number Diff line change
@@ -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',
),
]
6 changes: 2 additions & 4 deletions mdb/spectra/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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(
Expand Down
33 changes: 29 additions & 4 deletions mdb/spectra_search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
}
Expand All @@ -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 *
Expand All @@ -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???
Expand All @@ -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'],
}
}

Expand All @@ -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))
Expand Down
34 changes: 30 additions & 4 deletions rplumber/plumber.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]]),
Expand Down

0 comments on commit 866bbfa

Please sign in to comment.