annotate bedClass.py @ 0:3eca7b2537aa draft default tip

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