Skip to content

Commit 0eb66d5

Browse files
committed
Added comments
1 parent cb5823b commit 0eb66d5

File tree

2 files changed

+26
-35
lines changed

2 files changed

+26
-35
lines changed

Bio/SeqFeature.py

+22-31
Original file line numberDiff line numberDiff line change
@@ -1038,45 +1038,37 @@ def _flip(self, length):
10381038
strand=flip_strand,
10391039
)
10401040

1041+
""" Returns a shifted position given position, shift and N, where
1042+
position is the given position, shift is the amount and N is the length of the sequence
1043+
"""
10411044
def _shift_position(self, position, shift, N):
1042-
#Caso ExactPosition
10431045
if type(position)==ExactPosition:
10441046
position_new = (int(position) + shift) % N
10451047
return ExactPosition(position_new)
10461048
if type(position)==UncertainPosition:
10471049
position_new = (int(position) + shift) % N
10481050
return UncertainPosition(position_new)
1049-
#Caso UnknownPosition
10501051
if type(position)==UnknownPosition:
10511052
return position
1052-
#Caso WithinPosition
10531053
if type(position)==WithinPosition:
10541054
position_curr_default = int(position)
1055-
#Ricava il valore di left right usando la rappresentazione in stringa
1056-
#Elimina le parentesi e converte i valori separati da punto in interi
1055+
#Get left and right values by using string representation
1056+
#Delete parenthesis and converts values separated by dot into integers
10571057
position_curr_left_right = list(map(int, str(position)[1:-1].split('.')))
1058-
10591058
position_new_default = (position_curr_default + shift) % N
10601059
position_new_left = (position_curr_left_right[0] + shift) % N
10611060
position_new_right = (position_curr_left_right[1] + shift) % N
1062-
10631061
return WithinPosition(position_new_default, position_new_left, position_new_right)
1064-
#Caso BeforePosition
10651062
if type(position)==BeforePosition:
10661063
position_new = (int(position) + shift) % N
10671064
return BeforePosition(position_new)
1068-
#Caso AfterPosition
10691065
if type(position)==AfterPosition:
10701066
position_new = (int(position) + shift) % N
10711067
return AfterPosition(position_new)
1072-
#Caso OneOfPosition
10731068
if type(position)==OneOfPosition:
10741069
position_curr_default = int(position)
1075-
#Ricava il valore di left right usando la rappresentazione in stringa
1076-
#Elimina le parentesi e converte i valori separati da virgola in interi
10771070
position_new_default = (position_curr_default + shift) % N
10781071
new_choices = [self._shift_position(pos, shift, N) for pos in position.position_choices]
1079-
#Riordino le posizioni in base alla posizione
10801072
new_choices.sort()
10811073
return OneOfPosition(position_new_default, new_choices)
10821074

@@ -1138,25 +1130,23 @@ def nofuzzy_end(self):
11381130
return None
11391131
raise
11401132

1133+
"""Returns a rotated location given shift and N, where shift is the amount and N is the length of the sequence.
1134+
Special case 1: if the shift is large enough the feature could be broken in two separated regions, so it becomes a
1135+
CompoundLocation including the two regions.
1136+
Special case 2: if the feature length is N, it doesn't get broken in two regions since they would be contigous.
1137+
"""
11411138
def rotate(self, shift, N):
11421139
position_start = self._shift_position(self._start, shift, N)
11431140
position_end = self._shift_position(self._end, shift, N)
11441141
if (position_start > position_end):
11451142
#Get middle location
1146-
position_middle_1 = self._shift_position(self._start, -self._start, N) #conserva il tipo di location
1143+
position_middle_1 = self._shift_position(self._start, -self._start, N) #To preserve the type of position
11471144
position_middle_2 = self._shift_position(position_end, N-int(position_end)-1, N)
11481145
f1 = FeatureLocation(position_start, position_middle_2, self.strand, ref=self.ref, ref_db=self.ref_db)
1149-
f2 = FeatureLocation(position_middle_1, position_end, self.strand, ref=self.ref, ref_db=self.ref_db)
1150-
1151-
#location1 - location2 attaccate
1152-
#if f2.start - 1 == f1.end:
1153-
# return FeatureLocation(f1.start,f2.end, self.strand, ref=self.ref, ref_db=self.ref_db)
1154-
1155-
#location2 - location1 attaccate
1146+
f2 = FeatureLocation(position_middle_1, position_end, self.strand, ref=self.ref, ref_db=self.ref_db)
11561147
if f1.start - 1 == f2.end:
1157-
return FeatureLocation(f2.start,f1.end, self.strand, ref=self.ref, ref_db=self.ref_db)
1158-
1159-
return CompoundLocation([f2, f1])
1148+
return FeatureLocation(f2.start,f1.end, self.strand, ref=self.ref, ref_db=self.ref_db) #Special case 2
1149+
return CompoundLocation([f2, f1]) #Special case 1
11601150
else:
11611151
return FeatureLocation(position_start, position_end, self.strand, ref=self.ref, ref_db=self.ref_db)
11621152

