lib5c.operators.trimming module

class lib5c.operators.trimming.InteractionTrimmer(value_threshold_lower=None, value_threshold_upper=None, locus_percentage_threshold_lower=None, locus_percentage_threshold_upper=None, global_percentage_threshold_lower=None, global_percentage_threshold_upper=None, locus_fold_threshold_lower=None, locus_fold_threshold_upper=None, global_fold_threshold_lower=None, global_fold_threshold_upper=None)[source]

Bases: lib5c.operators.base.InteractionMatrixOperator

Operator for removing specific interactions from an InteractionMatrix object according to specified criteria by setting their values to np.nan.

value_threshold_lower

If not None, interactions with values lower than this number will be removed.

Type

float or None

value_threshold_upper

If not None, interactions with values higher than this number will be removed.

Type

float or None

locus_percentage_threshold_lower

If not None, this percentage of interactions at each locus with the lowest values will be removed.

Type

float or None

locus_percentage_threshold_upper

If not None, this percentage of interactions at each locus with the highest values will be removed.

Type

float or None

global_percentage_threshold_lower

If not None, this percentage of interactions with the lowest values will be removed.

Type

float or None

global_percentage_threshold_upper

If not None, this percentage of interactions with the highest values will be removed.

Type

float or None

locus_fold_threshold_lower

If not None, interactions whose values are less than this many times the median value across either participating locus will be removed.

Type

float or None

locus_fold_threshold_upper

If not None, interactions whose values are more than this many times the median value across either participating locus will be removed.

Type

float or None

global_fold_threshold_lower

If not None, interactions whose values are less than this many times the median value across all interactions will be removed.

Type

float or None

global_fold_threshold_upper

If not None, interactions whose values are more than this many times the median value across all interactions will be removed.

Type

float or None

apply_inplace(target, **kwargs)[source]

Apply the trimming operation to the target InteractionMatrix.

Parameters
  • target (InteractionMatrix) – The InteractionMatrix object to trim.

  • kwargs (other keyword arguments) – To be utilized by subclasses.

Returns

The trimmed InteractionMatrix.

Return type

InteractionMatrix

Examples

>>> import numpy as np
>>> from lib5c.core.interactions import InteractionMatrix
>>> from lib5c.operators.trimming import InteractionTrimmer
>>> X = np.arange(16, dtype=float).reshape((4, 4))
>>> im = InteractionMatrix(X + X.T)
>>> print(im)
InteractionMatrix of size 4
[[ 0.  5. 10. 15.]
 [ 5. 10. 15. 20.]
 [10. 15. 20. 25.]
 [15. 20. 25. 30.]]
>>> trimmer = InteractionTrimmer(value_threshold_lower=5.0)
>>> print(trimmer.apply(im))
InteractionMatrix of size 4
[[nan nan 10. 15.]
[nan 10. 15. 20.]
[10. 15. 20. 25.]
[15. 20. 25. 30.]]
>>> trimmer = InteractionTrimmer(value_threshold_upper=25.0)
>>> print(trimmer.apply(im))
InteractionMatrix of size 4
[[ 0.  5. 10. 15.]
 [ 5. 10. 15. 20.]
 [10. 15. 20. nan]
 [15. 20. nan nan]]
>>> trimmer = InteractionTrimmer(locus_percentage_threshold_lower=25.0)
>>> print(trimmer.apply(im))
InteractionMatrix of size 4
[[nan nan nan nan]
 [nan 10. 15. 20.]
 [nan 15. 20. 25.]
 [nan 20. 25. 30.]]
>>> trimmer = InteractionTrimmer(locus_percentage_threshold_upper=75.0)
>>> print(trimmer.apply(im))
InteractionMatrix of size 4
[[ 0.  5. 10. nan]
 [ 5. 10. 15. nan]
 [10. 15. 20. nan]
 [nan nan nan nan]]
>>> trimmer = InteractionTrimmer(global_percentage_threshold_lower=25.0)
>>> print(trimmer.apply(im))
InteractionMatrix of size 4
[[nan nan nan 15.]
 [nan nan 15. 20.]
 [nan 15. 20. 25.]
 [15. 20. 25. 30.]]
