"""
Function of this code: data preprocess.
Contact for help: gch_liu@163.com 
""" 

import pandas as pd
import re
# Make all sequence letters uppercase
def firsttrans(string):
    string=string.upper()
    string = string.replace('\n', '')
    string = string.replace('\t', '')
    # pattern=re.compile(r'([A-Z])')
    # temp=pattern.findall(string)
    # return ''.join(temp)
    return string

def Isother(string):
    pattern=re.compile(r'[^A|C|G|T]')
    ish=re.search(pattern,string)
    print(ish)
    return ish
def start(filename,label):
    original_data=pd.DataFrame(columns=("name","value"))      
    data=pd.DataFrame(columns=("name","value"))  
#    original_data=pd.DataFrame()      
#    data=pd.DataFrame()    
    flag=0
    temp_name=''
    temp_value=''
    for line in open(filename,'r+'):
        if line[0] == '>':
            if flag!=0:
                temp_value=firsttrans(temp_value)
                original_data.loc[flag]=[temp_name.split()[0],temp_value]
            temp_name=line
            temp_value=''
            flag += 1
            print("now ",flag)
        else:
            temp_value+=line
    temp_value=firsttrans(temp_value)
    original_data.loc[flag]=[temp_name.split()[0],temp_value]
    flag=1
#    print(original_data.iloc[0][1])
    line_index = []
    for index in original_data.index:
        index=index-1
        name=original_data.iloc[index][0]
        string=original_data.iloc[index][1]
        if Isother(string):
            print(1)
            continue
        else:
            print(0)
            flag+=1
            data.loc[flag]=[name,string]
            line_index.append(index)
#    original_data.iloc[:,0].to_csv(label+'original_seq_name.txt',index=False)
#    original_data.iloc[:,1].to_csv(label+'original_seq_data.txt',index=False)
    data.iloc[:,0].to_csv(label+'preprossed_seq_name.txt',index=False)
    data.iloc[:,1].to_csv(label+'preprossed_seq_data.txt',index=False)
    return data, line_index
if __name__=='__main__':
    start('example_test.fasta',"human")
