示例#1
0
 def run(self):
     lens1 = base.newlens(self.lens1, 'order')
     lens2 = base.newlens(self.lens2, 'order')
     gff1 = base.newgff(self.gff1)
     gff2 = base.newgff(self.gff2)
     gff1 = gff1[gff1['chr'].isin(lens1.index)]
     gff2 = gff2[gff2['chr'].isin(lens2.index)]
     blast = base.newblast(self.blast, int(self.score), float(self.evalue),
                           gff1, gff2, self.blast_reverse)
     blast = self.deal_blast(blast, gff1, gff2, int(self.multiple),
                             int(self.repeat_number))
     blast['loc1'] = blast[0].map(gff1.loc[:, self.position])
     blast['loc2'] = blast[1].map(gff2.loc[:, self.position])
     blast['chr1'] = blast[0].map(gff1.loc[:, 'chr'])
     blast['chr2'] = blast[1].map(gff2.loc[:, 'chr'])
     total = []
     for (chr1, chr2), group in blast.groupby(['chr1', 'chr2']):
         df = pd.DataFrame(np.zeros((lens1[chr1], lens2[chr2])))
         for index, row in group.iterrows():
             df.loc[row['loc1'], row['loc2']] = row['grading']
         df = df.loc[:, df.sum(axis=0) != 0]
         df = df.loc[df.sum(axis=1) != 0, :]
         collinearity = improvedcollinearity.collinearity(self.options, df)
         data = collinearity.run()
         fp = self.dir + '/' + str(chr1) + '.vs.' + str(chr2) + '.blk'
         gf1, gf2 = gff1[gff1['chr'] == chr1], gff2[gff2['chr'] == chr1]
         self.write_block(fp, data, chr1, chr2, gff1, gff2)
     args = ['cat', self.dir + '/*.blk', '>', self.savefile]
     command = ' '.join([str(k) for k in args])
     os.system(command)
     shutil.rmtree(self.dir)
     sys.exit(0)
示例#2
0
 def run(self):
     lens1 = base.newlens(self.lens1, 'order')
     lens2 = base.newlens(self.lens2, 'order')
     lens1 = lens1[lens1 > 4]
     lens2 = lens2[lens2 > 4]
     gff1 = base.newgff(self.gff1)
     gff2 = base.newgff(self.gff2)
     gff1 = gff1[gff1['chr'].isin(lens1.index)]
     gff2 = gff2[gff2['chr'].isin(lens2.index)]
     blast = base.newblast(self.blast, int(self.score), float(self.evalue),
                           gff1, gff2)
     df = self.deal_blast(blast, gff1, gff2, int(self.repnum))
     for (chr1, chr2), group in df.groupby(['chr1', 'chr2']):
         group = group.sort_values(by=['loc1', 'loc2'])
         dir1 = './' + self.dir + '/pair/' + str(chr1) + '.vs.' + str(
             chr2) + '.pair'
         dir2 = './' + self.dir + '/block/' + str(chr1) + '.vs.' + str(
             chr2) + '.blk'
         group[[0, 'stand1', 'loc1', 1, 'stand2',
                'loc2']].to_csv(dir1, sep=' ', index=None, header=None)
         args = [
             'blockscan', '-chr1len', lens1[str(chr1)], '-chr2len',
             lens2[str(chr2)], '-mg1', self.mg[0], '-mg2', self.mg[1], dir1,
             '>' + dir2
         ]
         command = ' '.join([str(k) for k in args])
         os.system(command)
     args = [
         'cat', self.dir + '/block/*.blk', '>', self.dir + '.block.old.txt'
     ]
     command = ' '.join([str(k) for k in args])
     os.system(command)
     self.rewriteblock(blast, self.dir + '.block.old.txt',
                       self.dir + '.block.txt')
示例#3
0
 def run(self):
     lens1 = base.newlens(self.lens1, self.position)
     lens2 = base.newlens(self.lens2, self.position)
     if re.search('\d', self.figsize):
         self.figsize = [float(k) for k in self.figsize.split(',')]
     else:
         self.figsize = np.array(
             [1, float(lens1.sum()) / float(lens2.sum())]) * 10
     plt.rcParams['ytick.major.pad'] = 0
     fig, ax = plt.subplots(figsize=self.figsize)
     ax.xaxis.set_ticks_position('top')
     step1 = 1 / float(lens1.sum())
     step2 = 1 / float(lens2.sum())
     base.dotplot_frame(fig, ax, lens1, lens2, step1, step2,
                        self.genome1_name, self.genome2_name)
     gff1 = base.newgff(self.gff1)
     gff2 = base.newgff(self.gff2)
     gff1 = base.gene_location(gff1, lens1, step1, self.position)
     gff2 = base.gene_location(gff2, lens2, step2, self.position)
     blast = base.newblast(self.blast, int(self.score), float(self.evalue),
                           gff1, gff2)
     df = self.pair_positon(blast, gff1, gff2, int(self.multiple),
                            int(self.repnum))
     plt.scatter(df['loc2'],
                 df['loc1'],
                 s=float(self.markersize),
                 c=df['color'],
                 alpha=0.5,
                 edgecolors=None,
                 linewidths=0,
                 marker='o')
     plt.subplots_adjust(left=0.07, right=0.97, top=0.93, bottom=0.03)
     plt.savefig(self.savefile, dpi=500)
     sys.exit(0)
