File size: 2,801 Bytes
a388195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
class Sequence:
    def __init__(self, sequence):
        self.sequence = sequence.lower()

    def get_seq_length(self):
        return len(self.sequence)
        # Total length of sequence

    def get_unit_count(self, unit):
        unit = unit.lower()
        return self.sequence.count(unit)
        # Unit count of specified unit in sequence

    def get_unit_percentage(self, unit):
        total_units = self.get_seq_length()
        unit_count = self.get_unit_count(unit)
        unit_percentage = (unit_count / total_units) * 100
        return unit_percentage
        # unit percentage for specified unit in sequence

    def get_unit_at_position(self, position):
        pos = position - 1

        if 0 <= pos < len(self.sequence):
            base_at_pos = self.sequence[pos]
            return base_at_pos
        else:
            return "Position is out of range. Positions should be 1 - {}".format(len(self.sequence))
        # Returns the unit at a specified position in the sequence

    def get_unit_at_positions(self, position_list):
        if self.check_positions(position_list):
            pos_dict = {i: self.sequence[i - 1] for i in position_list if 0 <= i < len(self.sequence)}
            return pos_dict
        else:
            return "Position is out of range.Positions should be 1 - {}".format(len(self.sequence))
        # Returns unit for each position in list

    def check_positions(self, position_list):
        # Check if the positions are within the range of the sequence length
        # Value = 0 -> position out of sequence range
        # Value = 1 -> position within sequence range

        checked = {}
        for pos in position_list:
            if pos <= 0 or pos > len(self.sequence):
                checked[pos] = 0
            else:
                checked[pos] = 1

        # Check if all values are equal to 1 / All positions in the list are within the range of the sequence length
        all_values_equal_to_1 = all(value == 1 for value in checked.values())

        if all_values_equal_to_1:
            valid = True
        else:
            valid = False

        return valid

    def get_subsequence(self, start_position, end_position):
        # Ensure the start and end positions are within the bounds of the sequence
        if start_position > 0 and end_position <= len(self.sequence):
            return self.sequence[start_position - 1:end_position]
        else:
            return "Position is out of range. Positions should be 1 - {}".format(len(self.sequence))
        # Returns the subsequence based on given positions

    def subsequence_total_units(self, start_position, end_position):
        return len(self.get_subsequence(start_position, end_position))
        # Returns the total number of units in the subsequence