From c9b0ab18457929bd7ca458f7207e50fe14099d6a Mon Sep 17 00:00:00 2001 From: zsloan Date: Mon, 9 Apr 2018 16:51:54 +0000 Subject: Removed the basicStatistics directory/contents because nothing there was being used except corestats, which I moved to utility Removed box plot code from Plot.py since we no longer use it either --- wqflask/utility/corestats.py | 99 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 wqflask/utility/corestats.py (limited to 'wqflask/utility/corestats.py') diff --git a/wqflask/utility/corestats.py b/wqflask/utility/corestats.py new file mode 100644 index 00000000..c48183ed --- /dev/null +++ b/wqflask/utility/corestats.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python + +# corestats.py (COREy STATS) +# Copyright (c) 2006-2007, Corey Goldberg (corey@goldb.org) +# +# statistical calculation class +# for processing numeric sequences +# +# license: GNU LGPL +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. + +import sys + +#ZS: Should switch to using some third party library for this; maybe scipy has an equivalent +class Stats: + + def __init__(self, sequence): + # sequence of numbers we will process + # convert all items to floats for numerical processing + self.sequence = [float(item) for item in sequence] + + + def sum(self): + if len(self.sequence) < 1: + return None + else: + return sum(self.sequence) + + + def count(self): + return len(self.sequence) + + + def min(self): + if len(self.sequence) < 1: + return None + else: + return min(self.sequence) + + + def max(self): + if len(self.sequence) < 1: + return None + else: + return max(self.sequence) + + + def avg(self): + if len(self.sequence) < 1: + return None + else: + return sum(self.sequence) / len(self.sequence) + + + def median(self): + if len(self.sequence) < 1: + return None + else: + self.sequence.sort() + return self.sequence[len(self.sequence) // 2] + + + def stdev(self): + if len(self.sequence) < 1: + return None + else: + avg = self.avg() + sdsq = sum([(i - avg) ** 2 for i in self.sequence]) + stdev = (sdsq / (len(self.sequence) - 1)) ** .5 + return stdev + + + def percentile(self, percentile): + if len(self.sequence) < 1: + value = None + elif (percentile >= 100): + sys.stderr.write('ERROR: percentile must be < 100. you supplied: %s\n'% percentile) + value = None + else: + element_idx = int(len(self.sequence) * (percentile / 100.0)) + self.sequence.sort() + value = self.sequence[element_idx] + return value + + +# Sample script using this class: +# ------------------------------------------- +# #!/usr/bin/env python +# import corestats +# +# sequence = [1, 2.5, 7, 13.4, 8.0] +# stats = corestats.Stats(sequence) +# print stats.avg() +# print stats.percentile(90) +# ------------------------------------------- \ No newline at end of file -- cgit v1.2.3 From ad71cfe2828a2cc7378b83c0b0f476b53e7dd03b Mon Sep 17 00:00:00 2001 From: zsloan Date: Mon, 9 Apr 2018 17:18:15 +0000 Subject: Removed unused code from convert_geno_to_bimbam.py and corestats.py --- wqflask/maintenance/convert_geno_to_bimbam.py | 77 +++++---------------------- wqflask/utility/corestats.py | 16 ------ 2 files changed, 12 insertions(+), 81 deletions(-) (limited to 'wqflask/utility/corestats.py') diff --git a/wqflask/maintenance/convert_geno_to_bimbam.py b/wqflask/maintenance/convert_geno_to_bimbam.py index 05006d5c..45522705 100644 --- a/wqflask/maintenance/convert_geno_to_bimbam.py +++ b/wqflask/maintenance/convert_geno_to_bimbam.py @@ -17,17 +17,12 @@ import glob import traceback import gzip -#import numpy as np -#from pyLMM import lmm - import simplejson as json from pprint import pformat as pf class EmptyConfigurations(Exception): pass - - class Marker(object): def __init__(self): self.name = None @@ -39,47 +34,34 @@ class Marker(object): class ConvertGenoFile(object): def __init__(self, input_file, output_files): - self.input_file = input_file self.output_files = output_files - + self.mb_exists = False self.cm_exists = False self.markers = [] - + self.latest_row_pos = None self.latest_col_pos = None - + self.latest_row_value = None self.latest_col_value = None - - def convert(self): + def convert(self): self.haplotype_notation = { '@mat': "1", '@pat': "0", '@het': "0.5", '@unk': "NA" } - + self.configurations = {} - #self.skipped_cols = 3 - - #if self.input_file.endswith(".geno.gz"): - # print("self.input_file: ", self.input_file) - # self.input_fh = gzip.open(self.input_file) - #else: self.input_fh = open(self.input_file) - - with open(self.output_files[0], "w") as self.geno_fh: - #if self.file_type == "geno": - self.process_csv() - #elif self.file_type == "snps": - # self.process_snps_file() + self.process_csv() def process_csv(self): - for row_count, row in enumerate(self.process_rows()): + for row in self.process_rows(): row_items = row.split("\t") this_marker = Marker() @@ -102,53 +84,30 @@ class ConvertGenoFile(object): this_marker.genotypes.append(self.configurations[genotype.upper().strip()]) else: this_marker.genotypes.append("NA") - - #print("this_marker is:", pf(this_marker.__dict__)) - #if this_marker.chr == "14": + self.markers.append(this_marker.__dict__) self.write_to_bimbam() - - # with open(self.output_file, 'w') as fh: - # json.dump(self.markers, fh, indent=" ", sort_keys=True) - - # print('configurations:', str(configurations)) - #self.latest_col_pos = item_count + self.skipped_cols - #self.latest_col_value = item - - #if item_count != 0: - # self.output_fh.write(" ") - #self.output_fh.write(self.configurations[item.upper()]) - - #self.output_fh.write("\n") def write_to_bimbam(self): with open(self.output_files[0], "w") as geno_fh: - # geno_fh.write(str(len(self.sample_list)) + "\n") - # geno_fh.write("2\n") - # geno_fh.write("IND") - # for sample in self.sample_list: - # geno_fh.write(" " + sample) - # geno_fh.write("\n") for marker in self.markers: geno_fh.write(marker['name']) geno_fh.write(", X, Y") geno_fh.write(", " + ", ".join(marker['genotypes'])) geno_fh.write("\n") - - #pheno_fh = open(self.output_files[1], 'w') + with open(self.output_files[1], "w") as pheno_fh: for sample in self.sample_list: pheno_fh.write("1\n") - + with open(self.output_files[2], "w") as snp_fh: for marker in self.markers: if self.mb_exists: snp_fh.write(marker['name'] +", " + str(int(float(marker['Mb'])*1000000)) + ", " + marker['chr'] + "\n") else: snp_fh.write(marker['name'] +", " + str(int(float(marker['cM'])*1000000)) + ", " + marker['chr'] + "\n") - - + def get_sample_list(self, row_contents): self.sample_list = [] if self.mb_exists: @@ -164,8 +123,6 @@ class ConvertGenoFile(object): def process_rows(self): for self.latest_row_pos, row in enumerate(self.input_fh): - #if self.input_file.endswith(".geno.gz"): - # print("row: ", row) self.latest_row_value = row # Take care of headers if not row.strip(): @@ -208,10 +165,8 @@ class ConvertGenoFile(object): convertob.convert() except EmptyConfigurations as why: print(" No config info? Continuing...") - #excepted = True continue except Exception as why: - print(" Exception:", why) print(traceback.print_exc()) print(" Found in row %s at tabular column %s" % (convertob.latest_row_pos, @@ -219,12 +174,6 @@ class ConvertGenoFile(object): print(" Column is:", convertob.latest_col_value) print(" Row is:", convertob.latest_row_value) break - - #def process_snps_file(cls, snps_file, new_directory): - # output_file = os.path.join(new_directory, "mouse_families.json") - # print("%s -> %s" % (snps_file, output_file)) - # convertob = ConvertGenoFile(input_file, output_file) - if __name__=="__main__": Old_Geno_Directory = """/home/zas1024/genotype_files/genotype/""" @@ -234,6 +183,4 @@ if __name__=="__main__": #convertob = ConvertGenoFile("/home/zas1024/gene/genotype_files/genotypes/SRxSHRSPF2.geno", "/home/zas1024/gene/genotype_files/new_genotypes/SRxSHRSPF2.json") #convertob.convert() ConvertGenoFile.process_all(Old_Geno_Directory, New_Geno_Directory) - #ConvertGenoFiles(Geno_Directory) - - #process_csv(Input_File, Output_File) \ No newline at end of file + #ConvertGenoFiles(Geno_Directory) \ No newline at end of file diff --git a/wqflask/utility/corestats.py b/wqflask/utility/corestats.py index c48183ed..67ca3ad3 100644 --- a/wqflask/utility/corestats.py +++ b/wqflask/utility/corestats.py @@ -23,47 +23,33 @@ class Stats: # convert all items to floats for numerical processing self.sequence = [float(item) for item in sequence] - def sum(self): if len(self.sequence) < 1: return None else: return sum(self.sequence) - def count(self): return len(self.sequence) - def min(self): if len(self.sequence) < 1: return None else: return min(self.sequence) - def max(self): if len(self.sequence) < 1: return None else: return max(self.sequence) - def avg(self): if len(self.sequence) < 1: return None else: return sum(self.sequence) / len(self.sequence) - - def median(self): - if len(self.sequence) < 1: - return None - else: - self.sequence.sort() - return self.sequence[len(self.sequence) // 2] - - def stdev(self): if len(self.sequence) < 1: return None @@ -73,7 +59,6 @@ class Stats: stdev = (sdsq / (len(self.sequence) - 1)) ** .5 return stdev - def percentile(self, percentile): if len(self.sequence) < 1: value = None @@ -86,7 +71,6 @@ class Stats: value = self.sequence[element_idx] return value - # Sample script using this class: # ------------------------------------------- # #!/usr/bin/env python -- cgit v1.2.3