1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
|
Tabular data
------------
.. authors, Gavin Huttley, Kristian Rother, Patrick Yannul
.. doctest::
:hide:
>>> # just saving some tabular data for subsequent data
>>> from cogent import LoadTable
>>> rows = (('NP_003077', 'Con', 2.5386013224378985),
... ('NP_004893', 'Con', 0.12135142635634111e+06),
... ('NP_005079', 'Con', 0.95165949788861326e+07),
... ('NP_005500', 'NonCon', 0.73827030202664901e-07),
... ('NP_055852', 'NonCon', 1.0933217708952725e+07))
>>> table = LoadTable(header=['Locus', 'Region', 'Ratio'], rows=rows)
>>> table.writeToFile('stats.txt', sep=',')
Loading delimited formats
^^^^^^^^^^^^^^^^^^^^^^^^^
We load a comma separated data file using the generic ``LoadTable`` function.
.. doctest::
>>> from cogent import LoadTable
>>> table = LoadTable('stats.txt', sep=',')
>>> print table
====================================
Locus Region Ratio
------------------------------------
NP_003077 Con 2.5386
NP_004893 Con 121351.4264
NP_005079 Con 9516594.9789
NP_005500 NonCon 0.0000
NP_055852 NonCon 10933217.7090
------------------------------------
Reading large files
^^^^^^^^^^^^^^^^^^^
For really large files the automated conversion used by the standard read mechanism can be quite slow. If the data within a column is consistently of one type, set the ``LoadTable`` argument ``static_column_types=True``. This causes the ``Table`` object to create a custom reader.
.. doctest::
>>> table = LoadTable('stats.txt', static_column_types=True)
>>> print table
====================================
Locus Region Ratio
------------------------------------
NP_003077 Con 2.5386
NP_004893 Con 121351.4264
NP_005079 Con 9516594.9789
NP_005500 NonCon 0.0000
NP_055852 NonCon 10933217.7090
------------------------------------
Formatting
^^^^^^^^^^
Changing displayed numerical precision
""""""""""""""""""""""""""""""""""""""
We change the ``Ratio`` column to using scientific notation.
.. doctest::
>>> table.setColumnFormat('Ratio', '%.1e')
>>> print table
==============================
Locus Region Ratio
------------------------------
NP_003077 Con 2.5e+00
NP_004893 Con 1.2e+05
NP_005079 Con 9.5e+06
NP_005500 NonCon 7.4e-08
NP_055852 NonCon 1.1e+07
------------------------------
Change digits or column spacing
"""""""""""""""""""""""""""""""
This can be done on table loading,
.. doctest::
>>> table = LoadTable('stats.txt', sep=',', digits=1, space=2)
>>> print table
=============================
Locus Region Ratio
-----------------------------
NP_003077 Con 2.5
NP_004893 Con 121351.4
NP_005079 Con 9516595.0
NP_005500 NonCon 0.0
NP_055852 NonCon 10933217.7
-----------------------------
or, for spacing at least, by modifying the attributes
.. doctest::
>>> table.Space = ' '
>>> print table
=================================
Locus Region Ratio
---------------------------------
NP_003077 Con 2.5
NP_004893 Con 121351.4
NP_005079 Con 9516595.0
NP_005500 NonCon 0.0
NP_055852 NonCon 10933217.7
---------------------------------
Changing column headings
^^^^^^^^^^^^^^^^^^^^^^^^
The table ``Header`` is immutable. Changing column headings is done as follows.
.. doctest::
>>> table = LoadTable('stats.txt', sep=',')
>>> print table.Header
['Locus', 'Region', 'Ratio']
>>> table = table.withNewHeader('Ratio', 'Stat')
>>> print table.Header
['Locus', 'Region', 'Stat']
Creating new columns from existing ones
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This can be used to take a single, or multiple columns and generate a new column of values. Here we'll take 2 columns and return True/False based on a condition.
.. doctest::
>>> table = LoadTable('stats.txt', sep=',')
>>> table = table.withNewColumn('LargeCon',
... lambda (r,v): r == 'Con' and v>10.0,
... columns=['Region', 'Ratio'])
>>> print table
================================================
Locus Region Ratio LargeCon
------------------------------------------------
NP_003077 Con 2.5386 False
NP_004893 Con 121351.4264 True
NP_005079 Con 9516594.9789 True
NP_005500 NonCon 0.0000 False
NP_055852 NonCon 10933217.7090 False
------------------------------------------------
Appending tables
^^^^^^^^^^^^^^^^
Can be done without specifying a new column. Here we simply use the same table data.
.. doctest::
>>> table1 = LoadTable('stats.txt', sep=',')
>>> table2 = LoadTable('stats.txt', sep=',')
>>> table = table1.appended(None, table2)
>>> print table
====================================
Locus Region Ratio
------------------------------------
NP_003077 Con 2.5386
NP_004893 Con 121351.4264
NP_005079 Con 9516594.9789
NP_005500 NonCon 0.0000
NP_055852 NonCon 10933217.7090
NP_003077 Con 2.5386
NP_004893 Con 121351.4264
NP_005079 Con 9516594.9789
NP_005500 NonCon 0.0000
NP_055852 NonCon 10933217.7090
------------------------------------
or with a new column
.. doctest::
>>> table1.Title = 'Data1'
>>> table2.Title = 'Data2'
>>> table = table1.appended('Data#', table2, title='')
>>> print table
=============================================
Data# Locus Region Ratio
---------------------------------------------
Data1 NP_003077 Con 2.5386
Data1 NP_004893 Con 121351.4264
Data1 NP_005079 Con 9516594.9789
Data1 NP_005500 NonCon 0.0000
Data1 NP_055852 NonCon 10933217.7090
Data2 NP_003077 Con 2.5386
Data2 NP_004893 Con 121351.4264
Data2 NP_005079 Con 9516594.9789
Data2 NP_005500 NonCon 0.0000
Data2 NP_055852 NonCon 10933217.7090
---------------------------------------------
.. note:: We assigned an empty string to ``title``, otherwise the resulting table has the same ``Title`` attribute as that of ``table1``.
Summing a single column
^^^^^^^^^^^^^^^^^^^^^^^
.. doctest::
>>> table = LoadTable('stats.txt', sep=',')
>>> table.summed('Ratio')
20571166.652847398
Summing multiple columns or rows - strictly numerical data
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
We define a strictly numerical table,
.. doctest::
>>> all_numeric = LoadTable(header=['A', 'B', 'C'], rows=[range(3),
... range(3,6), range(6,9), range(9,12)])
>>> print all_numeric
=============
A B C
-------------
0 1 2
3 4 5
6 7 8
9 10 11
-------------
and sum all columns (default condition)
.. doctest::
>>> all_numeric.summed()
[18, 22, 26]
and all rows
.. doctest::
>>> all_numeric.summed(col_sum=False)
[3, 12, 21, 30]
Summing multiple columns or rows with mixed non-numeric/numeric data
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
We define a table with mixed data, like a distance matrix.
.. doctest::
>>> mixed = LoadTable(header=['A', 'B', 'C'], rows=[['*',1,2], [3,'*', 5],
... [6,7,'*']])
>>> print mixed
===========
A B C
-----------
* 1 2
3 * 5
6 7 *
-----------
and sum all columns (default condition), ignoring non-numerical data
.. doctest::
>>> mixed.summed(strict=False)
[9, 8, 7]
and all rows
.. doctest::
>>> mixed.summed(col_sum=False, strict=False)
[3, 8, 13]
Filtering table rows
^^^^^^^^^^^^^^^^^^^^
We can do this by providing a reference to an external function
.. doctest::
>>> table = LoadTable('stats.txt', sep=',')
>>> sub_table = table.filtered(lambda x: x < 10.0, columns='Ratio')
>>> print sub_table
=============================
Locus Region Ratio
-----------------------------
NP_003077 Con 2.5386
NP_005500 NonCon 0.0000
-----------------------------
or using valid python syntax within a string, which is executed
.. doctest::
>>> sub_table = table.filtered("Ratio < 10.0")
>>> print sub_table
=============================
Locus Region Ratio
-----------------------------
NP_003077 Con 2.5386
NP_005500 NonCon 0.0000
-----------------------------
You can also filter for values in multiple columns
.. doctest::
>>> sub_table = table.filtered("Ratio < 10.0 and Region == 'NonCon'")
>>> print sub_table
=============================
Locus Region Ratio
-----------------------------
NP_005500 NonCon 0.0000
-----------------------------
Filtering table columns
^^^^^^^^^^^^^^^^^^^^^^^
We select only columns that have a sum > 20 from the ``all_numeric`` table constructed above.
.. doctest::
>>> big_numeric = all_numeric.filteredByColumn(lambda x: sum(x)>20)
>>> print big_numeric
========
B C
--------
1 2
4 5
7 8
10 11
--------
Sorting
^^^^^^^
Standard sorting
""""""""""""""""
.. doctest::
>>> table = LoadTable('stats.txt', sep=',')
>>> print table.sorted(columns='Ratio')
====================================
Locus Region Ratio
------------------------------------
NP_005500 NonCon 0.0000
NP_003077 Con 2.5386
NP_004893 Con 121351.4264
NP_005079 Con 9516594.9789
NP_055852 NonCon 10933217.7090
------------------------------------
Reverse sorting
"""""""""""""""
.. doctest::
>>> print table.sorted(columns='Ratio', reverse='Ratio')
====================================
Locus Region Ratio
------------------------------------
NP_055852 NonCon 10933217.7090
NP_005079 Con 9516594.9789
NP_004893 Con 121351.4264
NP_003077 Con 2.5386
NP_005500 NonCon 0.0000
------------------------------------
Sorting involving multiple columns, one reversed
""""""""""""""""""""""""""""""""""""""""""""""""
.. doctest::
>>> print table.sorted(columns=['Region', 'Ratio'], reverse='Ratio')
====================================
Locus Region Ratio
------------------------------------
NP_005079 Con 9516594.9789
NP_004893 Con 121351.4264
NP_003077 Con 2.5386
NP_055852 NonCon 10933217.7090
NP_005500 NonCon 0.0000
------------------------------------
Getting raw data
^^^^^^^^^^^^^^^^
For a single column
"""""""""""""""""""
.. doctest::
>>> table = LoadTable('stats.txt', sep=',')
>>> raw = table.getRawData('Region')
>>> print raw
['Con', 'Con', 'Con', 'NonCon', 'NonCon']
For multiple columns
""""""""""""""""""""
.. doctest::
>>> table = LoadTable('stats.txt', sep=',')
>>> raw = table.getRawData(['Locus', 'Region'])
>>> print raw
[['NP_003077', 'Con'], ['NP_004893', 'Con'], ...
Iterating over table rows
^^^^^^^^^^^^^^^^^^^^^^^^^
.. doctest::
>>> table = LoadTable('stats.txt', sep=',')
>>> for row in table:
... print row['Locus']
...
NP_003077
NP_004893
NP_005079
NP_005500
NP_055852
Table slicing
^^^^^^^^^^^^^
Using column names
""""""""""""""""""
.. doctest::
>>> table = LoadTable('stats.txt', sep=',')
>>> print table[:2, :'Region']
=========
Locus
---------
NP_003077
NP_004893
---------
Using column indices
""""""""""""""""""""
.. doctest::
>>> table = LoadTable('stats.txt', sep=',')
>>> print table[:2,: 1]
=========
Locus
---------
NP_003077
NP_004893
---------
SQL-like capabilities
^^^^^^^^^^^^^^^^^^^^^
Distinct values
"""""""""""""""
.. doctest::
>>> table = LoadTable('stats.txt', sep=',')
>>> assert table.getDistinctValues('Region') == set(['NonCon', 'Con'])
Counting
""""""""
.. doctest::
>>> table = LoadTable('stats.txt', sep=',')
>>> assert table.count("Region == 'NonCon' and Ratio > 1") == 1
Joining tables
""""""""""""""
SQL-like join operations requires tables have different ``Title`` attributes which are not ``None``. We do a standard inner join here for a restricted subset. We must specify the columns that will be used for the join. Here we just use ``Locus`` but multiple columns can be used, and their names can be different between the tables. Note that the second table's title becomes a part of the column names.
.. doctest::
>>> rows = [['NP_004893', True], ['NP_005079', True],
... ['NP_005500', False], ['NP_055852', False]]
>>> region_type = LoadTable(header=['Locus', 'LargeCon'], rows=rows,
... title='RegionClass')
>>> stats_table = LoadTable('stats.txt', sep=',', title='Stats')
>>> new = stats_table.joined(region_type, columns_self='Locus')
>>> print new
============================================================
Locus Region Ratio RegionClass_LargeCon
------------------------------------------------------------
NP_004893 Con 121351.4264 True
NP_005079 Con 9516594.9789 True
NP_005500 NonCon 0.0000 False
NP_055852 NonCon 10933217.7090 False
------------------------------------------------------------
Exporting
^^^^^^^^^
Writing delimited formats
"""""""""""""""""""""""""
.. doctest::
>>> table = LoadTable('stats.txt', sep=',')
>>> table.writeToFile('stats_tab.txt', sep='\t')
Writing latex format
""""""""""""""""""""
It is also possible to specify column alignment, table caption and other arguments.
.. doctest::
>>> table = LoadTable('stats.txt', sep=',')
>>> print table.tostring(format='latex')
\begin{longtable}[htp!]{ r r r }
\hline
\bf{Locus} & \bf{Region} & \bf{Ratio} \\
\hline
\hline
NP_003077 & Con & 2.5386 \\
NP_004893 & Con & 121351.4264 \\
NP_005079 & Con & 9516594.9789 \\
NP_005500 & NonCon & 0.0000 \\
NP_055852 & NonCon & 10933217.7090 \\
\hline
\end{longtable}
.. we remove the table data
.. doctest::
:hide:
>>> import os
>>> os.remove('stats.txt')
>>> os.remove('stats_tab.txt')
|