File: chapter_blast.rst

package info (click to toggle)
python-biopython 1.85%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 126,372 kB
  • sloc: xml: 1,047,995; python: 332,722; ansic: 16,944; sql: 1,208; makefile: 140; sh: 81
file content (2011 lines) | stat: -rw-r--r-- 74,604 bytes parent folder | download
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
.. _`chapter:blast`:

BLAST (new)
===========

Hey, everybody loves BLAST right? I mean, geez, how can it get any
easier to do comparisons between one of your sequences and every other
sequence in the known world? But, of course, this section isn’t about
how cool BLAST is, since we already know that. It is about the problem
with BLAST – it can be really difficult to deal with the volume of data
generated by large runs, and to automate BLAST runs in general.

Fortunately, the Biopython folks know this only too well, so they’ve
developed lots of tools for dealing with BLAST and making things much
easier. This section details how to use these tools and do useful things
with them.

Dealing with BLAST can be split up into two steps, both of which can be
done from within Biopython. Firstly, running BLAST for your query
sequence(s), and getting some output. Secondly, parsing the BLAST output
in Python for further analysis.

Your first introduction to running BLAST was probably via the `NCBI
BLAST web page <https://blast.ncbi.nlm.nih.gov/Blast.cgi>`__. In fact,
there are lots of ways you can run BLAST, which can be categorized in
several ways. The most important distinction is running BLAST locally
(on your own machine), and running BLAST remotely (on another machine,
typically the NCBI servers). We’re going to start this chapter by
invoking the NCBI online BLAST service from within a Python script.

.. _`sec:running-www-blast`:

Running BLAST over the Internet
-------------------------------

We use the function ``qblast`` in the ``Bio.Blast`` module to call the
online version of BLAST.

The `NCBI
guidelines <https://blast.ncbi.nlm.nih.gov/doc/blast-help/developerinfo.html#developerinfo>`__
state:

#. Do not contact the server more often than once every 10 seconds.

#. Do not poll for any single RID more often than once a minute.

#. Use the URL parameter email and tool, so that the NCBI can contact
   you if there is a problem.

#. Run scripts weekends or between 9 pm and 5 am Eastern time on
   weekdays if more than 50 searches will be submitted.

``Blast.qblast`` follows the first two points automatically. To fulfill
the third point, set the ``Blast.email`` variable (the ``Blast.tool``
variable is already set to ``"biopython"`` by default):

.. doctest

.. code:: pycon

   >>> from Bio import Blast
   >>> Blast.tool
   'biopython'
   >>> Blast.email = "A.N.Other@example.com"

.. _`subsec:blast-arguments`:

BLAST arguments
~~~~~~~~~~~~~~~

The ``qblast`` function has three non-optional arguments:

-  The first argument is the BLAST program to use for the search, as a
   lower case string. The programs and their options are described at
   the `NCBI BLAST web
   page <https://blast.ncbi.nlm.nih.gov/Blast.cgi>`__. Currently
   ``qblast`` only works with blastn, blastp, blastx, tblast and
   tblastx.

-  The second argument specifies the databases to search against. Again,
   the options for this are available on `NCBI’s BLAST Help
   pages <https://blast.ncbi.nlm.nih.gov/doc/blast-help/>`__.

-  The third argument is a string containing your query sequence. This
   can either be the sequence itself, the sequence in fasta format, or
   an identifier like a GI number.

The ``qblast`` function also takes a number of other option arguments,
which are basically analogous to the different parameters you can set on
the BLAST web page. We’ll just highlight a few of them here:

-  The argument ``url_base`` sets the base URL for running BLAST over
   the internet. By default it connects to the NCBI.

-  The ``qblast`` function can return the BLAST results in various
   formats, which you can choose with the optional ``format_type``
   keyword: ``"XML"``, ``"HTML"``, ``"Text"``, ``"XML2"``, ``"JSON2"``,
   or ``"Tabular"``. The default is ``"XML"``, as that is the format
   expected by the parser, described in
   section :ref:`sec:parsing-blast` below.

-  The argument ``expect`` sets the expectation or e-value threshold.

For more about the optional BLAST arguments, we refer you to the NCBI’s
own documentation, or that built into Biopython:

.. code:: pycon

   >>> from Bio import Blast
   >>> help(Blast.qblast)

Note that the default settings on the NCBI BLAST website are not quite
the same as the defaults on QBLAST. If you get different results, you’ll
need to check the parameters (e.g., the expectation value threshold and
the gap values).

For example, if you have a nucleotide sequence you want to search
against the nucleotide database (nt) using BLASTN, and you know the GI
number of your query sequence, you can use:

.. code:: pycon

   >>> from Bio import Blast
   >>> result_stream = Blast.qblast("blastn", "nt", "8332116")

Alternatively, if we have our query sequence already in a FASTA
formatted file, we just need to open the file and read in this record as
a string, and use that as the query argument:

.. code:: pycon

   >>> from Bio import Blast
   >>> fasta_string = open("m_cold.fasta").read()
   >>> result_stream = Blast.qblast("blastn", "nt", fasta_string)

We could also have read in the FASTA file as a ``SeqRecord`` and then
supplied just the sequence itself:

.. code:: pycon

   >>> from Bio import Blast
   >>> from Bio import SeqIO
   >>> record = SeqIO.read("m_cold.fasta", "fasta")
   >>> result_stream = Blast.qblast("blastn", "nt", record.seq)

Supplying just the sequence means that BLAST will assign an identifier
for your sequence automatically. You might prefer to call ``format`` on
the ``SeqRecord`` object to make a FASTA string (which will include the
existing identifier):

.. code:: pycon

   >>> from Bio import Blast
   >>> from Bio import SeqIO
   >>> records = SeqIO.parse("ls_orchid.gbk", "genbank")
   >>> record = next(records)
   >>> result_stream = Blast.qblast("blastn", "nt", format(record, "fasta"))

This approach makes more sense if you have your sequence(s) in a
non-FASTA file format which you can extract using ``Bio.SeqIO`` (see
Chapter :ref:`chapter:seqio`).

.. _`subsec:saving-blast-results`:

Saving BLAST results
~~~~~~~~~~~~~~~~~~~~

Whatever arguments you give the ``qblast()`` function, you should get
back your results as a stream of ``bytes`` data (by default in XML
format). The next step would be to parse the XML output into Python
objects representing the search results
(Section :ref:`sec:parsing-blast`), but you might want to save a
local copy of the output file first. I find this especially useful when
debugging my code that extracts info from the BLAST results (because
re-running the online search is slow and wastes the NCBI computer time).

We need to be a bit careful since we can use ``result_stream.read()`` to
read the BLAST output only once – calling ``result_stream.read()`` again
returns an empty ``bytes`` object.

.. code:: pycon

   >>> with open("my_blast.xml", "wb") as out_stream:
   ...     out_stream.write(result_stream.read())
   ...
   >>> result_stream.close()

After doing this, the results are in the file ``my_blast.xml`` and
``result_stream`` has had all its data extracted (so we closed it).
However, the ``parse`` function of the BLAST parser (described
in :ref:`sec:parsing-blast`) takes a file-like object, so we can
just open the saved file for input as ``bytes``:

.. code:: pycon

   >>> result_stream = open("my_blast.xml", "rb")

Now that we’ve got the BLAST results back into a data stream again, we
are ready to do something with them, so this leads us right into the
parsing section (see Section :ref:`sec:parsing-blast` below). You
may want to jump ahead to that now ….

.. _`subsec:blast-other-formats`:

Obtaining BLAST output in other formats
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

By using the ``format_type`` argument when calling ``qblast``, you can
obtain BLAST output in formats other than XML. Below is an example of
reading BLAST output in JSON format. Using ``format_type="JSON2"``, the
data provided by ``Blast.qblast`` will be in zipped JSON format:

.. code:: pycon

   >>> from Bio import Blast
   >>> from Bio import SeqIO
   >>> record = SeqIO.read("m_cold.fasta", "fasta")
   >>> result_stream = Blast.qblast("blastn", "nt", record.seq, format_type="JSON2")
   >>> data = result_stream.read()
   >>> data[:4]
   b'PK\x03\x04'

which is the ZIP file magic number.

.. code:: pycon

   >>> with open("myzipfile.zip", "wb") as out_stream:
   ...     out_stream.write(data)
   ...
   13813

Note that we read and write the data as ``bytes``. Now open the ZIP file
we created:

.. code:: pycon

   >>> import zipfile
   >>> myzipfile = zipfile.ZipFile("myzipfile.zip")
   >>> myzipfile.namelist()
   ['N5KN7UMJ013.json', 'N5KN7UMJ013_1.json']
   >>> stream = myzipfile.open("N5KN7UMJ013.json")
   >>> data = stream.read()

These data are ``bytes``, so we need to decode them to get a string
object:

.. code:: pycon

   >>> data = data.decode()
   >>> print(data)
   {
       "BlastJSON": [
           {"File": "N5KN7UMJ013_1.json" }
       ]
   }

Now open the second file contained in the ZIP file to get the BLAST
results in JSON format:

