annotate bedClass.py @ 0:b001b50f2009 draft default tip

Imported from capsule None
author devteam
date Mon, 27 Jan 2014 09:28:06 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
1 #!/usr/bin/python
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
2
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
3 import os.path
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
4 import sys
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
5
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
6 class bed:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
7 def __init__(self):
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
8 self.numberTargets = 0
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
9 self.referenceSequences = {}
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
10 self.referenceSequenceList = []
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
11
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
12 def openBed(self, filename):
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
13 if filename == "stdin": self.filehandle = sys.stdin
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
14 else:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
15 try: self.filehandle = open(filename,"r")
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
16 except IOError:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
17 print >> sys.stderr, "Failed to find file: ",filename
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
18 exit(1)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
19
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
20 # Get a bed record.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
21 def getRecord(self):
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
22 self.record = self.filehandle.readline()
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
23 if not self.record: return False
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
24
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
25 self.numberTargets = self.numberTargets + 1
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
26 self.ref = ""
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
27 self.start = 0
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
28 self.end = 0
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
29
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
30 # bed file should be 0-based, half-open, so the start coordinate
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
31 # must be that in the bed file plus one.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
32 entries = self.record.rstrip("\n").split("\t")
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
33 self.referenceSequence = entries[0]
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
34
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
35 # Add the reference sequence to the dictionary. If it didn't previously
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
36 # exist append the reference sequence to the end of the list as well.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
37 # This ensures that the order in which the reference sequences appeared
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
38 # in the header can be preserved.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
39 if self.referenceSequence not in self.referenceSequences:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
40 self.referenceSequences[self.referenceSequence] = True
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
41 self.referenceSequenceList.append(self.referenceSequence)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
42
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
43 try: self.start = int(entries[1]) + 1
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
44 except:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
45 text = "start position need is not an integer"
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
46 self.generalError(text, "start", entries[1])
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
47
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
48 try: self.end = int(entries[2])
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
49 except:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
50 text = "end position need is not an integer"
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
51 self.generalError(text, "end", entries[2])
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
52
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
53 # Check that the record is a valid interval.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
54 if self.end - self.start < 0:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
55 print >> sys.stderr, "Invalid target interval:\n\t", self.record
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
56 exit(1)
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
57
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
58 return True
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
59
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
60 # Parse through the bed file until the correct reference sequence is
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
61 # encountered and the end position is greater than or equal to that requested.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
62 def parseBed(self, referenceSequence, position):
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
63 success = True
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
64 if self.referenceSequence != referenceSequence:
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
65 while self.referenceSequence != referenceSequence and success: success = self.getRecord()
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
66
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
67 while self.referenceSequence == referenceSequence and self.end < position and success: success = self.getRecord()
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
68
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
69 return success
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
70
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
71 # Close the bed file.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
72 def closeBed(self, filename):
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
73 self.filehandle.close()
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
74
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
75 # Define error messages for different handled errors.
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
76 def generalError(self, text, field, fieldValue):
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
77 print >> sys.stderr, "\nError encountered when attempting to read:"
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
78 if field != "": print >> sys.stderr, "\t", field, ": ", fieldValue
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
79 print >> sys.stderr, "\n", text
b001b50f2009 Imported from capsule None
devteam
parents:
diff changeset
80 exit(1)