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

**URL:** <https://discourse.julialang.org/t/how-to-convert-a-molecule-s-to-bitvector-to-matrix/87738>\
**Category:** Chemistry\
**Tags:** flux\
**Created:** [September 24, 2022, 10:13am UTC](https://discourse.julialang.org/t/how-to-convert-a-molecule-s-to-bitvector-to-matrix/87738 "2022-09-24T10:13:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nick\_m](https://avatars.discourse-cdn.com/v4/letter/n/8c91f0/32.png) [@nick\_m](https://discourse.julialang.org/u/nick_m)\
**Post date:** [September 24, 2022, 10:13am UTC](https://discourse.julialang.org/t/how-to-convert-a-molecule-s-to-bitvector-to-matrix/87738/1 "2022-09-24T10:13:08Z")

</div>

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

```julia
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.

```julia
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}?

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [September 24, 2022, 10:23am UTC](https://discourse.julialang.org/t/how-to-convert-a-molecule-s-to-bitvector-to-matrix/87738/2 "2022-09-24T10:23:37Z")

</div>

```julia
reshape(list_of_vector, (2,512))

```

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [September 24, 2022, 10:27am UTC](https://discourse.julialang.org/t/how-to-convert-a-molecule-s-to-bitvector-to-matrix/87738/3 "2022-09-24T10:27:05Z")

</div>

In your particular example, you could also do

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

```

---

<div class="post-metadata">

**Author:** ![nick\_m](https://avatars.discourse-cdn.com/v4/letter/n/8c91f0/32.png) [@nick\_m](https://discourse.julialang.org/u/nick_m)\
**Post date:** [September 24, 2022, 10:47am UTC](https://discourse.julialang.org/t/how-to-convert-a-molecule-s-to-bitvector-to-matrix/87738/4 "2022-09-24T10:47:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [September 24, 2022, 10:58am UTC](https://discourse.julialang.org/t/how-to-convert-a-molecule-s-to-bitvector-to-matrix/87738/5 "2022-09-24T10:58:13Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![nick\_m](https://avatars.discourse-cdn.com/v4/letter/n/8c91f0/32.png) [@nick\_m](https://discourse.julialang.org/u/nick_m)\
**Post date:** [September 24, 2022, 11:00am UTC](https://discourse.julialang.org/t/how-to-convert-a-molecule-s-to-bitvector-to-matrix/87738/6 "2022-09-24T11:00:29Z")

</div>

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'`

---

<div class="post-metadata">

**Author:** ![nick\_m](https://avatars.discourse-cdn.com/v4/letter/n/8c91f0/32.png) [@nick\_m](https://discourse.julialang.org/u/nick_m)\
**Post date:** [September 24, 2022, 11:10am UTC](https://discourse.julialang.org/t/how-to-convert-a-molecule-s-to-bitvector-to-matrix/87738/7 "2022-09-24T11:10:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [September 24, 2022, 1:53pm UTC](https://discourse.julialang.org/t/how-to-convert-a-molecule-s-to-bitvector-to-matrix/87738/8 "2022-09-24T13:53:25Z")

</div>

> [@nick\_m](#):
>
> What does apostrophe do to the vector  
> `morgan_fp'`

`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.
