BioJulia/BioStructures.jl

Create a ContactMap from a BitArray

George3d6 opened this issue · 3 comments

Would it be possible to provide the functionality to create a ContactMap from a BitArray{2} ? Or to modify the .data property of an existing ContactMap ?

Fortunately, this already exists:

julia> a = rand(Bool, 5, 5)
5×5 Array{Bool,2}:
 false  false   true  false  false
 false  false  false  false  false
  true   true   true   true   true
  true  false   true  false  false
 false  false  false   true  false

julia> c = ContactMap(a)
Contact map of size (5, 5)

julia> c.data
5×5 BitArray{2}:
 false  false   true  false  false
 false  false  false  false  false
  true   true   true   true   true
  true  false   true  false  false
 false  false  false   true  false

julia> c.data[1, 1]
false

julia> c.data[1, 1] = true
true

julia> c.data[1, 1]
true

julia> c.data
5×5 BitArray{2}:
  true  false   true  false  false
 false  false  false  false  false
  true   true   true   true   true
  true  false   true  false  false
 false  false  false   true  false

A ContactMap is a struct so can't be modified, but the elements in the BitArray can be modified individually as above.

For more, run ?ContactMap:

  ContactMap(element, contact_distance)
  ContactMap(element_one, element_two, contact_distance)
  ContactMap(bit_array_2D)

  Calculate the contact map for a StructuralElementOrList, or between two StructuralElementOrLists.

  This returns a ContactMap type containing a BitArray{2} with true where the sub-elements are no further than the contact
  distance and false otherwise. When one element is given as input this returns a symmetric square matrix.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  cbetas_A = collectatoms(struc["A"], cbetaselector)
  cbetas_B = collectatoms(struc["B"], cbetaselector)
  
  # Contact map of chain A using standard C-beta and 8.0 Å definitions
  ContactMap(cbetas_A, 8.0)
  
  # Rectangular contact map of chains A and B
  ContactMap(cbetas_A, cbetas_B, 8.0)

Ah, my bad, I did try constructing from a BitArray but I guess the type of the variable might have been wrong in some way and an error was given.

Sorry for bothering you and thanks for the quick answer :)

No worries, and I'm happy to answer any questions you have about the package.

For reference when something is passed to a type constructor it is passed through the convert method if it isn't the correct type. So anything that can be converted to a BitArray{2} will work, but if it can't be then it wont.