How to convert a molecule(s) to bitvector to matrix

I am new to Julia and trying to convert a list of molecules to a matrix. I am using rdkit, so far I’ve got

mol = RDKitMinimalLib.get_mol("CC(=O)Oc1ccccc1C(=O)O")
fp_details = Dict{String, Any}("nBits" => 512, "radius" => 2)
morgan_fp = Int8.(collect(RDKitMinimalLib.get_morgan_fp(mol, fp_details))) .-Int8(48)

I am appending an empty list.

list_of_vector = []
append!(list_of_vector,morgan_fp)
append!(list_of_vector,morgan_fp)

How can I convert this vector{Any} to a matrix{2x512}?

reshape(list_of_vector, (2,512))

In your particular example, you could also do

morgan_fp_mat = [morgan_fp' ; morgan_fp']   # or vcat(morgan_fp', morgan_fp')

Thanks but for this example I got this error ERROR: LoadError: DimensionMismatch(“new dimensions (2, 512) must be consistent with array size 2”)

In this case, the reshape command only works if the initial vector is of length 1024, because then you can reorder it into a matrix with column length 512. You can check the length of a vector with length(list_of_vector).

Here is the full example

morgan_fp = randn(512) # or the output you get from the first snippet of your post
length(morgan_fp) # = 512
list_of_vectors = []
length(list_of_vectors) # = 0
append!(list_of_vectors, morgan_fp)
append!(list_of_vectors, morgan_fp)
length(list_of_vectors) # = 1024
# now you can reshape
mat_morgan_fp = reshape(list_of_vectors, (2,512))

But the solution @SteffenPL posted is more elegant in this case.

Thanks this worked! I should do a bit more googling but say I had a long list of fingerprints how would I convert the code? What does apostrophe do to the vector
morgan_fp'

Great thanks for the extra working, this is also working for me. :grinning:

morgan_fp' is equivalent to transpose(morgan_fp)
It is needed, because in Julia vectors are column vectors by default. Concatenating along the vertical axis (vcat) requires you to pass in two row vectors of length 512 if your matrix is supposed to be of size 2 x 512 then.

1 Like