File size: 37,161 Bytes
69fb171 |
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 |
/**
* Project: GNormPlus
* Function: Gene Normalization
*/
package GNormPluslib;
import bioc.BioCAnnotation;
import bioc.BioCCollection;
import bioc.BioCDocument;
import bioc.BioCLocation;
import bioc.BioCPassage;
import bioc.io.BioCDocumentWriter;
import bioc.io.BioCFactory;
import bioc.io.woodstox.ConnectorWoodstox;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.BreakIterator;
import java.time.LocalDate;
import java.time.ZoneId;
import java.text.DecimalFormat;
import java.math.RoundingMode;
import javax.xml.stream.XMLStreamException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
public class GN
{
public static HashMap<String, String> MatchedTokens_hash = new HashMap<String, String>();
private double ScoringFunction(String geneid,HashMap<String,String> Mention_hash,String LF)
{
/*
* define gene/homo id
*/
//LF
LF = LF.toLowerCase();
LF = LF.replaceAll("([0-9])([a-z])", "$1 $2");
LF = LF.replaceAll("([a-z])([0-9])", "$1 $2");
LF = LF.replaceAll("([\\W\\-\\_])", " ");
LF = LF.replaceAll("[ ]+", " ");
String LF_tkn[]=LF.split(" ");
int LF_ParticalMatch = 0;
Pattern ptmp = Pattern.compile("[0-9]+\\-([0-9]+)");
Matcher mtmp = ptmp.matcher(geneid);
Pattern ptmp2 = Pattern.compile("([0-9]+)");
Matcher mtmp2 = ptmp.matcher(geneid);
if(mtmp.find())
{
geneid = "Homo:"+mtmp.group(1);
}
else
{
geneid = "Gene:"+geneid;
}
if(GNormPlus.GeneScoring_hash.containsKey(geneid))
{
HashMap<String,Double> TF = new HashMap<String,Double>(); // token i in gene j
HashMap<String,Double> TermFrequency = new HashMap<String,Double>();
/*
* Tokens in Query (Gene id lexicon)
*/
String l[]=GNormPlus.GeneScoring_hash.get(geneid).split("\t"); // Gene:2664293 cmk-1,cytidylate-1,kinase-1,mssa-1 0.4096 4 0.0625 1 2.0
String tkns_Gene[] = l[0].split(",");
for(int i=0;i<tkns_Gene.length;i++)
{
String Tkn_Freq[] = tkns_Gene[i].split("-");
TermFrequency.put(Tkn_Freq[0], Double.parseDouble(Tkn_Freq[1]));
}
Double Cj = Double.parseDouble(l[1]);
Double AllTknNum = Double.parseDouble(l[2]);
//Double Cj_max = Double.parseDouble(l[3]);
//Double MaxTknNum = Double.parseDouble(l[4]);
Double Norm = Double.parseDouble(l[5]);
if(Norm == 0.0){Norm=1.0;}
/*
* Tokens in Document (recognized mentions)
*/
for(String Mention : Mention_hash.keySet())
{
Mention = Mention.toLowerCase();
Mention = Mention.replaceAll("([0-9])([a-z])", "$1 $2");
Mention = Mention.replaceAll("([a-z])([0-9])", "$1 $2");
Mention = Mention.replaceAll("([\\W\\-\\_])", " ");
Mention = Mention.replaceAll("[ ]+", " ");
String tkns_Mention[]=Mention.split(" ");
for(int i=0;i<tkns_Mention.length;i++)
{
if(TermFrequency.containsKey(tkns_Mention[i]))
{
TF.put(tkns_Mention[i], TermFrequency.get(tkns_Mention[i]));
}
}
}
Double score=0.0;
for(String Tkn : TF.keySet())
{
//LF
for(int t=0;t<LF_tkn.length;t++)
{
if(LF_tkn[t].equals(Tkn))
{
LF_ParticalMatch++;
}
}
double TFij = TF.get(Tkn)/AllTknNum;
double IDFi=GNormPlus.GeneScoringDF_hash.get(Tkn);
score=score+TFij*IDFi*(1/(1-TFij));
}
//score = Cj * (1/Norm) *score;
if(LF_ParticalMatch>0){score = score + LF_ParticalMatch;/*System.out.println(geneid+"\t"+LF+"\t"+score);*/}
return score;
}
else
{
//System.out.println("Error: cannot find geneid: "+geneid+" in GeneScoring_hash");
return 0.0;
}
}
public void PreProcessing4GN(String Filename,String FilenameBioC) throws IOException, XMLStreamException
{
for (int i = 0; i < GNormPlus.BioCDocobj.Annotations.size(); i++)
{
for (int j = 0; j < GNormPlus.BioCDocobj.Annotations.get(i).size(); j++)
{
for (int k = 0; k < GNormPlus.BioCDocobj.Annotations.get(i).get(j).size(); k++)
{
String anno[] = GNormPlus.BioCDocobj.Annotations.get(i).get(j).get(k).split("\t");
String start=anno[0];
String last=anno[1];
String mentions=anno[2];
String type=anno[3];
String id="";
if(anno.length>=5)
{
id=anno[4];
}
if(type.equals("Gene"))
{
String mentionArr[] = mentions.split("\\|");
boolean update=false;
for(int m=0;m<mentionArr.length;m++)
{
Pattern ptmp = Pattern.compile("^(.*[0-9A-Z])[ ]*p$");
Matcher mtmp = ptmp.matcher(mentionArr[m]);
Pattern ptmp2 = Pattern.compile("^(.+)nu$");
Matcher mtmp2 = ptmp2.matcher(mentionArr[m]);
Pattern ptmp3 = Pattern.compile("^(.*)alpha(.*)$");
Matcher mtmp3 = ptmp3.matcher(mentionArr[m]);
Pattern ptmp4 = Pattern.compile("^(.*)beta(.*)$");
Matcher mtmp4 = ptmp4.matcher(mentionArr[m]);
Pattern ptmp5 = Pattern.compile("^(.+[0-9])a$");
Matcher mtmp5 = ptmp5.matcher(mentionArr[m]);
Pattern ptmp6 = Pattern.compile("^(.+[0-9])b$");
Matcher mtmp6 = ptmp6.matcher(mentionArr[m]);
Pattern ptmp7 = Pattern.compile("^(.+)II([a-z])$");
Matcher mtmp7 = ptmp7.matcher(mentionArr[m]);
Pattern ptmp8 = Pattern.compile("^(.+)III([a-z])$");
Matcher mtmp8 = ptmp8.matcher(mentionArr[m]);
if(mtmp.find())
{
mentions=mentions+"|"+mtmp.group(1);
update=true;
}
if(mtmp2.find())
{
mentions=mentions+"|"+mtmp2.group(1);
update=true;
}
if(mtmp3.find())
{
mentions=mentions+"|"+mtmp3.group(1)+"a"+mtmp3.group(2);
update=true;
}
if(mtmp4.find())
{
mentions=mentions+"|"+mtmp4.group(1)+"b"+mtmp4.group(2);
update=true;
}
if(mtmp5.find())
{
mentions=mentions+"|"+mtmp5.group(1)+"alpha";
update=true;
}
if(mtmp6.find())
{
mentions=mentions+"|"+mtmp6.group(1)+"beta";
update=true;
}
if(mtmp7.find())
{
mentions=mentions+"|"+mtmp7.group(1)+"2"+mtmp7.group(2);
update=true;
}
if(mtmp8.find())
{
mentions=mentions+"|"+mtmp8.group(1)+"3"+mtmp8.group(2);
update=true;
}
}
if(update == true)
{
GNormPlus.BioCDocobj.Annotations.get(i).get(j).set(k, start + "\t" + last + "\t" + mentions + "\t" + type + "\t" + id );
}
}
}
}
}
//GNormPlus.BioCDocobj.BioCOutput(Filename,FilenameBioC,GNormPlus.BioCDocobj.Annotations,false,true);
}
public void ChromosomeRecognition(String Filename,String FilenameBioC) throws IOException, XMLStreamException
{
for (int i = 0; i < GNormPlus.BioCDocobj.PMIDs.size(); i++) /** PMIDs : i */
{
String Pmid = GNormPlus.BioCDocobj.PMIDs.get(i);
for (int j = 0; j < GNormPlus.BioCDocobj.PassageNames.get(i).size(); j++) /** Paragraphs : j */
{
String PassageContext = GNormPlus.BioCDocobj.PassageContexts.get(i).get(j); // Passage context
/** Chromosome recognition */
ArrayList<String> locations = GNormPlus.PT_GeneChromosome.SearchMentionLocation(PassageContext,"ChromosomeLocation");
for (int k = 0 ; k < locations.size() ; k++)
{
String anno[]=locations.get(k).split("\t");
//int start= Integer.parseInt(anno[0]);
//int last= Integer.parseInt(anno[1]);
//String mention = anno[2];
String ids = anno[3];
//GNormPlus.BioCDocobj.Annotations.get(i).get(j).add(start+"\t"+last+"\t"+mention+"\tChromosomeLocation\t"+ids); //paragraph
String IDs[] = ids.split("[\\|,]");
for(int idcount=0;idcount<IDs.length;idcount++)
{
//IDs[idcount] = IDs[idcount].replaceAll("\\-[0-9]+", "");
GNormPlus.Pmid2ChromosomeGene_hash.put(Pmid+"\t"+IDs[idcount],"");
}
}
}
}
//GNormPlus.BioCDocobj.BioCOutput(Filename,FilenameBioC,GNormPlus.BioCDocobj.Annotations,false,true);
}
public void GeneNormalization(String Filename,String FilenameBioC,boolean GeneIDMatch) throws IOException, XMLStreamException
{
final DecimalFormat df = new DecimalFormat("0.####");
df.setRoundingMode(RoundingMode.HALF_UP);
//Tokenization
for (int i = 0; i < GNormPlus.BioCDocobj.Annotations.size(); i++) /** PMIDs : i */
{
String Pmid = GNormPlus.BioCDocobj.PMIDs.get(i);
/** Species */
HashMap<String,String> Species_hash = new HashMap<String,String>();
for (int j = 0; j < GNormPlus.BioCDocobj.Annotations.get(i).size(); j++) /** Paragraphs : j */
{
for (int k = 0; k < GNormPlus.BioCDocobj.Annotations.get(i).get(j).size(); k++) /** Annotation : k */
{
String anno[] = GNormPlus.BioCDocobj.Annotations.get(i).get(j).get(k).split("\t");
String mentions=anno[2];
String type=anno[3];
if(type.matches("(Species|Genus|Strain|CellLine|Cell)"))
{
Species_hash.put(mentions,"");
}
}
}
/*
* Collect Gene mentions :
*
* GeneMention-taxid -> "ID" : geneid
* -> "type" : "Gene"
* -> start1-last1 : ""
* -> start2-last2 : ""
* -> start3-last3 : ""
*/
String tiabs="";
for (int j = 0; j < GNormPlus.BioCDocobj.PassageContexts.get(i).size(); j++) /** Paragraphs : j */
{
tiabs=tiabs+GNormPlus.BioCDocobj.PassageContexts.get(i).get(j).toLowerCase();
}
HashMap<String,HashMap<String,String>> GeneMention_hash = new HashMap<String,HashMap<String,String>>();
HashMap<String,String> Mention_hash = new HashMap<String,String>();
for (int j = 0; j < GNormPlus.BioCDocobj.Annotations.get(i).size(); j++) /** Paragraphs : j */
{
for (int k = 0; k < GNormPlus.BioCDocobj.Annotations.get(i).get(j).size(); k++) /** Annotation : k */
{
String anno[] = GNormPlus.BioCDocobj.Annotations.get(i).get(j).get(k).split("\t");
String start=anno[0];
String last=anno[1];
String mentions=anno[2];
String type=anno[3];
String taxids="Tax:9606";
if(anno.length>=5)
{
taxids=anno[4];
}
String mentions_tmp=mentions.toLowerCase();
mentions_tmp=mentions_tmp.replaceAll("[\\W\\-\\_]","");
mentions_tmp=mentions_tmp.replaceAll("[0-9]","0");
taxids=taxids.replaceAll("(Focus|Right|Left|Prefix|Tax):","");
if(taxids.equals(""))
{
taxids="9606";
}
/** Filtering */
boolean found_filter = false;
if(GNormPlus.Filtering_hash.containsKey(mentions_tmp)) // filtering
{
found_filter=true;
}
if(found_filter==false) //abbreviation
{
for(String f : GNormPlus.Filtering_WithLongForm_hash.keySet())
{
if( GNormPlus.BioCDocobj.Annotations.get(i).get(j).get(k).matches(".*[\\t\\|]"+f+"\tGene.*") ||
GNormPlus.BioCDocobj.Annotations.get(i).get(j).get(k).matches(".*\\t"+f+"\\|[^\t]+\tGene.*")
)
{
String lf=GNormPlus.Filtering_WithLongForm_hash.get(f);
if(tiabs.matches(".*"+lf+".*"))
{
found_filter=true;
break;
}
}
}
}
if(found_filter==false)
{
if( GNormPlus.BioCDocobj.Annotations.get(i).get(j).get(k).matches(".*[\\t\\|][a-z]\tGene.*") ||
GNormPlus.BioCDocobj.Annotations.get(i).get(j).get(k).matches(".*\\t[a-z]\\|[^\t]+\tGene.*") //32171191 Wuhan's
)
{
found_filter=true;
}
}
if(found_filter == false)
{
if(type.matches("Gene"))
{
if(GeneMention_hash.containsKey(mentions+"\t"+taxids))
{
GeneMention_hash.get(mentions+"\t"+taxids).put(start+"\t"+last,"");
}
else
{
HashMap<String,String> offset_hash = new HashMap<String,String>();
offset_hash.put(start+"\t"+last,"");
GeneMention_hash.put(mentions+"\t"+taxids, offset_hash);
GeneMention_hash.get(mentions+"\t"+taxids).put("type", type);
Mention_hash.put(mentions,"Gene");
}
}
else if(type.matches("(FamilyName|DomainMotif)"))
{
String GMs[]=mentions.split("\\|");
for(int g=0;g<GMs.length;g++)
{
String mention = GMs[g];
Mention_hash.put(mention,"FamilyDomain");
}
}
}
}
}
/*
* Gene id refinement:
* 1. Official name
* 2. only one gene
*/
HashMap<String,String> GuaranteedGene2ID = new HashMap<String,String>();
HashMap<String,String> MultiGene2ID = new HashMap<String,String>();
for(String GeneMentionTax : GeneMention_hash.keySet())
{
String GT[]=GeneMentionTax.split("\\t");
String mentions=GT[0];
String taxids=GT[1];
String GMs[]=mentions.split("\\|");
HashMap<String,String> taxids_hash = new HashMap<String,String>();
String taxids_arr[]=taxids.split(",");
for(int t=0;t<taxids_arr.length;t++)
{
taxids_hash.put(taxids_arr[t], "");
}
for(int ms=0;ms<GMs.length;ms++)
{
String mention = GMs[ms];
String IDstr = GNormPlus.PT_Gene.MentionMatch(mention); /** searched by PT_Gene */
String IDs[]=IDstr.split("\\|");
/*
* printing the ambiguous gene mentions and candidates
*/
//String IDs_s[]=IDstr.split(",");
//if(IDs_s.length>1)
//{
// System.out.println(Pmid+"\t"+mention+"\t"+mentions+"\t"+IDstr);
//}
for(int c=0;c<IDs.length;c++)
{
String tax2ID[]=IDs[c].split(":"); // tax2ID[0] = taxid ; tax2ID[1] = geneids
if(taxids_hash.containsKey(tax2ID[0]))
{
String geneid=tax2ID[1];
String TargetTax=tax2ID[0];
GeneMention_hash.get(GeneMentionTax).put("ID", geneid);
GeneMention_hash.get(GeneMentionTax).put("TargetTax", TargetTax);
break;
}
}
//geneid refinement
if(GeneMention_hash.get(GeneMentionTax).containsKey("ID"))
{
Pattern ptmp = Pattern.compile("\\*([0-9]+(\\-[0-9]+|))");
Matcher mtmp = ptmp.matcher(GeneMention_hash.get(GeneMentionTax).get("ID"));
if(mtmp.find()) // 1. Official Name
{
GeneMention_hash.get(GeneMentionTax).put("ID",mtmp.group(1));
GuaranteedGene2ID.put(GeneMentionTax,mtmp.group(1));
}
else if(GeneMention_hash.get(GeneMentionTax).get("ID").matches("[0-9]+(\\-[0-9]+|)")) // 2. only one gene
{
GuaranteedGene2ID.put(GeneMentionTax,GeneMention_hash.get(GeneMentionTax).get("ID"));
}
else
{
String ID[] = GeneMention_hash.get(GeneMentionTax).get("ID").split(",");
boolean FoundByChroLoca=false;
for(int idcount=0;idcount<ID.length;idcount++)
{
if(GNormPlus.Pmid2ChromosomeGene_hash.containsKey(Pmid+"\t"+ID[idcount])) // 3. Chromosome location
{
GuaranteedGene2ID.put(GeneMentionTax,ID[idcount]);
FoundByChroLoca=true;
break;
}
}
if(FoundByChroLoca == false)
{
MultiGene2ID.put(GeneMentionTax, GeneMention_hash.get(GeneMentionTax).get("ID"));
}
}
}
if(GNormPlus.suffixprefix_orig2modified.containsKey(mention) && (!IDstr.equals("-1")) && (!IDstr.equals("-2")) && (!IDstr.equals("-3")))
{
break;
}
}
}
/*
* Gene id refinement:
* 3. multiple genes but can be inferred by 1. and 2.
*/
for(String GeneMentionTax_M : MultiGene2ID.keySet())
{
for(String GeneMentionTax_G : GuaranteedGene2ID.keySet())
{
String MG[] = MultiGene2ID.get(GeneMentionTax_M).split(",");
for(int m=0;m<MG.length;m++)
{
if(MG[m].equals(GuaranteedGene2ID.get(GeneMentionTax_G)))
{
GeneMention_hash.get(GeneMentionTax_M).put("ID",MG[m]);
}
}
}
}
/*
* Gene id refinement:
* 4. FullName -> Abbreviation
*/
for(String GeneMentionTax : GeneMention_hash.keySet())
{
String MT[] = GeneMentionTax.split("\\t");
if(GNormPlus.PmidLF2Abb_hash.containsKey(Pmid+"\t"+MT[0]))
{
String GeneMentionTax_Abb = GNormPlus.PmidLF2Abb_hash.get(Pmid+"\t"+MT[0]) + "\t" + MT[1];
if(GeneMention_hash.containsKey(GeneMentionTax_Abb) && GeneMention_hash.get(GeneMentionTax).containsKey("ID"))
{
GeneMention_hash.get(GeneMentionTax_Abb).put("ID", GeneMention_hash.get(GeneMentionTax).get("ID"));
}
}
}
/*
* Gene id refinement:
* 5. Ranking by scoring function (inference network)
*/
for(String GeneMentionTax : GeneMention_hash.keySet())
{
if(GeneMention_hash.get(GeneMentionTax).containsKey("ID") && GeneMention_hash.get(GeneMentionTax).get("ID").matches(".+,.+"))
{
String geneids=GeneMention_hash.get(GeneMentionTax).get("ID");
String geneid[] = geneids.split(",");
String OutputStyle="Top1";
if(OutputStyle.equals("Top1"))
{
//only return the best one
double max_score=0.0;
String target_geneid="";
for(int g=0;g<geneid.length;g++)
{
String MT[] = GeneMentionTax.split("\\t");
String LF="";
if(GNormPlus.PmidAbb2LF_hash.containsKey(Pmid+"\t"+MT[0]))
{
LF = GNormPlus.PmidAbb2LF_hash.get(Pmid+"\t"+MT[0]);
}
double score = ScoringFunction(geneid[g],Mention_hash,LF);
if(score>max_score)
{
max_score=score;
target_geneid=geneid[g];
}
else if(score == 0.0)
{
//System.out.println(GeneMentionTax);
}
}
GeneMention_hash.get(GeneMentionTax).put("ID", target_geneid);
}
else // "All"
{
//return all geneids
String geneSTR="";
for(int g=0;g<geneid.length;g++)
{
String MT[] = GeneMentionTax.split("\\t");
String LF="";
if(GNormPlus.PmidAbb2LF_hash.containsKey(Pmid+"\t"+MT[0]))
{
LF = GNormPlus.PmidAbb2LF_hash.get(Pmid+"\t"+MT[0]);
}
double score = ScoringFunction(geneid[g],Mention_hash,LF);
String hoge = df.format(score);
score=Double.parseDouble(hoge);
if(geneSTR.equals(""))
{
geneSTR=geneid[g]+"-"+score;
}
else
{
geneSTR=geneSTR+","+geneid[g]+"-"+score;
}
}
GeneMention_hash.get(GeneMentionTax).put("ID", geneSTR);
}
}
}
/*
* Gene id refinement: - removed (Reason: cause too much False Positive)
* 6. Abbreviation -> FullName
*
*/
for(String GeneMentionTax : GeneMention_hash.keySet())
{
String MT[] = GeneMentionTax.split("\\t");
if(GNormPlus.PmidAbb2LF_hash.containsKey(Pmid+"\t"+MT[0]))
{
String GeneMentionTax_LF = GNormPlus.PmidAbb2LF_hash.get(Pmid+"\t"+MT[0]) + "\t" + MT[1];
if(GeneMention_hash.containsKey(GeneMentionTax_LF) && GeneMention_hash.get(GeneMentionTax).containsKey("ID"))
{
GeneMention_hash.get(GeneMentionTax_LF).put("ID", GeneMention_hash.get(GeneMentionTax).get("ID"));
}
}
}
/*
* Gene id refinement:
* 7. The inference network tokens of Abbreviation.ID should contain at least LF tokens
* 8. The short mention should be filtered if not long form support
*/
ArrayList<String> removeGMT = new ArrayList<String>();
for(String GeneMentionTax : GeneMention_hash.keySet())
{
String GT[]=GeneMentionTax.split("\\t");
String mentions=GT[0];
String tax=GT[1];
if(GeneMention_hash.get(GeneMentionTax).containsKey("type") && GeneMention_hash.get(GeneMentionTax).get("type").equals("Gene") && GeneMention_hash.get(GeneMentionTax).containsKey("ID"))
{
String type = GeneMention_hash.get(GeneMentionTax).get("type");
String id = GeneMention_hash.get(GeneMentionTax).get("ID");
String geneid="";
Pattern ptmp1 = Pattern.compile("^([0-9]+)\\-([0-9]+)$");
Pattern ptmp2 = Pattern.compile("^([0-9]+)$");
Matcher mtmp1 = ptmp1.matcher(id);
Matcher mtmp2 = ptmp2.matcher(id);
//System.out.println(id);
if(mtmp1.find())
{
geneid = "Homo:"+mtmp1.group(2);
}
else if(mtmp2.find())
{
geneid = "Gene:"+mtmp2.group(1);
}
boolean LongFormTknMatch= false;
boolean LongFormExist= true;
if(GNormPlus.GeneScoring_hash.containsKey(geneid))
{
if(GNormPlus.PmidAbb2LF_lc_hash.containsKey(Pmid+"\t"+mentions.toLowerCase()))
{
/*
* token in lexicon : tkn_lexicon
* token in mention : tkn_mention
*/
String l[]=GNormPlus.GeneScoring_hash.get(geneid).split("\t"); // Gene:2664293 cmk-1,cytidylate-1,kinase-1,mssa-1 0.4096 4 0.0625 1 2.0
String tkns_Gene[] = l[0].split(",");
ArrayList<String> tkn_lexicon = new ArrayList<String>();
for(int ti=0;ti<tkns_Gene.length;ti++)
{
String Tkn_Freq[] = tkns_Gene[ti].split("-");
tkn_lexicon.add(Tkn_Freq[0]);
}
String LF_lc=GNormPlus.PmidAbb2LF_lc_hash.get(Pmid+"\t"+mentions.toLowerCase());
LF_lc = LF_lc.replaceAll("([0-9])([A-Za-z])", "$1 $2");
LF_lc = LF_lc.replaceAll("([A-Za-z])([0-9])", "$1 $2");
String tkn_mention[] = LF_lc.split("[\\W\\-\\_]");
for(int tl=0;tl<tkn_lexicon.size();tl++)
{
for(int tm=0;tm<tkn_mention.length;tm++)
{
if(tkn_lexicon.get(tl).equals(tkn_mention[tm]) && (!tkn_mention[tm].matches("[0-9]+")))
{
LongFormTknMatch = true;
}
}
}
}
else{LongFormExist = false;}
}
else{LongFormTknMatch = true;} // exception
if(LongFormTknMatch == false && LongFormExist == true) // 7.
{
removeGMT.add(GeneMentionTax); //remove short form
removeGMT.add(GNormPlus.PmidAbb2LF_hash.get(Pmid+"\t"+mentions)+"\t"+tax); //remove long form
}
else if(mentions.length()<=2 && LongFormExist == false) // 8.
{
removeGMT.add(GeneMentionTax);
}
}
}
for(int gmti=0;gmti<removeGMT.size();gmti++) // remove
{
GeneMention_hash.remove(removeGMT.get(gmti));
}
// Append gene ids
for (int j = 0; j < GNormPlus.BioCDocobj.Annotations.get(i).size(); j++) // Paragraphs : j
{
for (int k = 0; k < GNormPlus.BioCDocobj.Annotations.get(i).get(j).size(); k++) // Annotation : k
{
String anno[] = GNormPlus.BioCDocobj.Annotations.get(i).get(j).get(k).split("\t");
String start=anno[0];
String last=anno[1];
String mentions=anno[2];
String type=anno[3];
String taxid_org="Tax:9606";
if(anno.length>=5)
{
taxid_org=anno[4];
}
String taxids=taxid_org.replaceAll("(Focus|Right|Left|Prefix|Tax):","");
String GMs[]=mentions.split("\\|");
if(GeneMention_hash.containsKey(mentions+"\t"+taxids) && GeneMention_hash.get(mentions+"\t"+taxids).containsKey("TargetTax"))
{
String taxtype=taxid_org.replaceAll(":([0-9,]+)","");
String taxid=GeneMention_hash.get(mentions+"\t"+taxids).get("TargetTax");
GNormPlus.BioCDocobj.Annotations.get(i).get(j).set(k, start+"\t"+last+"\t"+mentions+"\t"+type+"\t"+taxtype+":"+taxid);
}
if(type.equals("Gene"))
{
GNormPlus.BioCDocobj.Annotations.get(i).get(j).set(k, GNormPlus.BioCDocobj.Annotations.get(i).get(j).get(k) + "|");
if(GeneMention_hash.containsKey(mentions+"\t"+taxids) && GeneMention_hash.get(mentions+"\t"+taxids).containsKey("ID"))
{
GNormPlus.BioCDocobj.Annotations.get(i).get(j).set(k, GNormPlus.BioCDocobj.Annotations.get(i).get(j).get(k) + GeneMention_hash.get(mentions+"\t"+taxids).get("ID") + "," );
}
else // cannot find appropriate species
{
//System.out.println(mention+"\t"+taxid);
}
GNormPlus.BioCDocobj.Annotations.get(i).get(j).set(k, GNormPlus.BioCDocobj.Annotations.get(i).get(j).get(k).substring(0, GNormPlus.BioCDocobj.Annotations.get(i).get(j).get(k).length()-1)); // remove ",$"
}
}
}
//Extend to all gene mentions
HashMap<String,String> GeneMentions = new HashMap<String,String>(); // Extending Gene mentions
HashMap<String,String> GeneMentionLocation = new HashMap<String,String>(); // Extending Gene mentions
for(int j=0;j<GNormPlus.BioCDocobj.Annotations.get(i).size();j++) // Paragraph
{
for (int k = 0; k < GNormPlus.BioCDocobj.Annotations.get(i).get(j).size(); k++) // Annotation : k
{
String anno[] = GNormPlus.BioCDocobj.Annotations.get(i).get(j).get(k).split("\t");
int start = Integer.parseInt(anno[0]);
int last = Integer.parseInt(anno[1]);
String mentions=anno[2];
String type=anno[3];
String id="Tax:9606";
if(anno.length>=5)
{
id=anno[4];
}
if(type.equals("Gene") && id.matches("(Focus|Right|Left|Prefix|Tax)\\:([0-9]+)\\|([0-9]+)\\-([0-9]+)"))
{
GeneMentions.put(mentions.toLowerCase(), id);
for (int s=start ;s<=last;s++)
{
GeneMentionLocation.put(j+"\t"+s,"");
}
}
else if(type.equals("Gene") && id.matches("(Focus|Right|Left|Prefix|Tax)\\:([0-9]+)\\|([0-9]+)"))
{
GeneMentions.put(mentions.toLowerCase(), id);
for (int s=start ;s<=last;s++)
{
GeneMentionLocation.put(j+"\t"+s,"");
}
}
}
}
for(int j=0;j<GNormPlus.BioCDocobj.Annotations.get(i).size();j++) // Paragraph
{
if(GNormPlus.BioCDocobj.PassageContexts.size()>i && GNormPlus.BioCDocobj.PassageContexts.get(i).size()>j)
{
String PassageContexts = " " + GNormPlus.BioCDocobj.PassageContexts.get(i).get(j) + " ";
String PassageContexts_tmp = PassageContexts.toLowerCase();
for(String gm : GeneMentions.keySet())
{
String id = GeneMentions.get(gm);
if(gm.length()>=3)
{
gm = gm.replaceAll("[ ]*[\\|]*$", "");
gm = gm.replaceAll("^[\\|]*[ ]*", "");
gm = gm.replaceAll("[\\|][\\|]+", "\\|");
if(!gm.matches("[\\W\\-\\_]*"))
{
gm = gm.replaceAll("([^A-Za-z0-9\\| ])", "\\\\$1");
Pattern ptmp = Pattern.compile("^(.*[\\W\\-\\_])("+gm+")([\\W\\-\\_].*)$");
Matcher mtmp = ptmp.matcher(PassageContexts_tmp);
while(mtmp.find())
{
String pre = mtmp.group(1);
String gmtmp = mtmp.group(2);
String post = mtmp.group(3);
int start = pre.length()-1;
int last = start+gmtmp.length();
if(PassageContexts.length()>=last+1)
{
String mention = PassageContexts.substring(start+1,last+1);
if(!GeneMentionLocation.containsKey(j+"\t"+start) && !GeneMentionLocation.containsKey(j+"\t"+last))
{
GNormPlus.BioCDocobj.Annotations.get(i).get(j).add(start+"\t"+last+"\t"+mention+"\tGene\t"+id);
}
}
gmtmp = gmtmp.replaceAll(".", "\\@");
PassageContexts_tmp=pre+""+gmtmp+""+post;
mtmp = ptmp.matcher(PassageContexts_tmp);
}
}
}
}
}
}
//Apply to FamilyNames
HashMap<String,String> geneids = new HashMap<String,String>(); // Extending Gene mentions
for(int j=0;j<GNormPlus.BioCDocobj.Annotations.get(i).size();j++) // Paragraph
{
for (int k = 0; k < GNormPlus.BioCDocobj.Annotations.get(i).get(j).size(); k++) // Annotation : k
{
String anno[] = GNormPlus.BioCDocobj.Annotations.get(i).get(j).get(k).split("\t");
String type=anno[3];
if(type.equals("Gene"))
{
String id="Tax:9606";
if(anno.length>=5)
{
id=anno[4];
}
Pattern ptmp0 = Pattern.compile("^(Focus|Right|Left|Prefix|GeneID|Tax)\\:([0-9]+)\\|([0-9]+)$");
Matcher mtmp0 = ptmp0.matcher(id);
Pattern ptmp1 = Pattern.compile("^(Focus|Right|Left|Prefix|GeneID|Tax)\\:([0-9]+)\\|([0-9]+)\\-([0-9]+)$");
Matcher mtmp1 = ptmp1.matcher(id);
if(mtmp0.find())
{
geneids.put(mtmp0.group(3), "");
}
if(mtmp1.find())
{
geneids.put(mtmp1.group(3), "");
}
}
}
}
for(int j=0;j<GNormPlus.BioCDocobj.Annotations.get(i).size();j++) // Paragraph
{
for (int k = GNormPlus.BioCDocobj.Annotations.get(i).get(j).size()-1; k >=0 ; k--) // Annotation : k
{
String anno[] = GNormPlus.BioCDocobj.Annotations.get(i).get(j).get(k).split("\t");
String mention=anno[2];
String type=anno[3];
if(type.matches("(FamilyName|DomainMotif)"))
{
String id="Tax:9606";
if(anno.length>=5)
{
id=anno[4];
}
String IDstrs = GNormPlus.PT_FamilyName.MentionMatch(mention);
String IDstr[]=IDstrs.split("\\|");
String ids="";
for(int id_i=0;id_i<IDstr.length;id_i++)
{
if(geneids.containsKey(IDstr[id_i]))
{
if(ids.equals(""))
{
ids=IDstr[id_i];
}
else
{
ids=ids+";"+IDstr[id_i];
}
}
}
if(!ids.equals(""))
{
if(type.equals("FamilyName")){type="Gene";}
String Annotation_k=anno[0]+"\t"+anno[1]+"\t"+anno[2]+"\t"+type+"\tTax:9606";
if(anno.length>=5)
{
Annotation_k=anno[0]+"\t"+anno[1]+"\t"+anno[2]+"\t"+type+"\t"+anno[4];
}
GNormPlus.BioCDocobj.Annotations.get(i).get(j).set(k,Annotation_k+"|"+ids);
}
else
{
GNormPlus.BioCDocobj.Annotations.get(i).get(j).remove(k);
}
}
}
}
//Species "*" and "(anti)" removed.
for(int j=0;j<GNormPlus.BioCDocobj.Annotations.get(i).size();j++) // Paragraph
{
for (int k = GNormPlus.BioCDocobj.Annotations.get(i).get(j).size()-1; k >=0 ; k--) // Annotation : k
{
String anno[] = GNormPlus.BioCDocobj.Annotations.get(i).get(j).get(k).split("\t");
String type=anno[3];
if(type.equals("Species") || type.equals("Genus") || type.equals("Strain") || type.equals("CellLine") || type.equals("Cell"))
{
String id=anno[4];
id=id.replaceAll("\\*", "");
id=id.replaceAll("\\(anti\\)", "");
String Annotation_k=anno[0]+"\t"+anno[1]+"\t"+anno[2]+"\t"+type+"\t"+id;
GNormPlus.BioCDocobj.Annotations.get(i).get(j).set(k,Annotation_k);
}
}
}
for(int j=0;j<GNormPlus.BioCDocobj.Annotations.get(i).size();j++) // Paragraph
{
for (int k = GNormPlus.BioCDocobj.Annotations.get(i).get(j).size()-1; k >=0 ; k--) // Annotation : k
{
String anno[] = GNormPlus.BioCDocobj.Annotations.get(i).get(j).get(k).split("\t");
int start = Integer.parseInt(anno[0]);
int last = Integer.parseInt(anno[1]);
String mention = anno[2];
String type = anno[3];
String id = anno[4];
if(type.equals("Gene") && Species_hash.containsKey(mention))
{
GNormPlus.BioCDocobj.Annotations.get(i).get(j).remove(k);
}
else if(type.equals("Gene") && id.equals(""))
{
GNormPlus.BioCDocobj.Annotations.get(i).get(j).remove(k);
}
else
{
for (int k1 = GNormPlus.BioCDocobj.Annotations.get(i).get(j).size()-1; k1 >=0 ; k1--) // Annotation : k
{
if(k1 != k)
{
String anno1[] = GNormPlus.BioCDocobj.Annotations.get(i).get(j).get(k1).split("\t");
int start1 = Integer.parseInt(anno1[0]);
int last1 = Integer.parseInt(anno1[1]);
if((start1<start && last1>=last) || (start1<=start && last1>last))
{
GNormPlus.BioCDocobj.Annotations.get(i).get(j).remove(k);
break;
}
}
}
}
}
}
}
if(GeneIDMatch == true)
{
//GNormPlus.BioCDocobj.BioCOutput(Filename,FilenameBioC,GNormPlus.BioCDocobj.Annotations,false,true);
}
else
{
GNormPlus.BioCDocobj.BioCOutput(Filename,FilenameBioC,GNormPlus.BioCDocobj.Annotations,true,true);
}
}
/*
* Search Potential GeneID in the Prefix Tree
*/
public ArrayList<String> SearchGeneIDLocation(String Doc)
{
ArrayList<String> location = new ArrayList<String>();
String Doc_tmp=" "+Doc+" ";
Pattern ptmp = Pattern.compile("^(.*[^A-Za-z0-9]+)([0-9]+\\S*[A-Za-z]+|[A-Za-z]+\\S*[0-9]+|[0-9]+\\S*[A-Za-z]+\\S*[0-9]+|[A-Za-z]+\\S*[0-9]+\\S*[A-Za-z]+)([^A-Za-z0-9]+.*)$");
Matcher mtmp = ptmp.matcher(Doc_tmp);
while(mtmp.find())
{
String str1=mtmp.group(1);
String str2=mtmp.group(2);
String str3=mtmp.group(3);
for(int m=str1.length();m<=(str1.length()+str2.length());m++)
{
int start = str1.length()-1;
int last = start+str2.length();
String mention = Doc.substring(start, last);
if(!mention.matches(".*[\\'\\;\\[\\]\\+\\*\\\\].*"))
{
if(last-start>6 && (mention.matches(".*\\(.*\\).*") || mention.matches("[^\\(\\)]+")) )
{
Pattern ptmp1 = Pattern.compile("^(.+[^0-9])([0-9]+)\\-([0-9]+)$");
Matcher mtmp1 = ptmp1.matcher(mention);
Pattern ptmp2 = Pattern.compile("^(.+[^0-9])([0-9]+)\\-(.+[^0-9])([0-9]+)$");
Matcher mtmp2 = ptmp2.matcher(mention);
if(mtmp1.find())
{
String S1 = mtmp1.group(1);
if(mtmp1.group(2).length()<=6 && mtmp1.group(3).length()<=6)
{
int Num1 = Integer.parseInt(mtmp1.group(2));
int Num2 = Integer.parseInt(mtmp1.group(3));
String prefix = "";
Pattern ptmp3 = Pattern.compile("^([0]+)");
Matcher mtmp3 = ptmp3.matcher(mtmp1.group(2));
if(mtmp3.find())
{
prefix = mtmp3.group(1);
}
if(Num2-Num1>0 && (Num2-Num1<=20))
{
for(int n=Num1;n<=Num2;n++)
{
String StrNum=S1+prefix+n;
if(StrNum.length()>=5)
{
location.add(start+"\t"+last+"\t"+StrNum+"\tGeneID");
}
}
}
}
}
else if(mtmp2.find())
{
if(mtmp2.group(2).length()<=6 && mtmp2.group(4).length()<=6)
{
String S1 = mtmp2.group(1);
int Num1 = Integer.parseInt(mtmp2.group(2));
String S2 = mtmp2.group(3);
int Num2 = Integer.parseInt(mtmp2.group(4));
if(S1.equals(S2))
{
String prefix = "";
Pattern ptmp3 = Pattern.compile("^([0]+)");
Matcher mtmp3 = ptmp3.matcher(mtmp2.group(2));
if(mtmp3.find())
{
prefix = mtmp3.group(1);
}
if(Num2-Num1>0 && (Num2-Num1<=20))
{
for(int n=Num1;n<=Num2;n++)
{
String StrNum=S1+prefix+n;
if(StrNum.length()>=5)
{
location.add(start+"\t"+last+"\t"+StrNum+"\tGeneID");
}
}
}
}
}
}
}
location.add(start+"\t"+last+"\t"+mention+"\tGeneID");
}
}
String men="";
for(int m=0;m<str2.length();m++){men=men+"@";}
Doc_tmp=str1+men+str3;
mtmp = ptmp.matcher(Doc_tmp);
}
return location;
}
public void GeneIDRecognition(String Filename,String FilenameBioC) throws IOException, XMLStreamException
{
for (int i = 0; i < GNormPlus.BioCDocobj.PMIDs.size(); i++) /** PMIDs : i */
{
for (int j = 0; j < GNormPlus.BioCDocobj.PassageNames.get(i).size(); j++) /** Paragraphs : j */
{
String PassageContext = GNormPlus.BioCDocobj.PassageContexts.get(i).get(j); // Passage context
/** GeneID recognition by pattern match */
ArrayList<String> locations = SearchGeneIDLocation(PassageContext);
for (int k = 0 ; k < locations.size() ; k++)
{
String anno[]=locations.get(k).split("\t");
String mention = anno[2].toLowerCase();
mention = mention.replaceAll("[\\W\\-\\_]+", "");
if(GNormPlus.GeneIDs_hash.containsKey(mention))
{
GNormPlus.BioCDocobj.Annotations.get(i).get(j).add(locations.get(k)+"\tGeneID:"+GNormPlus.GeneIDs_hash.get(mention)); //paragraph
}
}
}
}
GNormPlus.BioCDocobj.BioCOutput(Filename,FilenameBioC,GNormPlus.BioCDocobj.Annotations,true,true);
}
} |