示例#4
0
 def run(self):
     bkinfo = pd.read_csv(self.blockinfo, index_col='id')
     bkinfo['chr1'] = bkinfo['chr1'].astype(str)
     bkinfo['chr2'] = bkinfo['chr2'].astype(str)
     if self.blockinfo_reverse == True or self.blockinfo_reverse.upper(
     ) == 'TRUE':
         bkinfo[['chr1', 'chr2']] = bkinfo[['chr2', 'chr1']]
         bkinfo[['block1', 'block2']] = bkinfo[['block2', 'block1']]
     bkinfo = bkinfo[bkinfo['length'] > int(self.block_length)]
     bkinfo['class1'] = ''
     bkinfo['col1'] = ''
     bkinfo['class2'] = ''
     bkinfo['col2'] = ''
     gff1 = base.newgff(self.gff1)
     gff2 = base.newgff(self.gff2)
     lens = base.newlens(self.the_other_lens, self.position)
     blast = base.newblast(self.blast, int(self.score), float(self.evalue),
                           gff1, gff2, self.blast_reverse)
     index = [
         group.sort_values(
             by=11,
             ascending=False)[:int(self.repeat_number)].index.tolist()
         for name, group in blast.groupby([0])
     ]
     blast = blast.loc[np.concatenate(
         np.
         array([k[:int(self.repeat_number)] for k in index], dtype=object)),
                       [0, 1]]
     pairs = self.colinear_gene_pairs(bkinfo, gff1, gff2)
     data = []
     for k, v in lens.items():
         for i in range(1, v + 1):
             data.append([k, i, 0])
     df = pd.DataFrame(data)
     gff1['color'] = np.nan
     gff2['color'] = np.nan
     gff1['classification'] = np.nan
     gff2['classification'] = np.nan
     if hasattr(self, 'ancestor_top'):
         ancestor = base.read_calassfication(self.ancestor_top)
         data = self.karyotype_top(pairs, ancestor, gff1, gff2)
     if hasattr(self, 'ancestor_left'):
         ancestor = base.read_calassfication(self.ancestor_left)
         data = self.karyotype_left(pairs, ancestor, gff1, gff2)
     the_other_ancestor_file = self.karyotype_map(data, lens)
     the_other_ancestor_file = self.new_ancestor(the_other_ancestor_file,
                                                 gff1, gff2, blast)
     the_other_ancestor_file.to_csv(self.the_other_ancestor_file,
                                    sep='\t',
                                    header=False,
                                    index=False)
示例#5
0
 def run(self):
     lens1 = base.newlens(self.lens1, self.position)
     lens2 = base.newlens(self.lens2, self.position)
     gff1 = base.newgff(self.gff1)
     gff2 = base.newgff(self.gff2)
     gff1 = gff1[gff1['chr'].isin(lens1.index)]
     gff2 = gff2[gff2['chr'].isin(lens2.index)]
     blast = base.newblast(self.blast, int(self.score), float(self.evalue),
                           gff1, gff2)
     blast = self.blast_homo(blast, gff1, gff2, int(self.repnum))
     blast.index = blast[0] + ',' + blast[1]
     colinearity = base.read_colinearscan(self.colinearity)
     ks = base.read_ks(self.ks, self.ks_col)
     data = self.block_position(colinearity, blast, gff1, gff2, ks)
示例#6
0
 def run(self):
     lens1 = base.newlens(self.lens1, 'order')
     lens2 = base.newlens(self.lens2, 'order')
     gff1 = base.newgff(self.gff1)
     gff2 = base.newgff(self.gff2)
     gff1 = gff1[gff1['chr'].isin(lens1.index)]
     gff2 = gff2[gff2['chr'].isin(lens2.index)]
     blast = base.newblast(self.blast, int(self.score), float(self.evalue),
                           gff1, gff2, self.blast_reverse)
     blast = self.deal_blast(blast, gff1, gff2, int(self.multiple),
                             int(self.repeat_number))
     blast['loc1'] = blast[0].map(gff1.loc[:, self.position])
     blast['loc2'] = blast[1].map(gff2.loc[:, self.position])
     blast['chr1'] = blast[0].map(gff1.loc[:, 'chr'])
     blast['chr2'] = blast[1].map(gff2.loc[:, 'chr'])
     total = []
     for (chr1, chr2), group in blast.groupby(['chr1', 'chr2']):
         total.append([chr1, chr2, group])
     del blast, group
     gc.collect()
     n = int(np.ceil(len(total) / float(self.process)))
     result, data = '', []
     try:
         pool = Pool(self.process)
         for i in range(0, len(total), n):
             data.append(
                 pool.apply_async(self.single_pool,
                                  args=(total[i:i + n], gff1, gff2, lens1,
                                        lens2)))
         pool.close()
         pool.join()
     except:
         pool.terminate()
     for k in data:
         result += k.get()
     result = re.split('\n', result)
     fout = open(self.savefile, 'w')
     num = 1
     for line in result:
         if re.match(r"# Alignment", line):
             s = '# Alignment ' + str(num) + ':'
             fout.writelines(s + line.split(':')[1] + '\n')
             num += 1
             continue
         if len(line) > 0:
             fout.writelines(line + '\n')
     fout.close()
     sys.exit(0)
