Package: dataintegrator
DataIntegratorData Integration tools for genetic data in PythonPrimary FunctionsLiftover Genome BuildGiven a input genome build version and a output genome build version, liftover the genome build of the input chr:pos pair to the desired version.Add RsIDAdd rsID according to chr and pos position in the data file.Flip Strand to ForwardStep 1: Query dbSnp153 databse to get the two alleles of the forward strand according to chr and pos.Step 2: Compare the two alleles with the input GWAS summary file, if all ATCG appears, flip the GWAS summary file using the A-T and C-G rule.Align Effect alleleInput two gwas summary files, then use the first one as reference and make sure the second file has the same effect allele. If the effect allele is changed, the effect size will also be changed accordingly.DependenciesBefore installing theDataIntegratorpackage, the following packages are required to be installed in advance:pyliftoverpip install pyliftovernumpypip install numpypandaspip install pandaspyBigWigpip install pyBigWigUsageTo use the pacakge, please follow the steps below:Install dependencies mentioned aboveInstall the packagepip install dataintegratorTo use thequery_data()function, you have to provide the local path to thedbSnp153.bbfile downloaded from the UCSC website(download here); otherwise, the function will query the data online instead of querying data from local file (which will significantly reduce the run time).In python, get started with the following stepsfromdataintegratorimportDataIntegratorasdiSetting inital parameters:input_path (the path of the data to be processed)output_path (the path you want the processed result to be saved to)input_format (e.g. "hg19")output_format (e.g. "hg38")Start using the provided functions, e.g.:input_path="<path_to_your_data_file>.gz"df=di.read_data(input_path,'\t',"#chrom","pos","rsids","alt","ref","maf","beta","sebeta","pval")#print(df)To view example calls of the main functions, clone this repository and see the.pyfiles under the/examplesdirectory.Data Integrating work flow:Functions Provided1. Function to read data in formatted waysRead Dataread_data(input_path,Chr_col_name,BP_col_name,SNP_col_name,A1_col_name,A2_col_name,EAF_col_name,Beta_col_name,Se_col_name,P_col_name,separate_by="\t")Description: This function reads the data to be processed and output it in a formatted wayParametersinput_path (str): the complete or relative path of the input dataChr_col_name (str): the column name in the original data representing chromosomeBP_col_name (str): the column name in the original data representing base pair positionSNP_col_name (str): the column name in the original data representing rsIDA1_col_name (str): the column name in the original data representing effect alleleA2_col_name (str): the column name in the original data representing non-effect alleleEAF_col_name (str): the column name in the original data represneting allele frequency for effect alleleBeta_col_name (str): the column name in the original data represneting effect size for effect alleleSe_col_name (str): the column name in the original data represneting standard error for effect sizeP_col_name (str): the column name in the original data represneting p-valueseparate_by (str): the delimiter of the original data, '\t' by default (tab separated)Returns:pandas.DataFrame: return formatted data in the form of pandas DataFrame in the following ways:ChrBPSNPA1A2EAFBetaSeP1438956rs4596GA0.0021-0.5380.58020.3533X704956rs1234TC0.02420.16850.24690.0843Example Usage:input_path="<path_to_your_data_file>.gz"df=di.read_data(input_path,'\t',"#chrom","pos","rsids","alt","ref","maf","beta","sebeta","pval")#print(df)2. Functions to clean data for further processingFilter bi-allelicfilter_bi_allelic(df,rest=False)Function to filter only bi-allelic cases in the dataParameters:df (pandas.DataFrame): The data frame to be filtered.rest (boolean): value indicating wether or not to keep (mark only) the non-bi-allelic cases. Default to False.Returns:pandas.DataFrame: return filtered data in the form of pandas DataFrame.Example:bi_allelic=di.filter_bi_allelic(df)# if you want to check the non-bi-allelic cases, use the following commandbi_allelic=di.filter_bi_allelic(df,rest=True)Deduplicatededuplicate(df)Function to drop rows in data containing dduplicate keys (Chr + BP)Parameters:df (pandas.DataFrame): The data frame to be deduplicated.Returns:pandas.DataFrame: return filtered data in the form of pandas DataFrame.Example:deduplicated=di.deduplicate(df)Sort by Chr and BPsort_by_chr_bp(df)Function to sort the data based on Chr and BPParameters:df (pandas.DataFrame): the data to be sortedReturns:pandas.DataFrame: return the sorted dataExample:sorted_data=di.sort_by_chr_bp(df)3. Functions to query data from dbSnp153 for further processingQuery UCSC Database for dbSNP153 infoquery_data(df,link="http://hgdownload.soe.ucsc.edu/gbdb/hg38/snp/dbSnp153.bb")Function to query required data from dbSnp153Parameters:df (pandas.DataFrame): the data we want more infolink (str): path or link of the '.bb' file of dbSnp153Returns:pandas.DataFrame: return complete information from dbSnp153 as a python dictionaryExample:link="<path_to_your_dbSnp153_path>.bb"dbSnp153=di.query_data(df,link)# This will usually take longer timeSave Objectsave_obj(obj,name)Function to save python data structure on diskParameters:obj (obj): the data structure/object to be saved on disk.name (str): the name for the obj to be saved as.Returns:return nothingExample:di.save_obj(dbSnp153,"obj/dbSnp153")Load Objectload_obj(obj_path)Function to load saved python data structure from diskParameters:name (str): the name of the saved obj on disk to be loadedReturns:pandas.DataFrame: return complete information from dbSnp153 as a python dictionaryExample:dbSnp153=di.load_obj("obj/dbSnp153.pkl")4. Functions to process dataLift Overlift_over(df,lo_dict,keep_all=False,inplace=False,comment=False)Function to lift over genome buildParameters:df (pandas.DataFrame): the data to be lifted overlo_dict (python dictionary): the lift over dictionary return from the create_lo functionkeep_unconvertible (boolean): if true, the function will keep and mark the rows that are not convertible. Default to False.keep_original_version (boolean): if true, the function will keep the Chr + BP of original genome build. Default to False.Returns:pandas.DataFrame: return the data being lifted over to the desired genome buildUse two tables to illustrate outputExample:input_format="hg<**>"output_format="hg<**>"lo=create_lo(input_format,output_format)# create chain file as reference for genome-build-lift-over.# drop unconvertible rows and keep only the result after lift over.lift_over=di.lift_over(df,lo)print(lift_over)# keep and mark rows that are not convertiblelift_over=di.lift_over(df,lo,keep_unconvertible=True)# keep the original genome build version as separate columns in the data setlift_over=di.lift_over(df,lo,keep_orginal_version=True)Add Rsid'sadd_rsid(df, data, keep_all=False, inplace=False, show_comment=False, show_errors=False)Function to query and add rs ID for rows missing rsIDs.Parameters:df (pandas.DataFrame): the data to be added rs_idsdata (python dictionary): the dictionary containing required info from dbSnp153keep_all (boolean): value indicating whether the function should keep all rows in the original dataset. Default to False.inplace (boolean): value indicating whether the function should replace the original rsID column with the new added_rsid column. Default to True.show_comment (boolean): value indicating whether the function should add a column indicating the status of adding rsID. Default to False.show_errors (boolean): value indicating whether the function will output a table containing rows that cannot be properly added rsIDs. Default to False."added" : missing rsID in orginal dataset. The Chr+BP key can be found in dbSNP153 and the rsID is successfully added"same": the original dataset have the same rsID as dbSnp153. No need to modify or add."different": the original dataset have different rsID as compared to dbSnp153. Use dbSnp153 153 as reference to repalce the original."key not found" : The Chr+BP key in original dataset cannot be found in dbSnp153. Fill in NA value. Mark in the comment column.Returns:pandas.DataFrame: return the data being added rs_ids.Example Call:added_rsid=di.add_rsid(df,dbSnp153)A few example output:inplace = False, show_comment=False, keep_all=FalseChrBPSNPA1A2EAFBetaSePadded_rsid1438956GA0.0021-0.5380.58020.3533rs12445X704956rs1234TC0.02420.16850.24690.0843rs1234inplace = True, show_comment=False, keep_all=FalseChrBPSNPA1A2EAFBetaSeP1438956rs12445GA0.0021-0.5380.58020.3533X704956rs1234TC0.02420.16850.24690.0843inplace = False, show_comment=True, keep_all=FalseChrBPSNPA1A2EAFBetaSePadded_rsidcomment1438956GA0.0021-0.5380.58020.3533rs12445"added"X704956rs1234TC0.02420.16850.24690.0843rs1234"same"check_errors=TrueChrBPSNPA1A2EAFBetaSePadded_rsidcomment1438956GA0.0021-0.5380.58020.3533"key not found"X704956TC0.02420.16850.24690.0843"key not found"Flip Strandflip_strand(df,data,keep_all=False,inplace=False,show_comment=False,show_errors=False)Function to flip the input data to forward strandParameters:df (pandas.DataFrame): the data to be flipped to forward stranddata (python dictionary): the dictionary containing required info from dbSnp153keep_all (boolean): value indicating whether the function should keep all rows in the original dataset. Default to False.inplace (boolean): value indicating whether the function should replace the original A1 and A2 columns with the new_A1 and new_A2 columns. Default to False.show_comment (boolean): value indicating whether the function should add a column indicating the status of flipping strand. Default to False.show_errors (boolean): value indicating whether the function will output a table containing rows where strand cannot be properly flipped. Default to False."flipped" : The Chr+BP key can be found in dbSNP153 and the strand is successfully flipped."same": the original dataset uses the same strand as dbSnp153. No need to modify or add."different": the original data set and its correspondence in dbSnp153 show completely different strand patter that cannot be flipped (e.g. T/C vs. C/A)"dbSnp153: Indel" : the A1 and A2 in dbSnp153 correponds to the Chr+BP in the processed data set contain Indel"key not found" : The Chr+BP key in original dataset cannot be found in dbSnp153. Fill in NA value. Mark in the comment column.Returns:pandas.DataFrame: return the data being flipped to forward strandExample:flipped=di.flip_strand(df,dbSnp153)A few examples:inplace = False, show_comment=False, keep_all=FalseChrBPSNPA1A2EAFBetaSePnew_A1new_A21438956GA0.0021-0.5380.58020.3533AGX704956rs1234TC0.02420.16850.24690.0843CTinplace = True, show_comment=False, keep_all=FalseChrBPSNPA1A2EAFBetaSeP1438956AG0.0021-0.5380.58020.3533X704956rs1234CT0.02420.16850.24690.0843inplace = False, show_comment=True, keep_all=FalseChrBPSNPA1A2EAFBetaSePnew_A1new_A2comment1438956GA0.0021-0.5380.58020.3533GA"kept original"13704956TC0.02420.12350.24690.0673CT"flipped"22568952AG0.02670.74850.78690.0843"key not found"X274586CT0.02430.13570.24350.1243GT"different"check_errors=TrueChrBPSNPA1A2EAFBetaSePnew_A1new_A2comment22568952AG0.02670.74850.78690.0843"key not found"X274586CT0.02430.13570.24350.1243GT"different"Align Effect Allele and Effect Size between Two Datasetsalign_effect_allele(reference,df,show_errors=False)This function will align the effect allele of input data based on a reference dataParameters:reference (pandas.DataFrame): the reference tabledf (pandas.DataFrame): the data to be alignedcheck_error_rows (boolean): if true, the function will output the rows that cannot be aligned. Default to False.Returns:pandas.DataFrame: return the data with its effect allele being aligned with the reference table.Example:reference_path="<path_to_your_reference_data_file>.gz"reference_df=di.read_data(input_path,"chromosome","base_pair_location","variant_id","effect_allele","other_allele","effect_allele_frequency","beta","standard_error","p_value")# for examplealigned=align_effect_allele(reference_df,df)# be sure df and reference_df are using the same genome build, and both data are properlly cleaned!!# if you want to see rows that cannot be alignedaligned=align_effect_allele(reference_df,df,check_error_rows=True)5. Functions to save resultSave Datasave_data(output_path,df,name,save_format="gzip")function to save the processed data in the tsv form as a gz fileParameters:output_path (str): the path you want the data to be saved.df (pandas.DataFrame): the processed data to be saved.name (str): the output name of the data.save_format (str): the saving format. Choose between 'gzip' or 'csv'. Default to gz.Returns:
pandas.DataFrame: return filtered data in the form of pandas DataFrameExample:output_path="<path_to_your_output_directory>"# "result" for exampledi.save_data(output_path,aligned,"aligned")# if you want to save the file as csvdi.save_data(output_path,aligned,"csv")Functions to be ImplementedInsert/ Filter/ DeleteCreate Tbi Index