.. code:: pycon

   >>> stream = myzipfile.open("N5KN7UMJ013_1.json")
   >>> data = stream.read()
   >>> len(data)
   145707
   >>> data = data.decode()
   >>> print(data)
   {
     "BlastOutput2": {
       "report": {
         "program": "blastn",
         "version": "BLASTN 2.14.1+",
         "reference": "Stephen F. Altschul, Thomas L. Madden, Alejandro A. ...
         "search_target": {
           "db": "nt"
         },
         "params": {
           "expect": 10,
           "sc_match": 2,
           "sc_mismatch": -3,
           "gap_open": 5,
           "gap_extend": 2,
           "filter": "L;m;"
         },
         "results": {
           "search": {
             "query_id": "Query_69183",
             "query_len": 1111,
             "query_masking": [
               {
                 "from": 797,
                 "to": 1110
               }
             ],
             "hits": [
               {
                 "num": 1,
                 "description": [
                   {
                     "id": "gi|1219041180|ref|XM_021875076.1|",
   ...

We can use the JSON parser in Python’s standard library to convert the
JSON data into a regular Python dictionary:

.. code:: pycon

   >>> import json
   >>> d = json.loads(data)
   >>> print(d)
   {'BlastOutput2': {'report': {'program': 'blastn', 'version': 'BLASTN 2.14.1+',
    'reference': 'Stephen F. Altschul, Thomas L. Madden, Alejandro A. Sch&auml;ffer,
    Jinghui Zhang, Zheng Zhang, Webb Miller, and David J. Lipman (1997),
    "Gapped BLAST and PSI-BLAST: a new generation of protein database search programs",
    Nucleic Acids Res. 25:3389-3402.',
    'search_target': {'db': 'nt'}, 'params': {'expect': 10, 'sc_match': 2,
    'sc_mismatch': -3, 'gap_open': 5, 'gap_extend': 2, 'filter': 'L;m;'},
    'results': {'search': {'query_id': 'Query_128889', 'query_len': 1111,
    'query_masking': [{'from': 797, 'to': 1110}], 'hits': [{'num': 1,
    'description': [{'id': 'gi|1219041180|ref|XM_021875076.1|', 'accession':
    'XM_021875076', 'title':
    'PREDICTED: Chenopodium quinoa cold-regulated 413 plasma membrane protein 2-like (LOC110697660), mRNA',
    'taxid': 63459, 'sciname': 'Chenopodium quinoa'}], 'len': 1173, 'hsps':
    [{'num': 1, 'bit_score': 435.898, 'score': 482, 'evalue': 9.02832e-117,
    'identity': 473, 'query_from'
   ...

.. _`sec:running-local-blast`:

Running BLAST locally
---------------------

Introduction
~~~~~~~~~~~~

Running BLAST locally (as opposed to over the internet, see
Section :ref:`sec:running-www-blast`) has at least major two
advantages:

-  Local BLAST may be faster than BLAST over the internet;

-  Local BLAST allows you to make your own database to search for
   sequences against.

Dealing with proprietary or unpublished sequence data can be another
reason to run BLAST locally. You may not be allowed to redistribute the
sequences, so submitting them to the NCBI as a BLAST query would not be
an option.

Unfortunately, there are some major drawbacks too – installing all the
bits and getting it setup right takes some effort:

-  Local BLAST requires command line tools to be installed.

-  Local BLAST requires (large) BLAST databases to be setup (and
   potentially kept up to date).

Standalone NCBI BLAST+
~~~~~~~~~~~~~~~~~~~~~~

The “new” `NCBI
BLAST+ <https://blast.ncbi.nlm.nih.gov/Blast.cgi?CMD=Web&PAGE_TYPE=BlastDocs&DOC_TYPE=Download>`__
suite was released in 2009. This replaces the old NCBI “legacy” BLAST
package (see :ref:`subsec:other-blast-versions`).

This section will show briefly how to use these tools from within
Python. If you have already read or tried the alignment tool examples in
Section :ref:`sec:alignment-tools` this should all
seem quite straightforward. First, we construct a command line string
(as you would type in at the command line prompt if running standalone
BLAST by hand). Then we can execute this command from within Python.

For example, taking a FASTA file of gene nucleotide sequences, you might
want to run a BLASTX (translation) search against the non-redundant (NR)
protein database. Assuming you (or your systems administrator) has
downloaded and installed the NR database, you might run:

.. code:: console

   $ blastx -query opuntia.fasta -db nr -out opuntia.xml -evalue 0.001 -outfmt 5

This should run BLASTX against the NR database, using an expectation
cut-off value of :math:`0.001` and produce XML output to the specified
file (which we can then parse). On my computer this takes about six
minutes - a good reason to save the output to a file so you can repeat
any analysis as needed.

From within python we can use the ``subprocess`` module to build the
command line string, and run it:

.. code:: pycon

   >>> import subprocess
   >>> cmd = "blastx -query opuntia.fasta -db nr -out opuntia.xml"
   >>> cmd += " -evalue 0.001 -outfmt 5"
   >>> subprocess.run(cmd, shell=True)

In this example there shouldn’t be any output from BLASTX to the
terminal. You may want to check the output file ``opuntia.xml`` has been
created.

As you may recall from earlier examples in the tutorial, the
``opuntia.fasta`` contains seven sequences, so the BLAST XML output
should contain multiple results. Therefore use ``Bio.Blast.parse()`` to
parse it as described below in Section :ref:`sec:parsing-blast`.

.. _`subsec:other-blast-versions`:

Other versions of BLAST
~~~~~~~~~~~~~~~~~~~~~~~

NCBI BLAST+ (written in C++) was first released in 2009 as a replacement
for the original NCBI “legacy” BLAST (written in C) which is no longer
being updated. You may also come across `Washington University
BLAST <http://blast.wustl.edu/>`__ (WU-BLAST), and its successor,
`Advanced Biocomputing BLAST <https://blast.advbiocomp.com>`__
(AB-BLAST, released in 2009, not free/open source). These packages
include the command line tools ``wu-blastall`` and ``ab-blastall``,
which mimicked ``blastall`` from the NCBI “legacy” BLAST suite.
Biopython does not currently provide wrappers for calling these tools,
but should be able to parse any NCBI compatible output from them.

.. _`sec:parsing-blast`:

Parsing BLAST output
--------------------

As mentioned above, BLAST can generate output in various formats, such as XML,
HTML, and plain text. Originally, Biopython had parsers for BLAST plain text
and HTML output, as these were the only output formats offered at the time.
These parsers have now been removed from Biopython, as the BLAST output in
these formats kept changing, each time breaking the Biopython parsers.
Nowadays, Biopython can parse BLAST output in the XML format, the XML2 format,
and tabular format. This chapter describes the parser for BLAST output in the
XML and XML2 formats using the ``Bio.Blast.parse`` function. This function
automatically detects if the XML file is in the XML format or in the XML2
format.
BLAST output in tabular format can be parsed as alignments using the
``Bio.Align.parse`` function (see the section :ref:`subsec:align_tabular`).

You can get BLAST output in XML format in various ways. For the parser,
it doesn’t matter how the output was generated, as long as it is in the
XML format.

-  You can use Biopython to run BLAST over the internet, as described in
   section :ref:`sec:running-www-blast`.

-  You can use Biopython to run BLAST locally, as described in
   section :ref:`sec:running-local-blast`.

-  You can do the BLAST search yourself on the NCBI site through your
   web browser, and then save the results. You need to choose XML as the
   format in which to receive the results, and save the final BLAST page
   you get (you know, the one with all of the interesting results!) to a
   file.

-  You can also run BLAST locally without using Biopython, and save the
   output in a file. Again, you need to choose XML as the format in
   which to receive the results.

The important point is that you do not have to use Biopython scripts to
fetch the data in order to be able to parse it. Doing things in one of
these ways, you then need to get a file-like object to the results. In
Python, a file-like object or handle is just a nice general way of
describing input to any info source so that the info can be retrieved
using ``read()`` and ``readline()`` functions (see
Section :ref:`sec:appendix-handles`).

If you followed the code above for interacting with BLAST through a
script, then you already have ``result_stream``, the file-like object to
the BLAST results. For example, using a GI number to do an online
search:

.. code:: pycon

   >>> from Bio import Blast
   >>> result_stream = Blast.qblast("blastn", "nt", "8332116")

If instead you ran BLAST some other way, and have the BLAST output (in
XML format) in the file ``my_blast.xml``, all you need to do is to open
the file for reading (as ``bytes``):

.. code:: pycon

   >>> result_stream = open("my_blast.xml", "rb")

Now that we’ve got a data stream, we are ready to parse the output. The
code to parse it is really quite small. If you expect a single BLAST
result (i.e., you used a single query):

.. code:: pycon

   >>> from Bio import Blast
   >>> blast_record = Blast.read(result_stream)

or, if you have lots of results (i.e., multiple query sequences):

.. code:: pycon

   >>> from Bio import Blast
   >>> blast_records = Blast.parse(result_stream)

Just like ``Bio.SeqIO`` and ``Bio.Align`` (see
Chapters :ref:`chapter:seqio`
and :ref:`chapter:align`), we have a pair of input
functions, ``read`` and ``parse``, where ``read`` is for when you have
exactly one object, and ``parse`` is an iterator for when you can have
lots of objects – but instead of getting ``SeqRecord`` or ``Alignment``
objects, we get BLAST record objects.

To be able to handle the situation where the BLAST file may be huge,
containing thousands of results, ``Blast.parse()`` returns an iterator.
In plain English, an iterator allows you to step through the BLAST
output, retrieving BLAST records one by one for each BLAST search
result:

.. code:: pycon

   >>> from Bio import Blast
   >>> blast_records = Blast.parse(result_stream)
   >>> blast_record = next(blast_records)
   # ... do something with blast_record
   >>> blast_record = next(blast_records)
   # ... do something with blast_record
   >>> blast_record = next(blast_records)
   # ... do something with blast_record
   >>> blast_record = next(blast_records)
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   StopIteration
   # No further records

Or, you can use a ``for``-loop:

.. code:: pycon

   >>> for blast_record in blast_records:
   ...     pass  # Do something with blast_record
   ...

Note though that you can step through the BLAST records only once.
Usually, from each BLAST record you would save the information that you
are interested in.

Alternatively, you can use ``blast_records`` as a list, for example by
extracting one record by index, or by calling ``len`` or ``print`` on
``blast_records``. The parser will then automatically iterate over the records
and store them:

.. doctest ../Tests/Blast

.. code:: pycon

   >>> from Bio import Blast
   >>> blast_records = Blast.parse("xml_2222_blastx_001.xml")
   >>> len(blast_records)  # this causes the parser to iterate over all records
   7
   >>> blast_records[2].query.description
   'gi|5690369|gb|AF158246.1|AF158246 Cricetulus griseus glucose phosphate isomerase (GPI) gene, partial intron sequence'

If your BLAST file is huge though, you may run into memory problems
trying to save them all in a list.

If you start iterating over the records *before* using ``blast_records`` as
a list, the parser will first reset the file stream to the beginning of the
data to ensure that all records are neing read. Note that this will fail if the
stream cannot be reset to the beginning, for example if the data are being
read remotely (e.g. by qblast; see subsection :ref:`sec:running-www-blast`).
In those cases, you can explicitly read the records into a list by calling
``blast_records = blast_records[:]`` before iterating over them. After reading
in the records, it is safe to iterate over them or use them as a list.

Instead of opening the file yourself, you can just provide the file
name:

.. doctest examples

.. code:: pycon

   >>> from Bio import Blast
   >>> with Blast.parse("my_blast.xml") as blast_records:
   ...     for blast_record in blast_records:
   ...         pass  # Do something with blast_record
   ...

In this case, Biopython opens the file for you, and closes it as soon as
the file is not needed any more (while it is possible to simply use
``blast_records = Blast.parse("my_blast.xml")``, it has the disadvantage
that the file may stay open longer than strictly necessary, thereby
wasting resources).

You can ``print`` the records to get a quick overview of their contents:

.. doctest examples

.. code:: pycon

   >>> from Bio import Blast
   >>> with Blast.parse("my_blast.xml") as blast_records:
   ...     print(blast_records)
   ...
   Program: BLASTN 2.2.27+
        db: refseq_rna
   <BLANKLINE>
     Query: 42291 (length=61)
            mystery_seq
      Hits: ----  -----  ----------------------------------------------------------
               #  # HSP  ID + description
            ----  -----  ----------------------------------------------------------
               0      1  gi|262205317|ref|NR_030195.1|  Homo sapiens microRNA 52...
               1      1  gi|301171311|ref|NR_035856.1|  Pan troglodytes microRNA...
               2      1  gi|270133242|ref|NR_032573.1|  Macaca mulatta microRNA ...
               3      2  gi|301171322|ref|NR_035857.1|  Pan troglodytes microRNA...
               4      1  gi|301171267|ref|NR_035851.1|  Pan troglodytes microRNA...
               5      2  gi|262205330|ref|NR_030198.1|  Homo sapiens microRNA 52...
               6      1  gi|262205302|ref|NR_030191.1|  Homo sapiens microRNA 51...
               7      1  gi|301171259|ref|NR_035850.1|  Pan troglodytes microRNA...
               8      1  gi|262205451|ref|NR_030222.1|  Homo sapiens microRNA 51...
               9      2  gi|301171447|ref|NR_035871.1|  Pan troglodytes microRNA...
              10      1  gi|301171276|ref|NR_035852.1|  Pan troglodytes microRNA...
              11      1  gi|262205290|ref|NR_030188.1|  Homo sapiens microRNA 51...
              12      1  gi|301171354|ref|NR_035860.1|  Pan troglodytes microRNA...
              13      1  gi|262205281|ref|NR_030186.1|  Homo sapiens microRNA 52...
              14      2  gi|262205298|ref|NR_030190.1|  Homo sapiens microRNA 52...
              15      1  gi|301171394|ref|NR_035865.1|  Pan troglodytes microRNA...
              16      1  gi|262205429|ref|NR_030218.1|  Homo sapiens microRNA 51...
              17      1  gi|262205423|ref|NR_030217.1|  Homo sapiens microRNA 52...
              18      1  gi|301171401|ref|NR_035866.1|  Pan troglodytes microRNA...
              19      1  gi|270133247|ref|NR_032574.1|  Macaca mulatta microRNA ...
              20      1  gi|262205309|ref|NR_030193.1|  Homo sapiens microRNA 52...
              21      2  gi|270132717|ref|NR_032716.1|  Macaca mulatta microRNA ...
              22      2  gi|301171437|ref|NR_035870.1|  Pan troglodytes microRNA...
              23      2  gi|270133306|ref|NR_032587.1|  Macaca mulatta microRNA ...
              24      2  gi|301171428|ref|NR_035869.1|  Pan troglodytes microRNA...
              25      1  gi|301171211|ref|NR_035845.1|  Pan troglodytes microRNA...
              26      2  gi|301171153|ref|NR_035838.1|  Pan troglodytes microRNA...
              27      2  gi|301171146|ref|NR_035837.1|  Pan troglodytes microRNA...
              28      2  gi|270133254|ref|NR_032575.1|  Macaca mulatta microRNA ...
              29      2  gi|262205445|ref|NR_030221.1|  Homo sapiens microRNA 51...
              ~~~
              97      1  gi|356517317|ref|XM_003527287.1|  PREDICTED: Glycine ma...
              98      1  gi|297814701|ref|XM_002875188.1|  Arabidopsis lyrata su...
              99      1  gi|397513516|ref|XM_003827011.1|  PREDICTED: Pan panisc...

Usually, you’ll be running one BLAST search at a time. Then, all you
need to do is to pick up the first (and only) BLAST record in
``blast_records``:

.. doctest examples

.. code:: pycon

   >>> from Bio import Blast
   >>> blast_records = Blast.parse("my_blast.xml")
   >>> blast_record = next(blast_records)

or more elegantly:

.. code:: pycon

   >>> from Bio import Blast
   >>> blast_record = Blast.read(result_stream)

or, equivalently,

.. code:: pycon

   >>> from Bio import Blast
   >>> blast_record = Blast.read("my_blast.xml")

(here, you don’t need to use a ``with`` block as ``Blast.read`` will
read the whole file and close it immediately afterwards).

I guess by now you’re wondering what is in a BLAST record.

The BLAST Records, Record, and Hit classes
------------------------------------------

.. _`subsec:blast-records`:

The BLAST Records class
~~~~~~~~~~~~~~~~~~~~~~~

A single BLAST output file can contain output from multiple BLAST queries. In
Biopython, the information in a BLAST output file is stored in an
``Bio.Blast.Records`` object. This is an iterator returning one
``Bio.Blast.Record`` object (see subsection :ref:`subsec:blast-record`) for
each query. The ``Bio.Blast.Records`` object has the following attributes describing the BLAST run:

-  ``source``: The input data from which the ``Bio.Blast.Records``
   object was constructed (this could be a file name or path, or a
   file-like object).

-  ``program``: The specific BLAST program that was used (e.g.,
   ’blastn’).

-  ``version``: The version of the BLAST program (e.g., ’BLASTN
   2.2.27+’).

-  ``reference``: The literature reference to the BLAST publication.

-  ``db``: The BLAST database against which the query was run (e.g.,
   ’nr’).

-  ``query``: A ``SeqRecord`` object which may contain some or all of
   the following information:

   -  ``query.id``: SeqId of the query;

   -  ``query.description``: Definition line of the query;

   -  ``query.seq``: The query sequence.

-  ``param``: A dictionary with the parameters used for the BLAST run.
   You may find the following keys in this dictionary:

   -  ``'matrix'``: the scoring matrix used in the BLAST run (e.g.,
      ’BLOSUM62’) (string);

   -  ``'expect'``: threshold on the expected number of chance matches
      (float);

   -  ``'include'``: e-value threshold for inclusion in multipass model
      in psiblast (float);

   -  ``'sc-match'``: score for matching nucleotides (integer);

   -  ``'sc-mismatch'``: score for mismatched nucleotides (integer;

   -  ``'gap-open'``: gap opening cost (integer);

   -  ``'gap-extend'``: gap extension cost (integer);

   -  ``'filter'``: filtering options applied in the BLAST run (string);

   -  ``'pattern'``: PHI-BLAST pattern (string);

   -  ``'entrez-query'``: Limit of request to Entrez query (string).

-  ``mbstat``: A dictionary with Mega BLAST search statistics. See the
   description of the ``Record.stat`` attribute below (in subsection
   :ref:`subsec:blast-record`) for a description of the items in
   this dictionary. Only older versions of Mega BLAST store this
   information. As it is stored near the end of the BLAST output file,
   this attribute can only be accessed after the file has been read
   completely (by iterating over the records until a ``StopIteration``
   is issued).

For our example, we find:

.. cont-doctest

.. code:: pycon

   >>> blast_records
   <Bio.Blast.Records source='my_blast.xml' program='blastn' version='BLASTN 2.2.27+' db='refseq_rna'>
   >>> blast_records.source
   'my_blast.xml'
   >>> blast_records.program
   'blastn'
   >>> blast_records.version
   'BLASTN 2.2.27+'
   >>> blast_records.reference
   'Stephen F. Altschul, Thomas L. Madden, Alejandro A. Schäffer, Jinghui Zhang, Zheng Zhang, Webb Miller, and David J. Lipman (1997), "Gapped BLAST and PSI-BLAST: a new generation of protein database search programs", Nucleic Acids Res. 25:3389-3402.'
   >>> blast_records.db
   'refseq_rna'
   >>> blast_records.param
   {'expect': 10.0, 'sc-match': 2, 'sc-mismatch': -3, 'gap-open': 5, 'gap-extend': 2, 'filter': 'L;m;'}
   >>> print(blast_records)  # doctest:+ELLIPSIS
   Program: BLASTN 2.2.27+
        db: refseq_rna
   <BLANKLINE>
     Query: 42291 (length=61)
            mystery_seq
      Hits: ----  -----  ----------------------------------------------------------
               #  # HSP  ID + description
            ----  -----  ----------------------------------------------------------
               0      1  gi|262205317|ref|NR_030195.1|  Homo sapiens microRNA 52...
               1      1  gi|301171311|ref|NR_035856.1|  Pan troglodytes microRNA...
               2      1  gi|270133242|ref|NR_032573.1|  Macaca mulatta microRNA ...
               3      2  gi|301171322|ref|NR_035857.1|  Pan troglodytes microRNA...
   ...


.. _`subsec:blast-record`:

The BLAST Record class
~~~~~~~~~~~~~~~~~~~~~~

A ``Bio.Blast.Record`` object stores the information provided by BLAST for a
single query. The ``Bio.Blast.Record`` class inherits from ``list``, and is
essentially a list of ``Bio.Blast.Hit`` objects (see section
:ref:`subsec:blast-hit`).
A ``Bio.Blast.Record`` object has the following two attributes:

-  ``query``: A ``SeqRecord`` object which may contain some or all of
   the following information:

   -  ``query.id``: SeqId of the query;

   -  ``query.description``: Definition line of the query;

   -  ``query.seq``: The query sequence.

-  ``stat``: A dictionary with statistical data of the BLAST hit. You
   may find the following keys in this dictionary:

   -  ``'db-num'``: number of sequences in BLAST db (integer);

   -  ``'db-len'``: length of BLAST db (integer);

   -  ``'hsp-len'``: effective HSP (High Scoring Pair) length (integer);

   -  ``'eff-space'``: effective search space (float);

   -  ``'kappa'``: Karlin-Altschul parameter K (float);

   -  ``'lambda'``: Karlin-Altschul parameter Lambda (float);

   -  ``'entropy'``: Karlin-Altschul parameter H (float)

-  ``message``: Some (error?) information.

Continuing with our example,

.. cont-doctest

.. code:: pycon

   >>> blast_record
   <Bio.Blast.Record query.id='42291'; 100 hits>
   >>> blast_record.query
   SeqRecord(seq=Seq(None, length=61), id='42291', name='<unknown name>', description='mystery_seq', dbxrefs=[])
   >>> blast_record.stat
   {'db-num': 3056429, 'db-len': 673143725, 'hsp-len': 0, 'eff-space': 0, 'kappa': 0.41, 'lambda': 0.625, 'entropy': 0.78}
   >>> print(blast_record)  # doctest:+ELLIPSIS
     Query: 42291 (length=61)
            mystery_seq
      Hits: ----  -----  ----------------------------------------------------------
               #  # HSP  ID + description
            ----  -----  ----------------------------------------------------------
               0      1  gi|262205317|ref|NR_030195.1|  Homo sapiens microRNA 52...
               1      1  gi|301171311|ref|NR_035856.1|  Pan troglodytes microRNA...
               2      1  gi|270133242|ref|NR_032573.1|  Macaca mulatta microRNA ...
               3      2  gi|301171322|ref|NR_035857.1|  Pan troglodytes microRNA...
   ...


As the ``Bio.Blast.Record`` class inherits from ``list``, you can use it as
such. For example, you can iterate over the record:

.. cont-doctest

.. code:: pycon

   >>> for hit in blast_record:  # doctest:+ELLIPSIS
   ...     hit
   ...
   <Bio.Blast.Hit target.id='gi|262205317|ref|NR_030195.1|' query.id='42291'; 1 HSP>
   <Bio.Blast.Hit target.id='gi|301171311|ref|NR_035856.1|' query.id='42291'; 1 HSP>
   <Bio.Blast.Hit target.id='gi|270133242|ref|NR_032573.1|' query.id='42291'; 1 HSP>
   <Bio.Blast.Hit target.id='gi|301171322|ref|NR_035857.1|' query.id='42291'; 2 HSPs>
   <Bio.Blast.Hit target.id='gi|301171267|ref|NR_035851.1|' query.id='42291'; 1 HSP>
   ...

To check how many hits the ``blast_record`` has, you can simply invoke Python’s
``len`` function:

.. cont-doctest

.. code:: pycon

   >>> len(blast_record)
   100

Like Python lists, you can retrieve hits from a ``Bio.Blast.Record`` using
indices:

.. cont-doctest

.. code:: pycon

   >>> blast_record[0]  # retrieves the top hit
   <Bio.Blast.Hit target.id='gi|262205317|ref|NR_030195.1|' query.id='42291'; 1 HSP>
   >>> blast_record[-1]  # retrieves the last hit
   <Bio.Blast.Hit target.id='gi|397513516|ref|XM_003827011.1|' query.id='42291'; 1 HSP>

To retrieve multiple hits from a ``Bio.Blast.Record``, you can use the slice
notation. This will return a new ``Bio.Blast.Record`` object containing only
the sliced hits:

.. cont-doctest

.. code:: pycon

   >>> blast_slice = blast_record[:3]  # slices the first three hits
   >>> print(blast_slice)
     Query: 42291 (length=61)
            mystery_seq
      Hits: ----  -----  ----------------------------------------------------------
               #  # HSP  ID + description
            ----  -----  ----------------------------------------------------------
               0      1  gi|262205317|ref|NR_030195.1|  Homo sapiens microRNA 52...
               1      1  gi|301171311|ref|NR_035856.1|  Pan troglodytes microRNA...
               2      1  gi|270133242|ref|NR_032573.1|  Macaca mulatta microRNA ...

To create a copy of the ``Bio.Blast.Record``, take the full slice:

.. cont-doctest

.. code:: pycon

   >>> blast_record_copy = blast_record[:]
   >>> type(blast_record_copy)
   <class 'Bio.Blast.Record'>
   >>> blast_record_copy  # list of all hits
   <Bio.Blast.Record query.id='42291'; 100 hits>

This is particularly useful if you want to sort or filter the BLAST record
(see :ref:`subsec:blast-sorting-filtering`), but want to retain a copy of the original BLAST output.

You can also access ``blast_record`` as a Python dictionary and retrieve hits
using the hit’s ID as key:

.. cont-doctest

.. code:: pycon

   >>> blast_record["gi|262205317|ref|NR_030195.1|"]
   <Bio.Blast.Hit target.id='gi|262205317|ref|NR_030195.1|' query.id='42291'; 1 HSP>

If the ID is not found in the ``blast_record``, a ``KeyError`` is raised:

.. cont-doctest

.. code:: pycon

   >>> blast_record["unicorn_gene"]
   Traceback (most recent call last):
   ...
   KeyError: 'unicorn_gene'


You can get the full list of keys by using ``.keys()`` as usual:

.. cont-doctest

.. code:: pycon

   >>> blast_record.keys()  # doctest:+ELLIPSIS
   ['gi|262205317|ref|NR_030195.1|', 'gi|301171311|ref|NR_035856.1|', 'gi|270133242|ref|NR_032573.1|', ...]

What if you just want to check whether a particular hit is present in the query
results? You can do a simple Python membership test using the ``in`` keyword:

.. cont-doctest

.. code:: pycon

   >>> "gi|262205317|ref|NR_030195.1|" in blast_record
   True
   >>> "gi|262205317|ref|NR_030194.1|" in blast_record
   False

Sometimes, knowing whether a hit is present is not enough; you also want to
know the rank of the hit. Here, the ``index`` method comes to the rescue:

.. cont-doctest

.. code:: pycon

   >>> blast_record.index("gi|301171437|ref|NR_035870.1|")
   22

Remember that Python uses zero-based indexing, so the first hit will be at
index 0.


.. _`subsec:blast-hit`:

The BLAST Hit class
~~~~~~~~~~~~~~~~~~~

Each ``Bio.Blast.Hit`` object in the ``blast_record`` list represents one BLAST
hit of the query against a target.

.. cont-doctest

.. code:: pycon

   >>> hit = blast_record[0]
   >>> hit
   <Bio.Blast.Hit target.id='gi|262205317|ref|NR_030195.1|' query.id='42291'; 1 HSP>
   >>> hit.target
   SeqRecord(seq=Seq(None, length=61), id='gi|262205317|ref|NR_030195.1|', name='NR_030195', description='Homo sapiens microRNA 520b (MIR520B), microRNA', dbxrefs=[])

We can get a summary of the ``hit`` by printing it:

.. cont-doctest

.. code:: pycon

   >>> print(blast_record[3])
   Query: 42291
          mystery_seq
     Hit: gi|301171322|ref|NR_035857.1| (length=86)
          Pan troglodytes microRNA mir-520c (MIR520C), microRNA
    HSPs: ----  --------  ---------  ------  ---------------  ---------------------
             #   E-value  Bit score    Span      Query range              Hit range
          ----  --------  ---------  ------  ---------------  ---------------------
             0   8.9e-20     100.47      60           [1:61]                [13:73]
             1   3.3e-06      55.39      60           [0:60]                [73:13]

You see that we’ve got the essentials covered here:

-  A hit is always for one query; the query ID and description are shown at the
   top of the summary.

-  A hit consists of one or more alignments of the query against one target
   sequence. The target information is shown next is the summary. As shown
   above, the target can be accessed via the ``target`` attribute of the hit.

-  Finally, there’s a table containing quick information about the alignments
   each hit contains. In BLAST parlance, these alignments are called
   "High-scoring Segment Pairs", or HSPs (see section :ref:`subsec:blast-hsp`).
   Each row in the table summarizes one HSP, including the HSP index, e-value,
   bit score, span (the alignment length including gaps), query coordinates,
   and target coordinates.

The ``Bio.Blast.Hit`` class is a subclass of ``Bio.Align.Alignments`` (plural;
see Section :ref:`sec:alignments`), and therefore in essence is a list of
``Bio.Align.Alignment`` (singular; see Section :ref:`sec:alignmentobject`)
objects. In particular when aligning nucleotide sequences against the genome,
the ``Bio.Blast.Hit`` object may consist of more than one
``Bio.Align.Alignment`` if a particular query aligns to more than one region of
a chromosome. For protein alignments, usually a hit consists of only one
alignment, especially for alignments of highly homologous sequences.

.. cont-doctest

.. code:: pycon

   >>> type(hit)
   <class 'Bio.Blast.Hit'>
   >>> from Bio.Align import Alignments
   >>> isinstance(hit, Alignments)
   True
   >>> len(hit)
   1

For BLAST output in the XML2 format, a hit may have several targets with
identical sequences but different sequence IDs and descriptions. These targets
are accessible as the ``hit.targets`` attribute. In most cases, ``hit.targets``
has length 1 and only contains ``hit.target``:

.. doctest ../Tests/Blast

.. code:: pycon

   >>> from Bio import Blast
   >>> blast_record = Blast.read("xml_2900_blastx_001_v2.xml")
   >>> for hit in blast_record:
   ...     print(len(hit.targets))
   ...
   1
   1
   2
   1
   1
   1
   1
   1
   1
   1

However, as you can see in the output above, the third hit has multiple
targets.

.. cont-doctest

.. code:: pycon

   >>> hit = blast_record[2]
   >>> hit.targets[0].seq
   Seq(None, length=246)
   >>> hit.targets[1].seq
   Seq(None, length=246)
   >>> hit.targets[0].id
   'gi|684409690|ref|XP_009175831.1|'
   >>> hit.targets[1].id
   'gi|663044098|gb|KER20427.1|'
   >>> hit.targets[0].name
   'XP_009175831'
   >>> hit.targets[1].name
   'KER20427'
   >>> hit.targets[0].description
   'hypothetical protein T265_11027 [Opisthorchis viverrini]'
   >>> hit.targets[1].description
   'hypothetical protein T265_11027 [Opisthorchis viverrini]'

As the sequence contents for the two targets are identical to each other, their
sequence alignments are also identical. The alignments for this hit therefore
only refers to ``hit.targets[0]`` (which is identical to ``hit.target``), as
the alignment for ``hit.targets[1]`` would be the same anyway.


.. _`subsec:blast-hsp`:

The BLAST HSP class
~~~~~~~~~~~~~~~~~~~

Let's return to our main example, and look at the first (and only) alignment in
the first hit. This alignment is an instance of the ``Bio.Blast.HSP`` class,
which is a subclass of the ``Alignment`` class in ``Bio.Align``:

.. doctest examples

.. code:: pycon

   >>> from Bio import Blast
   >>> blast_record = Blast.read("my_blast.xml")
   >>> hit = blast_record[0]
   >>> len(hit)
   1
   >>> alignment = hit[0]
   >>> alignment
   <Bio.Blast.HSP target.id='gi|262205317|ref|NR_030195.1|' query.id='42291'; 2 rows x 61 columns>
   >>> type(alignment)
   <class 'Bio.Blast.HSP'>
   >>> from Bio.Align import Alignment
   >>> isinstance(alignment, Alignment)
   True

The ``alignment`` object has attributes pointing to the target and query
sequences, as well as a ``coordinates`` attribute describing the sequence
alignment.

.. cont-doctest

.. code:: pycon

   >>> alignment.target
   SeqRecord(seq=Seq('CCCTCTACAGGGAAGCGCTTTCTGTTGTCTGAAAGAAAAGAAAGTGCTTCCTTT...GGG'), id='gi|262205317|ref|NR_030195.1|', name='NR_030195', description='Homo sapiens microRNA 520b (MIR520B), microRNA', dbxrefs=[])
   >>> alignment.query
   SeqRecord(seq=Seq('CCCTCTACAGGGAAGCGCTTTCTGTTGTCTGAAAGAAAAGAAAGTGCTTCCTTT...GGG'), id='42291', name='<unknown name>', description='mystery_seq', dbxrefs=[])
   >>> alignment.target
   SeqRecord(seq=Seq('CCCTCTACAGGGAAGCGCTTTCTGTTGTCTGAAAGAAAAGAAAGTGCTTCCTTT...GGG'), id='gi|262205317|ref|NR_030195.1|', name='NR_030195', description='Homo sapiens microRNA 520b (MIR520B), microRNA', dbxrefs=[])
   >>> alignment.query
   SeqRecord(seq=Seq('CCCTCTACAGGGAAGCGCTTTCTGTTGTCTGAAAGAAAAGAAAGTGCTTCCTTT...GGG'), id='42291', name='<unknown name>', description='mystery_seq', dbxrefs=[])
   >>> print(alignment.coordinates)
   [[ 0 61]
    [ 0 61]]

For translated BLAST searches, the ``features`` attribute of the target or
query may contain a ``SeqFeature`` of type CDS that stores the amino acid
sequence region. The ``qualifiers`` attribute of such a feature is a
dictionary with a single key ``'coded_by'``; the corresponding value specifies
the nucleotide sequence region, in a GenBank-style string with 1-based
coordinates, that encodes the amino acid sequence.

Each ``Alignment`` object has the following additional attributes:

-  ``score``: score of the High Scoring Pair (HSP);

-  ``annotations``: a dictionary that may contain the following keys:

   -  ``'bit score'``: score (in bits) of HSP (float);

   -  ``'evalue'``: e-value of HSP (float);

   -  ``'identity``’: number of identities in HSP (integer);

   -  ``'positive'``: number of positives in HSP (integer);

   -  ``'gaps'``: number of gaps in HSP (integer);

   -  ``'midline'``: formatting middle line.

The usual ``Alignment`` methods (see Section :ref:`sec:alignmentobject`) can be
applied to the ``alignment``. For example, we can print the alignment:

.. cont-doctest

.. code:: pycon

   >>> print(alignment)
   Query : 42291 Length: 61 Strand: Plus
           mystery_seq
   Target: gi|262205317|ref|NR_030195.1| Length: 61 Strand: Plus
           Homo sapiens microRNA 520b (MIR520B), microRNA
   <BLANKLINE> 
   Score:111 bits(122), Expect:5e-23,
   Identities:61/61(100%),  Positives:61/61(100%),  Gaps:0.61(0%)
   <BLANKLINE> 
   gi|262205         0 CCCTCTACAGGGAAGCGCTTTCTGTTGTCTGAAAGAAAAGAAAGTGCTTCCTTTTAGAGG
                     0 ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
   42291             0 CCCTCTACAGGGAAGCGCTTTCTGTTGTCTGAAAGAAAAGAAAGTGCTTCCTTTTAGAGG
   <BLANKLINE> 
   gi|262205        60 G 61
                    60 | 61
   42291            60 G 61
   <BLANKLINE>
   <BLANKLINE>

Let’s just print out some summary info about all hits in our BLAST record
greater than a particular threshold:

.. cont-doctest

.. code:: pycon

   >>> E_VALUE_THRESH = 0.04
   >>> for alignments in blast_record:  # doctest:+ELLIPSIS
   ...     for alignment in alignments:
   ...         if alignment.annotations["evalue"] < E_VALUE_THRESH:
   ...             print("****Alignment****")
   ...             print("sequence:", alignment.target.id, alignment.target.description)
   ...             print("length:", len(alignment.target))
   ...             print("score:", alignment.score)
   ...             print("e value:", alignment.annotations["evalue"])
   ...             print(alignment[:, :50])
   ...
   ****Alignment****
   sequence: gi|262205317|ref|NR_030195.1| Homo sapiens microRNA 520b (MIR520B), microRNA
   length: 61
   score: 122.0
   e value: 4.91307e-23
   gi|262205         0 CCCTCTACAGGGAAGCGCTTTCTGTTGTCTGAAAGAAAAGAAAGTGCTTC 50
                     0 |||||||||||||||||||||||||||||||||||||||||||||||||| 50
   42291             0 CCCTCTACAGGGAAGCGCTTTCTGTTGTCTGAAAGAAAAGAAAGTGCTTC 50
   <BLANKLINE>
   ****Alignment****
   sequence: gi|301171311|ref|NR_035856.1| Pan troglodytes microRNA mir-520b (MIR520B), microRNA
   length: 60
   score: 120.0
   e value: 1.71483e-22
   gi|301171         0 CCTCTACAGGGAAGCGCTTTCTGTTGTCTGAAAGAAAAGAAAGTGCTTCC 50
                     0 |||||||||||||||||||||||||||||||||||||||||||||||||| 50
   42291             1 CCTCTACAGGGAAGCGCTTTCTGTTGTCTGAAAGAAAAGAAAGTGCTTCC 51
   <BLANKLINE>
   ****Alignment****
   sequence: gi|270133242|ref|NR_032573.1| Macaca mulatta microRNA mir-519a (MIR519A), microRNA
   length: 85
   score: 112.0
   e value: 2.54503e-20
   gi|270133        12 CCCTCTAGAGGGAAGCGCTTTCTGTGGTCTGAAAGAAAAGAAAGTGCTTC 62
                     0 |||||||.|||||||||||||||||.|||||||||||||||||||||||| 50
   42291             0 CCCTCTACAGGGAAGCGCTTTCTGTTGTCTGAAAGAAAAGAAAGTGCTTC 50
   ...

.. _`subsec:blast-sorting-filtering`:

Sorting and filtering BLAST output
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If the ordering of hits in the BLAST output file doesn’t suit your taste, you
can use the ``sort`` method to resort the hits in the ``Bio.Blast.Record``
object. As an example, here we sort the hits based on the sequence length of
each target, setting the ``reverse`` flag to ``True`` so that we sort in
descending order.

.. cont-doctest

.. code:: pycon

   >>> for hit in blast_record[:5]:
   ...     print(f"{hit.target.id} {len(hit.target)}")
   ...
   gi|262205317|ref|NR_030195.1| 61
   gi|301171311|ref|NR_035856.1| 60
   gi|270133242|ref|NR_032573.1| 85
   gi|301171322|ref|NR_035857.1| 86
   gi|301171267|ref|NR_035851.1| 80

   >>> sort_key = lambda hit: len(hit.target)
   >>> blast_record.sort(key=sort_key, reverse=True)
   >>> for hit in blast_recordt[:5]:
   ...     print(f"{hit.target.id} {len(hit.target)}")
   ...
   gi|397513516|ref|XM_003827011.1| 6002
   gi|390332045|ref|XM_776818.2| 4082
   gi|390332043|ref|XM_003723358.1| 4079
   gi|356517317|ref|XM_003527287.1| 3251
   gi|356543101|ref|XM_003539954.1| 2936

This will sort ``blast_record`` in place.
Use ``original_blast_record = blast_record[:]`` before sorting if you want to
retain a copy of the original, unsorted BLAST output.

To filter BLAST hits based on their properties, you can use Python's built-in
``filter`` with the approciate callback function to evaluate each hit. The
callback function must accept as its argument a single ``Hit`` object and
return ``True`` or ``False``.  Here is an example in which we filter out
``Hit`` objects that only have one HSP:

.. cont-doctest

.. code:: pycon

   >>> filter_func = lambda hit: len(hit) > 1  # the callback function
   >>> len(blast_record)  # no. of hits before filtering
   100
   >>> blast_record[:] = filter(filter_func, blast_record)
   >>> len(blast_record)  # no. of hits after filtering
   37
   >>> for hit in blast_record[:5]:  # quick check for the hit lengths
   ...     print(f"{hit.target.id} {len(hit)}")
   ...
   gi|301171322|ref|NR_035857.1| 2
   gi|262205330|ref|NR_030198.1| 2
   gi|301171447|ref|NR_035871.1| 2
   gi|262205298|ref|NR_030190.1| 2
   gi|270132717|ref|NR_032716.1| 2

Similarly, you can filter HSPs in each hit, for example on their e-value:

.. cont-doctest

.. code:: pycon

   >>> filter_func = lambda hsp: hsp.annotations["evalue"] < 1.0e-12
   >>> for hit in blast_record:
   ...     hit[:] = filter(filter_func, hit)
   ...

Probably you'd want to follow this up by removing all hits with no HSPs
remaining:

.. cont-doctest

.. code:: pycon

   >>> filter_func = lambda hit: len(hit) > 0
   >>> blast_record[:] = filter(filter_func, blast_record)
   >>> len(blast_record)
   16

Use Python's built-in ``map`` function to modify hits or HSPs in the BLAST
record. The ``map`` function accepts a callback function returning the modified
hit object. For example, we can use ``map`` to rename the hit IDs:

.. cont-doctest

.. code:: pycon

   >>> for hit in blast_record[:5]:
   ...     print(hit.target.id)
   ...
   gi|301171322|ref|NR_035857.1|
   gi|262205330|ref|NR_030198.1|
   gi|301171447|ref|NR_035871.1|
   gi|262205298|ref|NR_030190.1|
   gi|270132717|ref|NR_032716.1|
   >>> import copy
   >>> original_blast_record = copy.deepcopy(blast_record)
   >>> def map_func(hit):
   ...     # renames "gi|301171322|ref|NR_035857.1|" to "NR_035857.1"
   ...     hit.target.id = hit.target.id.split("|")[3]
   ...     return hit
   ...
   >>> blast_record[:] = map(map_func, blast_record)
   >>> for hit in blast_record[:5]:
   ...     print(hit.target.id)
   ...
   NR_035857.1
   NR_030198.1
   NR_035871.1
   NR_030190.1
   NR_032716.1
   >>> for hit in original_blast_record[:5]:
   ...     print(hit.target.id)
   ...
   gi|301171322|ref|NR_035857.1|
   gi|262205330|ref|NR_030198.1|
   gi|301171447|ref|NR_035871.1|
   gi|262205298|ref|NR_030190.1|
   gi|270132717|ref|NR_032716.1|

Note that in this example, ``map_func`` modifies the hit in-place.
In contrast to sorting and filtering (see above), using
``original_blast_record = blast_record[:]`` is not sufficient to retain a copy
of the unmodified BLAST record, as it creates a shallow copy of the BLAST
record, consisting of pointers to the same ``Hit`` objects. Instead, we use
``copy.deepcopy`` to create a copy of the BLAST record in which each ``Hit``
object is duplicated.

Writing BLAST records
---------------------

Use the ``write`` function in ``Bio.Blast`` to save BLAST records as an XML
file. By default, the (DTD-based) XML format is used; you can also save the
BLAST records in the (schema-based) XML2 format by using the ``fmt="XML2"``
argument to the ``write`` function.

.. code:: pycon

   >>> from Bio import Blast
   >>> stream = Blast.qblast("blastn", "nt", "8332116")
   >>> records = Blast.parse(stream)
   >>> Blast.write(records, "my_qblast_output.xml")

   or

.. code:: pycon

   >>> Blast.write(records, "my_qblast_output.xml", fmt="XML2")


In this example, we could have saved the data returned by ``Blast.qblast``
directly to an XML file (see section :ref:`subsec:saving-blast-results`).
However, by parsing the data returned by qblast into records, we can sort or
filter the BLAST records before saving them. For example, we may be interested
only in BLAST HSPs with a positive score of at least 400:

.. code:: pycon

   >>> filter_func = lambda hsp: hsp.annotations["positive"] >= 400
   >>> for hit in records[0]:
   ...     hit[:] = filter(filter_func, hit)
   ...
   >>> Blast.write(records, "my_qblast_output_selected.xml")

Instead of a file name, the second argument to ``Blast.write`` can also be a
file stream. In that case, the stream must be opened in binary format for
writing:

.. code:: pycon

   >>> with open("my_qblast_output.xml", "wb") as stream:
   ...     Blast.write(records, stream)
   ...


Dealing with PSI-BLAST
----------------------

You can run the standalone version of PSI-BLAST (``psiblast``) directly
from the command line or using python’s ``subprocess`` module.

At the time of writing, the NCBI do not appear to support tools running
a PSI-BLAST search via the internet.

Note that the ``Bio.Blast`` parser can read the XML output from current
versions of PSI-BLAST, but information like which sequences in each
iteration is new or reused isn’t present in the XML file.

Dealing with RPS-BLAST
----------------------

You can run the standalone version of RPS-BLAST (``rpsblast``) directly
from the command line or using python’s ``subprocess`` module.

At the time of writing, the NCBI do not appear to support tools running
an RPS-BLAST search via the internet.

You can use the ``Bio.Blast`` parser to read the XML output from current
versions of RPS-BLAST.

.. _`chapter:blast_old`:

BLAST (old)
===========

Hey, everybody loves BLAST right? I mean, geez, how can it get any
easier to do comparisons between one of your sequences and every other
sequence in the known world? But, of course, this section isn’t about
how cool BLAST is, since we already know that. It is about the problem
with BLAST – it can be really difficult to deal with the volume of data
generated by large runs, and to automate BLAST runs in general.

Fortunately, the Biopython folks know this only too well, so they’ve
developed lots of tools for dealing with BLAST and making things much
easier. This section details how to use these tools and do useful things
with them.

Dealing with BLAST can be split up into two steps, both of which can be
done from within Biopython. Firstly, running BLAST for your query
sequence(s), and getting some output. Secondly, parsing the BLAST output
in Python for further analysis.

Your first introduction to running BLAST was probably via the NCBI
web-service. In fact, there are lots of ways you can run BLAST, which
can be categorized in several ways. The most important distinction is
running BLAST locally (on your own machine), and running BLAST remotely
(on another machine, typically the NCBI servers). We’re going to start
this chapter by invoking the NCBI online BLAST service from within a
Python script.

*NOTE*: The following Chapter :ref:`chapter:searchio`
describes ``Bio.SearchIO``. We intend this to ultimately replace the
older ``Bio.Blast`` module, as it provides a more general framework
handling other related sequence searching tools as well. However, for
now you can use either that or the older ``Bio.Blast`` module for
dealing with NCBI BLAST.

Running BLAST over the Internet
-------------------------------

We use the function ``qblast()`` in the ``Bio.Blast.NCBIWWW`` module to
call the online version of BLAST. This has three non-optional arguments:

-  The first argument is the blast program to use for the search, as a
   lower case string. The options and descriptions of the programs are
   available at https://blast.ncbi.nlm.nih.gov/Blast.cgi. Currently
   ``qblast`` only works with blastn, blastp, blastx, tblast and
   tblastx.

-  The second argument specifies the databases to search against. Again,
   the options for this are available on the NCBI Guide to BLAST
   https://blast.ncbi.nlm.nih.gov/doc/blast-help/.

-  The third argument is a string containing your query sequence. This
   can either be the sequence itself, the sequence in fasta format, or
   an identifier like a GI number.

The NCBI guidelines, from
https://blast.ncbi.nlm.nih.gov/doc/blast-help/developerinfo.html#developerinfo
state:

#. Do not contact the server more often than once every 10 seconds.

#. Do not poll for any single RID more often than once a minute.

#. Use the URL parameter email and tool, so that the NCBI can contact
   you if there is a problem.

#. Run scripts weekends or between 9 pm and 5 am Eastern time on
   weekdays if more than 50 searches will be submitted.

To fulfill the third point, one can set the ``NCBIWWW.email`` variable.

.. doctest

.. code:: pycon

   >>> from Bio.Blast import NCBIWWW
   >>> NCBIWWW.email = "A.N.Other@example.com"

The ``qblast`` function also takes a number of other option arguments,
which are basically analogous to the different parameters you can set on
the BLAST web page. We’ll just highlight a few of them here:

-  The argument ``url_base`` sets the base URL for running BLAST over
   the internet. By default it connects to the NCBI.

-  The ``qblast`` function can return the BLAST results in various
   formats, which you can choose with the optional ``format_type``
   keyword: ``"HTML"``, ``"Text"``, ``"ASN.1"``, or ``"XML"``. The
   default is ``"XML"``, as that is the format expected by the parser,
   described in section :ref:`sec:parsing-blast` below.

-  The argument ``expect`` sets the expectation or e-value threshold.

For more about the optional BLAST arguments, we refer you to the NCBI’s
own documentation, or that built into Biopython:

.. code:: pycon

   >>> from Bio.Blast import NCBIWWW
   >>> help(NCBIWWW.qblast)

Note that the default settings on the NCBI BLAST website are not quite
the same as the defaults on QBLAST. If you get different results, you’ll
need to check the parameters (e.g., the expectation value threshold and
the gap values).

For example, if you have a nucleotide sequence you want to search
against the nucleotide database (nt) using BLASTN, and you know the GI
number of your query sequence, you can use:

.. code:: pycon

   >>> from Bio.Blast import NCBIWWW
   >>> result_handle = NCBIWWW.qblast("blastn", "nt", "8332116")

Alternatively, if we have our query sequence already in a FASTA
formatted file, we just need to open the file and read in this record as
a string, and use that as the query argument:

.. code:: pycon

   >>> from Bio.Blast import NCBIWWW
   >>> fasta_string = open("m_cold.fasta").read()
   >>> result_handle = NCBIWWW.qblast("blastn", "nt", fasta_string)

We could also have read in the FASTA file as a ``SeqRecord`` and then
supplied just the sequence itself:

.. code:: pycon

   >>> from Bio.Blast import NCBIWWW
   >>> from Bio import SeqIO
   >>> record = SeqIO.read("m_cold.fasta", format="fasta")
   >>> result_handle = NCBIWWW.qblast("blastn", "nt", record.seq)

Supplying just the sequence means that BLAST will assign an identifier
for your sequence automatically. You might prefer to use the
``SeqRecord`` object’s format method to make a FASTA string (which will
include the existing identifier):

.. code:: pycon

   >>> from Bio.Blast import NCBIWWW
   >>> from Bio import SeqIO
   >>> record = SeqIO.read("m_cold.fasta", format="fasta")
   >>> result_handle = NCBIWWW.qblast("blastn", "nt", record.format("fasta"))

This approach makes more sense if you have your sequence(s) in a
non-FASTA file format which you can extract using ``Bio.SeqIO`` (see
Chapter :ref:`chapter:seqio`).

Whatever arguments you give the ``qblast()`` function, you should get
back your results in a handle object (by default in XML format). The
next step would be to parse the XML output into Python objects
representing the search results (Section :ref:`sec:parsing-blast`),
but you might want to save a local copy of the output file first. I find
this especially useful when debugging my code that extracts info from
the BLAST results (because re-running the online search is slow and
wastes the NCBI computer time).

We need to be a bit careful since we can use ``result_handle.read()`` to
read the BLAST output only once – calling ``result_handle.read()`` again
returns an empty string.

.. code:: pycon

   >>> with open("my_blast.xml", "w") as out_handle:
   ...     out_handle.write(result_handle.read())
   ...
   >>> result_handle.close()

After doing this, the results are in the file ``my_blast.xml`` and the
original handle has had all its data extracted (so we closed it).
However, the ``parse`` function of the BLAST parser (described
in :ref:`sec:parsing-blast`) takes a file-handle-like object, so we
can just open the saved file for input:

.. code:: pycon

   >>> result_handle = open("my_blast.xml")

Now that we’ve got the BLAST results back into a handle again, we are
ready to do something with them, so this leads us right into the parsing
section (see Section :ref:`sec:parsing-blast` below). You may want
to jump ahead to that now ….

Running BLAST locally
---------------------

.. _introduction-1:

Introduction
~~~~~~~~~~~~

Running BLAST locally (as opposed to over the internet, see
Section :ref:`sec:running-www-blast`) has at least major two
advantages:

-  Local BLAST may be faster than BLAST over the internet;

-  Local BLAST allows you to make your own database to search for
   sequences against.

Dealing with proprietary or unpublished sequence data can be another
reason to run BLAST locally. You may not be allowed to redistribute the
sequences, so submitting them to the NCBI as a BLAST query would not be
an option.

Unfortunately, there are some major drawbacks too – installing all the
bits and getting it setup right takes some effort:

-  Local BLAST requires command line tools to be installed.

-  Local BLAST requires (large) BLAST databases to be setup (and
   potentially kept up to date).

To further confuse matters there are several different BLAST packages
available, and there are also other tools which can produce imitation
BLAST output files, such as BLAT.

.. _standalone-ncbi-blast-1:

Standalone NCBI BLAST+
~~~~~~~~~~~~~~~~~~~~~~

The “new” `NCBI
BLAST+ <https://blast.ncbi.nlm.nih.gov/Blast.cgi?CMD=Web&PAGE_TYPE=BlastDocs&DOC_TYPE=Download>`__
suite was released in 2009. This replaces the old NCBI “legacy” BLAST
package (see below).

This section will show briefly how to use these tools from within
Python. If you have already read or tried the alignment tool examples in
Section :ref:`sec:alignment-tools` this should all
seem quite straightforward. First, we construct a command line string
(as you would type in at the command line prompt if running standalone
BLAST by hand). Then we can execute this command from within Python.

For example, taking a FASTA file of gene nucleotide sequences, you might
want to run a BLASTX (translation) search against the non-redundant (NR)
protein database. Assuming you (or your systems administrator) has
downloaded and installed the NR database, you might run:

.. code:: console

   $ blastx -query opuntia.fasta -db nr -out opuntia.xml -evalue 0.001 -outfmt 5

This should run BLASTX against the NR database, using an expectation
cut-off value of :math:`0.001` and produce XML output to the specified
file (which we can then parse). On my computer this takes about six
minutes - a good reason to save the output to a file so you can repeat
any analysis as needed.

From within python we can use the ``subprocess`` module to build the
command line string, and run it:

.. code:: pycon

   >>> import subprocess
   >>> cmd = "blastx -query opuntia.fasta -db nr -out opuntia.xml"
   >>> cmd += " -evalue 0.001 -outfmt 5"
   >>> subprocess.run(cmd, shell=True)

In this example there shouldn’t be any output from BLASTX to the
terminal. You may want to check the output file ``opuntia.xml`` has been
created.

As you may recall from earlier examples in the tutorial, the
``opuntia.fasta`` contains seven sequences, so the BLAST XML output
should contain multiple results. Therefore use
``Bio.Blast.NCBIXML.parse()`` to parse it as described below in
Section :ref:`sec:parsing-blast`.

Other versions of BLAST
~~~~~~~~~~~~~~~~~~~~~~~

NCBI BLAST+ (written in C++) was first released in 2009 as a replacement
for the original NCBI “legacy” BLAST (written in C) which is no longer
being updated. There were a lot of changes – the old version had a
single core command line tool ``blastall`` which covered multiple
different BLAST search types (which are now separate commands in
BLAST+), and all the command line options were renamed. Biopython’s
wrappers for the NCBI “legacy” BLAST tools have been deprecated and will
be removed in a future release. To try to avoid confusion, we do not
cover calling these old tools from Biopython in this tutorial.

You may also come across `Washington University
BLAST <http://blast.wustl.edu/>`__ (WU-BLAST), and its successor,
`Advanced Biocomputing BLAST <https://blast.advbiocomp.com>`__
(AB-BLAST, released in 2009, not free/open source). These packages
include the command line tools ``wu-blastall`` and ``ab-blastall``,
which mimicked ``blastall`` from the NCBI “legacy” BLAST suite.
Biopython does not currently provide wrappers for calling these tools,
but should be able to parse any NCBI compatible output from them.

Parsing BLAST output
--------------------

As mentioned above, BLAST can generate output in various formats, such
as XML, HTML, and plain text. Originally, Biopython had parsers for
BLAST plain text and HTML output, as these were the only output formats
offered at the time. Unfortunately, the BLAST output in these formats
kept changing, each time breaking the Biopython parsers. Our HTML BLAST
parser has been removed, while the deprecated plain text BLAST parser is
now only available via ``Bio.SearchIO``. Use it at your own risk, it may
or may not work, depending on which BLAST version you’re using.

As keeping up with changes in BLAST became a hopeless endeavor,
especially with users running different BLAST versions, we now recommend
to parse the output in XML format, which can be generated by recent
versions of BLAST. Not only is the XML output more stable than the plain
text and HTML output, it is also much easier to parse automatically,
making Biopython a whole lot more stable.

You can get BLAST output in XML format in various ways. For the parser,
it doesn’t matter how the output was generated, as long as it is in the
XML format.

-  You can use Biopython to run BLAST over the internet, as described in
   section :ref:`sec:running-www-blast`.

-  You can use Biopython to run BLAST locally, as described in
   section :ref:`sec:running-local-blast`.

-  You can do the BLAST search yourself on the NCBI site through your
   web browser, and then save the results. You need to choose XML as the
   format in which to receive the results, and save the final BLAST page
   you get (you know, the one with all of the interesting results!) to a
   file.

-  You can also run BLAST locally without using Biopython, and save the
   output in a file. Again, you need to choose XML as the format in
   which to receive the results.

The important point is that you do not have to use Biopython scripts to
fetch the data in order to be able to parse it. Doing things in one of
these ways, you then need to get a handle to the results. In Python, a
handle is just a nice general way of describing input to any info source
so that the info can be retrieved using ``read()`` and ``readline()``
functions (see
Section :ref:`sec:appendix-handles`).

If you followed the code above for interacting with BLAST through a
script, then you already have ``result_handle``, the handle to the BLAST
results. For example, using a GI number to do an online search:

.. code:: pycon

   >>> from Bio.Blast import NCBIWWW
   >>> result_handle = NCBIWWW.qblast("blastn", "nt", "8332116")

If instead you ran BLAST some other way, and have the BLAST output (in
XML format) in the file ``my_blast.xml``, all you need to do is to open
the file for reading:

.. code:: pycon

   >>> result_handle = open("my_blast.xml")

Now that we’ve got a handle, we are ready to parse the output. The code
to parse it is really quite small. If you expect a single BLAST result
(i.e., you used a single query):

.. code:: pycon

   >>> from Bio.Blast import NCBIXML
   >>> blast_record = NCBIXML.read(result_handle)

or, if you have lots of results (i.e., multiple query sequences):

.. code:: pycon

   >>> from Bio.Blast import NCBIXML
   >>> blast_records = NCBIXML.parse(result_handle)

Just like ``Bio.SeqIO`` and ``Bio.Align`` (see
Chapters :ref:`chapter:seqio`
and :ref:`chapter:align`), we have a pair of input
functions, ``read`` and ``parse``, where ``read`` is for when you have
exactly one object, and ``parse`` is an iterator for when you can have
lots of objects – but instead of getting ``SeqRecord`` or
``MultipleSeqAlignment`` objects, we get BLAST record objects.

To be able to handle the situation where the BLAST file may be huge,
containing thousands of results, ``NCBIXML.parse()`` returns an
iterator. In plain English, an iterator allows you to step through the
BLAST output, retrieving BLAST records one by one for each BLAST search
result:

.. code:: pycon

   >>> from Bio.Blast import NCBIXML
   >>> blast_records = NCBIXML.parse(result_handle)
   >>> blast_record = next(blast_records)
   # ... do something with blast_record
   >>> blast_record = next(blast_records)
   # ... do something with blast_record
   >>> blast_record = next(blast_records)
   # ... do something with blast_record
   >>> blast_record = next(blast_records)
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   StopIteration
   # No further records

Or, you can use a ``for``-loop:

.. code:: pycon

   >>> for blast_record in blast_records:
   ...     pass  # Do something with blast_record
   ...

Note though that you can step through the BLAST records only once.
Usually, from each BLAST record you would save the information that you
are interested in. If you want to save all returned BLAST records, you
can convert the iterator into a list:

.. code:: pycon

   >>> blast_records = list(blast_records)

Now you can access each BLAST record in the list with an index as usual.
If your BLAST file is huge though, you may run into memory problems
trying to save them all in a list.

Usually, you’ll be running one BLAST search at a time. Then, all you
need to do is to pick up the first (and only) BLAST record in
``blast_records``:

.. code:: pycon

   >>> from Bio.Blast import NCBIXML
   >>> blast_records = NCBIXML.parse(result_handle)
   >>> blast_record = next(blast_records)

or more elegantly:

.. code:: pycon

   >>> from Bio.Blast import NCBIXML
   >>> blast_record = NCBIXML.read(result_handle)

I guess by now you’re wondering what is in a BLAST record.

The BLAST record class
----------------------

A BLAST Record contains everything you might ever want to extract from
the BLAST output. Right now we’ll just show an example of how to get
some info out of the BLAST report, but if you want something in
particular that is not described here, look at the info on the record
class in detail, and take a gander into the code or automatically
generated documentation – the docstrings have lots of good info about
what is stored in each piece of information.

To continue with our example, let’s just print out some summary info
about all hits in our blast report greater than a particular threshold.
The following code does this:

.. code:: pycon

   >>> E_VALUE_THRESH = 0.04

   >>> for alignment in blast_record.alignments:
   ...     for hsp in alignment.hsps:
   ...         if hsp.expect < E_VALUE_THRESH:
   ...             print("****Alignment****")
   ...             print("sequence:", alignment.title)
   ...             print("length:", alignment.length)
   ...             print("e value:", hsp.expect)
   ...             print(hsp.query[0:75] + "...")
   ...             print(hsp.match[0:75] + "...")
   ...             print(hsp.sbjct[0:75] + "...")
   ...

This will print out summary reports like the following:

.. code:: text

   ****Alignment****
   sequence: >gb|AF283004.1|AF283004 Arabidopsis thaliana cold acclimation protein WCOR413-like protein
   alpha form mRNA, complete cds
   length: 783
   e value: 0.034
   tacttgttgatattggatcgaacaaactggagaaccaacatgctcacgtcacttttagtcccttacatattcctc...
   ||||||||| | ||||||||||| || ||||  || || |||||||| |||||| |  | |||||||| ||| ||...
   tacttgttggtgttggatcgaaccaattggaagacgaatatgctcacatcacttctcattccttacatcttcttc...

Basically, you can do anything you want to with the info in the BLAST
report once you have parsed it. This will, of course, depend on what you
want to use it for, but hopefully this helps you get started on doing
what you need to do!

An important consideration for extracting information from a BLAST
report is the type of objects that the information is stored in. In
Biopython, the parsers return ``Record`` objects, either ``Blast`` or
``PSIBlast`` depending on what you are parsing. These objects are
defined in ``Bio.Blast.Record`` and are quite complete.

Figures :ref:`fig:blastrecord` and :ref:`fig:psiblastrecord` and
are my attempts at UML class diagrams for the ``Blast`` and ``PSIBlast``
record classes. The PSIBlast record object is similar, but has support
for the rounds that are used in the iteration steps of PSIBlast.

.. figure:: ../images/BlastRecord.png
   :alt: Class diagram for the Blast Record class representing a report
   :name: fig:blastrecord
   :width: 80.0%

   Class diagram for the Blast Record class representing a BLAST report.

.. figure:: ../images/PSIBlastRecord.png
   :alt: Class diagram for the PSIBlast Record class.
   :name: fig:psiblastrecord
   :width: 80.0%

   Class diagram for the PSIBlast Record class.

If you are good at UML and see mistakes/improvements that can be made,
please let me know.

.. _dealing-with-psi-blast-1:

Dealing with PSI-BLAST
----------------------

You can run the standalone version of PSI-BLAST (the legacy NCBI command
line tool ``blastpgp``, or its replacement ``psiblast``) directly from
the command line or using python’s ``subprocess`` module.

At the time of writing, the NCBI do not appear to support tools running
a PSI-BLAST search via the internet.

Note that the ``Bio.Blast.NCBIXML`` parser can read the XML output from
current versions of PSI-BLAST, but information like which sequences in
each iteration is new or reused isn’t present in the XML file.

.. _dealing-with-rps-blast-1:

Dealing with RPS-BLAST
----------------------

You can run the standalone version of RPS-BLAST (either the legacy NCBI
command line tool ``rpsblast``, or its replacement with the same name)
directly from the command line or using python’s ``subprocess`` module.

At the time of writing, the NCBI do not appear to support tools running
an RPS-BLAST search via the internet.

You can use the ``Bio.Blast.NCBIXML`` parser to read the XML output from
current versions of RPS-BLAST.