mapping2MCO_v5.py 15.2 KB
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
# -*- coding: utf-8 -*-
"""
#Setup
"""

#################### Setup ####################
from optparse import OptionParser
import os
from numpy.core.fromnumeric import sort
from pandas import read_csv, DataFrame, merge, concat, read_table
from numpy import exp, nan
import seaborn as sns
from numpy import mean
 
import matplotlib.pyplot as plt 
import matplotlib
matplotlib.style.use('ggplot')
# %matplotlib inline

from collections import Counter, defaultdict
import json

from fuzzywuzzy import fuzz
from fuzzywuzzy import process

#import format_fun
import format_fun_v6 as format_fun
import mapping_fun
import sys

"""
# input parameters
--inputPath         /home/egaytan/automatic-extraction-growth-conditions/mapping_MCO/input/
--iAnnotatedFile    srr_IV_model_Run3_v10_S1_False_S2_True_S3_False_S4_False_Run3_v10_named.tsv
--iOntoFile         gc_ontology_terms_v2.txt
--iLinksFile        gc_ontology_terms_link_v2.txt  
--iSynFile          mco_terms_v0.2.json
--outputPath        /home/egaytan/automatic-extraction-growth-conditions/mapping_MCO/output/
--outputFile        all_srr_IV_mapped.tsv
--minPerMatch       90


#Example
# nohup python3 /home/egaytan/automatic-extraction-growth-conditions/mapping_MCO/bin/mapping2MCO_v5.py --inputPath      /home/egaytan/automatic-extraction-growth-conditions/mapping_MCO/input/ --iAnnotatedFile srr_htregulondb_model_Run3_v10_S1_False_S2_True_S3_False_S4_False_Run3_v10.tsv --iOntoFile      gc_ontology_terms_v2.txt   --iSynFile       mco_terms_v0.2.json --outputPath     /home/egaytan/automatic-extraction-growth-conditions/mapping_MCO/output/v2/ --outputFile     srr_htregulondb_v2.tsv --minPerMatch  80  --minCRFProbs 0.9 > /home/egaytan/automatic-extraction-growth-conditions/mapping_MCO/output/v2/srr_htregulondb_mapping_report_v2.out &
"""
#################### Defining parameters ####################
if __name__ == "__main__":
    parser = OptionParser()
    parser.add_option(
        "--inputPath",
        dest="input_path",
        help="Path of npl tagged file (crf output)",
        metavar="PATH")
    parser.add_option(
         "--iAnnotatedFile",
        dest="npl_fname",
        help="Input file of npl tagged file (crf output)",
        metavar="FILE",
        default="")
    parser.add_option(
         "--iOntoFile",
        dest="onto_fname",
        help="Input file with the ontology entities",
        metavar="FILE",
        default="")
    parser.add_option(
         "--iLinksFile",
        dest="links_fname",
        help="Input file with links and id for the ontology",
        metavar="FILE",
        default=None)
    parser.add_option(
         "--iSynFile",
        dest="syn_fname",
        help="Input file for the additional ontology of synonyms",
        metavar="FILE",
        default=None)
    parser.add_option(
         "--outputPath",
        dest="output_path",
        help="Output path to place output files",
        metavar="PATH")
    parser.add_option(
         "--outputFile",
        dest="out_fname",
        help="Output file name for the mapping process",
        metavar="FILE",
        default="")
    parser.add_option(
         "--minPerMatch",
        dest="min_score",
        help="Minimal string matching percentage")  
    parser.add_option(
         "--minCRFProbs",
        dest="min_probs",
        help="Minimal crf probabilities")
    
    (options, args) = parser.parse_args()
    if len(args) > 0:
        parser.error("Any parameter given.")
        sys.exit(1)

    #################### DISP PARAMETERS ####################
    print('\n\n-------------------------------- PARAMETERS --------------------------------\n')
    print("--inputPath      Path of npl tagged file: " + str(options.input_path))
    print("--iAnnotatedFile Input file of npl tagged file: " + str(options.npl_fname))
    print("--iOntoFile      Input file with the ontology entities (MCO-terms): " + str(options.onto_fname))
    print("--iLinksFile     Input file with links and id for the ontology (MCO-type-links): " + str(options.links_fname))
    print("--iSynFile       Input file for the additional ontology of synonyms (MCO-syn-json): " + str(options.syn_fname))
    print("--outputPath     Output path to place output files: " + str(options.output_path))
    print("--outputFile     Output of the mapping process: " + str(options.out_fname))
    print("--minPerMatch    Minimal string matching percentage: " + str(options.min_score))
    print("--minCRFProbs    Minimal crf probabilities allowed: " + str(options.min_probs))

    print("\n\n")
    repognrl = "http://pakal.ccg.unam.mx/cmendezc"
    reponame = "automatic-extraction-growth-conditions/tree/master/extraction-geo/download/srr_htregulondb"
    repo_url = '/'.join([repognrl,reponame])
    
    # Input files ========================================================================================
    min_score = int(options.min_score)
    min_probs = float(options.min_probs)
    npl_ifile =  os.path.join(options.input_path, options.npl_fname)
    mco_ifile =  os.path.join(options.input_path, options.onto_fname)
    mco_syn_ifile =  os.path.join(options.input_path, options.syn_fname)

    # Output files =======================================================================================

    #Save by mapping stratergy
    raw_ofname = "_".join(["raw", options.out_fname])
    rawmap_ofile =  os.path.join(options.output_path, raw_ofname)
    str_ofname = "_".join(["sim", options.out_fname])
    strmap_ofile =  os.path.join(options.output_path, str_ofname)

    #Saving map und unmap
    full_map_ofile = os.path.join(options.output_path, "full_map_"+options.out_fname)
    full_unmap_ofile = os.path.join(options.output_path, "full_unmap_"+options.out_fname)

    #Save JSONs
    json_ofile = os.path.join(options.output_path, options.out_fname)
    json_ofile_map  = json_ofile.replace(".tsv", "_map.json")
    json_ofile_unmap= json_ofile.replace(".tsv", "_unmap.json")
    json_ofile_full = json_ofile.replace(".tsv", "_full.json")
    
    # Load input data ====================================================================================
    
    #Columns for the NPL-CRF extraction
    exp_cols = {"SRR", "GSE", "GSM", "GPL", "PMID", "FULL_TEXT", "BANGLINE", "TERM_NAME", "TERM_TYPE", "PROB"}
    
    #Load CRF-annotation
    npl_full = read_table(npl_ifile,  sep = "\t")
    
    #Check input
    obs_cols = set(npl_full.columns)
    if exp_cols.intersection(obs_cols) != exp_cols:
        ocol = ", ".join(list(exp_cols))        
        sys.exit(ocol + " expected columns for iAnnotatedFile" )

    #Filter Input by probs
    npl_df = npl_full[npl_full.PROB >= min_probs]
    npl_df = npl_df.drop_duplicates(keep="first")
    npl_df = npl_df.dropna()
    
    #Cleaning input
    npl_df['TERM_TYPE'] = [mapping_fun.transterm_npl2mco(term) for term in npl_df.TERM_TYPE]     
    #filter non-mco terms types
    npl_df = npl_df[npl_df.TERM_TYPE != "exTag Type"]

    #add repofile_ source. access to stored files at gitLab    
    source_access = ['/'.join([repo_url,gse,gse+'.soft.gz']) for gse in npl_df['GSE']]
    npl_df['REPO_FILE'] = source_access

    ##remove additional spaces
    npl_df['TERM_NAME'] = [txt.strip() for txt in npl_df['TERM_NAME']]
    npl_df['PMID'] = [pmid.replace("PMID_", "") for pmid in npl_df['PMID']]

    #Columns for MCO
    exp_cols = {"TERM_ID", "TERM_NAME"}

    #Load MCO term names
    mco_df_full = read_table(mco_ifile,  sep = "\t")
    
    #Check input MCO
    obs_cols = set(mco_df_full.columns)
    if exp_cols.intersection(obs_cols) != exp_cols:
        sys.exit("\"TERM_ID\" and \"TERM_NAME\" expected columns for iOntoFile" )

    #Clean MCO input        
    mco_df = mco_df_full[["TERM_ID","TERM_NAME"]]
    mco_df = mco_df.drop_duplicates(keep="first")
    mco_df = mco_df.dropna()

    #Load MCO links
    if options.links_fname is not None:
        print("\nLoad types...")
        mcolink_ifile =  os.path.join(options.input_path, options.links_fname)
        exp_cols = {"TERM_ID", "TERM_TYPE"}
        mco_links_full = read_table(mcolink_ifile, sep = "\t")

        obs_cols = set(mco_links_full.columns)

        if exp_cols.intersection(obs_cols) != exp_cols:
            sys.exit("at least \"TERM_ID\" and \"TERM_TYPE\" expected columns for iLinksFile" )

        mco_links = mco_links_full[["TERM_ID", "TERM_TYPE"]]
        mco_links = mco_links.drop_duplicates(keep="first")
        mco_links = mco_links.dropna()
    else:
        mco_links = None

    #Load MCO terms synonyms
    mco_json = open(mco_syn_ifile )
    
    #format json from mco to dataframe
    data = json.load(mco_json)
    mco_syn = format_fun.json2DataFrame(data)


    print('\n\n-------------------------------- INPUTS --------------------------------\n')

    print("\nnpl tagged file\n")
    print(npl_df.head(3))
    print("\nontology entities\n")
    print(mco_df.head(3))
    if options.links_fname is not None:
        print("\nlinks and id for the ontology (MCO-type-links)\n")
        print(mco_links.head(3))
    print("\nadditional ontology of synonyms (MCO-syn-json)\n")
    print(mco_syn.head(3))


    print('\n\n-------------------------------- RESULTS --------------------------------\n')
   
    #################### mappping to MCO exact string ####################
    #npl_df = npl_df.drop_duplicates("TERM_NAME",  keep="first")
    #npl_df = npl_df.head(10)
    
    print("\nTracking exact terms to MCO...")
    print(f"\nMapping {len(npl_df.index)} terms to MCO based on exact strings...")
    
    #Exact mapping to MCO
    raw_matches = mapping_fun.raw_map_mco(
        npl_df = npl_df, 
        mco_df = mco_df, 
        mco_links = mco_links, 
        unmap = True)

    #save file name source of the raw mapping
    raw_matches["SOURCE"] = mco_ifile
    #additional column to merge
    raw_matches["ENTITY_NAME"] = ""
    
    #################### mappping to MCO.syn exact string ####################
    
    #define unmapped
    raw_mco_unmap = raw_matches[raw_matches.isna().TERM_ID]
    #input for te second step
    raw_mco_unmap = raw_mco_unmap[list(npl_df.columns)]

    print(f"\nMapping {len(raw_mco_unmap.index)} terms to MCO - synonyms based on exact strings...\n")
    
    #exact mapping to synonims
    raw_matches_syn = mapping_fun.raw_map_mco(
        npl_df = raw_mco_unmap, 
        mco_df = mco_syn, 
        unmap = True)
    
    #additional column to merge
    raw_matches_syn["SOURCE"] = mco_syn_ifile
    #raw_matches_syn["TERM_TYPE"] = ""
    
    #################### save mapped terms based on exact strings ####################
    
    #all mapped
    raw_map_odf = concat(
        [raw_matches, raw_matches_syn], 
        sort=True).dropna()
    
    #print(raw_map_odf.head(3))
    print(f"Total of terms mapped by exact strings: {len(raw_map_odf.index)}")
    print("Saving filtered terms from raw mapping...\n\n")
    
    raw_map_odf.to_csv(
        rawmap_ofile, 
        sep = "\t", 
        header =True, 
        index=False)

    #################### unmmaped raw terms ####################     
    raw_mco_syn_unmap =  raw_matches_syn[raw_matches_syn.isna().TERM_ID]
    raw_mco_syn_unmap = raw_mco_syn_unmap[list(npl_df.columns)]
        
    print(f"{len(raw_mco_syn_unmap.index)} unmapped terms based on exact strings")
    print("Dropping duplicated unmapped term names...")
    raw_mco_syn_unmap = raw_mco_syn_unmap.drop_duplicates("TERM_NAME")        
    
    print(f"{len(raw_mco_syn_unmap.index)} unmapped unique terms based on exact strings")

    #################### string similarity mapping ####################
    ###Matching unmaped terms by string similarity   
    print("\ncompute string similarty...")

    print(f"\nMapping to MCO {len(raw_mco_syn_unmap.index)} terms based on string similarity...")

    str_matches = mapping_fun.str_match_map_mco(
        raw_mco_syn_unmap, mco_df, 
        mco_links = mco_links,  
        min_match=0, 
        npl_merges=False)

    str_matches_odf = str_matches[str_matches.SET >= min_score]
    str_matches_odf["SOURCE"] = mco_ifile    

    #################### unmmaped sim terms (MCO) ####################
    str_mco_unmap = str_matches[str_matches.SET < min_score]
    #str_mco_unmap = str_mco_unmap[list(npl_df.columns)]
    str_mco_unmap = str_mco_unmap.drop_duplicates("TERM_NAME")

    print(f"\nMapping to MCO - synonyms {len(str_mco_unmap.index)} terms based on string siilarity..\n")
    str_matches_syn = mapping_fun.str_match_map_mco(
        str_mco_unmap, mco_syn, 
        min_match=min_score, 
        npl_merges=False)

    str_matches_syn_odf = str_matches_syn[str_matches_syn.SET >= min_score]
    str_matches_syn_odf["SOURCE"] = mco_syn_ifile
  
    #################### save str-sim map terms ####################    
    all_str_matches_odf =  concat(
        [str_matches_odf, str_matches_syn_odf], 
        sort = True).dropna()     
        
    print(f"Unique terms mapped by string similarity: {len(all_str_matches_odf.index)}")

    all_str_matches_npl_odf = merge(
        npl_df, all_str_matches_odf, 
        on = ["TERM_NAME"], 
        how="inner")    
    
    print(f"Total of terms mapped by string similarity: {len(all_str_matches_npl_odf.index)}")
    print("Saving filtered terms from str mapping...\n\n")
    
    all_str_matches_npl_odf.to_csv(
        strmap_ofile, 
        sep = "\t", 
        header =True, 
        index=False)

    #################### save all map terms ####################
    raw_map_odf["CASE_MATCH"] = "MCO"
    raw_map_odf["SET"] = 100
    raw_map_odf["SORT"] = 100

    full_map = concat(
        [all_str_matches_npl_odf, raw_map_odf], 
        sort = True)
    full_map["MAP"]=True
    
    full_map.to_csv(full_map_ofile, 
    sep = "\t", 
    header =True, 
    index=False)

    print("--------------------END----------------------")
    print(f"Total of terms mapped: {len(full_map.index)}\n")    
    
    ###################### Merge all unmapped ######################
    full_unmap = merge(npl_df, full_map[["TERM_NAME", "TERM_ID"]], on = ["TERM_NAME"], how='left')
    full_unmap = full_unmap[full_unmap.isna().TERM_ID]
    #print(full_unmap.head(3))
    
    print(f"Total of terms unmapped: {len(full_unmap.index)}")
    
    full_unmap["SOURCE"] = ""
    full_unmap["CASE_MATCH"] = ""
    full_unmap["SET"] = 0
    full_unmap["SORT"] = 0
    full_unmap["MAP"]=False

    full_unmap.to_csv(
        full_unmap_ofile, 
        sep = "\t", 
        header =True, 
        index=False)

    #################### Formatting json ####################
    
    format_fun.to_json(
        df = full_map,
        source_info = "GEO", 
        evidence_source = "NPL-CRF", 
        ofname = json_ofile_map
        )

    
    format_fun.to_json(
        df = full_unmap,
        source_info = "GEO", 
        evidence_source = "NPL-CRF", 
        ofname = json_ofile_unmap
        )

    #Merge output all
    full_merge = concat([full_map, full_unmap], sort=True)
    format_fun.to_json(
        df = full_merge,        
        source_info = "GEO", 
        evidence_source = "NPL-CRF", 
        ofname = json_ofile_full
        )