From bde077a7432df3d60711aa0c8354d5e49135cce1 Mon Sep 17 00:00:00 2001 From: Suman Neupane Date: Fri, 21 Apr 2017 18:46:17 -0400 Subject: [PATCH 1/2] Delete tree.py --- tree.py | 140 -------------------------------------------------------- 1 file changed, 140 deletions(-) delete mode 100644 tree.py diff --git a/tree.py b/tree.py deleted file mode 100644 index f210c20..0000000 --- a/tree.py +++ /dev/null @@ -1,140 +0,0 @@ -import random - -class node(object): - def __init__(self, ndnum): # initialization function - self.rsib = None # right sibling - self.lchild = None # left child - self.par = None # parent node - self.number = ndnum # node number - self.edgelen = 0.0 # branch length - self.height = 0.0 # height above root - self.deep = False # True if this node resulted from a deep coalescence - self.descendants = set([ndnum]) # set containing descendant leaf set - -def joinRandomPair(node_list, next_node_number, is_deep_coalescence): - # pick first of two lineages to join and delete from node_list - i = random.randint(1, len(node_list)) - ndi = node_list[i-1] - del node_list[i-1] - - # pick second of two lineages to join and delete from node_list - j = random.randint(1, len(node_list)) - ndj = node_list[j-1] - del node_list[j-1] - - # join selected nodes and add ancestor to node_list - ancnd = node(next_node_number) - ancnd.deep = is_deep_coalescence - ancnd.lchild = ndi - ancnd.descendants = set() - ancnd.descendants |= ndi.descendants - ancnd.descendants |= ndj.descendants - ndi.rsib = ndj - ndi.par = ancnd - ndj.par = ancnd - node_list.append(ancnd) - - return node_list - -def joinSpecificPair(node_list, i, j, next_node_number, is_deep_coalescence): - assert j != i - - ndi = None - for k,nd in enumerate(node_list): - if nd.number == i: - ndi = node_list.pop(k) - assert ndi is not None - - ndj = None - for k,nd in enumerate(node_list): - if nd.number == j: - ndj = node_list.pop(k) - assert ndj is not None - - # join selected nodes and add ancestor to node_list - ancnd = node(next_node_number) - ancnd.deep = is_deep_coalescence - ancnd.lchild = ndi - ancnd.descendants = set() - ancnd.descendants |= ndi.descendants - ancnd.descendants |= ndj.descendants - ndi.rsib = ndj - ndi.par = ancnd - ndj.par = ancnd - node_list.append(ancnd) - - return node_list - -def getPreorder(nd, start = True): - global _preorder - if start: - _preorder = [] - - _preorder.append(nd) - - if nd.lchild: - getPreorder(nd.lchild, False) - - if nd.rsib: - getPreorder(nd.rsib, False) - - return _preorder - -def flipAllNodesAbove(root): - node_list = getPreorder(root) - for nd in node_list: - if nd.lchild: - lc = nd.lchild - rc = lc.rsib - nd.lchild = rc - assert rc.rsib is None - rc.rsib = lc - lc.rsib = None - -def makeNewick(nd, brlen_scaler = 1.0, start = True): - global _newick - global _TL - if start: - _newick = '' - _TL = 0.0 - - if nd.lchild: - _newick += '(' - makeNewick(nd.lchild, brlen_scaler, False) - else: - blen = nd.edgelen*brlen_scaler - _TL += blen - _newick += '%d:%.5f' % (nd.number, blen) - - if nd.rsib: - _newick += ',' - makeNewick(nd.rsib, brlen_scaler, False) - elif nd.par is not None: - blen = nd.par.edgelen*brlen_scaler - _TL += blen - _newick += '):%.5f' % blen - - return _newick, _TL - -def calcActualHeight(root): - h = 0.0 - nd = root - while nd.lchild: - nd = nd.lchild - h += nd.edgelen - return h - -def sumEdgeLengths(root): - sum_edge_lengths = 0.0 - pre = getPreorder(root) - for nd in pre: - sum_edge_lengths += nd.edgelen - return sum_edge_lengths - -def countDeepCoalescences(root): - n = 0 - preorder = getPreorder(root) - for nd in preorder: - if nd.deep: - n += 1 - return n From 20c438eb5cc43b3a4bbb499e5f32791772507bb2 Mon Sep 17 00:00:00 2001 From: Suman Neupane Date: Fri, 21 Apr 2017 18:57:01 -0400 Subject: [PATCH 2/2] Delete readseq.py --- readseq.py | 89 ------------------------------------------------------ 1 file changed, 89 deletions(-) delete mode 100644 readseq.py diff --git a/readseq.py b/readseq.py deleted file mode 100644 index 75bdb56..0000000 --- a/readseq.py +++ /dev/null @@ -1,89 +0,0 @@ -def patterns(): - - - import re, os, glob, itertools, fnmatch, sys, shutil - from itertools import combinations - from collections import Counter - - script_dir = os.path.dirname(os.path.realpath(sys.argv[0])) - path = os.path.join(script_dir, 'nexus') - - - genes = [] - data = {} -# print 'Reading nexus files...' - for filename in glob.glob(os.path.join(path, '*.nex')): - - m = re.match('(.+).nex', os.path.basename(filename)) - gene_name = m.group(1) -# print 'gene_name=', gene_name - genes.append(gene_name) - f = open(filename, 'r').read() - - m = re.search('ntax\s*=\s*(\d+)', f, re.M | re.S) - ntax = int(m.group(1)) -# print 'ntax=', ntax - - m = re.search('nchar\s*=\s*(\d+)', f, re.M | re.S) - nchar = int(m.group(1)) -# print 'nchar=', nchar - - - m = re.search('Matrix\s+(.+?);', f, re.M | re.S) - matrix = m.group(1).strip() - matrix_lines = matrix.split('\n') - - taxon_names = [] - sequences = {} - sequences_list = [] - for line in matrix_lines: - parts = line.strip().split() - assert len(parts) == 2 - taxon_name = parts[0] - sequence = parts[1] - # print 'sequence===', sequence - - taxon_names.append(taxon_name) - sequences_list.append(sequence) - sequences[taxon_name] = sequence - - - - # print 'sequences dict==', sequences -# print 'sequences list==', sequences_list - - pattern_list = [] - - k=0 - while k < nchar: - site_pattern = '' - for i,m in enumerate(sequences_list): - site_pattern += m[k] - # print 'site_pattern=', site_pattern - pattern_list.append(site_pattern) - k+=1 -# print '!!!!!!!!' -# print pattern_list -# print - pattern_dict = dict() - for i in pattern_list: - pattern_dict[i] = pattern_dict.get(i, 0) + 1 #http://www.pythonlearn.com/html-008/cfbook010.html# - -# print'------------' -# print pattern_dict -# print - tmp = [] - for key in pattern_dict.keys(): ###convert dict to key of tupules -# print 'key=', key - tmp.append((pattern_dict[key],key)) - - sorted_values = sorted(tmp) ###sorted according to key smaller to larger -# print sorted_values -# print '**********' - # sorted_values.reverse() ###sorted according to key values larger to smaller - sorted_values.sort(cmp = lambda x,y:cmp(x[1],y[1])) ###sorted according to values in alphabetical order -# print sorted_values - - return pattern_dict - -