Commit 7ae6dc12 authored by Mathieu Courcelles's avatar Mathieu Courcelles

Fixes issues with PeakList

Fixes exception catching with parsing (Issue #12)
Fixes file problem with duplicate name
parent 558d867c
Pipeline #10201640 passed with stage
in 2 minutes and 47 seconds
No preview for this file type
......@@ -954,7 +954,7 @@ class RawDatasetAdmin(DatasetAdminMixin, admin.ModelAdmin):
'search_algorithm', 'parsing_status',
'creation_date', 'get_CLPeptides_URL')
list_filter = ('project', 'cross_linker', 'instrument_name',
list_filter = ('project', 'cross_linker', 'instrument_name',
'fasta_db', 'search_algorithm')
readonly_fields = ('parsing_log', 'parsing_status')
......@@ -1171,12 +1171,14 @@ class PDBAdmin(admin.ModelAdmin):
class PeakListAdmin(admin.ModelAdmin):
fields = ('file',)
fields = ('file', 'replace_ms2_ref', 'parsing_log', 'parsing_status')
list_display = ('pk', 'file')
list_display = ('pk', 'file', 'replace_ms2_ref', 'parsing_log',
'parsing_status')
search_fields = ('file',)
readonly_fields = ('parsing_log', 'parsing_status')
search_fields = ('file',)
@staticmethod
@receiver(post_save, sender=PeakList)
......@@ -1185,48 +1187,44 @@ class PeakListAdmin(admin.ModelAdmin):
This method initiates the parsing of uploaded MGF file.
"""
if instance.parsing_log is None and created is False:
pass
else:
return None
# Read MGF file
# Extract title, scan number, retention time, peak list position
mgf_file = instance.file.path
try:
mgf_parser = MGFParser(mgf_file)
mgf_scans = mgf_parser.get_scans_dict(key='scan_number',
remove_fields=['peak_list'])
# try:
mgf_parser = MGFParser(mgf_file)
mgf_scans = mgf_parser.get_scans_dict(key='scan_number',
remove_fields=['peak_list'])
except (AttributeError, KeyError, TypeError, ValueError) as e:
#print(mgf_scans)
error = 'Unexpected issue while parsing %s. Please check file format' \
' or if file is corrupted.' % mgf_file
instance.parsing_log = error
instance.save()
# except (AttributeError, KeyError, TypeError, ValueError) as e:
#
# error = 'Unexpected issue while parsing %s. Please check file format' \
# ' or if file is corrupted.' % mgf_file.name
#
# print(error)
return None
mgf_file = instance.filename.replace('.mgf', '')
#mgf_file = re.sub('_[^_]+\.mgf', '', instance.filename)
#print(mgf_file)
for clpeptide in CLPeptide.objects.filter(run_name=mgf_file):
#print(clpeptide.id)
for clpeptide in CLPeptide.objects.filter(run_name=mgf_file):
if clpeptide.scan_file_index == -1:
if clpeptide.scan_file_index == -1 or instance.replace_ms2_ref:
index = mgf_scans[clpeptide.scan_number]['scan_file_index']
#print(clpeptide.scan_file_index, index)
# if clpeptide.scan_number == 30511:
# print(clpeptide.scan_number)
# print(mgf_scans[clpeptide.scan_number])
clpeptide.scan_file_index = index
clpeptide.retention_time = mgf_scans[clpeptide.scan_number]['RTINSECONDS']
clpeptide.save()
instance.parsing_status = True
instance.parsing_log = 'OK'
instance.save()
class QuantificationAdmin(admin.ModelAdmin):
......@@ -1268,7 +1266,7 @@ class QuantificationAdmin(admin.ModelAdmin):
parser_generic.parseQuantFC(instance)
## Register admin panels
# Register admin panels
admin.site.register(RawDataset, RawDatasetAdmin)
admin.site.register(ProcessedDataset, ProcessedDatasetAdmin)
admin.site.register(CrossLinker)
......@@ -1282,5 +1280,7 @@ admin.site.register(CLPeptideFilter, CLPeptideFilterAdmin)
admin.site.register(PDB, PDBAdmin)
admin.site.register(PeakList, PeakListAdmin)
admin.site.register(Quantification, QuantificationAdmin)
#admin.site.register(QuantificationFC)
# admin.site.register(QuantificationFC)
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('CLMSVault_app', '0013_auto_20170626_1059'),
]
operations = [
migrations.AddField(
model_name='peaklist',
name='parsing_log',
field=models.CharField(max_length=1000, null=True, blank=True),
preserve_default=True,
),
migrations.AddField(
model_name='peaklist',
name='parsing_status',
field=models.BooleanField(default=False),
preserve_default=True,
),
migrations.AddField(
model_name='peaklist',
name='replace_ms2_ref',
field=models.BooleanField(default=True, help_text=b'Replace existing MS/MS references.'),
preserve_default=True,
),
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('CLMSVault_app', '0014_auto_20170725_1016'),
]
operations = [
migrations.AlterField(
model_name='peaklist',
name='replace_ms2_ref',
field=models.BooleanField(default=False, help_text=b'Replace existing MS/MS references.'),
),
]
# # Copyright 2013-2017 Mathieu Courcelles
# # Mike Tyers's lab / IRIC / Universite de Montreal
"""
Data model module for the CLMSpipeline_app.
Copyright 2013-2017 Mathieu Courcelles
CAPA - Center for Advanced Proteomics Analyses and Thibault's lab
IRIC - Universite de Montreal
Data model module for the CLMSVault_app.
"""
# Import standard libraries
......@@ -17,8 +18,6 @@ from django.db.models.signals import post_save, pre_save, pre_delete
from django.dispatch import receiver
from django.utils.safestring import mark_safe
# Import project libraries
......@@ -356,7 +355,7 @@ def skip_saving_file(sender, instance, **kwargs):
Waits that object has been saved before saving files.
"""
if sender in [RawDataset, FastaDB, Quantification]:
if sender in [RawDataset, FastaDB, PeakList, Quantification]:
if not instance.pk and not hasattr(instance, _UNSAVED_FILEFIELD):
setattr(instance, _UNSAVED_FILEFIELD, instance.file)
......@@ -375,7 +374,7 @@ def save_file(sender, instance, created, **kwargs):
Saves the files now that the dataset object has been saved.
"""
if sender in [RawDataset, FastaDB, Quantification]:
if sender in [RawDataset, FastaDB, PeakList, Quantification]:
if created and hasattr(instance, _UNSAVED_FILEFIELD):
instance.file = getattr(instance, _UNSAVED_FILEFIELD)
......@@ -883,20 +882,36 @@ class PDB(models.Model):
ordering = ['identifier']
def upload_path_handler_peak_list(instance, filename):
def upload_path_handler_peak_list(instance, file_name):
"""
Path handler for dataset file upload.
Path handler for file upload.
:param instance: Model instance
:param file_name: String
:return: String
"""
return "{classname}/{filename}".format(classname=instance.__class__.__name__,
filename=filename)
file_path = os.path.join(type(instance).__name__, str(instance.pk), file_name)
if os.path.exists(file_path):
raise OSError('File exists at: %s' % file_path)
return file_path
class PeakList(models.Model):
"""
This class holds MGF PeakList stored on the server.
"""
file = models.FileField(help_text='MGF file',
upload_to=upload_path_handler_peak_list)
replace_ms2_ref = models.BooleanField(default=False,
help_text='Replace existing MS/MS references.')
parsing_status = models.BooleanField(default=False)
parsing_log = models.CharField(max_length=1000, blank=True, null=True)
@property
def filename(self):
......
......@@ -4,7 +4,7 @@
# Import standard librariesdjang
# Import standard libraries
# Import Django related libraries
......
......@@ -24,7 +24,6 @@ for arg in sys.argv:
# Other wise check if local or production file is there
if loaded is False:
print('hey')
# Local settings
try:
from .local import *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment