-
Notifications
You must be signed in to change notification settings - Fork 0
/
Feronia.py
1260 lines (1093 loc) · 54.7 KB
/
Feronia.py
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
#! /usr/bin/python
# -*- coding: utf-8 -*-
#===============================================================================#
# Feronia - Builds biodiversity databases from species checklists #
# (C) 2016 by Mauro J. Cavalcanti #
# <[email protected]> #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
# #
# Requirements: #
# Python 2.7+ (www.python.org) #
# PyQt 4.8+ (www.riverbankcomputing.com/software/pyqt) #
# formlayout 1.0+ (code.google.com/p/formlayout) #
# lxml 3.6+ (lxml.de) #
# openpyxl 2.0+ (openpyxl.readthedocs.org/en/2.0) #
# ezodf 0.2+ (pythonhosted.org/ezodf) #
# xlrd (www.python-excel.org) #
# MySQLdb (sourceforge.net/projects/mysql-python) #
# mysql.connector (dev.mysql.com/downloads/connector/python/2.1.html) #
# psycopg2 (initd.org/psycopg) #
# fdb (www.firebirdsql.org/en/devel-python-driver) #
# pygbif 0.1+ (github.com/sckott/pygbif) #
# BioPython (biopython.org/wiki/Main_Page) #
# wikipedia (github.com/goldsmith/Wikipedia) #
# #
# REVISION HISTORY: #
# Version 1.00, 17th May 16 - Initial release #
#===============================================================================#
import sys
import os
import os.path
import time
import platform
import warnings
import unicodedata
import simplejson
import urllib
import lxml.etree as ET
from os.path import basename
from PyQt4 import QtCore, QtGui
from formlayout import fedit
import resources
def encode_for_xml(unicode_data, encoding="ascii"):
return unicode_data.encode(encoding, "xmlcharrefreplace")
def iif(boolVar, ifTrue, ifFalse):
if boolVar:
return ifTrue
else:
return ifFalse
def is_ascii(s):
return all(ord(c) < 128 for c in s)
def unicode_to_ascii(str):
return unicodedata.normalize("NFKD", unicode(str)).encode("ascii","ignore")
#--- Disable all warnings
warnings.filterwarnings("ignore")
__version__ = "1.0.2"
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.initUI()
def initUI(self):
self.data = []
self.db = None
self.adapter = ""
self.filename = ""
self.openAction = QtGui.QAction("&Open", self)
self.openAction.setShortcut("Ctrl+O")
self.openAction.setStatusTip("Open a file")
self.openAction.triggered.connect(self.openFile)
self.connectAction = QtGui.QAction("&Connect", self)
self.connectAction.setStatusTip("Connect to a database")
self.connectAction.triggered.connect(self.connectDb)
self.closeAction = QtGui.QAction("&Quit", self)
self.closeAction.setShortcut("Ctrl+Q")
self.closeAction.setStatusTip("Close application")
self.closeAction.triggered.connect(self.close)
self.helpAction = QtGui.QAction("&About", self)
self.helpAction.setStatusTip("Display information")
self.helpAction.triggered.connect(self.about)
self.bibliographyAction = QtGui.QAction("&Bibliography", self)
self.bibliographyAction.setStatusTip("Bibliography")
self.bibliographyAction.triggered.connect(self.bibliography)
self.conservationAction = QtGui.QAction("&Conservation", self)
self.conservationAction.setStatusTip("Conservation Status")
self.conservationAction.triggered.connect(self.conservation)
self.distributionAction = QtGui.QAction("&Distribution", self)
self.distributionAction.setStatusTip("Geographic Distribution")
self.distributionAction.triggered.connect(self.distribution)
self.genomeAction = QtGui.QAction("&Genome", self)
self.genomeAction.setStatusTip("Genome")
self.genomeAction.triggered.connect(self.genome)
self.habitatsAction = QtGui.QAction("&Habitats", self)
self.habitatsAction.setStatusTip("Habitats")
self.habitatsAction.triggered.connect(self.habitats)
self.notesAction = QtGui.QAction("&Notes", self)
self.notesAction.setStatusTip("Structured Notes")
self.notesAction.triggered.connect(self.notes)
self.synonymsAction = QtGui.QAction("&Synonyms", self)
self.synonymsAction.setStatusTip("Synonyms")
self.synonymsAction.triggered.connect(self.synonyms)
##self.taxaAction = QtGui.QAction("&Taxa", self)
##self.taxaAction.setStatusTip("Taxa")
##self.taxaAction.triggered.connect(self.taxa)
self.commonnamesAction = QtGui.QAction("&Vernacular", self)
self.commonnamesAction.setStatusTip("Vernacular Names")
self.commonnamesAction.triggered.connect(self.commonnames)
menubar = self.menuBar()
fileMenu = menubar.addMenu("&File")
fileMenu.addAction(self.openAction)
fileMenu.addAction(self.connectAction)
fileMenu.addSeparator()
fileMenu.addAction(self.closeAction)
dataMenu = menubar.addMenu("&Data")
dataMenu.addAction(self.bibliographyAction)
dataMenu.addAction(self.conservationAction)
dataMenu.addAction(self.distributionAction)
dataMenu.addAction(self.genomeAction)
dataMenu.addAction(self.habitatsAction)
dataMenu.addAction(self.notesAction)
dataMenu.addAction(self.synonymsAction)
##dataMenu.addAction(self.taxaAction)
dataMenu.addAction(self.commonnamesAction)
helpMenu = menubar.addMenu("&Help")
helpMenu.addAction(self.helpAction)
statusbar = self.statusBar()
statusbar.setSizeGripEnabled(True)
statusbar.showMessage("Ready")
self.text = QtGui.QTextEdit(self)
self.text.setReadOnly(True)
self.text.setHtml(
"""Builds biodiversity databases from species checklists.
<br>© 2016 Mauro J. Cavalcanti.
<br>Ecoinformatics Studio, Rio de Janeiro, Brazil.
<br>E-mail: [email protected]""")
self.setCentralWidget(self.text)
self.setGeometry(100,100,650,350)
self.setWindowTitle("Feronia")
self.setWindowIcon(QtGui.QIcon(":/icon.png"))
self.updateUI()
self.show()
def updateUI(self):
self.connectAction.setEnabled(len(self.data) > 0)
enable = self.db is not None
self.bibliographyAction.setEnabled(enable)
self.conservationAction.setEnabled(enable)
self.distributionAction.setEnabled(enable)
self.genomeAction.setEnabled(enable)
self.habitatsAction.setEnabled(enable)
self.notesAction.setEnabled(enable)
self.synonymsAction.setEnabled(enable)
##self.taxaAction.setEnabled(enable)
self.commonnamesAction.setEnabled(enable)
def readData(self, filename):
file_ext = filename[-3:]
self.data = []
if file_ext == "xls":
import xlrd
wb = xlrd.open_workbook(filename)
sh1 = wb.sheet_by_index(0)
for rownum in range(sh1.nrows):
self.data += [sh1.row_values(rownum)]
elif file_ext == "csv":
import csv
reader = csv.reader(open(filename, "rb"))
for row in reader:
self.data += [row]
elif file_ext == "lsx":
from openpyxl.reader.excel import load_workbook
wb = load_workbook(filename=filename, use_iterators = True)
sheet = wb.get_active_sheet()
for row in sheet.iter_rows():
data_row = []
for cell in row:
data_row += [cell.value]
self.data += [data_row]
elif file_ext == "ods":
from ezodf import opendoc
wb = opendoc(filename)
s = wb.sheets[0]
for row in range(s.nrows()-1):
values = []
for col in range(s.ncols()-1):
values += [s[(row,col)].value]
self.data += [values]
self.text.append("<br><b>Read " + str(len(self.data)-1) + " records from file '" + basename(filename) + "'</b>")
del self.data[0]
def connectDb(self):
options = [("Adapter:",
[0, "MySQL",
"MariaDB",
"PostgreSQL",
"Firebird",
"SQLite"]),
("Username:", ""),
("Password:", ""),
("Database:", "")
]
self.db = None
while True:
result = fedit(options,
title="Database Adapter",
icon=QtGui.QIcon(":/icon.png"),
parent=self)
if result is None: break
adapter = result[0]
user = result[1]
pwd = result[2]
db_name = result[3]
self.adapter = options[0][1][adapter+1]
try:
if adapter == 0:
import MySQLdb
self.db = MySQLdb.connect(host="localhost", user=user, passwd=pwd, db=db_name)
elif adapter == 1:
import mysql.connector as mariadb
self.db = mariadb.connect(host="localhost", user=user, passwd=pwd, db=db_name)
elif adapter == 2:
import psycopg2
self.db = psycopg2.connect(host="localhost", user=user, passwd=pwd, db=db_name)
elif adapter == 3:
import fdb
self.db = fdb.connect(host="localhost", user=user, passwd=pwd, db=db_name)
elif adapter == 4:
import sqlite3
self.db = sqlite3.connect(db_name)
self.db.text_factory = str
#self.text.append("<br><b>Database '" + db_name + "' connected using " + options[0][1][adapter+1] + " adapter</b>")
self.text.append("<br><b>Database '" + db_name + "' connected using " + self.adapter + " adapter</b>")
break
except Exception, e:
QtGui.QMessageBox.critical(self, "Error", str(e[1]))
self.updateUI()
def createTables(self):
with self.db:
cursor = self.db.cursor()
#--- Bibliography table (from CoL / EOL)
##cursor.execute("DROP TABLE IF EXISTS bibliography")
cursor.execute("CREATE TABLE IF NOT EXISTS bibliography(B_NO INT PRIMARY KEY, B_TYPE VARCHAR(20), B_AUTHOR VARCHAR(128), \
B_YEAR INT, B_SEQUENCE CHAR(1), B_TITLE VARCHAR(254), B_DETAIL VARCHAR(512))")
#--- Conservation status table (from IUCN)
##cursor.execute("DROP TABLE IF EXISTS status")
if self.adapter == "SQLite":
cursor.execute("CREATE TABLE IF NOT EXISTS status(ID INTEGER PRIMARY KEY AUTOINCREMENT, T_NO INT, C_STATUS VARCHAR(22), C_TREND VARCHAR(12), B_NO INT)")
else:
cursor.execute("CREATE TABLE IF NOT EXISTS status(ID INT PRIMARY KEY AUTO_INCREMENT, T_NO INT, C_STATUS VARCHAR(22), C_TREND VARCHAR(12), B_NO INT)")
#--- Geographic distribution table (from GBIF)
##cursor.execute("DROP TABLE IF EXISTS distribution")
if self.adapter == "SQLite":
cursor.execute("CREATE TABLE IF NOT EXISTS distribution(ID INTEGER PRIMARY KEY AUTOINCREMENT, T_NO INT, P_CODE VARCHAR(50), P_CONTINENT VARCHAR(20), \
P_REGION VARCHAR(30), P_COUNTRY VARCHAR(30), P_STATE VARCHAR(30), P_COUNTY VARCHAR(30), \
P_LOCALITY VARCHAR(254), P_LATITUDE FLOAT, P_LONGITUDE FLOAT, P_I_STATUS VARCHAR(10), B_NO INT)")
else:
cursor.execute("CREATE TABLE IF NOT EXISTS distribution(ID INT PRIMARY KEY AUTO_INCREMENT, T_NO INT, P_CODE VARCHAR(50), P_CONTINENT VARCHAR(20), \
P_REGION VARCHAR(30), P_COUNTRY VARCHAR(30), P_STATE VARCHAR(30), P_COUNTY VARCHAR(30), \
P_LOCALITY VARCHAR(254), P_LATITUDE FLOAT, P_LONGITUDE FLOAT, P_I_STATUS VARCHAR(10), B_NO INT)")
#--- Genome table (from NCBI)
##cursor.execute("DROP TABLE IF EXISTS genome")
if self.adapter == "SQLite":
cursor.execute("CREATE TABLE IF NOT EXISTS genome(ID INTEGER PRIMARY KEY AUTOINCREMENT, T_NO INT, G_TAXID INT, G_SEQ_ID INT, G_SEQ_TYPE VARCHAR(12), \
G_DESCRIPTION VARCHAR(254), G_SEQUENCE TEXT, B_NO INT)")
else:
cursor.execute("CREATE TABLE IF NOT EXISTS genome(ID INT PRIMARY KEY AUTO_INCREMENT, T_NO INT, G_TAXID INT, G_SEQ_ID INT, G_SEQ_TYPE VARCHAR(12), \
G_DESCRIPTION VARCHAR(254), G_SEQUENCE TEXT, B_NO INT)")
#--- Habitats table (from IUCN)
##cursor.execute("DROP TABLE IF EXISTS habitats")
if self.adapter == "SQLite":
cursor.execute("CREATE TABLE IF NOT EXISTS habitats(ID INTEGER PRIMARY KEY AUTOINCREMENT, T_NO INT, H_PLACE VARCHAR(30), H_HABITAT VARCHAR(78), B_NO INT)")
else:
cursor.execute("CREATE TABLE IF NOT EXISTS habitats(ID INT PRIMARY KEY AUTO_INCREMENT, T_NO INT, H_PLACE VARCHAR(30), H_HABITAT VARCHAR(78), B_NO INT)")
#--- Metadata table (from user)
##cursor.execute("DROP TABLE IF EXISTS metadata")
cursor.execute("CREATE TABLE IF NOT EXISTS metadata(M_ID INT PRIMARY KEY, M_ACRONYM VARCHAR(50), M_TITLE VARCHAR(128), \
M_DESCRIPTION VARCHAR(255), M_SCOPE VARCHAR(20), M_ENVIRONMENT VARCHAR(20), \
M_COVERAGE VARCHAR(255), M_AUTHOR VARCHAR(255), M_VERSION VARCHAR(128), \
M_DATE DATE, M_PUBLISHER VARCHAR(128), M_URL VARCHAR(128), M_LOGO VARCHAR(128), \
M_BANNER VARCHAR(128))")
#--- Notes table (from Wikipedia)
##cursor.execute("DROP TABLE IF EXISTS notes")
if self.adapter == "SQLite":
cursor.execute("CREATE TABLE IF NOT EXISTS notes(ID INTEGER PRIMARY KEY AUTOINCREMENT, T_NO INT, N_NOTE VARCHAR(255), B_NO INT)")
else:
cursor.execute("CREATE TABLE IF NOT EXISTS notes(ID INT PRIMARY KEY AUTO_INCREMENT, T_NO INT, N_NOTE VARCHAR(255), B_NO INT)")
#--- Literature pointers
##cursor.execute("DROP TABLE IF EXISTS pointers")
if self.adapter == "SQLite":
cursor.execute("CREATE TABLE IF NOT EXISTS pointers(ID INTEGER PRIMARY KEY AUTOINCREMENT, T_NO INT, L_TYPE VARCHAR(20), B_NO INT)")
else:
cursor.execute("CREATE TABLE IF NOT EXISTS pointers(ID INT PRIMARY KEY AUTO_INCREMENT, T_NO INT, L_TYPE VARCHAR(20), B_NO INT)")
#--- Media resources table (from EOL / Wikipedia)
##cursor.execute("DROP TABLE IF EXISTS resources")
if self.adapter == "SQLite":
cursor.execute("CREATE TABLE IF NOT EXISTS resources(ID INTEGER PRIMARY KEY AUTOINCREMENT, T_NO INT, R_TYPE VARCHAR(20), \
R_RESOURCE VARCHAR(128), R_CAPTION VARCHAR(255), B_NO INT)")
else:
cursor.execute("CREATE TABLE IF NOT EXISTS resources(ID INT PRIMARY KEY AUTO_INCREMENT, T_NO INT, R_TYPE VARCHAR(20), \
R_RESOURCE VARCHAR(128), R_CAPTION VARCHAR(255), B_NO INT)")
#--- Synonyms table (from CoL)
##cursor.execute("DROP TABLE IF EXISTS synonyms")
if self.adapter == "SQLite":
cursor.execute("CREATE TABLE IF NOT EXISTS synonyms(ID INTEGER PRIMARY KEY AUTOINCREMENT, T_NO INT, S_STATUS VARCHAR(22), S_GENUS VARCHAR(30), \
S_G_AUTHOR VARCHAR(40), S_SUBGENUS VARCHAR(30), S_SPECIES VARCHAR(30), S_S_AUTHOR VARCHAR(50), \
S_RANK VARCHAR(7), S_SUBSP VARCHAR(30), S_SP_AUTHOR VARCHAR(40), B_NO INT)")
else:
cursor.execute("CREATE TABLE IF NOT EXISTS synonyms(ID INT PRIMARY KEY AUTO_INCREMENT, T_NO INT, S_STATUS VARCHAR(22), S_GENUS VARCHAR(30), \
S_G_AUTHOR VARCHAR(40), S_SUBGENUS VARCHAR(30), S_SPECIES VARCHAR(30), S_S_AUTHOR VARCHAR(50), \
S_RANK VARCHAR(7), S_SUBSP VARCHAR(30), S_SP_AUTHOR VARCHAR(40), B_NO INT)")
#--- Common names table (from CoL)
##cursor.execute("DROP TABLE IF EXISTS commonnames")
if self.adapter == "SQLite":
cursor.execute("CREATE TABLE IF NOT EXISTS commonnames(ID INTEGER PRIMARY KEY AUTOINCREMENT, T_NO INT, V_NAME VARCHAR(40), V_COUNTRY VARCHAR(30), \
V_LANGUAGE VARCHAR(30), B_NO INT)")
else:
cursor.execute("CREATE TABLE IF NOT EXISTS commonnames(ID INT PRIMARY KEY AUTO_INCREMENT, T_NO INT, V_NAME VARCHAR(40), V_COUNTRY VARCHAR(30), \
V_LANGUAGE VARCHAR(30), B_NO INT)")
#--- Taxa table (from user-provided species checklist)
##cursor.execute("DROP TABLE IF EXISTS taxa")
cursor.execute("CREATE TABLE IF NOT EXISTS taxa(T_NO INT PRIMARY KEY, T_STATUS VARCHAR(22), T_GENUS VARCHAR(30), \
T_G_AUTHOR VARCHAR(40), T_SUBGENUS VARCHAR(30), T_SPECIES VARCHAR(30), T_S_AUTHOR VARCHAR(50), \
T_RANK VARCHAR(7), T_SUBSP VARCHAR(30), T_SP_AUTHOR VARCHAR(40), B_NO INT)")
#--- Higher taxa table (from user-provided species checklist)
##cursor.execute("DROP TABLE IF EXISTS highertaxa")
cursor.execute("CREATE TABLE IF NOT EXISTS highertaxa(T_NO INT PRIMARY KEY, T_KINGDOM VARCHAR(50), T_PHYLUM VARCHAR(50), \
T_SUBPHYLUM VARCHAR(50), T_CLASS VARCHAR(50), T_SUBCLASS VARCHAR(50), T_ORDER VARCHAR(50), \
T_SUBORDER VARCHAR(50), T_FAMILY VARCHAR(50), T_SUPERFAMILY VARCHAR(50), T_SUBFAMILY VARCHAR(50), \
T_TRIBE VARCHAR(50))")
cursor.close()
def bibliography(self):
with self.db:
cursor = self.db.cursor()
cursor.execute("SELECT COUNT(*) from bibliography")
n = int(cursor.fetchone()[0])
if n > 0:
reply = QtGui.QMessageBox.question(self, "Confirmation",
"Delete data?\nThis operation cannot be undone!",
QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
cursor.execute("DROP TABLE IF EXISTS bibliography")
else:
return
reccount = 0
referencelist = []
self.text.append("<br><b>Fetching bibliographic references from CoL...</b><br>")
for i in range(len(self.data)):
#-- For each taxon in databaee
TaxNo = i + 1
Genus = self.data[i][5]
Species = self.data[i][6]
Subsp = self.data[i][8]
Name = Genus + ' ' + Species + ' ' + Subsp
#-- Retrieve data from CoL
result = urllib.urlopen("http://www.catalogueoflife.org/col/webservice?name=" + urllib.quote_plus(Name) + "&response=full").read()
root = ET.XML(result)
#-- Get a list of references
references = root.xpath("result/references/reference")
self.text.append(str(reccount + 1) + " <i>" + Genus + ' ' + Species + ' ' + Subsp + "</i> " + ' - ' + str(len(references)) + " reference(s)")
#-- Loop through the references
Type = "article"
Sequence = ''
for reference in references:
#-- Get the reference information
try:
Author = reference.xpath("author/text()")[0]
except IndexError:
Author = ""
try:
Year = reference.xpath("year/text()")[0]
except IndexError:
Year = 0
try:
Title = reference.xpath("title/text()")[0]
except IndexError:
Title = ""
try:
Source = reference.xpath("source/text()")[0]
except IndexError:
Source = ""
item = (Type, Author, Year, Sequence, Title, Source)
referencelist.append(item)
reccount += 1
self.text.repaint()
# Sleep for one second to prevent IP blocking from CoL
time.sleep(1)
#-- Remove deuplicates from reference list
uniquelist = set(referencelist)
referencelist = list(set(uniquelist))
#-- Add numbers to reference list
numberedlist = [tuple([index+1] + list(ref)) for index, ref in enumerate(referencelist)]
referencelist = numberedlist
refcount = len(referencelist)
#-- Insert records into the database
try:
cursor = self.db.cursor()
if self.adapter == "SQLite":
cursor.executemany("INSERT INTO bibliography \
(B_NO, B_TYPE, B_AUTHOR, B_YEAR, B_SEQUENCE, B_TITLE, B_DETAIL) \
VALUES (?, ?, ?, ?, ?, ?, ?)",
referencelist)
else:
cursor.executemany("INSERT INTO bibliography \
(B_NO, B_TYPE, B_AUTHOR, B_YEAR, B_SEQUENCE, B_TITLE, B_DETAIL) \
VALUES (%s, %s, %s, %s, %s, %s, %s)",
referencelist)
self.db.commit()
except Exception, e:
QtGui.QMessageBox.critical(self, "Error", str(e[1]))
self.db.rollback()
finally:
cursor.close()
self.text.append("<br>" + str(reccount) + " record(s) processed<br>")
def conservation(self):
with self.db:
cursor = self.db.cursor()
cursor.execute("SELECT COUNT(*) from status")
n = int(cursor.fetchone()[0])
if n > 0:
reply = QtGui.QMessageBox.question(self, "Confirmation",
"Delete data?\nThis operation cannot be undone!",
QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
cursor.execute("DROP TABLE IF EXISTS status")
else:
return
SEARCH_BASE = 'http://apiv3.iucnredlist.org/api/v3/'
SEARCH_SPECIES = "species/"
TOKEN = "<YOUR_API_KEY>"
category = {"NE": "Not Evaluated",
"DD": "Data Deficient",
"LC": "Least Concern",
"NT": "Not Threatened",
"VU": "Vulnerable",
"EN": "Endangered",
"CR": "Critically Endangered",
"EW": "Extinct in the Wild",
"EX": "Extinct"
}
reccount = 0
conservation = []
RefNo = 0
self.text.append("<br><b>Fetching conservation status from IUCN...</b><br>")
for i in range(len(self.data)):
#-- For each taxon in databaee
TaxNo = i + 1
Genus = self.data[i][5]
Species = self.data[i][6]
Subsp = self.data[i][8]
Name = Genus + ' ' + Species + iif(len(Subsp) > 0, ' ' + Subsp, "")
#-- Get conservation status from IUCN
url = SEARCH_BASE + SEARCH_SPECIES + Name + "?token=" + TOKEN
results = simplejson.load(urllib.urlopen(url))
result = results['result']
Trend = "Unknown"
try:
Status = category[result[0]['category']]
except IndexError:
Status = category["NE"]
self.text.append(str(reccount + 1) + " <i>" + Genus + ' ' + Species + ' ' + Subsp + "</i> " + " - Status: " + Status)
item = (TaxNo, Status, Trend, RefNo)
conservation.append(item)
reccount += 1
self.text.repaint()
# Sleep for two seconds to prevent IP blocking from IUCN
time.sleep(2)
#-- Insert records into the database
try:
#-- Insert records into the database
cursor = self.db.cursor()
if self.adapter == "SQLite":
cursor.executemany("INSERT INTO status \
(T_NO, C_STATUS, C_TREND, B_NO) \
VALUES (?, ?, ?, ?)",
conservation)
else:
cursor.executemany("INSERT INTO status \
(T_NO, C_STATUS, C_TREND, B_NO) \
VALUES (%s, %s, %s, %s)",
conservation)
self.db.commit()
except Exception, e:
QtGui.QMessageBox.critical(self, "Error", str(e[1]))
self.db.rollback()
finally:
cursor.close()
self.text.append("<br>" + str(reccount) + " record(s) processed<br>")
def distribution(self):
from pygbif import species, occurrences
with self.db:
cursor = self.db.cursor()
cursor.execute("SELECT COUNT(*) from distribution")
n = int(cursor.fetchone()[0])
if n > 0:
reply = QtGui.QMessageBox.question(self, "Confirmation",
"Delete data?\nThis operation cannot be undone!",
QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
cursor.execute("DROP TABLE IF EXISTS distribution")
else:
return
reccount = 0
distribution = []
RefNo = 0
self.text.append("<br><b>Fetching distribution data from GBIF...</b><br>")
for i in range(len(self.data)):
#-- For each taxon in databaee
TaxNo = i + 1
Genus = self.data[i][5]
Species = self.data[i][6]
Subsp = self.data[i][8]
Name = Genus + ' ' + Species + ' ' + Subsp
#-- Get geographic distribution data from GBIF
key = species.name_backbone(name=Name, rank="species")["usageKey"]
n = occurrences.count(taxonKey=key, isGeoreferenced=True)
if n > 300:
max = 300
else:
max = n
results = occurrences.search(taxonKey=key, limit=max)
self.text.append(str(reccount + 1) + " <i>" + Genus + ' ' + Species + ' ' + Subsp + "</i> " + ' - ' + format(n, ',') + " occurrence(s)")
for x in results["results"]:
Region = None
Status = None
try:
Code = x['institutionCode'] + '-' + x['catalogNumber']
except:
Code = ""
try:
if x['continent'].find('_') != -1:
Continent = ' '.join(x['continent'].split('_')).title()
else:
Continent = x['continent'].capitalize()
except:
Continent = ""
try:
Country = x['country'].encode('latin-1', 'replace')
except:
Country = ""
try:
State = x['stateProvince'].encode('latin-1', 'replace')
except:
State = ""
try:
County = x['county'].encode('latin-1', 'replace')
except:
County = ""
try:
Locality = x['locality'].encode('latin-1', 'replace')
except:
Locality = ""
try:
Latitude = x['decimalLatitude']
except:
Latitude = 0.0
try:
Longitude = x['decimalLongitude']
except:
Longitude = 0.0
item = (TaxNo, Code, Continent, Region, Country, State, County, Locality, Latitude, Longitude, Status, RefNo)
distribution.append(item)
reccount += 1
self.text.repaint()
# Sleep for two seconds to prevent IP blocking from GBIF
time.sleep(1)
#-- Insert records into the database
try:
cursor = self.db.cursor()
if self.adapter == "SQLite":
cursor.executemany("INSERT INTO distribution \
(T_NO, P_CODE, P_CONTINENT, P_REGION, P_COUNTRY, P_STATE, P_COUNTY, P_LOCALITY, P_LATITUDE, P_LONGITUDE, P_I_STATUS, B_NO) \
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
distribution)
else:
cursor.executemany("INSERT INTO distribution \
(T_NO, P_CODE, P_CONTINENT, P_REGION, P_COUNTRY, P_STATE, P_COUNTY, P_LOCALITY, P_LATITUDE, P_LONGITUDE, P_I_STATUS, B_NO) \
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
distribution)
self.db.commit()
except Exception, e:
QtGui.QMessageBox.critical(self, "Error", str(e[1]))
db.rollback()
finally:
cursor.close()
self.text.append("<br>" + str(reccount) + " record(s) processed<br>")
def genome(self):
from Bio import SeqIO, Entrez
with self.db:
cursor = self.db.cursor()
cursor.execute("SELECT COUNT(*) from genome")
n = int(cursor.fetchone()[0])
if n > 0:
reply = QtGui.QMessageBox.question(self, "Confirmation",
"Delete data?\nThis operation cannot be undone!",
QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
cursor.execute("DROP TABLE IF EXISTS genome")
else:
return
#-- Save Genbank results to temporary file
tempfile = "temp.dat"
#-- Always tell NCBI who you are
Entrez.email = "<your_email_address>"
reccount = 0
sequences = []
RefNo = 0
self.text.append("<br><b>Fetching genomic data from NCBI...</b><br>")
for i in range(len(self.data)):
#-- For each taxon in databaee
TaxNo = i + 1
Genus = self.data[i][5]
Species = self.data[i][6]
Subsp = self.data[i][8]
Name = Genus + ' ' + Species + iif(len(Subsp) > 0, ' ' + Subsp, "")
Taxon = Genus + '+' + Species + iif(len(Subsp) > 0, '+' + Subsp, "")
try:
#-- Retrieve taxonomy from NCBI
handle = Entrez.esearch(db="taxonomy",term=Taxon)
record = Entrez.read(handle)
try:
TaxID = record['IdList'][0]
handle = Entrez.esummary(db="taxonomy", id=TaxID)
record = Entrez.read(handle)
#-- Retrieve nucleotide sequences
handle = Entrez.esearch(db="nucleotide",term=Taxon)
record = Entrez.read(handle)
nucl = int(record['RetMax'])
for i in range(nucl):
#-- Download sequence
SeqID = record['IdList'][i]
net_handle = Entrez.efetch(db="nucleotide", id=SeqID, rettype="fasta", retmode="text")
out_handle = open(tempfile, "w")
out_handle.write(net_handle.read())
out_handle.close()
net_handle.close()
seq_record = SeqIO.read(tempfile, "fasta")
sequence = (TaxNo, TaxID, SeqID, "nucleotide", seq_record.description, str(seq_record.seq), RefNo)
sequences.append(sequence)
except:
nucl = 0
try:
#-- Retrieve protein sequences from NCBI
handle = Entrez.esearch(db="protein",term=Taxon)
record = Entrez.read(handle)
prot = int(record['RetMax'])
for j in range(prot):
#-- Download sequence
seqid = record['IdList'][j]
net_handle = Entrez.efetch(db="protein", id=SeqID, rettype="fasta", retmode="text")
out_handle = open(tempfile, "w")
out_handle.write(net_handle.read())
out_handle.close()
net_handle.close()
seq_record = SeqIO.read(tempfile, "fasta")
sequence = (TaxNo, TaxID, SeqID, "protein", seq_record.description, str(seq_record.seq), RefNo)
sequences.append(sequence)
except:
prot = 0
except:
continue
self.text.append(str(reccount + 1) + " <i>" + Genus + ' ' + Species + ' ' + Subsp + "</i> " + " - Sequences: " + "nucleotide: " + str(nucl) + ', ' + "protein: " + str(prot))
reccount += 1
self.text.repaint()
# Sleep for one second to prevent IP blocking from NCBI
time.sleep(1)
if os.path.exists(tempfile):
os.remove(tempfile)
#-- Insert records into the database
try:
cursor = self.db.cursor()
if self.adapter == "SQLite":
cursor.executemany("INSERT INTO genome \
(T_NO, G_TAXID, G_SEQ_ID, G_SEQ_TYPE, G_DESCRIPTION, G_SEQUENCE, B_NO) \
VALUES (?, ?, ?, ?, ?, ?, ?)",
sequences)
else:
cursor.executemany("INSERT INTO genome \
(T_NO, G_TAXID, G_SEQ_ID, G_SEQ_TYPE, G_DESCRIPTION, G_SEQUENCE, B_NO) \
VALUES (%s, %s, %s, %s, %s, %s, %s)",
sequences)
self.db.commit()
except Exception, e:
QtGui.QMessageBox.critical(self, "Error", str(e[1]))
self.db.rollback()
finally:
cursor.close()
self.text.append("<br>" + str(reccount) + " record(s) processed<br>")
def habitats(self):
with self.db:
cursor = self.db.cursor()
cursor.execute("SELECT COUNT(*) from habitats")
n = int(cursor.fetchone()[0])
if n > 0:
reply = QtGui.QMessageBox.question(self, "Confirmation",
"Delete data?\nThis operation cannot be undone!",
QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
cursor.execute("DROP TABLE IF EXISTS habitats")
else:
return
SEARCH_BASE = 'http://apiv3.iucnredlist.org/api/v3/'
SEARCH_HABITATS = "habitats/species/name/"
TOKEN = "<YOUR_API_KEY>"
reccount = 0
habitats = []
RefNo = 0
self.text.append("<br><b>Fetching habitat data from IUCN...</b><br>")
for i in range(len(self.data)):
#-- For each taxon in databaee
TaxNo = i + 1
Genus = self.data[i][5]
Species = self.data[i][6]
Subsp = self.data[i][8]
Name = Genus + ' ' + Species + iif(len(Subsp) > 0, ' ' + Subsp, "")
#-- Get conservation status from IUCN
k = 0
Place = ""
url = SEARCH_BASE + SEARCH_HABITATS + Name + "?token=" + TOKEN
results = simplejson.load(urllib.urlopen(url))
for j in range(len(results)):
result = results['result']
try:
Habitat = result[j]['habitat']
k += 1
item = (TaxNo, Place, Habitat, RefNo)
habitats.append(item)
except IndexError:
Habitat = None
if k < 1: k = 0
self.text.append(str(reccount + 1) + " <i>" + Genus + ' ' + Species + ' ' + Subsp + "</i> " + ' - ' + str(k) + " habitat(s)")
reccount += 1
self.text.repaint()
# Sleep for two seconds to prevent IP blocking from IUCN
time.sleep(2)
#-- Insert records into the database
try:
cursor = self.db.cursor()
if self.adapter == "SQLite":
cursor.executemany("INSERT INTO habitats \
(T_NO, H_PLACE, H_HABITAT, B_NO) \
VALUES (?, ?, ?, ?)",
habitats)
else:
cursor.executemany("INSERT INTO habitats \
(T_NO, H_PLACE, H_HABITAT, B_NO) \
VALUES (%s, %s, %s, %s)",
habitats)
self.db.commit()
except Exception, e:
QtGui.QMessageBox.critical(self, "Error", str(e[1]))
self.db.rollback()
finally:
cursor.close()
self.text.append("<br>" + str(reccount) + " record(s) processed<br>")
def notes(self):
import wikipedia
with self.db:
cursor = self.db.cursor()
cursor.execute("SELECT COUNT(*) from notes")
n = int(cursor.fetchone()[0])
if n > 0:
reply = QtGui.QMessageBox.question(self, "Confirmation",
"Delete data?\nThis operation cannot be undone!",
QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
cursor.execute("DROP TABLE IF EXISTS notes")
else:
return
reccount = 0
notes = []
RefNo = 0
self.text.append("<br><b>Fetching text snippets from Wikipedia...</b><br>")
for i in range(len(self.data)):
#-- For each taxon in databaee
TaxNo = i + 1
Genus = self.data[i][5]
Species = self.data[i][6]
Subsp = self.data[i][8]
Name = Genus + ' ' + Species + ' ' + Subsp
#-- Get a text snippet from Wikipedia
try:
Summary = wikipedia.summary(Name.replace(' ', '_'), sentences=1)
item = (TaxNo, Summary.encode('latin-1', 'replace'), RefNo)
notes.append(item)
except:
Summary = ""
self.text.append(str(reccount + 1) + " <i>" + Genus + ' ' + Species + ' ' + Subsp + "</i>" + ' - ' + iif(len(Summary) > 0, str(1), str(0)) + " snippet(s)")
reccount += 1
self.text.repaint()
# Sleep for one second to prevent IP blocking from Wikipedia
time.sleep(1)
#-- Insert records into the database
try:
cursor = self.db.cursor()
if self.adapter == "SQLite":
cursor.executemany("INSERT INTO notes \
(T_NO, N_NOTE, B_NO) \
VALUES (?, ?, ?)",
notes)
else:
cursor.executemany("INSERT INTO notes \
(T_NO, N_NOTE, B_NO) \
VALUES (%s, %s, %s)",
notes)
self.db.commit()
except Exception, e:
QtGui.QMessageBox.critical(self, "Error", str(e[1]))
self.db.rollback()
finally:
cursor.close()
self.text.append("<br>" + str(reccount) + " record(s) processed<br>")
def synonyms(self):
with self.db:
cursor = self.db.cursor()
cursor.execute("SELECT COUNT(*) from synonyms")
n = int(cursor.fetchone()[0])
if n > 0:
reply = QtGui.QMessageBox.question(self, "Confirmation",
"Delete data?\nThis operation cannot be undone!",
QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
cursor.execute("DROP TABLE IF EXISTS synonyms")
else:
return
reccount = 0
synonymdata = []
RefNo = 0
self.text.append("<br><b>Fetching synonyms from CoL...</b><br>")
for i in range(len(self.data)):
#-- For each taxon in databaee
TaxNo = i + 1
Genus = self.data[i][5]
Species = self.data[i][6]
Subsp = self.data[i][8]
Name = Genus + ' ' + Species + ' ' + Subsp
#-- Retrieve data from CoL
result = urllib.urlopen("http://www.catalogueoflife.org/col/webservice?name=" + urllib.quote_plus(Name) + "&response=full").read()
root = ET.XML(result)
#-- Get a list of synonyms
synonyms = root.xpath("result/synonyms/synonym")
self.text.append(str(reccount + 1) + " <i>" + Genus + ' ' + Species + ' ' + Subsp + "</i> " + ' - ' + str(len(synonyms)) + " synonym(s)")
#-- Loop through the synonyms
SGAuthor = None
SSubgen = None
SSAuthor = None
SSubsp = None
SRank = None
for synonym in synonyms:
SStatus = synonym.xpath("name_status/text()")[0]
SGenus = synonym.xpath("genus/text()")[0]
SSpecies = synonym.xpath("species/text()")[0]
try:
SAuthor = synonym.xpath("author/text()")[0]
SRank = synonym.xpath("infraspecies_marker/text()")[0]
SSubsp = synonym.xpath("infraspecies/text()")[0]
except:
SAuthor = unicode_to_ascii(SAuthor)
pass
item = (TaxNo, SStatus, SGenus, SGAuthor, SSubgen, SSpecies, SAuthor, SRank, SSubsp, SSAuthor, RefNo)