示例#7
0
 def run(self):
     lens1 = base.newlens(self.lens1, self.position)
     lens2 = base.newlens(self.lens2, self.position)
     gff1 = base.newgff(self.gff1)
     gff2 = base.newgff(self.gff2)
     gff1 = gff1[gff1['chr'].isin(lens1.index)]
     gff2 = gff2[gff2['chr'].isin(lens2.index)]
     blast = base.newblast(self.blast, int(self.score), float(self.evalue),
                           gff1, gff2, self.blast_reverse)
     blast = self.blast_homo(blast, gff1, gff2, int(self.repeat_number))
     blast.index = blast[0] + ',' + blast[1]
     collinearity = self.auto_file(gff1, gff2)
     ks = base.read_ks(self.ks, self.ks_col)
     data = self.block_position(collinearity, blast, gff1, gff2, ks)
     data['class1'] = 0
     data['class2'] = 0
     data.to_csv(self.savefile, index=None)
示例#8
0
 def run(self):
     length = 1
     axis = [0, 1, 1, 0]
     left, right, top, bottom = 0.07, 0.97, 0.93, 0.03
     lens1 = base.newlens(self.lens1, self.position)
     lens2 = base.newlens(self.lens2, self.position)
     step1 = 1 / float(lens1.sum())
     step2 = 1 / float(lens2.sum())
     if self.ancestor_left != None:
         axis[0] = -0.02
         lens_ancestor_left = pd.read_csv(self.ancestor_left,
                                          sep="\t",
                                          header=None)
         lens_ancestor_left[0] = lens_ancestor_left[0].astype(str)
         lens_ancestor_left[3] = lens_ancestor_left[3].astype(str)
         lens_ancestor_left[4] = lens_ancestor_left[4].astype(int)
         lens_ancestor_left[
             4] = lens_ancestor_left[4] / lens_ancestor_left[4].max()
         lens_ancestor_left = lens_ancestor_left[lens_ancestor_left[0].isin(
             lens1.index)]
     if self.ancestor_top != None:
         axis[3] = -0.02
         lens_ancestor_top = pd.read_csv(self.ancestor_top,
                                         sep="\t",
                                         header=None)
         lens_ancestor_top[0] = lens_ancestor_top[0].astype(str)
         lens_ancestor_top[3] = lens_ancestor_top[3].astype(str)
         lens_ancestor_top[4] = lens_ancestor_top[4].astype(int)
         lens_ancestor_top[
             4] = lens_ancestor_top[4] / lens_ancestor_top[4].max()
         lens_ancestor_top = lens_ancestor_top[lens_ancestor_top[0].isin(
             lens2.index)]
     if re.search('\d', self.figsize):
         self.figsize = [float(k) for k in self.figsize.split(',')]
     else:
         self.figsize = np.array(
             [1, float(lens1.sum()) / float(lens2.sum())]) * 10
     plt.rcParams['ytick.major.pad'] = 0
     fig, ax = plt.subplots(figsize=self.figsize)
     ax.xaxis.set_ticks_position('top')
     base.dotplot_frame(fig, ax, lens1, lens2, step1, step2,
                        self.genome1_name, self.genome2_name,
                        [axis[0], axis[3]])
     gff1 = base.newgff(self.gff1)
     gff2 = base.newgff(self.gff2)
     gff1 = base.gene_location(gff1, lens1, step1, self.position)
     gff2 = base.gene_location(gff2, lens2, step2, self.position)
     if self.ancestor_top != None:
         top = top
         self.ancestor_posion(ax, gff2, lens_ancestor_top, 'top')
     if self.ancestor_left != None:
         left = left
         self.ancestor_posion(ax, gff1, lens_ancestor_left, 'left')
     blast = base.newblast(self.blast, int(self.score), float(self.evalue),
                           gff1, gff2, self.blast_reverse)
     df = self.pair_positon(blast, gff1, gff2, int(self.multiple),
                            int(self.repeat_number))
     ax.scatter(df['loc2'],
                df['loc1'],
                s=float(self.markersize),
                c=df['color'],
                alpha=0.5,
                edgecolors=None,
                linewidths=0,
                marker='o')
     ax.axis(axis)
     plt.subplots_adjust(left=left, right=right, top=top, bottom=bottom)
     plt.savefig(self.savefig, dpi=500)
     plt.show()
     sys.exit(0)