Mercurial > repos > yufei-luo > s_mart
comparison commons/core/parsing/BowtieParser.py @ 6:769e306b7933
Change the repository level.
author | yufei-luo |
---|---|
date | Fri, 18 Jan 2013 04:54:14 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
5:ea3082881bf8 | 6:769e306b7933 |
---|---|
1 # | |
2 # Copyright INRA-URGI 2009-2011 | |
3 # | |
4 # This software is governed by the CeCILL license under French law and | |
5 # abiding by the rules of distribution of free software. You can use, | |
6 # modify and/ or redistribute the software under the terms of the CeCILL | |
7 # license as circulated by CEA, CNRS and INRIA at the following URL | |
8 # "http://www.cecill.info". | |
9 # | |
10 # As a counterpart to the access to the source code and rights to copy, | |
11 # modify and redistribute granted by the license, users are provided only | |
12 # with a limited warranty and the software's author, the holder of the | |
13 # economic rights, and the successive licensors have only limited | |
14 # liability. | |
15 # | |
16 # In this respect, the user's attention is drawn to the risks associated | |
17 # with loading, using, modifying and/or developing or reproducing the | |
18 # software by the user in light of its specific status of free software, | |
19 # that may mean that it is complicated to manipulate, and that also | |
20 # therefore means that it is reserved for developers and experienced | |
21 # professionals having in-depth computer knowledge. Users are therefore | |
22 # encouraged to load and test the software's suitability as regards their | |
23 # requirements in conditions enabling the security of their systems and/or | |
24 # data to be ensured and, more generally, to use and operate it in the | |
25 # same conditions as regards security. | |
26 # | |
27 # The fact that you are presently reading this means that you have had | |
28 # knowledge of the CeCILL license and that you accept its terms. | |
29 # | |
30 import re | |
31 import sys | |
32 from commons.core.parsing.MapperParser import MapperParser | |
33 from SMART.Java.Python.structure.Mapping import Mapping | |
34 from SMART.Java.Python.structure.SubMapping import SubMapping | |
35 from SMART.Java.Python.structure.Interval import Interval | |
36 | |
37 class BowtieParser(MapperParser): | |
38 """A class that parses BowTie format""" | |
39 | |
40 def __init__(self, fileName, verbosity = 0): | |
41 super(BowtieParser, self).__init__(fileName, verbosity) | |
42 | |
43 | |
44 def __del__(self): | |
45 super(BowtieParser, self).__del__() | |
46 | |
47 | |
48 def getFileFormats(): | |
49 return ["bowtie"] | |
50 getFileFormats = staticmethod(getFileFormats) | |
51 | |
52 | |
53 def skipFirstLines(self): | |
54 pass | |
55 | |
56 | |
57 def parseLine(self, line): | |
58 line = line.strip() | |
59 fields = line.split("\t") | |
60 if len(fields) not in (7, 8): | |
61 raise Exception("Line %d '%s' does not look like a BowTie line (number of fields is %d instead of 7 or 8)" % (self.currentLineNb, line, len(fields))) | |
62 name = fields[0] | |
63 direction = 1 if fields[1] == "+" else -1 | |
64 chromosome = fields[2] | |
65 genomeStart = int(fields[3]) + 1 | |
66 sequence = fields[4] | |
67 quality = fields[5] | |
68 number = int(fields[6]) | |
69 nbMismatches = 0 | |
70 if len(fields) == 8: | |
71 tags = fields[7] | |
72 nbMismatches = len(tags.split(",")) | |
73 | |
74 mapping = Mapping() | |
75 queryInterval = Interval() | |
76 queryInterval.setName(name) | |
77 queryInterval.setStart(1) | |
78 queryInterval.setEnd(len(sequence) + 1) | |
79 targetInterval = Interval() | |
80 targetInterval.setChromosome(chromosome) | |
81 targetInterval.setStart(genomeStart) | |
82 targetInterval.setEnd(genomeStart + len(sequence) - 1) | |
83 subMapping = SubMapping() | |
84 subMapping.setQueryInterval(queryInterval) | |
85 subMapping.setTargetInterval(targetInterval) | |
86 mapping.addSubMapping(subMapping) | |
87 mapping.setSize(len(sequence)) | |
88 mapping.setNbMismatches(nbMismatches) | |
89 mapping.setDirection(direction) | |
90 return mapping | |
91 |