Mercurial > repos > yating-l > jbrowse_hub
annotate trackObject.py @ 5:e7c80e9b70ae draft
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
author | yating-l |
---|---|
date | Tue, 14 Mar 2017 12:24:37 -0400 |
parents | e4f3f2ed4fa5 |
children | e1f188b43750 |
rev | line source |
---|---|
0
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
1 #!/usr/bin/env python |
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
2 |
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
3 import os |
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
4 import shutil |
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
5 import utils |
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
6 import bedToGff3 |
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
7 import blastxmlToGff3 |
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
8 |
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
9 class trackObject: |
5
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
10 def __init__(self, chrom_size, genome, extra_files_path): |
0
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
11 self.chrom_size = chrom_size |
5
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
12 outputDirect = os.path.join(extra_files_path, genome) |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
13 self.raw_folder = os.path.join(outputDirect, 'raw') |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
14 print self.raw_folder |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
15 self.tracks = [] |
0
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
16 try: |
5
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
17 if os.path.exists(self.raw_folder): |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
18 if os.path.isdir(self.raw_folder): |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
19 shutil.rmtree(self.raw_folder) |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
20 else: |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
21 os.remove(self.raw_folder) |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
22 os.makedirs(self.raw_folder) |
0
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
23 except OSError as oserror: |
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
24 print "Cannot create raw folder error({0}): {1}".format(oserror.errno, oserror.strerror) |
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
25 |
5
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
26 def addToRaw(self, dataFile, dataType): |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
27 ''' |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
28 Convert gff3, BED, blastxml and gtf files into gff3 files |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
29 and store converted files in folder 'raw' |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
30 ''' |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
31 fileName = os.path.basename(dataFile) |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
32 des_path = os.path.join(self.raw_folder, fileName) |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
33 if dataType == 'gff3' or dataType == 'fasta' or dataType == 'bam' or dataType == 'bigwig' or dataType == 'bai': |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
34 if dataType == 'gff3': |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
35 self.checkGff3(dataFile) |
0
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
36 try: |
5
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
37 shutil.copyfile(dataFile, des_path) |
0
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
38 except shutil.Error as err1: |
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
39 print "Cannot move file, error({0}: {1})".format(err1.errno, err1.strerror) |
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
40 except IOError as err2: |
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
41 print "Cannot move file, error({0}: {1})".format(err2.errno, err2.strerror) |
5
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
42 elif dataType == 'bedSimpleRepeats': |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
43 bedToGff3.bedToGff3(dataFile, self.chrom_size, 'trfbig', des_path) |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
44 elif dataType == 'bedSpliceJunctions': |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
45 bedToGff3.bedToGff3(dataFile, self.chrom_size, 'regtools', des_path) |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
46 elif dataType == 'blastxml': |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
47 blastxmlToGff3.blastxml2gff3(dataFile, des_path) |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
48 elif dataType == 'gtf': |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
49 utils.gtfToGff3(dataFile, des_path, self.chrom_size) |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
50 track = { |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
51 'fileName': fileName, |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
52 'dataType': dataType |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
53 } |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
54 self.tracks.append(track) |
0
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
55 |
5
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
56 def checkGff3(self, dataFile): |
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
57 with open(dataFile, 'r') as f: |
0
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
58 for line in f: |
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
59 if not line.startswith('#'): |
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
60 seq_type = line.rstrip().split('\t')[2] |
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
61 if seq_type == 'transcript': |
5
e7c80e9b70ae
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
0
diff
changeset
|
62 dataType = 'gff3-transcript' |
0
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
63 break |
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
64 if seq_type == 'mRNA': |
e4f3f2ed4fa5
planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff
changeset
|
65 break |