>>> trimmer = InteractionTrimmer(global_percentage_threshold_upper=75.0)
>>> print(trimmer.apply(im))
InteractionMatrix of size 4
[[ 0.  5. 10. 15.]
 [ 5. 10. 15. nan]
 [10. 15. nan nan]
 [15. nan nan nan]]
>>> trimmer = InteractionTrimmer(locus_fold_threshold_lower=0.5)
>>> print(trimmer.apply(im))
InteractionMatrix of size 4
[[nan nan 10. 15.]
 [nan 10. 15. 20.]
 [10. 15. 20. 25.]
 [15. 20. 25. 30.]]
>>> trimmer = InteractionTrimmer(locus_fold_threshold_upper=2.0)
>>> print(trimmer.apply(im))
InteractionMatrix of size 4
[[ 0.  5. 10. nan]
 [ 5. 10. 15. 20.]
 [10. 15. 20. 25.]
 [nan 20. 25. 30.]]
>>> trimmer = InteractionTrimmer(global_fold_threshold_lower=0.25)
>>> print(trimmer.apply(im))
InteractionMatrix of size 4
[[nan  5. 10. 15.]
 [ 5. 10. 15. 20.]
 [10. 15. 20. 25.]
 [15. 20. 25. 30.]]
>>> trimmer = InteractionTrimmer(global_fold_threshold_upper=2.0)
>>> result = trimmer.apply(im)
>>> print(result)
InteractionMatrix of size 4
[[ 0.  5. 10. 15.]
 [ 5. 10. 15. 20.]
 [10. 15. 20. 25.]
 [15. 20. 25. nan]]
>>> result.print_log()
InteractionMatrix created
interactions trimmed with:
    global_fold_threshold_upper=2.0
class lib5c.operators.trimming.LocusTrimmer(sum_threshold_upper=None, sum_threshold_lower=None, max_threshold=None, min_threshold=None, percentage_threshold_lower=None, percentage_threshold_upper=None, fold_threshold_upper=None, fold_threshold_lower=None)[source]

Bases: lib5c.operators.base.InteractionMatrixOperator

Operator for removing Loci from an InteractionMatrix object according to specified criteria.

sum_threshold_upper

If not None, Loci whose row sums are greater than this value will be removed.

Type

float or None

sum_threshold_lower

If not None, Loci whose row sums are less than this value will be removed.

Type

float or None

max_threshold

If not None, Loci containing at least one interaction above this value will be removed.

Type

float or None

min_threshold

If not None, Loci containing at least one interaction below this value will be removed.

Type

float or None

percentage_threshold_lower

If not None, this percentage of of the Loci with the lowest row sums will be removed.

Type

float or None

percentage_threshold_upper

If not None, this percentage of of the Loci with the highest row sums will be removed.

Type

float or None

fold_threshold_upper

If not None, Loci whose row sums are more than this many times the median row sum will be removed.

Type

float or None

fold_threshold_lower

If not None, Loci whose row sums are less than this many times the median row sum will be removed.

Type

float or None

apply_inplace(target, **kwargs)[source]

Apply the trimming operation to the target InteractionMatrix.

Parameters
  • target (InteractionMatrix) – The InteractionMatrix object to trim.

  • kwargs (other keyword arguments) – To be utilized by subclasses.

Returns

The trimmed InteractionMatrix.

Return type

InteractionMatrix

Examples

>>> import numpy as np
>>> from lib5c.core.interactions import InteractionMatrix
>>> from lib5c.operators.trimming import LocusTrimmer
>>> X = np.arange(16, dtype=float).reshape((4, 4))
>>> im = InteractionMatrix(X + X.T)
>>> print(im)
InteractionMatrix of size 4
[[ 0.  5. 10. 15.]
 [ 5. 10. 15. 20.]
 [10. 15. 20. 25.]
 [15. 20. 25. 30.]]
