<reponame>kaclark/DHS_intergenic_analysis
#Gathers SMCs that DHS site has membership in

#mm10_SMCs.csv generated by modification of Genome liftover of mm9_SMCs from SMC paper in SMC_id_assignment.py
#DHS_intergenic_#.csv generated by UNKONWN

#Exports DHS_#_with_SMC.csv

import pandas as pd 
import matplotlib.pyplot as plt
import csv

printed = False
def duplicates(list, item):
    """Returns index locations of item in list"""
    return [i for i, x in enumerate(list) if x == item]

chromosomes = []
start = []
end = []
id = []
chromosome_locations = {}

#load SMC data
SMC_data = pd.read_csv("data/mm10_data/SMC/mm10_SMCs.csv", header=None, index_col=False)
# #Get rid of header row in csv
# SMC_data = SMC_data.iloc[1:] -- header does not exist in mm10 file
for row in SMC_data[0]:
    chromosomes.append(row)
for row in SMC_data[1]:
    start.append(row)
for row in SMC_data[2]:
    end.append(row)
for row in SMC_data[3]:
    id.append(row)

uq_chrom = set(chromosomes)
for item in uq_chrom:
    chromosome_locations[item] = duplicates(chromosomes, item)
SMC_groups = []
for x in range(len(start)):
    SMC_groups.append([chromosomes[x],start[x], end[x], id[x]])
SMC_groups_1 = {}
SMC_groups_2 = {}
SMC_groups_4 = {}
SMC_groups_8 = {}
SMC_dhs_count_1 = []
SMC_dhs_count_2 = []
SMC_dhs_count_4 = []
SMC_dhs_count_8 = []
files = ['1','2','4','8']
for file in files:
    DHS_data = []
    DHS_chromosomes = []
    DHS_start = []
    DHS_end = []
    DHS_type = []
    DHS_annotated = []
    mismatched_SMCs = []
    SMC_dhs_count = None
    if file == '1':
        SMC_group = SMC_groups_1
        SMC_dhs_count = SMC_dhs_count_1
    if file == '2':
        SMC_group = SMC_groups_2
        SMC_dhs_count = SMC_dhs_count_2
    if file == '4':
        SMC_group = SMC_groups_4
        SMC_dhs_count = SMC_dhs_count_4
    if file == '8':
        SMC_group = SMC_groups_8 
        SMC_dhs_count = SMC_dhs_count_8
    #load DHS data
    #TODO: Find where this is before rerunning
    csv_file = "data/mm10_data/DHSs_intergenic_" + file + ".csv"
    DHS_data_df = pd.read_csv(csv_file, header=None, index_col=False)
    for row in DHS_data_df[0]:
        DHS_chromosomes.append(row)
    for row in DHS_data_df[1]:
        DHS_start.append(row)
    for row in DHS_data_df[2]:
        DHS_end.append(row)
    for row in DHS_data_df[3]:
        DHS_type.append(row)
    for x in range(len(DHS_chromosomes)):
        DHS_data.append([DHS_chromosomes[x], DHS_start[x], DHS_end[x], DHS_type[x]])

    #Iterate through chromosomes
    for chrom in uq_chrom:
        chrom_SMCs = []
        chrom_DHSs = []
        #populate the SMCs for this chromosome
        for SMC_data in SMC_groups:
            if SMC_data[0] == chrom:
                chrom_SMCs.append(SMC_data)
        #populate the dhs sites for this chromosome
        for dhs_data in DHS_data:
            if dhs_data[0] == chrom:
                chrom_DHSs.append(dhs_data)
        #For each SMC, assign it to the DHS site if the DHS site is within the SMC bounds
        for SMC in chrom_SMCs:
            dhs_in_SMC = []
            for dhs in chrom_DHSs:
                if int(SMC[1]) < int(dhs[1]) and int(dhs[2]) < int(SMC[2]):
                    dhs_in_SMC.append(dhs)
                    dhs.append(SMC[3])
                    DHS_annotated.append(dhs)
    #Export data
    DHS_annotated_df = pd.DataFrame(DHS_annotated)
    path = "data/mm10_data/DHS_" + file + "_cell_with_SMC.csv"
    DHS_annotated_df.to_csv(path, index=False, header=False)


