-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.py
3363 lines (2670 loc) · 137 KB
/
backend.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
import requests
from html.parser import HTMLParser
from chemdataextractor import Document
import easyocr
import json
from elsapy.elsclient import ElsClient
from elsapy.elsdoc import FullDoc
from molecular_Structure_Similarity import molecularSimles
import os
# Parsers cannot exit from inside, the reset() method needs to be called from outside
def exitParser(parser):
parser.reset()
# identify whether a string is "ic50", OCR result could be "icso", "icSo" etc.
def ic50(string):
string = string.lower()
pos = string.find("ic")
if(pos == -1):
return False
pos += 2
if(pos >= len(string)):
return False
if not(string[pos] == "5" or string[pos] == "s" or string[pos] == "S"):
return False
pos += 1
if(pos >= len(string)):
return False
if not (string[pos] == "0" or string[pos] == "O" or string[pos] == "o"):
return False
return True
# identify whether a string is in the form of a compound name, pure number like 18, or number followed by letter like 18ae
def compoundName(string):
if(string == ""):
return False
string = string.lower().strip()
for c in string:
if(c.isspace()):
return False
if(string.isdigit()):
return True
if(len(string) >= 2 and string[0].isdigit()):
onlyDigit = True
for c in string:
if(not onlyDigit and not c.isalpha()):
return False
if(onlyDigit and not c.isdigit()):
onlyDigit = False
return True
return False
# identify whether a string is in the form of a molecule name, either pure letters, or contains numbers with dash("-")
def moleculeName(string):
string = string.lower().strip()
if(string.isalpha()):
return True
hasNumber = False
hasDash = False
for letter in string:
if(letter.isdigit()):
hasNumber = True
elif(letter == "-"):
hasDash = True
if(hasNumber and not hasDash):
return False
return True
class BodyText:
class Section:
class Paragraph:
def __init__(self, header = ""):
self.header = header
self.contents = [] # list[str]
self.boldContents = []
def __init__(self, title):
self.title = title
self.paragraphs = [] # list[self.Paragraph]
def __init__(self):
self.sections = [] # list[self.Section]
class Table:
class Grid:
class Row:
def __init__(self):
# a cell may hold empty string, if html element is " "
self.cells = [] # list[str]
def __init__(self):
self.columnNum = 0
self.header = [] # list[self.Row]
self.body = [] # list[self.Row]
def __init__(self):
self.caption = ""
self.descriptions = [] # list[str]
self.grid = self.Grid()
# --------------------------------------------------------------------------------------------------------------
class ACS:
DOMAIN = "https://pubs.acs.org"
TARGET = ""
class AmountParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.tagFound = False
self.articleTotalAmount = 0
def handle_starttag(self, tag, attrs):
if (tag == "span" and len(attrs) == 1 and attrs[0][1] == "result__count"):
self.tagFound = True
def handle_data(self, data):
if(self.tagFound):
self.articleTotalAmount = int(data)
exitParser(self)
class QueryParser(HTMLParser):
hasNextPage = True
nextPageURL = ""
def __init__(self):
HTMLParser.__init__(self)
self.articleTagFound = False
self.pageListFound = False
self.nextButtonFound = False
self.addressArr = []
def handle_starttag(self, tag, attrs):
if (tag == "h2" and len(attrs) == 1 and attrs[0][1] == "issue-item_title"):
self.articleTagFound = True
elif (self.articleTagFound and tag == "a"):
self.addressArr.append(ACS.DOMAIN + attrs[0][1])
self.articleTagFound = False
elif (tag == "ul" and len(attrs) == 1 and attrs[0][1] == "rlist--inline pagination__list"):
self.pageListFound = True
elif (self.pageListFound and tag == "span"):
self.nextButtonFound = True
elif (self.nextButtonFound and tag == "a"):
self.pageListFound = False
self.nextButtonFound = False
for attr in attrs:
if (attr[0] == "href"):
ACS.QueryParser.nextPageURL = attr[1]
def handle_endtag(self, tag):
if(self.pageListFound and tag == "nav"):
self.pageListFound = False
self.nextButtonFound = False
ACS.QueryParser.hasNextPage = False
class ContentParser(HTMLParser):
dateArr = []
tableAddressArr = []
drugPaperCount = 0
def __init__(self):
HTMLParser.__init__(self)
self.contentFound = False
self.ICFound = False
self.complete = False
self.dateRowFound = False
self.dateFound = False
self.date = ""
self.titleFound = False
self.abstractFound = False
self.figureFound = False
self.figureLinkFound = False
self.imgURL = ""
self.keywordFound = False
def handle_starttag(self, tag, attrs):
if (self.complete):
return
elif (tag == "div" and len(attrs) == 1 and attrs[0][1] == "NLM_p"):
self.contentFound = True
elif (tag == "div" and len(attrs) == 1 and attrs[0][1] == "article_header-epubdate"):
self.dateRowFound = True
elif (self.dateRowFound and len(attrs) == 1 and attrs[0][1] == "pub-date-value"):
self.dateFound = True
elif (tag == "div" and len(attrs) == 1 and attrs[0][1] == "article_content-title"):
self.titleFound = True
if(tag == "div" and len(attrs) >= 1):
for attr in attrs:
if (attr[0] == "class" and attr[1] == "article_abstract-content hlFld-Abstract"):
self.abstractFound = True
break
if(self.figureFound and tag == "figure"):
self.figureFound = True
if(self.figureFound and tag == "a" and len(attrs) >= 2):
title = link = ""
for attr in attrs:
if(attr[0] == "title"):
title = attr[1]
elif(attr[0] == "href"):
link = attr[1]
if(title == "High Resolution Image"):
self.figureLinkFound = True
self.imgURL = ACS.DOMAIN + link
if(tag == "div" and len(attrs) == 1 and attrs[0][1] == "article_content-title"):
if(not self.figureLinkFound):
exitParser(self)
def handle_data(self, data):
if (self.complete):
return
if(self.contentFound):
stringList = ["IC50", "EC50", "ED50"]
if(any(substring in data for substring in stringList)):
self.keywordFound = True
exitParser(self)
self.complete = True
elif(any(substring in data for substring in stringList)):
index = data.find("Ki")
if(index == -1):
index = data.find("Kd")
if(index == -1):
return
keywordFound = False
if((index + 2) >= len(data)):
keywordFound = True
else:
if(not data[index + 2].isalpha()):
keywordFound = True
if(keywordFound):
self.keywordFound = True
exitParser(self)
elif(self.ICFound):
if(len(data) >= 2 and data[:2] == "50"):
self.keywordFound = True
exitParser(self)
self.complete = True
else:
self.ICFound = False
elif(len(data) >= 2 and (data[-2:] in ["IC", "EC", "ED"])):
self.ICFound = True
elif(self.dateFound):
self.date = data.split()[-1]
elif(self.titleFound):
if(data.lower() in "references"):
exitParser(self)
def handle_endtag(self, tag):
if(self.complete):
return
elif(self.contentFound and tag == "div"):
self.contentFound = False
elif(self.dateRowFound and tag == "div"):
self.dateRowFound = False
elif(self.dateFound and tag == "span"):
self.dateFound = False
elif(self.titleFound and tag == "div"):
self.titleFound = False
# --------------------------------------------------------------------------------------------------------------
def prepare_query_url(targetName):
keyWord = targetName
URL = ""
queryString = ""
for word in keyWord.split():
if(not queryString):
queryString += word
else:
queryString += f"+{word}"
URL = f"https://pubs.acs.org/action/doSearch?field1=AllField&text1={queryString}&field2=AllField&text2=&ConceptID=&ConceptID=&publication=&publication%5B%5D=jmcmar&accessType=allContent&Earliest="
return URL
def get_article_amount_and_response(URL):
response = requests.get(URL, headers = {"User-Agent": "Mozilla/5.0"})
try:
amountParser = ACS.AmountParser()
amountParser.feed(response.text)
except AssertionError as ae:
pass
return (amountParser.articleTotalAmount, response)
def get_article_URLs(response):
queryParser = ACS.QueryParser()
queryParser.feed(response.text)
while(ACS.QueryParser.hasNextPage):
response = requests.get(ACS.QueryParser.nextPageURL, headers = {"User-Agent": "Mozilla/5.0"})
queryParser.feed(response.text)
return queryParser.addressArr
def get_drug_molecule_paper(addressArr):
drugPaperCount = 0
tableAddressArr = []
dateArr = []
simlesArr = []
fileId = 0
for address in addressArr:
contentParser = ACS.ContentParser()
simles = ""
try:
articleResponse = requests.get(address, headers = {"User-Agent": "Mozilla/5.0"})
contentParser.feed(articleResponse.text)
except AssertionError as ae:
if(contentParser.keywordFound and contentParser.imgURL):
image = requests.get(contentParser.imgURL).content
with open("abstract_image/image.jpeg", "wb") as handler:
handler.write(image)
simles = molecularSimles("abstract_image/image.jpeg")
if(simles):
os.rename("abstract_image/image.jpeg", f"abstract_image/image{fileId}.jpeg")
drugPaperCount += 1
tableAddressArr.append(address)
simlesArr.append(simles)
found = False
for yearOccur in dateArr:
if (yearOccur[0] == contentParser.date):
found = True
yearOccur[1] += 1
break
if(not found):
dateArr.append([contentParser.date, 1])
fileId += 1
dateArr.sort()
return (dateArr, tableAddressArr, drugPaperCount, simlesArr)
# --------------------------------------------------------------------------------------------------------------
class ACSArticle:
# Parse the reponse from online enquiry and store useful information
class TargetParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.tableFound = False
self.resultFound = False
# the returned abbreviation or full name
self.result = ""
self.columnNum = 0
self.frequencyFound = False
# frequency of occurrence of self.result found in database
self.frequency = 0
def handle_starttag(self, tag, attrs):
if(tag == "table"):
self.tableFound = True
if(tag == "table" and len(attrs) > 0):
for attr in attrs:
if(attr[0] == "class" and attr[1] == "sortable"):
self.resultFound = True
if(self.resultFound and tag == "td"):
self.columnNum += 1
if(self.columnNum == 2 and tag == "div"):
exitParser(self)
if(self.columnNum == 2 and tag == "br"):
self.frequencyFound = True
def handle_data(self, data):
if(self.frequencyFound):
frequencyStr = ""
for c in data:
if(c.isdigit()):
frequencyStr += c
self.frequency = int(frequencyStr)
return
if(self.columnNum == 2):
self.result += data
if(self.tableFound):
if("not found" in data):
exitParser(self)
def handle_endtag(self, tag):
if(self.resultFound and tag == "table"):
self.resultFound = False
if(self.tableFound and tag == "table"):
self.tableFound = False
# parsing a html file
# --------------------------------------------------------------------------------------------------------------
class TableParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.authorArr = []
self.year = -1
self.institution = []
self.paperCited = -1
self.doi = ""
self.journal = ""
self.authorFound = False
self.dateFound = False
self.institutionFound = False
self.citationFound = False
self.citationDivCount = 0
self.citationNumber = False
self.doiFound = False
self.doiLink = False
self.journalFound = False
self.journalName = False
# enable this flag to skip handle_data for the next element
self.disableRead = False
# the link(s) to access abstract image
self.imgArr = []
# complete abstract text content
self.abstractText = ""
self.abstractBoldText = ""
# all elements in abstract text in bold (<b></b>)
self.boldAbstractTextArr = []
self.abstractFound = False
self.figureFound = False
self.imgLinkFound = False
self.textFound = False
self.boldTextFound = False
self.titleFound = False
self.titleText = False
self.title = ""
# a BodyText object to hold the content of body text
self.bodyText = BodyText()
self.newSectionFound = False
self.sectionTitleFound = False
self.paragraphFound = False
self.paragraphDivCount = 0
# hold the content of the paragraph currently being parsed
self.paragraphText = ""
self.boldParagraphText = ""
self.boldParagraphFound = False
self.paragraphHeaderFound = False
# hold the title for the currently parsed paragraph (could be empty)
self.paragraphHeader = ""
self.paragraphBoldFound = False
self.paragraphBoldText = ""
# hold all the Table objects contained in the current article
self.tables = [] # list[Table]
self.tableFound = False
self.tableDivCount = 0
self.tableCaptionFound = False
self.tableCaptionDivCount = 0
# hold the caption of the table currently being parsed
self.tableCaption = ""
self.tableGridFound = False
self.tableColCountFound = False
self.gridHeaderFound = False
self.cellFound = False
self.gridBodyFound = False
# hold the content of the current parsing cell
self.cell = ""
self.cellSpace = False
self.tableDescriptionFound = False
self.tableDescriptionDivCount = 0
self.tableFootnoteFound = False
def handle_starttag(self, tag, attrs):
if(tag == "span" and len(attrs) == 1 and attrs[0][1] == "hlFld-ContribAuthor"):
self.authorFound = True
if(tag == "span" and len(attrs) == 1 and attrs[0][1] == "pub-date-value"):
self.dateFound = True
if(tag == "span" and len(attrs) == 1 and attrs[0][1] == "aff-text"):
self.institutionFound = True
self.institution.append("")
if(tag == "div" and len(attrs) == 1 and attrs[0][1] == "articleMetrics_count"):
self.citationFound = True
self.citationDivCount += 1
elif(self.citationFound and tag == "div"):
self.citationDivCount += 1
if(self.citationFound and tag == "a"):
self.citationNumber = True
if(tag == "div" and len(attrs) == 1 and attrs[0][1] == "article_header-doiurl"):
self.doiFound = True
if(self.doiFound and tag == "a"):
self.doiLink = True
if(not self.journalFound and tag == "input" and len(attrs) > 0):
value = ""
for attr in attrs:
if(attr[0] == "name" and attr[1] == "journalNameForjhpLink"):
self.journalFound = True
elif(attr[0] == "value"):
value = attr[1]
if(self.journalFound and value):
self.journal = value
# handle title, abstract image and abstract text
if(tag == "div" and len(attrs) >= 1):
for attr in attrs:
if (attr[0] == "class" and attr[1] == "article_abstract-content hlFld-Abstract"):
self.abstractFound = True
break
if(tag == "div" and len(attrs) == 1 and attrs[0][1] == "article_content"):
self.abstractText += " . "
self.abstractBoldText += " . "
self.abstractFound = False
if(self.abstractFound and tag == "figure"):
self.figureFound = True
if(self.figureFound and tag == "a" and len(attrs) >= 2):
title = link = ""
for attr in attrs:
if(attr[0] == "title"):
title = attr[1]
elif(attr[0] == "href"):
link = ACS.DOMAIN + attr[1]
if(title == "High Resolution Image"):
self.imgArr.append(link)
if(self.abstractFound and tag == "p" and len(attrs) == 1 and attrs[0][1] == "articleBody_abstractText"):
self.textFound = True
if(tag == "h1" and len(attrs) == 1 and attrs[0][1] == "article_header-title"):
self.titleFound =True
if(self.titleFound and tag == "span"):
self.titleText = True
if(self.textFound and tag == "b"):
self.boldTextFound = True
self.abstractBoldText += "<b>"
#handle body text
if(tag == "div" and len(attrs) == 1 and attrs[0][1] == "article_content-title"):
self.sectionTitleFound = True
self.newSectionFound = True
if(tag == "div" and len(attrs) == 1 and "NLM_p" in attrs[0][1]):
self.paragraphFound = True
self.paragraphDivCount += 1
elif(self.paragraphFound and tag == "div"):
self.paragraphDivCount += 1
if(tag == "h3" and len(attrs) > 0):
for attr in attrs:
if(attr[0] == "class" and attr[1] == "article-section__title"):
self.paragraphHeaderFound = True
if(self.paragraphFound and tag == "b"):
self.boldParagraphFound = True
self.boldParagraphText += "<b> "
# handle table caption
if(tag == "div" and len(attrs) > 1):
for attr in attrs:
if(attr[0] == "class" and attr[1] == "NLM_table-wrap"):
self.tableFound = True
self.tableDivCount += 1
self.tables.append(Table())
return
if(self.tableFound and tag == "div"):
self.tableDivCount += 1
if(self.tableFound and tag == "div" and len(attrs) > 0):
for attr in attrs:
if(attr[0] == "class" and attr[1] == "NLM_caption"):
self.tableCaptionFound = True
self.tableCaptionDivCount += 1
return
if(self.tableCaptionFound and tag == "div"):
self.tableCaptionDivCount += 1
if(self.tableCaptionFound and tag == "a"):
self.disableRead = True
# handle table grid
if(self.tableFound and tag == "table"):
self.tableGridFound = True
if(self.tableGridFound and tag == "colgroup"):
self.tableColCountFound = True
if(self.tableColCountFound and tag == "col"):
self.tables[-1].grid.columnNum += 1
if(self.tableGridFound and tag == "thead"):
self.gridHeaderFound = True
if(self.gridHeaderFound and tag == "tr"):
self.tables[-1].grid.header.append(Table.Grid.Row())
if(self.gridHeaderFound and tag == "th"):
self.cellFound = True
if(self.tableGridFound and tag == "tbody"):
self.gridBodyFound = True
if(self.gridBodyFound and tag == "tr"):
self.tables[-1].grid.body.append(Table.Grid.Row())
if(self.gridBodyFound and tag == "td"):
self.cellFound = True
if(self.cellFound and tag == "sup"):
self.cellSpace = True
if(self.cellFound and tag == "br"):
self.cell += " "
# handle table description
if(self.tableFound and (not self.tableCaptionFound) and (not self.tableGridFound) and tag == "div" and len(attrs) > 0):
for attr in attrs:
if(attr[0] == "class" and attr[1] == "NLM_table-wrap-foot"):
self.tableDescriptionFound = True
self.tableDescriptionDivCount += 1
return
if(self.tableDescriptionFound and tag == "div"):
self.tableDescriptionDivCount += 1
if(self.tableDescriptionFound and tag == "div" and len(attrs) > 0):
for attr in attrs:
if(attr[0] == "class" and attr[1] == "footnote"):
self.tableFootnoteFound = True
self.tables[-1].descriptions.append("")
if(self.tableFootnoteFound and tag in ["sup", "a"]):
self.disableRead = True
def handle_data(self, data):
if(self.disableRead):
return
if(self.cellSpace):
self.cell += " "
return
if(self.authorFound):
self.authorArr.append(data)
if(self.dateFound):
index = data.find(",")
self.year = int(data[index + 1 :].strip())
if(self.institutionFound):
self.institution[-1] += data
if(self.citationNumber):
self.paperCited = int(data)
if(self.doiLink):
index = data.find("https://doi.org/")
if(index != -1):
self.doi = data[16:]
# handle title and abstract
if(self.textFound):
self.abstractText += data
self.abstractBoldText += data
if(self.titleText):
self.title += data
if(self.boldTextFound):
self.boldAbstractTextArr.append(data)
# handle body text
# found a new section, append the section to bodyText
if(self.newSectionFound):
section = BodyText.Section(data)
self.bodyText.sections.append(section)
self.newSectionFound = False
# ignore any content after references
if(data == "References"):
exitParser(self)
if(self.paragraphFound):
self.paragraphText += data
self.boldParagraphText += data
if(self.paragraphHeaderFound):
self.paragraphHeader += data
# handle Tables
if(self.tableCaptionFound):
self.tableCaption += data
if(self.gridHeaderFound and self.cellFound):
self.cell += data
if(self.gridBodyFound and self.cellFound):
self.cell += data
if(self.tableFootnoteFound):
self.tables[-1].descriptions[-1] += data
def handle_endtag(self, tag):
if(self.authorFound and tag == "span"):
self.authorFound = False
if(self.dateFound and tag == "span"):
self.dateFound = False
if(self.institutionFound and tag == "span"):
self.institutionFound = False
if(self.citationFound and tag == "div" and self.citationDivCount == 1):
self.citationDivCount -= 1
self.citationFound = False
elif(self.citationFound and tag == "div" and self.citationDivCount > 1):
self.citationDivCount -= 1
if(self.citationNumber and tag == "a"):
self.citationNumber = False
if(self.doiFound and tag == "div"):
self.doiFound = False
if(self.doiLink and tag == "a"):
self.doiLink = False
# handle title and abstract
if(self.disableRead):
self.disableRead = False
if(self.cellSpace):
self.cellSpace = False
if(self.figureFound and tag == "figure"):
self.figureFound = False
if(self.textFound and tag == "p"):
self.textFound = False
if(self.titleFound and tag == "h1"):
self.titleFound = False
if(self.titleText and tag == "span"):
self.titleText = False
if(self.boldTextFound and tag == "b"):
self.boldTextFound = False
self.abstractBoldText += "</b>"
# handle body text
if(self.boldParagraphFound and tag == "b"):
self.boldParagraphText += " </b>"
self.boldParagraphFound = False
if(self.sectionTitleFound and tag == "div"):
self.sectionTitleFound = False
# found the end of a paragraph, append the content to the last section
if(self.paragraphFound and tag == "div" and self.paragraphDivCount == 1):
if(len(self.bodyText.sections) == 0):
newSection = BodyText.Section("")
self.bodyText.sections.append(newSection)
if(len(self.bodyText.sections[-1].paragraphs) == 0):
newParagraph = BodyText.Section.Paragraph()
self.bodyText.sections[-1].paragraphs.append(newParagraph)
self.bodyText.sections[-1].paragraphs[-1].contents.append(self.paragraphText)
self.bodyText.sections[-1].paragraphs[-1].boldContents.append(self.boldParagraphText)
self.paragraphText = ""
self.boldParagraphText = ""
self.paragraphFound = False
self.paragraphDivCount -= 1
elif(self.paragraphFound and tag == "div" and self.paragraphDivCount > 1):
self.paragraphDivCount -= 1
# found a paragraph header, append a new paragraph with header to the last section
if(self.paragraphHeaderFound and tag == "h3"):
self.paragraphHeaderFound = False
newParagraph = BodyText.Section.Paragraph(self.paragraphHeader)
self.bodyText.sections[-1].paragraphs.append(newParagraph)
self.paragraphHeader = ""
if(self.paragraphBoldFound and tag == "b"):
self.paragraphBoldText += "</b>"
self.paragraphBoldText = ""
self.paragraphBoldFound = False
# handle table caption
if(self.tableFound and tag == "div" and self.tableDivCount == 1):
self.tableFound = False
self.tableDivCount -= 1
elif(self.tableFound and tag == "div" and self.tableDivCount > 1):
self.tableDivCount -= 1
if(self.tableCaptionFound and tag == "div" and self.tableCaptionDivCount == 1):
self.tableCaptionFound = False
self.tableCaptionDivCount -= 1
self.tables[-1].caption = self.tableCaption
self.tableCaption = ""
elif(self.tableCaptionFound and tag == "div" and self.tableCaptionDivCount > 1):
self.tableCaptionDivCount -= 1
# handle table grip
if(self.tableGridFound and tag == "table"):
self.tableGridFound = False
if(self.tableColCountFound and tag == "colgroup"):
self.tableColCountFound = False
if(self.gridHeaderFound and tag == "thead"):
self.gridHeaderFound = False
if(self.gridHeaderFound and tag == "th" and self.cellFound):
self.tables[-1].grid.header[-1].cells.append(self.cell)
self.cell = ""
self.cellFound = False
if(self.gridBodyFound and tag == "tbody"):
self.gridBodyFound = False
if(self.gridBodyFound and tag == "td" and self.cellFound):
self.tables[-1].grid.body[-1].cells.append(self.cell)
self.cell = ""
self.cellFound = False
# handle table description
if(self.tableDescriptionFound and self.tableDescriptionDivCount == 1 and tag == "div"):
self.tableDescriptionDivCount -= 1
self.tableDescriptionFound = False
elif(self.tableDescriptionFound and tag == "div" and self.tableDescriptionDivCount > 1):
self.tableDescriptionDivCount -= 1
if(self.tableFootnoteFound and tag == "div"):
self.tableFootnoteFound = False
# --------------------------------------------------------------------------------------------------------------
def __init__(self, articleURL):
self.articleURL = articleURL
self.authorArr = []
self.year = -1
self.instituition = ""
self.paperCited = -1
self.doi = ""
self.journal = ""
# fullname and abbreviation is used in ic50 extraction in abstract image
# stores the fullname of the target gene, omit number, e.g. if target is "jak1", fullname is "janus kinase"
self.FULLNAME = ""
# stores the abbreviation of the target gene, omit number, e.g. if target is "jak1", abbreviation is "jak"
self.ABBREVIATION = ""
# Target name of the article's focus
self.focusedTarget = ""
self.tableParser = None
# hold title content after parsing html file
self.titleText = ""
# hold links to abstract images after parsing html file
self.imgArr = []
# hold abstract content after parsing html file
self.abstractText = ""
# BodyText object for holding body text
self.bodyText = None
# Table object for holding tables
self.tables = None
# hold the molecule name
self.molecule = ""
# hold the compound name
self.compound = ""
# hold the ic50 value
self.ic50Value = ""
# Arr variables provide additional and alternative information, in case the identified molecule, compound, ic50value are incorrect
# hold all identified molecule names
self.moleculeArr = []
# hold all identified compound names
self.compoundArr = []
# hold all identified ic50 values
self.ic50Arr = []
self.enzymeKeywords = [self.ABBREVIATION, self.FULLNAME, "enzyme", "enzymatic"]
self.cellKeywords = ["cell", "cellar"]
self.compoundKeywords = ["compound", "no", "id", "compd", "cpd", "cmp"]
self.enzymeIc50 = ""
self.cellIc50 = ""
self.enzymeKi = ""
self.cellKi = ""
self.enzymeKd = ""
self.cellKd = ""
self.enzymeSelectivity = ""
self.cellSelectivity = ""
self.cellSolubility = ""
self.vivoSolubility = ""
self.ec50 = ""
self.ed50 = ""
self.auc = ""
self.herg = ""
self.tHalf = ""
self.bioavailability = ""
self.retrieve_values()
def retrieve_values(self):
self.get_FULLNAME_ABBREVIATION()
self.retrieve_article_information()
self.retrieve_target()
positionResult = self.retrieve_image_text()
self.get_ic50_from_image(positionResult)
self.get_compound_from_image(positionResult)
self.get_molecule_from_title_abstract()
self.get_compound_from_abstract()
self.get_ic50_from_abstract()
self.get_multiple_values_from_body()
self.get_single_value_from_body()
def get_FULLNAME_ABBREVIATION(self):
# trim the number at the end of TARGET
i = len(ACS.TARGET) - 1
while(i >= 0):
if(not ACS.TARGET[i].isalpha()):
i -= 1
else:
break
queryTarget = ACS.TARGET[:i + 1]
# target name identification is performed through an online database: http://allie.dbcls.jp/
# at this point, the user might input a fullname or an abbreviation, so it needs to be queried twice
# queryLongUrl: treat the input as a fullname, find abbreviation
queryLongUrl = f"https://allie.dbcls.jp/long/exact/Any/{queryTarget.lower()}.html"
# queryShortUrl: treat the input as an abbreviation, find fullname
queryShortUrl = f"https://allie.dbcls.jp/short/exact/Any/{queryTarget.lower()}.html"
longResponse = requests.get(queryLongUrl)
shortReponse = requests.get(queryShortUrl)
longParser = ACS.ACSArticle.TargetParser()
shortParser = ACS.ACSArticle.TargetParser()
try:
longParser.feed(longResponse.text)
except AssertionError as ae:
pass
try:
shortParser.feed(shortReponse.text)
except AssertionError as ae:
pass
longForm = shortParser.result.lower().strip()