マップの分割


マップといっても地図の事ではなく、データが入っているテーブルの事。

以下の table A

- 列 id が、 1 ~ 24 の 24 列
- 行 id が、 1 ~ 16 の 16 行
- 計 384 の個別データが、それぞれのセルに保存

の様なデータがあって、それぞれを、 A, B, C, D の 96 x 4 の個別のテーブルに分けたい場合（ table B ）。


**table A**

|384|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|<b>1</b>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|
|<b>2</b>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|
|<b>3</b>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|
|<b>4</b>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|
|<b>5</b>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|
|<b>6</b>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|
|<b>7</b>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|
|<b>8</b>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|
|<b>9</b>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|
|<b>10</b>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|
|<b>11</b>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|
|<b>12</b>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|
|<b>13</b>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|
|<b>14</b>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|
|<b>15</b>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|<font color='red'>A</font>|<font color='blue'>C</font>|
|<b>16</b>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|<font color='green'>B</font>|<font color='orange'>D</font>|


**table B**
plate A のみを表示

|96|1|2|3|4|5|6|7|8|9|10|11|12|
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|<b>1</b>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|
|<b>2</b>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|
|<b>3</b>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|
|<b>4</b>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|
|<b>5</b>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|
|<b>6</b>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|
|<b>7</b>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|
|<b>8</b>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|<font color='red'>A</font>|


### 考え方、

384 穴プレートの行インデクスを `$row_ind_384` 、列インデクスを `$col_ind_384` とした場合。


- table A の列インデクスは、table B では、1/2 に。 => `int ( $col_ind_384 / 2 )`
	- `int` は、小数点以下切り捨て
- table A の行インデクスも、1/2 に。 => `int ( $row_ind_384 / 2 )`
- A, B は、table A の奇数列に。 => 目的の列は、 `$col_ind_384 % 2` で　0　を示す。
- C, D は、table A の偶数列に。 => 目的の列は、 `$col_ind_384 % 2` で　1　を示す。
- A, C は、table A の奇数行に。 => 目的の行は、 `$row_ind_384 % 2` で　0　を示す。
- B, D は、table A の偶数行に。 => 目的の行は、 `$row_ind_384 % 2` で　1　を示す。

後半の A, B, C, D の判断は、見た通り `( $col_ind_384 % 2) . ( $row_ind_384 % 2 )` で対応を取る事が出来る。

```
A => 00
B => 01 
C => 10
D => 11
```

これは、単純な二進数。

後は、行列のインデクスで全てのアドレスを舐めるだけ。


### で、コード

```pl:#1
for my $col_ind_384 ( 0 .. 23 ){
	for my $row_ind_384 ( 0 .. 15 ){
        my $row_ind_96 = int ( $row_ind_384 / 2 );
        my $col_ind_96 = int ( $col_ind_384 / 2 );
        my $plate_id  = ( qw( A B C D ) )[ oct '0b' . ( $col_ind_384 % 2 ) . ( $row_ind_384 % 2 ) ] ; 
        printf "%s\n", join "\t", $row_ind_384, $col_ind_384, $plate_id, $row_ind_96, $col_ind_96 ;
    }
}
```

- プレート id は、まあ、二進数数値そのままでも良い訳だけど、今回は A, B, C, D で。
- `oct '0b' . ( $col_ind_384 % 2 ) . ( $row_ind_384 % 2 )` は単に、二進数を十進数に変換している。
	- で、無名配列のインデクスとして利用

#### 行 id の変換

今迄書いた事は [384 穴プレート](https://www.google.co.jp/search?q=dna+plate+384&tbm=isch)から、 [96 穴プレート](https://www.google.co.jp/search?q=dna+plate+96&tbm=isch)への対応を取るコード。

だけど実際には、

- 行 id は、384 穴では A .. P, 96 穴では A .. H
- 列 id は、1 ~ 24 ( or 12 )
- アドレスとして、行 id . 列 id を使用。 ex) `J13`

で使ってるから、そちらにした方がなにかと都合が良い。
冗長になりがちなのは、行 id の変換だろう。



```
print chr ( int ( ( ord($_) - 65 ) / 2 ) + 65 ) for 'A' .. 'P' ;
```

- `ord()` は、文字からアスキーコードへの変換
	- `- 65`[^1]する事で、行のインデクスが得られる。
- `chr()` は、アスキーから文字列への変換
	- 行のインデクスに `+ 65` する事でアスキー。それを `chr`


[^1]: 65 は、 'A' のアスキーコード

条件式をだらだら書くよりは、 `chr`, `ord`, `oct` 使って纏めて書いた方が、混乱は少ない[^2]、、、かな。

[^2]: 変数代入など、きちんと行なってコメント書いとくのは当然として。

### 蛇足 1. ...

説明無し

```
my %cnv_384_to_96 
    = map{
        my $i = $_ ;
        map {
            $_ . $i,
            {
                id    => ( qw( A B C D ) )[ oct '0b' . ( ($i - 1) % 2 ) . ( (ord($_) - 65 ) % 2 ) ], 
                addr_96 => chr ( int ( ( ord($_) - 65 ) / 2 ) + 65 ) . int ( ( $i - 1 ) / 2 + 1 ),
            }
        } 'A' .. 'P'
        ;
    } 1 .. 24
    ;
```

### 蛇足 2. 気の迷い

\#1 を ruby にて書いてみた

```ruby:ruby
(0..23).step{ |col_ind_384|
    (0..15).step{ |row_ind_384|
        col_ind_96 = ( col_ind_384 / 2 ).to_i
        row_ind_96 = ( row_ind_384 / 2 ).to_i
        plate_id  = [ 'A', 'B', 'C', 'D' ].at( ( (col_ind_384 % 2 ).to_s + ( row_ind_384 % 2 ).to_s ).to_i(2) )
        puts [ row_ind_384, col_ind_384, plate_id, row_ind_96, col_ind_96 ].join("\t") 
    }
}
```