File: mod_utils.py

package info (click to toggle)
mmass 5.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 8,396 kB
  • sloc: python: 33,183; xml: 7,925; ansic: 1,722; makefile: 83; sh: 2
file content (108 lines) | stat: -rw-r--r-- 3,098 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# -------------------------------------------------------------------------
#     Copyright (C) 2005-2012 Martin Strohalm <www.mmass.org>

#     This program is free software; you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation; either version 3 of the License, or
#     (at your option) any later version.

#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#     GNU General Public License for more details.

#     Complete text of GNU GPL can be found in the file LICENSE.TXT in the
#     main directory of the program.
# -------------------------------------------------------------------------

# load libs
import os.path

# load stopper
from mod_stopper import CHECK_FORCE_QUIT

# load parsers
from parser_xy import parseXY
from parser_mzxml import parseMZXML
from parser_mzdata import parseMZDATA
from parser_mzml import parseMZML
from parser_mgf import parseMGF
from parser_fasta import parseFASTA


# UTILITIES
# ---------

def load(path, scanID=None, dataType='continuous'):
    """Load scan from given document."""
    
    # check path
    if not os.path.exists(path):
        raise IOError, 'File not found! --> ' + path
    
    # get filename and extension
    dirName, fileName = os.path.split(path)
    baseName, extension = os.path.splitext(fileName)
    fileName = fileName.lower()
    baseName = baseName.lower()
    extension = extension.lower()
    
    # get document type
    if extension == '.mzdata':
        docType = 'mzData'
    elif extension == '.mzxml':
        docType = 'mzXML'
    elif extension == '.mzml':
        docType = 'mzML'
    elif extension == '.mgf':
        docType = 'MGF'
    elif extension in ('.xy', '.txt', '.asc'):
        docType = 'XY'
    elif extension == '.xml':
        doc = open(path, 'r')
        data = doc.read(500)
        if '<mzData' in data:
            docType = 'mzData'
        elif '<mzXML' in data:
            docType = 'mzXML'
        elif '<mzML' in data:
            docType = 'mzML'
        doc.close()
    
    # check document type
    if not docType:
        raise ValueError, 'Unknown document type! --> ' + path
    
    # load document data
    if docType == 'mzData':
        parser = parseMZDATA(path)
        scan = parser.scan(scanID)
    elif docType == 'mzXML':
        parser = parseMZXML(path)
        scan = parser.scan(scanID)
    elif docType == 'mzML':
        parser = parseMZML(path)
        scan = parser.scan(scanID)
    elif docType == 'MGF':
        parser = parseMGF(path)
        scan = parser.scan(scanID)
    elif docType == 'XY':
        parser = parseXY(path)
        scan = parser.scan(dataType)
    
    return scan
# ----


def save(data, path):
    """"""
    
    buff = ''
    for point in data:
        buff += "%f\t%f\n" % tuple(point)
    
    save = file(path, 'w')
    save.write(buff.encode("utf-8"))
    save.close()
# ----