Reputation: 191
I have two Numpy arrays which I need to combine maintaining only certain columns from A
- size (888, 1114253)
, depending on the rows I have in B
- size (555861, 3)
.
The problem is that the header of A
is 55730
: each column has two values!
In other words I want to get only the columns of A
where the header corresponds to the rows in B
, but in A
each column is "double"
An example will clarify:
A
:
family id mum dad rs1 rs2 rs3 rs4 rs5 rs6 rs7 rs8 rs9 rs10 rs11 rs12
1 1 4 6 A T A A T T C C G G A T A G A A G A T A G G C C
2 2 7 9 T A G A C T C T G A T T A A A C G G T A C C C T
3 3 2 8 T T G G C T C T G G A T A G A C G G T T C C C C
4 4 5 1 A A A A T T C C G A T T A A A A G A T A G C C T
Since in this file each rsxxx
column header has two corresponding columns, I have to find a way to put them together, so I can read the file as an array
B
:
1 rs1 2345
1 rs2 2346
2 rs5 2348
4 rs8 2351
4 rs12 2360
The desired output is
Output
:
family id mum dad rs1 rs2 rs5 rs8 rs12
1 1 4 6 A T A A G G A A C C
2 2 7 9 T A G A G A A C C T
3 3 2 8 T T G G G G A C C C
4 4 5 1 A A A A G A A A C T
Ideas?
On the console
B
:
array([['1', 'rs3094315', '752566'],
['1', 'rs12562034', '768448'],
['1', 'rs3934834', '1005806'],
...,
['23', 'rs2032612', '21866491'],
['23', 'rs2032621', '21872738'],
['23', 'rs2032617', '21896261']],
dtype='<S10')
Upvotes: 1
Views: 1879
Reputation: 46530
It looks like each column is separated by two spaces, but that each gene pair is separated by one space. If this is so you can use
delimiter=' ' #two spaces
in np.loadtxt
:
import numpy as np
from StringIO import StringIO # for example file
a = StringIO("""family id mum dad rs1 rs2 rs3 rs4 rs5 rs6 rs7 rs8 rs9 rs10 rs11 rs12
1 1 4 6 A T A A T T C C G G A T A G A A G A T A G G C C
2 2 7 9 T A G A C T C T G A T T A A A C G G T A C C C T
3 3 2 8 T T G G C T C T G G A T A G A C G G T T C C C C
4 4 5 1 A A A A T T C C G A T T A A A A G A T A G C C T """)
nrs = 12 # number of `rs` columns, for dtype
dt = 'int,'*4 + 'S10,'*nrs
A = np.genfromtxt(a, delimiter=' ', names=True, dtype=dt)
A
:
array([ (1, 1, 4, 6, ' A T', 'A A', 'T T', 'C C', 'G G', 'A T', 'A G', 'A A', 'G A', 'T A', 'G G', 'C C'),
(2, 2, 7, 9, ' T A', 'G A', 'C T', 'C T', 'G A', 'T T', 'A A', 'A C', 'G G', 'T A', 'C C', 'C T'),
(3, 3, 2, 8, ' T T', 'G G', 'C T', 'C T', 'G G', 'A T', 'A G', 'A C', 'G G', 'T T', 'C C', 'C C'),
(4, 4, 5, 1, ' A A', 'A A', 'T T', 'C C', 'G A', 'T T', 'A A', 'A A', 'G A', 'T A', 'G C', 'C T')],
dtype=[('family', '<i8'), ('id', '<i8'), ('mum', '<i8'), ('dad', '<i8'), ('rs1', 'S10'), ('rs2', 'S10'), ('rs3', 'S10'), ('rs4', 'S10'), ('rs5', 'S10'), ('rs6', 'S10'), ('rs7', 'S10'), ('rs8', 'S10'), ('rs9', 'S10'), ('rs10', 'S10'), ('rs11', 'S10'), ('rs12', 'S10')])
Then to access only the columns from B
, do something like this:
b = StringIO("""1 rs1 2345
1 rs2 2346
2 rs5 2348
4 rs8 2351
4 rs12 2360""")
B = np.genfromtxt(b, usecols=[1], dtype='S10')
Now, use A[B]
:
A[B]
array([(' A T', 'A A', 'G G', 'A A', 'C C'),
(' T A', 'G A', 'G A', 'A C', 'C T'),
(' T T', 'G G', 'G G', 'A C', 'C C'),
(' A A', 'A A', 'G A', 'A A', 'C T')],
dtype=[('rs1', 'S10'), ('rs2', 'S10'), ('rs5', 'S10'), ('rs8', 'S10'), ('rs12', 'S10')])
Or, if you want the first four columns too:
A[['family', 'id', 'mum', 'dad'] + list(B)]
array([(1, 1, 4, 6, ' A T', 'A A', 'G G', 'A A', 'C C'),
(2, 2, 7, 9, ' T A', 'G A', 'G A', 'A C', 'C T'),
(3, 3, 2, 8, ' T T', 'G G', 'G G', 'A C', 'C C'),
(4, 4, 5, 1, ' A A', 'A A', 'G A', 'A A', 'C T')],
dtype=[('family', '<i8'), ('id', '<i8'), ('mum', '<i8'), ('dad', '<i8'), ('rs1', 'S10'), ('rs2', 'S10'), ('rs5', 'S10'), ('rs8', 'S10'), ('rs12', 'S10')])
Upvotes: 2