def mC1P_bab(mat, matb = bm.BinaryMatrix(), mat_rem = bm.BinaryMatrix()):
	ms = c1p.make_intersect_components(mat)		# intersect components
	
	j = 1		# iterator for tracing
	
	for m in ms:
		print str(j) + '/' + str(len(ms)) + ' ',
		sys.stdout.flush()
		
		j += 1
		
		# sort matrix to get heuristic as first answer
		m.sort()
		
		# branch and bound
		# optimal rows to remove to make compnonent mC1P
		rows = bab.branch_and_bound(m, False, MC1PTester(m))
		
		rows.sort()
		
		for i in xrange(len(rows) - 1, -1, -1):
			row = m.get_row_info(rows[i])		# row to remove
						
			mat_rem.add_row_info(row)
				
			m.remove_row(rows[i])
		#endfor
		
		# collect usable rows into the C1P matrix
		for r in m._rows:
			matb.add_row_info(r)
		#endfor
		
		print ''
	#endfor
	
	return matb, mat_rem
def C1P_bab(mat, matb = bm.BinaryMatrix(), mat_rem = bm.BinaryMatrix()):
	ms = c1p.split_matrix(mat)		# split matrices
				
	j = 1		# iterator for tracing
		
	for m in ms:
		print str(j) + '/' + str(len(ms))
		
		j += 1
		
		# sort matrix to get heuristic as first answer
		m.sort()
				
		# branch and bound
		# optimal rows to remove to make compnonent C1P
		rows = bab.branch_and_bound(m, False, BABTester(m._height))
			
		rows.sort()
			
		for i in xrange(len(rows) - 1, -1, -1):
			row = m.get_row_info(rows[i])		# row to remove
						
			mat_rem.add_row_info(row)
				
			m.remove_row(rows[i])
		#endfor
		
		# collect usable rows into the C1P matrix
		for r in m._rows:
			matb.add_row_info(r)
		#endfor
		
		print ''
	#endfor
	
	return matb, mat_rem
j = 1  # iterator for tracing

del mat

for m in ms:
    print str(j) + '/' + str(len(ms))

    j += 1

    # sort matrix to get heuristic as first answer
    m.sort()

    # branch and bound
    # optimal rows to remove to make compnonent C1P
    rows = bab.branch_and_bound(m, prop, BABTester(m._height))

    rows.sort()

    for i in xrange(len(rows) - 1, -1, -1):
        row = m.get_row_info(rows[i])  # row to remove

        mat_rem.add_row_info(row)

        m.remove_row(rows[i])
    #endfor

    # collect usable rows into the C1P matrix
    for r in m._rows:
        matb.add_row_info(r)
    #endfor
Exemple #4
0
mat_rem = bm.BinaryMatrix()  # rows removed

j = 1  # iterator for tracing

for m in ms:
    print str(j) + '/' + str(len(ms)) + ' ',
    sys.stdout.flush()

    j += 1

    # sort matrix to get heuristic as first answer
    m.sort()

    # branch and bound
    # optimal rows to remove to make compnonent mC1P
    rows = bab.branch_and_bound(m, prop, MC1PTester(m))

    rows.sort()

    for i in xrange(len(rows) - 1, -1, -1):
        row = m.get_row_info(rows[i])  # row to remove

        mat_rem.add_row_info(row)

        m.remove_row(rows[i])
    #endfor

    # collect usable rows into the C1P matrix
    for r in m._rows:
        matb.add_row_info(r)
    #endfor
