Spaces:
Sleeping
Sleeping
Kevin Louis
commited on
Commit
·
a388195
1
Parent(s):
7ef4de5
Upload sequence.py
Browse files- sequence.py +72 -0
sequence.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class Sequence:
|
2 |
+
def __init__(self, sequence):
|
3 |
+
self.sequence = sequence.lower()
|
4 |
+
|
5 |
+
def get_seq_length(self):
|
6 |
+
return len(self.sequence)
|
7 |
+
# Total length of sequence
|
8 |
+
|
9 |
+
def get_unit_count(self, unit):
|
10 |
+
unit = unit.lower()
|
11 |
+
return self.sequence.count(unit)
|
12 |
+
# Unit count of specified unit in sequence
|
13 |
+
|
14 |
+
def get_unit_percentage(self, unit):
|
15 |
+
total_units = self.get_seq_length()
|
16 |
+
unit_count = self.get_unit_count(unit)
|
17 |
+
unit_percentage = (unit_count / total_units) * 100
|
18 |
+
return unit_percentage
|
19 |
+
# unit percentage for specified unit in sequence
|
20 |
+
|
21 |
+
def get_unit_at_position(self, position):
|
22 |
+
pos = position - 1
|
23 |
+
|
24 |
+
if 0 <= pos < len(self.sequence):
|
25 |
+
base_at_pos = self.sequence[pos]
|
26 |
+
return base_at_pos
|
27 |
+
else:
|
28 |
+
return "Position is out of range. Positions should be 1 - {}".format(len(self.sequence))
|
29 |
+
# Returns the unit at a specified position in the sequence
|
30 |
+
|
31 |
+
def get_unit_at_positions(self, position_list):
|
32 |
+
if self.check_positions(position_list):
|
33 |
+
pos_dict = {i: self.sequence[i - 1] for i in position_list if 0 <= i < len(self.sequence)}
|
34 |
+
return pos_dict
|
35 |
+
else:
|
36 |
+
return "Position is out of range.Positions should be 1 - {}".format(len(self.sequence))
|
37 |
+
# Returns unit for each position in list
|
38 |
+
|
39 |
+
def check_positions(self, position_list):
|
40 |
+
# Check if the positions are within the range of the sequence length
|
41 |
+
# Value = 0 -> position out of sequence range
|
42 |
+
# Value = 1 -> position within sequence range
|
43 |
+
|
44 |
+
checked = {}
|
45 |
+
for pos in position_list:
|
46 |
+
if pos <= 0 or pos > len(self.sequence):
|
47 |
+
checked[pos] = 0
|
48 |
+
else:
|
49 |
+
checked[pos] = 1
|
50 |
+
|
51 |
+
# Check if all values are equal to 1 / All positions in the list are within the range of the sequence length
|
52 |
+
all_values_equal_to_1 = all(value == 1 for value in checked.values())
|
53 |
+
|
54 |
+
if all_values_equal_to_1:
|
55 |
+
valid = True
|
56 |
+
else:
|
57 |
+
valid = False
|
58 |
+
|
59 |
+
return valid
|
60 |
+
|
61 |
+
def get_subsequence(self, start_position, end_position):
|
62 |
+
# Ensure the start and end positions are within the bounds of the sequence
|
63 |
+
if start_position > 0 and end_position <= len(self.sequence):
|
64 |
+
return self.sequence[start_position - 1:end_position]
|
65 |
+
else:
|
66 |
+
return "Position is out of range. Positions should be 1 - {}".format(len(self.sequence))
|
67 |
+
# Returns the subsequence based on given positions
|
68 |
+
|
69 |
+
def subsequence_total_units(self, start_position, end_position):
|
70 |
+
return len(self.get_subsequence(start_position, end_position))
|
71 |
+
# Returns the total number of units in the subsequence
|
72 |
+
|