changeset 16:b6607b7e683f draft

planemo upload commit f2b3d1ff6bea930b2ce32c009e4d3de39a17edfb-dirty
author proteore
date Mon, 28 Jan 2019 11:08:47 -0500
parents b50d913ec067
children 1e45ea50f145
files id_converter.R id_converter.py id_converter.xml tool-data/proteore_id_mapping.loc.sample tool_data_table_conf.xml.sample
diffstat 5 files changed, 199 insertions(+), 201 deletions(-) [+]
line wrap: on
line diff
--- a/id_converter.R	Tue Dec 18 09:57:21 2018 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,185 +0,0 @@
-# Read file and return file content as data.frame
-read_file <- function(path,header){
-  file <- try(read.csv(path,header=header, sep="\t",stringsAsFactors = FALSE, quote="\"",check.names = F),silent=TRUE)
-  if (inherits(file,"try-error")){
-    stop("File not found !")
-  }else{
-    return(file)
-  }
-}
-
-str2bool <- function(x){
-  if (any(is.element(c("t","true"),tolower(x)))){
-    return (TRUE)
-  }else if (any(is.element(c("f","false"),tolower(x)))){
-    return (FALSE)
-  }else{
-    return(NULL)
-  }
-}
-
-mapping_ids <- function(list_id,input_id_type,ref_file,options){
-  list_id = list_id[!is.na(list_id)]
-  if (!any(grep(";",ref_file[input_id_type]))) {
-    res <- ref_file[match(list_id,ref_file[input_id_type][,]),c(input_id_type,options)]
-    res=as.data.frame(res)
-    res=res[which(!is.na(res[,1])),]
-    row.names(res)=res[,1]
-    res=res[2:ncol(res)]
-    } else {
-      if (length(options) > 1) {
-        res <- data.frame(t(sapply(list_id, function(x) apply(ref_file[grep(x,ref_file[input_id_type][,]),options],2,function(y) paste(y[y!=""],sep="",collapse=";")) )))
-      } else if (length(options)==1){
-        res <- data.frame(sapply(list_id, function(x) gsub(";+$","",paste(ref_file[grep(x,ref_file[input_id_type][,]),options],sep="",collapse=";"))))
-        colnames(res)=options
-      }
-    }
-  
-  return (res)
-}
-
-get_list_from_cp <-function(list){
-  list = strsplit(list, "[ \t\n]+")[[1]]
-  list = list[list != ""]    #remove empty entry
-  list = gsub("-.+", "", list)  #Remove isoform accession number (e.g. "-2")
-  return(list)
-}
-
-order_columns <- function (df,ncol){
-  if (ncol==1){ #already at the right position
-    return (df)
-  } else {
-    df = df[,c(2:ncol,1,(ncol+1):dim.data.frame(df)[2])]
-  }
-  return (df)
-}
-
-#take data frame, return  data frame
-split_ids_per_line <- function(line,ncol){
-  
-  #print (line)
-  header = colnames(line)
-  line[ncol] = gsub("[[:blank:]]|\u00A0","",line[ncol])
-  
-  if (length(unlist(strsplit(as.character(line[ncol]),";")))>1) {
-    if (length(line)==1 ) {
-      lines = as.data.frame(unlist(strsplit(as.character(line[ncol]),";")),stringsAsFactors = F)
-    } else {
-        if (ncol==1) {                                #first column
-        lines = suppressWarnings(cbind(unlist(strsplit(as.character(line[ncol]),";")), line[2:length(line)]))
-      } else if (ncol==length(line)) {                 #last column
-        lines = suppressWarnings(cbind(line[1:ncol-1],unlist(strsplit(as.character(line[ncol]),";"))))
-      } else {
-        lines = suppressWarnings(cbind(line[1:ncol-1], unlist(strsplit(as.character(line[ncol]),";"),use.names = F), line[(ncol+1):length(line)]))
-      }
-    }
-    colnames(lines)=header
-    return(lines)
-  } else {
-    return(line)
-  }
-}
- 
-#create new lines if there's more than one id per cell in the columns in order to have only one id per line
-one_id_one_line <-function(tab,ncol){
-  
-  if (ncol(tab)>1){
-    
-    tab[,ncol] = sapply(tab[,ncol],function(x) gsub("[[:blank:]]","",x))
-    header=colnames(tab)
-    res=as.data.frame(matrix(ncol=ncol(tab),nrow=0))
-    for (i in 1:nrow(tab) ) {
-      lines = split_ids_per_line(tab[i,],ncol)
-      res = rbind(res,lines)
-    }
-  }else {
-    res = unlist(sapply(tab[,1],function(x) strsplit(x,";")),use.names = F)
-    res = data.frame(res[which(!is.na(res[res!=""]))],stringsAsFactors = F)
-    colnames(res)=colnames(tab)
-  }
-  return(res)
-}
-
-get_args <- function(){
-  args <- commandArgs(TRUE)
-  if(length(args)<1) {
-    args <- c("--help")
-  }
-  
-  # Help section
-  if("--help" %in% args) {
-    cat("Selection and Annotation HPA
-    Arguments:
-        --ref_file: path to reference file (id_mapping_file.txt)
-        --input_type: type of input (list of id or filename)
-        --id_type: type of input IDs
-        --input: list of IDs (text or filename)
-        --column_number: the column number which contains list of input IDs
-        --header: true/false if your file contains a header
-        --target_ids: target IDs to map to 
-        --output: output filename \n")
-    q(save="no")
-  }
-  
-  # Parse arguments
-  parseArgs <- function(x) strsplit(sub("^--", "", x), "=")
-  argsDF <- as.data.frame(do.call("rbind", parseArgs(args)))
-  args <- as.list(as.character(argsDF$V2))
-  names(args) <- argsDF$V1
-  
-  return(args)
-}
-
-mapping = function() {
-  
-  args <- get_args()
-  
-  #save(args,file="/home/dchristiany/proteore_project/ProteoRE/tools/id_converter/args.rda")
-  #load("/home/dchristiany/proteore_project/ProteoRE/tools/id_converter/args.rda")
-  
-  input_id_type = args$id_type # Uniprot, ENSG....
-  list_id_input_type = args$input_type # list or file
-  options = strsplit(args$target_ids, ",")[[1]]
-  output = args$output
-  id_mapping_file = args$ref_file
-    
-  # Extract input IDs
-  if (list_id_input_type == "list") {
-    list_id = get_list_from_cp(args$input)
-  } else if (list_id_input_type == "file") {
-    filename = args$input
-    column_number = as.numeric(gsub("c", "" ,args$column_number))
-    header = str2bool(args$header)
-    file_all = read_file(filename, header)
-    file_all = one_id_one_line(file_all,column_number)
-    list_id = trimws(gsub("[$,\xc2\xa0]","",sapply(strsplit(as.character(file_all[,column_number]), ";"), "[", 1)))
-    # Remove isoform accession number (e.g. "-2")
-    list_id = unique(gsub("-.+", "", list_id))
-  }
-
-  # Extract ID maps
-  id_map = read_file(id_mapping_file, T)
-    
-  # Map IDs
-  res <- mapping_ids(list_id,input_id_type,id_map,options)
-
-  #merge data frames
-  if (list_id_input_type == "list"){
-    list_id <- data.frame(list_id)
-    output_content = merge(list_id,res,by.x=1,by.y="row.names",incomparables = NA, all.x=T)
-    colnames(output_content)[1]=input_id_type
-  } else if (list_id_input_type == "file") {
-    output_content = merge(file_all,res,by.x=column_number,by.y="row.names",incomparables = NA, all.x=T)
-    output_content = order_columns(output_content,column_number)
-  }
-  
-  #write output
-  header=colnames(output_content)
-  output_content <- as.data.frame(apply(output_content, c(1,2), function(x) gsub("^$|^ $", NA, x)))
-  colnames(output_content)=header
-  write.table(output_content, output, row.names = FALSE, sep = '\t', quote = FALSE)
-}
-
-mapping()
-
-#Rscript id_converter_UniProt.R "UniProt.AC" "test-data/UnipIDs.txt,c1,false" "file" "Ensembl_PRO,Ensembl,neXtProt_ID" "test-data/output.txt" ../../utils/mapping_file.txt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/id_converter.py	Mon Jan 28 11:08:47 2019 -0500
@@ -0,0 +1,183 @@
+import sys, os, argparse, re, csv
+
+def get_args() :
+    parser = argparse.ArgumentParser()
+    parser.add_argument("-d", "--ref_file", help="path to reference file: <species>_id_mapping.tsv", required=True)
+    parser.add_argument("--input_type", help="type of input (list of id or filename)", required=True)
+    parser.add_argument("-t", "--id_type", help="type of input IDs", required=True)
+    parser.add_argument("-i", "--input", help="list of IDs (text or filename)", required=True)
+    parser.add_argument("-c", "--column_number", help="list of IDs (text or filename)")
+    parser.add_argument("--header", help="true/false if your file contains a header")
+    parser.add_argument("--target_ids", help="target IDs to map to", required=True)
+    parser.add_argument("-o", "--output", help="output filename", required=True)
+    args = parser.parse_args()
+    return args
+
+#return list of (unique) ids from string
+def get_input_ids_from_string(input) :
+    ids_list = list(set(re.split(r'\s+',input.replace("\r","").replace("\n"," ").replace("\t"," "))))
+    if "" in ids_list : ids_list.remove("")
+    #if "NA" in ids_list : ids_list.remove("NA")
+    return ids_list
+
+#return input_file and list of unique ids from input file path
+def get_input_ids_from_file(input,nb_col,header) :
+    with open(input, "r") as csv_file :
+        input_file= list(csv.reader(csv_file, delimiter='\t'))
+
+    input_file, ids_list = one_id_one_line(input_file,nb_col,header)
+    if "" in ids_list : ids_list.remove("")
+    #if "NA" in ids_list : ids_list.remove("NA")
+
+    return input_file, ids_list
+
+#return input file by adding lines when there are more than one id per line
+def one_id_one_line(input_file,nb_col,header) :
+
+    if header : 
+        new_file = [input_file[0]]
+        input_file = input_file[1:]
+    else : 
+        new_file=[]
+    ids_list=[]
+
+    for line in input_file :
+        if line != [] and set(line) != {''}: 
+            line[nb_col] = re.sub(r"\s+","",line[nb_col])
+            if ";" in line[nb_col] :
+                ids = line[nb_col].split(";")
+                for id in ids :
+                    new_file.append(line[:nb_col]+[id]+line[nb_col+1:])
+                    ids_list.append(id)
+            else : 
+                new_file.append(line)
+                ids_list.append(line[nb_col])
+
+    ids_list= list(set(ids_list))
+
+    return new_file, ids_list
+
+#return the column number in int format
+def nb_col_to_int(nb_col):
+    try :
+        nb_col = int(nb_col.replace("c", "")) - 1
+        return nb_col
+    except :
+        sys.exit("Please specify the column where you would like to apply the filter with valid format")
+
+#replace all blank cells to NA
+def blank_to_NA(csv_file) :
+    tmp=[]
+    for line in csv_file :
+        line = ["NA" if cell=="" or cell==" " or cell=="NaN" else cell for cell in line]
+        tmp.append(line)
+    
+    return tmp
+
+def str2bool(v):
+    if v.lower() in ('yes', 'true', 't', 'y', '1'):
+        return True
+    elif v.lower() in ('no', 'false', 'f', 'n', '0'):
+        return False
+    else:
+        raise argparse.ArgumentTypeError('Boolean value expected.')
+
+#return result dictionary
+def map_to_dictionary(ids,ids_dictionary,id_in,id_out) :
+    
+    result_dict = {}
+    for id in ids : 
+        for target_id in id_out :
+            if id in ids_dictionary :
+                res = ";".join(ids_dictionary[id][target_id])
+            else :
+                res=""
+            
+            if id in result_dict :
+                result_dict[id].append(res)
+            else :
+                result_dict[id]=[res]
+
+    return result_dict
+
+#create empty dictionary with index for tab
+def create_ids_dictionary (ids_list) :
+    ids_dictionary = {}
+    ids_dictionary_index={}
+    for i,id in enumerate(ids_list) :
+        ids_dictionary_index[i]=id
+            
+    return(ids_dictionary,ids_dictionary_index)
+
+def main():
+    
+    #Get args from command line
+    args = get_args()
+    target_ids = args.target_ids.split(",")
+    header=False
+    if args.id_type in target_ids : target_ids.remove(args.id_type)
+    if args.input_type=="file" :
+        args.column_number = nb_col_to_int(args.column_number)
+        header = str2bool(args.header)
+
+    #Get ref file to build dictionary
+    csv.field_size_limit(sys.maxsize) # to handle big files
+    with open(args.ref_file, "r") as csv_file :
+        tab = csv.reader(csv_file, delimiter='\t')
+        tab = [line for line in tab]
+
+    ids_list=tab[0]
+        
+    #create empty dictionary and dictionary index
+    ids_dictionary, ids_dictionary_index = create_ids_dictionary(ids_list)
+
+    #fill dictionary and sub dictionaries with ids
+    id_index = ids_list.index(args.id_type)
+    for line in tab[1:] :
+        ref_ids=line[id_index]
+        other_id_type_index = [accession_id for accession_id in ids_dictionary_index.keys() if accession_id!=id_index]
+        for id in ref_ids.replace(" ","").split(";") :       #if there's more than one id, one key per id (example : GO)
+            if id not in ids_dictionary :      #if the key is not created yet
+                ids_dictionary[id]={}
+            for other_id_type in other_id_type_index :
+                if ids_dictionary_index[other_id_type] not in ids_dictionary[id] :
+                    ids_dictionary[id][ids_dictionary_index[other_id_type]] = set(line[other_id_type].replace(" ","").split(";"))
+                else :
+                    ids_dictionary[id][ids_dictionary_index[other_id_type]] |= set(line[other_id_type].replace(" ","").split(";"))
+                if len(ids_dictionary[id][ids_dictionary_index[other_id_type]]) > 1 and '' in ids_dictionary[id][ids_dictionary_index[other_id_type]] : 
+                    ids_dictionary[id][ids_dictionary_index[other_id_type]].remove('')
+
+    #Get file and/or ids from input 
+    if args.input_type == "list" :
+        ids = get_input_ids_from_string(args.input)
+    elif args.input_type == "file" :
+        input_file, ids = get_input_ids_from_file(args.input,args.column_number,args.header)
+
+    #Mapping ids
+    result_dict = map_to_dictionary(ids,ids_dictionary,args.id_type,target_ids)
+
+    #creating output file 
+    if header : 
+        output_file=[input_file[0]+target_ids]
+        input_file = input_file[1:]
+    else :
+        output_file=[[args.id_type]+target_ids]
+
+    if args.input_type=="file" :
+        for line in input_file :
+            output_file.append(line+result_dict[line[args.column_number]])
+    elif args.input_type=="list" :
+        for id in ids :
+            output_file.append([id]+result_dict[id])
+
+    #convert blank to NA
+    output_file = blank_to_NA(output_file)
+
+    #write output file 
+    with open(args.output,"w") as output :
+        writer = csv.writer(output,delimiter="\t")
+        writer.writerows(output_file)
+
+if __name__ == "__main__":
+    main()
+
--- a/id_converter.xml	Tue Dec 18 09:57:21 2018 -0500
+++ b/id_converter.xml	Mon Jan 28 11:08:47 2019 -0500
@@ -1,14 +1,13 @@
-<tool id="IDconverter" name="ID Converter" version="2018.12.18">
+<tool id="IDconverter" name="ID Converter" version="2019.01.28">
     <description>(Human, Mouse, Rat)
     </description>
     <requirements>
-      <requirement type="package" version="3.4.1">R</requirement>
     </requirements>
     <stdio>
         <exit_code range="1:" />
     </stdio>
-    <command interpreter="Rscript">
-        $__tool_directory__/id_converter.R
+    <command><![CDATA[
+        python $__tool_directory__/id_converter.py 
         --id_type="$species.idtypein"
         #if $input.ids == "text"
             --input="$input.txt"
@@ -21,9 +20,9 @@
         #end if
         --target_ids="$species.idto.idtypeout"
         --output="$output"
-        --ref_file="$__tool_directory__/${ filter( lambda x: str( x[0] ) == str( $species.mapping_file ), $__app__.tool_data_tables['proteore_id_mapping'].get_fields() )[0][-1] }"
+        --ref_file="$__tool_directory__/${ filter( lambda x: str( x[2] ) == str( $species.mapping_file ), $__app__.tool_data_tables['proteore_id_mapping'].get_fields() )[0][-1] }"
         
-    </command>
+    ]]></command>
     <inputs>
         <conditional name="input" >
             <param name="ids" type="select" label="Enter IDs" help="Copy/paste or from a file (e.g. table)" >
@@ -55,11 +54,11 @@
         <conditional name="species">
             <param name="mapping_file" type="select" label="Species" >
                 <options from_data_table="proteore_id_mapping"/>
-                <option value="human_id_mapping"></option>
-                <option value="mouse_id_mapping"></option>
-                <option value="rat_id_mapping"></option>
+                <option value="Human"></option>
+                <option value="Mouse"></option>
+                <option value="Rat"></option>
             </param>
-            <when value="human_id_mapping">
+            <when value="Human">
                 <param name="idtypein" type="select" label="Type/source of IDs" optional="false" >
                     <option value="neXtProt" >neXtProt ID (e.g. NX_P31946)</option>
                     <option value="UniProt-AC" selected="True" >UniProt accession number (e.g. P31946)</option>
@@ -101,7 +100,7 @@
                     </param>
                 </section>
             </when>
-            <when value="mouse_id_mapping">
+            <when value="Mouse">
                 <param name="idtypein" type="select" label="Type/source of IDs" optional="false" >
                     <option value="UniProt-AC" selected="True" >UniProt accession number (e.g. P31946)</option>
                     <option value="UniProt-ID" >UniProt ID (e.g 1433B_HUMAN)</option>
@@ -140,7 +139,7 @@
                     </param>
                 </section>
             </when>
-            <when value="rat_id_mapping">
+            <when value="Rat">
                 <param name="idtypein" type="select" label="Select type/source of identifier of your list" optional="false" >
                     <option value="UniProt-AC" selected="True" >UniProt accession number (e.g. P31946)</option>
                     <option value="UniProt-ID" >UniProt ID (e.g 1433B_HUMAN)</option>
--- a/tool-data/proteore_id_mapping.loc.sample	Tue Dec 18 09:57:21 2018 -0500
+++ b/tool-data/proteore_id_mapping.loc.sample	Mon Jan 28 11:08:47 2019 -0500
@@ -1,4 +1,5 @@
 #This file lists the locations of reference file for id_converter tool
-human_id_mapping	Human (Homo sapiens)	tool-data/human_id_mapping_23-10-2018.tsv
-mouse_id_mapping	Mouse (Mus musculus)	tool-data/mouse_id_mapping_23-10-2018.tsv
-rat_id_mapping	Rat (Rattus norvegicus)	tool-data/rat_id_mapping_23-10-2018.tsv
+#<id>	<name>	<value>	<path>
+human_id_mapping_23-10-2018	Human (homo sapiens)	Human	tool-data/human_id_mapping_23-10-2018.tsv
+mouse_id_mapping_23-10-2018	Mouse (Mus musculus)	Mouse	tool-data/mouse_id_mapping_23-10-2018.tsv
+rat_id_mapping_23-10-2018	Rat (Rattus norvegicus)	Rat	tool-data/rat_id_mapping_23-10-2018.tsv
--- a/tool_data_table_conf.xml.sample	Tue Dec 18 09:57:21 2018 -0500
+++ b/tool_data_table_conf.xml.sample	Mon Jan 28 11:08:47 2019 -0500
@@ -1,7 +1,7 @@
 <tables>
     <!-- Location of ID Mapping files for id_converter-->
     <table name="proteore_id_mapping" comment_char="#">
-        <columns>value, name, path</columns>
+        <columns>id, name, value, path</columns>
         <file path="tool-data/proteore_id_mapping.loc" />
     </table>
 </tables>
\ No newline at end of file