Exemple #5
0
    def do_c1p(self, suffix, message, make_C1P, compute_PQRtree, m=False):
        acs_c1p = self.c1p_dir + "/" + self.output_prefix + "ACS_C1P_" + suffix  # C1P matrix file
        acs_discarded = self.c1p_dir + "/" + self.output_prefix + "ACS_DISC_" + suffix  # matrix of removed rows file
        pq_tree = self.cars_dir + "/" + self.output_prefix + "PQTREE_" + suffix  # PQ-tree file
        if self.markers_doubled:
            pq_tree_doubled = self.cars_dir + "/" + self.output_prefix + "PQTREE_DOUBLED_" + suffix  # PQ-tree file
        #endif
        if not self.quiet:
            print("----> Making (weighted)ACS matrix C1P (" + message + "): " +
                  self.acs_file + " " + acs_discarded)
        #endif

        if m:
            if make_C1P == "heuristic":
                #m = bm.BinaryMatrix()       # matrix
                mat_rem = bm.BinaryMatrix()  # rows discarded

                #m.from_file(self.acs_file)

                rows = c1p.make_C1P(self.m)  # rows to remove to make C1P

                for j in xrange(len(rows) - 1, -1, -1):
                    mat_rem.add_row_info(m.get_row_info(rows[j]))

                    self.m.remove_row(rows[j])
                #endfor

                f = file(acs_c1p, 'w')  # C1P matrix file

                f.write(str(m))

                f.close()

                f = open(acs_discarded, 'w')

                mat_rem.write(f.write)

                f.close()

            elif make_C1P == "circ_heuristic":
                #m = bm.BinaryMatrix()

                #m.from_file(self.acs_file)

                mat2, mat_rem = cc1p.make_circC1P(self.m, 'max')

                f = open(self.acs_c1p, 'w')

                mat2.write(f.write)

                f.close()

                f = open(self.acs_discarded, 'w')

                mat_rem.write(f.write)

                f.close()

            elif make_C1P == "branch_and_bound":
                prop = False  # True if using weights as probs
                #mat = bm.BinaryMatrix()     # matrix

                #m.from_file(self.acs_file)

                ms = c1p.split_matrix(self.m)  # split matrices
                matb = bm.BinaryMatrix()  # C1P matrix
                mat_rem = bm.BinaryMatrix()  # rows removed

                j = 1  # iterator for tracing

                #del mat

                for m in ms:
                    print str(j) + '/' + str(len(ms))

                    j += 1

                    # sort matrix to get heuristic as first answer
                    m.sort()

                    # branch and bound
                    # optimal rows to remove to make compnonent C1P
                    rows = bab.branch_and_bound(m, prop, BABTester(m._height))

                    rows.sort()

                    for i in xrange(len(rows) - 1, -1, -1):
                        row = self.m.get_row_info(rows[i])  # row to remove

                        mat_rem.add_row_info(row)

                        m.remove_row(rows[i])
                    #endfor

                    # collect usable rows into the C1P matrix
                    for r in m._rows:
                        matb.add_row_info(r)
                    #endfor

                    print ''
                #endfor

                f = file(acs_c1p, 'w')

                f.write(str(matb))

                f.close()

                f = file(acs_discarded, 'w')

                f.write(str(mat_rem))

                f.close()
            elif make_C1P == "circ_branch_and_bound":
                #mat = bm.BinaryMatrix()     # matrix

                #mat.from_file(self.acs_file)

                mode = 'max'

                if mode == 'whole':
                    matb, mat_rem = circC1P_bab(self.m)
                elif mode == 'max':
                    maxs = c1p.make_intersect_components(
                        self.m)  # split matrices
                    matb = bm.BinaryMatrix()  # C1P matrix
                    mat_rem = bm.BinaryMatrix()  # rows removed

                    j = 1  # iterator for tracing

                    #del mat

                    for max_comp in maxs:
                        print 'Max:' + str(j) + '/' + str(len(maxs)) + ' '

                        j += 1

                        circC1P_bab(max_comp, matb, mat_rem)
                    #endfor
                #endif

                f = file(self.acs_c1p, 'w')

                f.write(str(matb))

                f.close()

                f = file(self.acs_discarded, 'w')

                f.write(str(mat_rem))

                f.close()

            elif make_C1P == "m_heuristic":
                #m = bm.BinaryMatrix()       # matrix
                mat_rem = bm.BinaryMatrix()  # rows discarded

                #m.from_file(self.acs_file)

                rows = mc1p.make_mC1P(m)  # rows to remove to make C1P

                for j in xrange(len(rows) - 1, -1, -1):
                    mat_rem.add_row_info(self.m.get_row_info(rows[j]))

                    self.m.remove_row(rows[j])
                #endfor

                f = file(self.acs_c1p, 'w')  # C1P matrix file

                f.write(str(self.m))

                f.close()

                f = open(self.acs_discarded, 'w')

                mat_rem.write(f.write)

                f.close()

            elif make_C1P == "m_branch_and_bound":
                prop = False
                #mat = bm.BinaryMatrix()     # matrix
                rows_rem = []  # rows removed

                #mat.from_file(self.acs_file)

                ms = c1p.make_intersect_components(
                    self.m)  # intersect components
                matb = bm.BinaryMatrix()  # mC1P matrix
                mat_rem = bm.BinaryMatrix()  # rows removed

                j = 1  # iterator for tracing

                for m in ms:
                    print str(j) + '/' + str(len(ms)) + ' ',
                    sys.stdout.flush()

                    j += 1

                    # sort matrix to get heuristic as first answer
                    self.m.sort()

                    # branch and bound
                    # optimal rows to remove to make compnonent mC1P
                    rows = bab.branch_and_bound(m, prop, MC1PTester(m))

                    rows.sort()

                    for i in xrange(len(rows) - 1, -1, -1):
                        row = m.get_row_info(rows[i])  # row to remove

                        mat_rem.add_row_info(row)

                        m.remove_row(rows[i])
                    #endfor

                    # collect usable rows into the C1P matrix
                    for r in self.m._rows:
                        matb.add_row_info(r)
                    #endfor

                    print ''
                #endfor

                f = file(acs_c1p, 'w')

                f.write(str(matb))

                f.close()

                f = file(acs_discarded, 'w')

                f.write(str(mat_rem))

                f.close()

            elif make_C1P == "m_branch_and_bound_both":
                prop = False  # True if using weights as probs
                #mat = bm.BinaryMatrix()     # matrix

                #mat.from_file(self.acs_file)

                matb, mat_rem = C1P_and_mC1P_bab(self.m)

                f = file(acs_c1p, 'w')

                f.write(str(matb))

                f.close()

                f = file(acs_discarded, 'w')

                f.write(str(mat_rem))

                f.close()

        else:
            if make_C1P == "heuristic":
                #m = bm.BinaryMatrix()       # matrix
                mat_rem = bm.BinaryMatrix()  # rows discarded

                #m.from_file(self.acs_file)

                rows = c1p.make_C1P(self.m)  # rows to remove to make C1P

                for j in xrange(len(rows) - 1, -1, -1):
                    mat_rem.add_row_info(m.get_row_info(rows[j]))

                    self.m.remove_row(rows[j])
                #endfor

                f = file(acs_c1p, 'w')  # C1P matrix file

                f.write(str(m))

                f.close()

                f = open(acs_discarded, 'w')

                mat_rem.write(f.write)

                f.close()

            elif make_C1P == "circ_heuristic":
                #m = bm.BinaryMatrix()

                #m.from_file(self.acs_file)

                mat2, mat_rem = cc1p.make_circC1P(self.m, 'max')

                f = open(self.acs_c1p, 'w')

                mat2.write(f.write)

                f.close()

                f = open(self.acs_discarded, 'w')

                mat_rem.write(f.write)

                f.close()

            elif make_C1P == "branch_and_bound":
                prop = False  # True if using weights as probs
                # mat = bm.BinaryMatrix()     # matrix

                # mat.from_file(self.acs_file)

                ms = c1p.split_matrix(self.m)  # split matrices
                matb = bm.BinaryMatrix()  # C1P matrix
                mat_rem = bm.BinaryMatrix()  # rows removed

                j = 1  # iterator for tracing

                del mat

                for m in ms:
                    print str(j) + '/' + str(len(ms))

                    j += 1

                    # sort matrix to get heuristic as first answer
                    m.sort()

                    # branch and bound
                    # optimal rows to remove to make compnonent C1P
                    rows = bab.branch_and_bound(m, prop, BABTester(m._height))

                    rows.sort()

                    for i in xrange(len(rows) - 1, -1, -1):
                        row = m.get_row_info(rows[i])  # row to remove

                        mat_rem.add_row_info(row)

                        m.remove_row(rows[i])
                    #endfor

                    # collect usable rows into the C1P matrix
                    for r in m._rows:
                        matb.add_row_info(r)
                    #endfor

                    print ''
                #endfor

                f = file(acs_c1p, 'w')

                f.write(str(matb))

                f.close()

                f = file(acs_discarded, 'w')

                f.write(str(mat_rem))

                f.close()
            elif make_C1P == "circ_branch_and_bound":
                # mat = bm.BinaryMatrix()     # matrix

                # mat.from_file(self.acs_file)

                mode = 'whole'

                if mode == 'whole':
                    matb, mat_rem = circC1P_bab(self.m)
                elif mode == 'max':
                    maxs = c1p.make_intersect_components(
                        self.m)  # split matrices
                    matb = bm.BinaryMatrix()  # C1P matrix
                    mat_rem = bm.BinaryMatrix()  # rows removed

                    j = 1  # iterator for tracing

                    # del mat

                    for max_comp in maxs:
                        print 'Max:' + str(j) + '/' + str(len(maxs)) + ' '

                        j += 1

                        circC1P_bab(max_comp, matb, mat_rem)
                    #endfor
                #endif

                f = file(self.acs_c1p, 'w')

                f.write(str(matb))

                f.close()

                f = file(self.acs_discarded, 'w')

                f.write(str(mat_rem))

                f.close()

            elif make_C1P == "m_heuristic":
                #m = bm.BinaryMatrix()       # matrix
                mat_rem = bm.BinaryMatrix()  # rows discarded

                #m.from_file(self.acs_file)

                rows = mc1p.make_mC1P(self.m)  # rows to remove to make C1P

                for j in xrange(len(rows) - 1, -1, -1):
                    mat_rem.add_row_info(m.get_row_info(rows[j]))

                    self.m.remove_row(rows[j])
                #endfor

                f = file(self.acs_c1p, 'w')  # C1P matrix file

                f.write(str(self.m))

                f.close()

                f = open(self.acs_discarded, 'w')

                mat_rem.write(f.write)

                f.close()

            elif make_C1P == "m_branch_and_bound":
                prop = False
                # mat = bm.BinaryMatrix()     # matrix
                rows_rem = []  # rows removed

                # mat.from_file(self.acs_file)

                ms = c1p.make_intersect_components(
                    self.m)  # intersect components
                matb = bm.BinaryMatrix()  # mC1P matrix
                mat_rem = bm.BinaryMatrix()  # rows removed

                j = 1  # iterator for tracing

                for m in ms:
                    print str(j) + '/' + str(len(ms)) + ' ',
                    sys.stdout.flush()

                    j += 1

                    # sort matrix to get heuristic as first answer
                    self.m.sort()

                    # branch and bound
                    # optimal rows to remove to make compnonent mC1P
                    rows = bab.branch_and_bound(m, prop, MC1PTester(m))

                    rows.sort()

                    for i in xrange(len(rows) - 1, -1, -1):
                        row = self.m.get_row_info(rows[i])  # row to remove

                        mat_rem.add_row_info(row)

                        m.remove_row(rows[i])
                    #endfor

                    # collect usable rows into the C1P matrix
                    for r in m._rows:
                        matb.add_row_info(r)
                    #endfor

                    print ''
                #endfor

                f = file(acs_c1p, 'w')

                f.write(str(matb))

                f.close()

                f = file(acs_discarded, 'w')

                f.write(str(mat_rem))

                f.close()

            elif make_C1P == "m_branch_and_bound_both":
                prop = False  # True if using weights as probs

                matb, mat_rem = C1P_and_mC1P_bab(self.m)

                f = file(acs_c1p, 'w')

                f.write(str(matb))

                f.close()

                f = file(acs_discarded, 'w')

                f.write(str(mat_rem))

                f.close()

        #endif

        if not self.quiet:
            print("----> Creating a PQ-tree: " + pq_tree)
        #endif

        if self.markers_doubled:
            self.computePQRtree()
            if not self.quiet:
                print("----> Halving PQ-tree columns")
            #endif
            self.halvePQRtree(pq_tree_doubled, pq_tree)
        else:
            self.computePQRtree()
