Commit a459c7d8 authored by Mathieu Courcelles's avatar Mathieu Courcelles

Adds export to xTract format

parent ded5f090
......@@ -588,6 +588,7 @@ class DatasetAdminMixin(object):
'make_interaction_matrix',
'make_Percolator_tsv',
'make_ProteoProfile_csv',
'make_xTract_csv',
'make_psi_mi_tab25',
'make_psi_mi_xml25',
'make_Xi_csv',
......@@ -747,7 +748,21 @@ class DatasetAdminMixin(object):
'Dataset_CLPeptide')
make_ProteoProfile_csv.short_description = 'Export as ProteoProfile CSV'
def make_xTract_csv(self, request, queryset):
"""
Action method to export cross-linked peptides to xTract CSV file format.
"""
clpeptide_set = dataset_set_2_clpeptide_set(queryset)
return export.make_xTract_csv(self, request,
clpeptide_set.select_related('fs_prot1_id',
'fs_prot2_id'),
'Dataset_CLPeptide')
make_xTract_csv.short_description = 'Export as xTract CSV'
def make_Percolator_tsv(self, request, queryset):
"""
Action method to export cross-linked peptides to Percolator TSV file format.
......@@ -1207,6 +1222,7 @@ class PeakListAdmin(admin.ModelAdmin):
# 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()
......
This diff is collapsed.
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
import CLMSVault.CLMSVault_app.models
class Migration(migrations.Migration):
dependencies = [
('CLMSVault_app', '0009_auto_20170208_1131'),
]
operations = [
migrations.AddField(
model_name='clpeptide',
name='retention_time',
field=models.BigIntegerField(default=0),
preserve_default=True,
),
migrations.AlterField(
model_name='clpeptidefilterparam',
name='field',
field=models.CharField(max_length=100, choices=[('id', 'id'), (b'run_name', b'run_name'), (b'scan_number', b'scan_number'), (b'precursor_mz', b'precursor_mz'), (b'precursor_charge', b'precursor_charge'), (b'precursor_intensity', b'precursor_intensity'), (b'rank', b'rank'), (b'match_score', b'match_score'), (b'spectrum_intensity_coverage', b'spectrum_intensity_coverage'), (b'total_fragment_matches', b'total_fragment_matches'), (b'delta', b'delta'), (b'error', b'error'), (b'peptide1', b'peptide1'), (b'peptide_wo_mod1', b'peptide_wo_mod1'), (b'display_protein1', b'display_protein1'), (b'fs_prot1_id', b'fs_prot1_id'), (b'peptide_position1', b'peptide_position1'), (b'pep1_link_pos', b'pep1_link_pos'), (b'peptide2', b'peptide2'), (b'peptide_wo_mod2', b'peptide_wo_mod2'), (b'display_protein2', b'display_protein2'), (b'fs_prot2_id', b'fs_prot2_id'), (b'peptide_position2', b'peptide_position2'), (b'pep2_link_pos', b'pep2_link_pos'), (b'autovalidated', b'autovalidated'), (b'validated', b'validated'), (b'rejected', b'rejected'), (b'notes', b'notes'), (b'link_type', b'link_type'), (b'cross_link', b'cross_link'), (b'not_decoy', b'not_decoy'), (b'scan_file_index', b'scan_file_index'), (b'retention_time', b'retention_time')]),
),
migrations.AlterField(
model_name='peaklist',
name='file',
field=models.FileField(help_text=b'MGF file', upload_to=CLMSVault.CLMSVault_app.models.upload_path_handler_peak_list),
),
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('CLMSVault_app', '0010_auto_20170316_0854'),
]
operations = [
migrations.AlterField(
model_name='clpeptide',
name='retention_time',
field=models.FloatField(default=0),
),
]
......@@ -469,7 +469,7 @@ class CLPeptide(AdminURLMixin, models.Model):
scan_file_index = models.BigIntegerField(default=-1)
retention_time = models.FloatField(default=0)
class Meta:
......
"""
Copyright 2015-2017 Mathieu Courcelles
CAPA - Center for Advanced Proteomics Analyses and Thibault's lab
IRIC - Universite de Montreal
"""
# Import standard libraries
import re
# Import project libraries
# Create your views here.
class MGFParser(object):
def __init__(self, file_name):
self.file_name = file_name
@staticmethod
def scan_total_ion_current(peak_list):
"""
Sums the intensity of all peaks
:param peak_list: List of [mz,intensity]
:return: Float
"""
tic = 0
for mz, intensity in peak_list:
tic += intensity
return tic
def parse_next_scan(self):
"""
Extract values from one scan.
:return: Dictionary
"""
scan = dict()
scan['peak_list'] = list()
# Search beginning of the scan
scan['scan_file_index'] = self.fh.tell()
line = self.fh.readline()
#print(scan['scan_file_index'])
#print(line)
while not line.startswith('BEGIN IONS'):
#print('hi')
scan['scan_file_index'] = self.fh.tell()
line = self.fh.readline()
if line == '':
return None
# Read fields
while line:
line = self.fh.readline().strip()
if '=' in line:
part1, part2 = (line.split('=', 1))
scan[part1] = part2
else:
if line.startswith('END'):
line = False
else:
fields = (line.split(' ', 1))
fields = [float(x) for x in fields]
scan['peak_list'].append(fields)
scan['peak_count'] = len(scan['peak_list'])
scan['total_ion_current'] = self.scan_total_ion_current(
scan['peak_list'])
return scan
def read_scans(self):
"""
Reads and parses all scan in the MGF file.
:return: Generator that yield a scan dictionary
"""
with open(self.file_name, 'rb') as self.fh:
scan = ''
while scan is not None:
scan = self.parse_next_scan()
if scan is not None:
yield scan
def get_scan_at_index(self, index):
"""
Extracts a single scan from the MGF by its file index.
:param index: Integer that point to a single scan in the MGF file.
:return: Scan dictionary
"""
with open(self.file_name, 'rb') as self.fh:
self.fh.seek(index)
return self.parse_next_scan()
def get_scans_dict(self, remove_fields=[], key='TITLE'):
"""
Returns dictionary with all scans.
:param remove_fields: String of the field to remove.
:return: Dictionary
"""
mgf_scans = dict()
#count = 0
for scan in self.read_scans():
for field in remove_fields:
scan.pop(field, None)
if key == 'TITLE':
mgf_scans[scan['TITLE']] = scan
elif key == 'scan_number':
m = re.search(r'scan=(?P<scan_number>\d+)', scan['TITLE'])
if m is not None:
values = m.groupdict()
mgf_scans[int(values['scan_number'])] = scan
# print('1', scan)
# fh = self.fh
# scan2 = self.get_scan_at_index(scan['scan_file_index'])
# print('2', scan2)
#
# self.fh = fh
#
#
#
# count += 1
#
# if count == 3:
# break
return mgf_scans
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