# Path: AGEpy/gtf.py
# def GTFtoBED(inGTF,name):
#     """
#     Transform a GTF dataframe into a bed dataframe
# 
#     :param inGTF: GTF dataframe for transformation
#     :param name: field of the GTF data frame to be use for the bed 'name' positon
# 
#     returns: a bed dataframe with the corresponding bed fiels: 'chrom','chromStart','chromEnd','name','score','strand'
#     """
# 
#     bed=inGTF.copy()
#     bed.reset_index(inplace=True, drop=True)
#     if name not in bed.columns.tolist():
#         field=retrieve_GTF_field(name, bed)
#         bed=pd.concat([bed,field],axis=1)
#     bed=bed[['seqname','start','end',name,'score','strand']]
#     bed.columns=['chrom','chromStart','chromEnd','name','score','strand']
#     bed.drop_duplicates(inplace=True)
#     bed.reset_index(inplace=True,drop=True)
#     return bed
# 
# Path: AGEpy/gtf.py
# def readGTF(infile):
#     """
#     Reads a GTF file and labels the respective columns in agreement with GTF file standards:
#     'seqname','source','feature','start','end','score','strand','frame','attribute'.
# 
#     :param infile: path/to/file.gtf
#     :returns: a Pandas dataframe of the respective GTF
# 
#     """
#     df=pd.read_table(infile, sep='\t', comment="#", header=None, dtype=str)
#     df.columns=['seqname','source','feature','start','end','score','strand','frame','attribute']
#     #df = df.astype(str) # from DTC
#     return df
# 
# Path: AGEpy/gtf.py
# def retrieve_GTF_field(field,gtf):
#     """
#     Returns a field of choice from the attribute column of the GTF
# 
#     :param field: field to be retrieved
#     :returns: a Pandas dataframe with one columns containing the field of choice
# 
#     """
#     inGTF=gtf.copy()
#     def splits(x):
#         l=x.split(";")
#         l=[ s.split(" ") for s in l]
#         res=np.nan
#         for s in l:
#             if field in s:
#                 if '"' in s[-1]:
#                     res=s[-1][1:-1]
#                 else:
#                     res=s[-1]
#         return res
# 
#     inGTF[field]=inGTF['attribute'].apply(lambda x: splits(x))
#     return inGTF[[field]]


import pandas as pd
import numpy as np
import os
import urllib.request as urllib2
import gzip
import pybedtools
import sys
from io import StringIO
from pybedtools import BedTool
from .gtf import GTFtoBED
from .gtf import readGTF
from .gtf import retrieve_GTF_field
    Transforms a pandas dataframe into a bedtool

    :param df: Pandas dataframe

    :returns: a bedtool
    """

    df=df.astype(str)
    df=df.drop_duplicates()
    df=df.values.tolist()
    df=["\t".join(s) for s in df ]
    df="\n".join(df)
    df=BedTool(df, from_string=True)
    return df

def GetPeaksExons(bed,parsedGTF):
    """
    Annotates a bedtool, BED narrow peak

    :param bed: a pandas dataframe in bed format
    :param parsedGTF: a parsed GTF file as outputed by parseGTF() with the following columns

    :returns: a Pandas dataframe
    """

    bedtool_AB=dfTObedtool(bed)

    exonsGTF=parsedGTF[parsedGTF["feature"]=="exon"]
    exonsGTF.reset_index(inplace=True, drop=True)


# Path: AGEpy/gtf.py
# def GTFtoBED(inGTF,name):
#     """
#     Transform a GTF dataframe into a bed dataframe
# 
#     :param inGTF: GTF dataframe for transformation
#     :param name: field of the GTF data frame to be use for the bed 'name' positon
# 
#     returns: a bed dataframe with the corresponding bed fiels: 'chrom','chromStart','chromEnd','name','score','strand'
#     """
# 
#     bed=inGTF.copy()
#     bed.reset_index(inplace=True, drop=True)
#     if name not in bed.columns.tolist():
#         field=retrieve_GTF_field(name, bed)
#         bed=pd.concat([bed,field],axis=1)
#     bed=bed[['seqname','start','end',name,'score','strand']]
#     bed.columns=['chrom','chromStart','chromEnd','name','score','strand']
#     bed.drop_duplicates(inplace=True)
#     bed.reset_index(inplace=True,drop=True)
#     return bed
# 
# Path: AGEpy/gtf.py
# def readGTF(infile):
#     """
#     Reads a GTF file and labels the respective columns in agreement with GTF file standards:
#     'seqname','source','feature','start','end','score','strand','frame','attribute'.
# 
#     :param infile: path/to/file.gtf
#     :returns: a Pandas dataframe of the respective GTF
# 
#     """
#     df=pd.read_table(infile, sep='\t', comment="#", header=None, dtype=str)
#     df.columns=['seqname','source','feature','start','end','score','strand','frame','attribute']
#     #df = df.astype(str) # from DTC
#     return df
# 
# Path: AGEpy/gtf.py
# def retrieve_GTF_field(field,gtf):
#     """
#     Returns a field of choice from the attribute column of the GTF
# 
#     :param field: field to be retrieved
#     :returns: a Pandas dataframe with one columns containing the field of choice
# 
#     """
#     inGTF=gtf.copy()
#     def splits(x):
#         l=x.split(";")
#         l=[ s.split(" ") for s in l]
#         res=np.nan
#         for s in l:
#             if field in s:
#                 if '"' in s[-1]:
#                     res=s[-1][1:-1]
#                 else:
#                     res=s[-1]
#         return res
# 
#     inGTF[field]=inGTF['attribute'].apply(lambda x: splits(x))
#     return inGTF[[field]]


# Path: AGEpy/bed.py
import pandas as pd
import numpy as np
import os
import urllib.request as urllib2
import gzip
import pybedtools
import sys
from io import StringIO
from pybedtools import BedTool
from .gtf import GTFtoBED
from .gtf import readGTF
from .gtf import retrieve_GTF_field

    Transforms a pandas dataframe into a bedtool

    :param df: Pandas dataframe

    :returns: a bedtool
    """

    df=df.astype(str)
    df=df.drop_duplicates()
    df=df.values.tolist()
    df=["\t".join(s) for s in df ]
    df="\n".join(df)
    df=BedTool(df, from_string=True)
    return df

def GetPeaksExons(bed,parsedGTF):
    """
    Annotates a bedtool, BED narrow peak

    :param bed: a pandas dataframe in bed format
    :param parsedGTF: a parsed GTF file as outputed by parseGTF() with the following columns

    :returns: a Pandas dataframe
    """

    bedtool_AB=dfTObedtool(bed)

    exonsGTF=parsedGTF[parsedGTF["feature"]=="exon"]
    exonsGTF.reset_index(inplace=True, drop=True)


    exonsBED=GTFtoBED(exonsGTF, "exon_id")