def C1P_and_mC1P_bab(mat, matb = bm.BinaryMatrix(), mat_rem = bm.BinaryMatrix()):
	ms = c1p.make_intersect_components(mat)		# intersect components
	
	j = 1		# iterator for tracing
	
	for m_full in ms:
		print str(j) + '/' + str(len(ms)) + ' ',
		sys.stdout.flush()
		
		j += 1
		
		m = bm.BinaryMatrix()
		m_tel = bm.BinaryMatrix()
		
		# split telomere rows and non telomere rows
		for row in m_full:
			if row._isT:
				m_tel.add_row_info(row)
			else:
				m.add_row_info(row)
			#endif
		#endfor
		
		# C1P bab
		print 'C1P'
		sys.stdout.flush()
		
		st = mat_rem._height
		
		# sort matrix to get heuristic as first answer
		m.sort()
		
		# branch and bound
		# optimal rows to remove to make compnonent mC1P
		rows = bab.branch_and_bound(m, False, MC1PTester(m))
		
		rows.sort()
		
		for i in xrange(len(rows) - 1, -1, -1):
			row = m.get_row_info(rows[i])		# row to remove
						
			mat_rem.add_row_info(row)
				
			m.remove_row(rows[i])
		#endfor
		
		# collect usable rows into the C1P matrix
		for r in m._rows:
			matb.add_row_info(r)
		#endfor

		# remove unused telomere rows
		for i in xrange(st, mat_rem._height):
			row = mat_rem.get_row(i)
		
			for j in xrange(m_tel._height):
				if m_tel.get_row(j) == row:
					m_tel.remove_row(j)
					
					break
				#endif
			#endfor
		#endfor
	
		# mC1P bab
		print 'telomeres'
		sys.stdout.flush()
			
		# sort matrix to get heuristic as first answer
		m_tel.sort()
			
		# branch and bound
		# optimal rows to remove to make compnonent mC1P
		rows = bab.branch_and_bound(m_tel, False, PQMC1PTester(m))
			
		rows.sort()
			
		for i in xrange(len(rows) - 1, -1, -1):
			row = m_tel.get_row_info(rows[i])		# row to remove
							
			mat_rem.add_row_info(row)
					
			m_tel.remove_row(rows[i])
		#endfor
			
		# collect usable rows into the C1P matrix
		for r in m_tel._rows:
			matb.add_row_info(r)
		#endfor
		
		print ''
	#endfor
			
	return matb, mat_rem