>>> locus_trimmer = LocusTrimmer(sum_threshold_lower=35)
>>> print(locus_trimmer.apply(im))
InteractionMatrix of size 3
[[10. 15. 20.]
 [15. 20. 25.]
 [20. 25. 30.]]
>>> locus_trimmer = LocusTrimmer(percentage_threshold_lower=50.0)
>>> print(locus_trimmer.apply(im))
InteractionMatrix of size 2
[[20. 25.]
 [25. 30.]]
>>> locus_trimmer = LocusTrimmer(sum_threshold_upper=80.0)
>>> print(locus_trimmer.apply(im))
InteractionMatrix of size 3
[[ 0.  5. 10.]
 [ 5. 10. 15.]
 [10. 15. 20.]]
>>> locus_trimmer = LocusTrimmer(percentage_threshold_upper=50.0)
>>> print(locus_trimmer.apply(im))
InteractionMatrix of size 2
[[  0.   5.]
 [  5.  10.]]
>>> locus_trimmer = LocusTrimmer(min_threshold=0.0)
>>> print(locus_trimmer.apply(im))
InteractionMatrix of size 3
[[10. 15. 20.]
 [15. 20. 25.]
 [20. 25. 30.]]
>>> locus_trimmer = LocusTrimmer(max_threshold=30.0)
>>> print(locus_trimmer.apply(im))
InteractionMatrix of size 3
[[ 0.  5. 10.]
 [ 5. 10. 15.]
 [10. 15. 20.]]
>>> locus_trimmer = LocusTrimmer(fold_threshold_lower=0.5)
>>> print(locus_trimmer.apply(im))
InteractionMatrix of size 3
[[10. 15. 20.]
 [15. 20. 25.]
 [20. 25. 30.]]
>>> locus_trimmer = LocusTrimmer(fold_threshold_upper=1.5)
>>> result = locus_trimmer.apply(im)
>>> result.print_log()
InteractionMatrix created
loci trimmed with:
    fold_threshold_upper=1.5
deleted locus at index 3
>>> print(result)
InteractionMatrix of size 3
[[ 0.  5. 10.]
 [ 5. 10. 15.]
 [10. 15. 20.]]
>>> result = locus_trimmer.apply_inplace(im)
>>> im.print_log()
InteractionMatrix created
loci trimmed with:
    fold_threshold_upper=1.5
deleted locus at index 3
>>> print(im)
InteractionMatrix of size 3
[[ 0.  5. 10.]
 [ 5. 10. 15.]
 [10. 15. 20.]]
>>> import numpy as np
>>> from lib5c.core.interactions import InteractionMatrix
>>> from lib5c.operators.trimming import LocusTrimmer
>>> from lib5c.core.loci import Locus, LocusMap
>>> locus_list = [Locus('chr3', 34109023, 34113109, region='Sox2'),
...               Locus('chr3', 34113147, 34116141, region='Sox2'),
...               Locus('chr3', 87282063, 87285636, region='Nestin'),
...               Locus('chr3', 87285637, 87295935, region='Nestin')]
...
>>> locus_map = LocusMap(locus_list)
>>> X = np.arange(16, dtype=float).reshape((4, 4))
>>> im = InteractionMatrix(X + X.T, locusmap=locus_map)
>>> im.matrix
matrix([[ 0.,  5., 10., 15.],
        [ 5., 10., 15., 20.],
        [10., 15., 20., 25.],
        [15., 20., 25., 30.]])
>>> locus_trimmer = LocusTrimmer(percentage_threshold_lower=50.0)
>>> result = locus_trimmer.apply_by_region(im)
>>> print(result)
InteractionMatrix of size 2
[[10.  0.]
 [ 0. 30.]]
Associated LocusMap:
LocusMap comprising 2 loci
    Range: chr3:34113147-34116141 to chr3:87285637-87295935
    Regions: ['Sox2', 'Nestin']
>>> result.print_log()
InteractionMatrix created
applying by region
extracted region Sox2
loci trimmed with:
    percentage_threshold_lower=50.0
deleted locus at index 0
done applying by region