@@ -1578,6 +1568,10 @@ def ref_db(self):
15781568
"""Not present in CompoundLocation, dummy method for API compatibility."""
15791569
return None
15801570

1571+
"""Returns a rotated location given shift and N, where shift is the amount and N is the length of the sequence.
1572+
Special Case: if the shift is large enough, separated regions could become contiguos, so must be joined into a single one.
1573+
If all regions become contigous it returns a FeatureLocation.
1574+
"""
15811575
def rotate(self, shift, N):
15821576
locations_tmp = [location.rotate(shift, N).parts for location in self.parts]
15831577
locations = []
@@ -1588,25 +1582,22 @@ def rotate(self, shift, N):
15881582
for location1 in locations:
15891583
for location2 in locations:
15901584
if location1 is not location2:
1591-
#location1 - location2 attaccate
1585+
#location1 - location2, in order
15921586
if location2.start - 1 == location1.end:
15931587
tmp_feature = FeatureLocation(location1.start,location2.end,location1.strand, ref=location1.ref, ref_db=location1.ref_db)
15941588
if tmp_feature not in to_append:
15951589
to_append.append(tmp_feature)
15961590
to_delete.append(location1)
15971591
to_delete.append(location2)
1598-
1599-
#location2 - location1 attaccate
1592+
#location2 - location1, in order
16001593
if location1.start - 1 == location2.end:
16011594
tmp_feature = FeatureLocation(location2.start,location1.end,location2.strand, ref=location1.ref, ref_db=location1.ref_db)
16021595
if tmp_feature not in to_append:
16031596
to_append.append(tmp_feature)
16041597
to_delete.append(location1)
16051598
to_delete.append(location2)
1606-
1607-
locations = list(filter(lambda a: a not in to_delete, locations)) + to_append
1608-
1609-
if len(locations) == 1: #tutte le feature della compound sono state contratte
1599+
locations = list(filter(lambda a: a not in to_delete, locations)) + to_append #Updated locations
1600+
if len(locations) == 1: #Special case
16101601
return locations[0]
16111602
else:
16121603
locations.sort(key=lambda loc: loc.start)

Bio/SeqRecord.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1057,16 +1057,16 @@ def lower(self):
10571057
annotations=self.annotations.copy(),
10581058
letter_annotations=self.letter_annotations.copy(),
10591059
)
1060+
1061+
"""Rotates the record by shifting the sequence by shift amount
1062+
and then updates all the features and letter annotations accordingly
1063+
"""
10601064
def rotate(self, shift):
10611065
if (self.annotations["topology"]!="circular"):
10621066
raise Exception("Topology is not circular!")
1063-
#Shifta la sequenza
1064-
#shift = -shift
10651067
self.seq = self.seq[shift:] + self.seq[:shift]
1066-
#Aggiorna le posizioni delle features
10671068
for feature in self.features:
10681069
feature.location = feature.location.rotate(shift, len(self.seq)+1)
1069-
#Aggiorna le per letter_annotation
10701070
for i in self.letter_annotations.keys():
10711071
self.letter_annotations[i] = self.letter_annotations[i][shift:] + self.letter_annotations[i][:shift]
10721072

0 commit comments

Comments
 (0)