annotate trackObject.py @ 26:a4a54b925c73 draft

planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit e81fecac281f299db00cfc88a068e056b031ae9d-dirty
author yating-l
date Wed, 15 Mar 2017 10:51:23 -0400
parents e1f188b43750
children 5580dbf2a31c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
26
a4a54b925c73 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit e81fecac281f299db00cfc88a068e056b031ae9d-dirty
yating-l
parents: 12
diff changeset
8 import tempfile
a4a54b925c73 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit e81fecac281f299db00cfc88a068e056b031ae9d-dirty
yating-l
parents: 12
diff changeset
9 import subprocess
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
10
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
11 class trackObject:
5
e7c80e9b70ae planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 0
diff changeset
12 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
13 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
14 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
15 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
16 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
17 self.tracks = []
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
18 try:
5
e7c80e9b70ae planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 0
diff changeset
19 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
20 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
21 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
22 else:
e7c80e9b70ae planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 0
diff changeset
23 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
24 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
25 except OSError as oserror:
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
26 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
27
5
e7c80e9b70ae planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 0
diff changeset
28 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
29 '''
e7c80e9b70ae planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 0
diff changeset
30 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
31 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
32 '''
e7c80e9b70ae planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 0
diff changeset
33 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
34 des_path = os.path.join(self.raw_folder, fileName)
26
a4a54b925c73 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit e81fecac281f299db00cfc88a068e056b031ae9d-dirty
yating-l
parents: 12
diff changeset
35 track = {}
12
e1f188b43750 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 5
diff changeset
36 if dataType == 'gff3_mrna' or dataType == 'gff3_transcript' or dataType == 'fasta' or dataType == 'bam' or dataType == 'bigwig' or dataType == 'bai':
26
a4a54b925c73 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit e81fecac281f299db00cfc88a068e056b031ae9d-dirty
yating-l
parents: 12
diff changeset
37 if dataType == 'bam':
a4a54b925c73 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit e81fecac281f299db00cfc88a068e056b031ae9d-dirty
yating-l
parents: 12
diff changeset
38 bam_index = utils.createBamIndex(dataFile)
a4a54b925c73 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit e81fecac281f299db00cfc88a068e056b031ae9d-dirty
yating-l
parents: 12
diff changeset
39 indexname = os.path.basename(bam_index)
a4a54b925c73 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit e81fecac281f299db00cfc88a068e056b031ae9d-dirty
yating-l
parents: 12
diff changeset
40 des_path_for_index = os.path.join(self.raw_folder, indexname)
a4a54b925c73 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit e81fecac281f299db00cfc88a068e056b031ae9d-dirty
yating-l
parents: 12
diff changeset
41 shutil.copyfile(bam_index, des_path_for_index)
a4a54b925c73 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit e81fecac281f299db00cfc88a068e056b031ae9d-dirty
yating-l
parents: 12
diff changeset
42 track['index'] = indexname
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
43 try:
5
e7c80e9b70ae planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 0
diff changeset
44 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
45 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
46 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
47 except IOError as err2:
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
48 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
49 elif dataType == 'bedSimpleRepeats':
e7c80e9b70ae planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 0
diff changeset
50 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
51 elif dataType == 'bedSpliceJunctions':
e7c80e9b70ae planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 0
diff changeset
52 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
53 elif dataType == 'blastxml':
e7c80e9b70ae planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 0
diff changeset
54 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
55 elif dataType == 'gtf':
e7c80e9b70ae planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 0
diff changeset
56 utils.gtfToGff3(dataFile, des_path, self.chrom_size)
26
a4a54b925c73 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit e81fecac281f299db00cfc88a068e056b031ae9d-dirty
yating-l
parents: 12
diff changeset
57 track['fileName'] = fileName
a4a54b925c73 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit e81fecac281f299db00cfc88a068e056b031ae9d-dirty
yating-l
parents: 12
diff changeset
58 track['dataType'] = dataType
5
e7c80e9b70ae planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 0
diff changeset
59 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
60
12
e1f188b43750 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 5
diff changeset
61
e1f188b43750 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 5
diff changeset
62
e1f188b43750 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 5
diff changeset
63 '''
e1f188b43750 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 5
diff changeset
64 def checkGff3(self, dataFile, dataType):
5
e7c80e9b70ae planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 0
diff changeset
65 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
66 for line in f:
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
67 if not line.startswith('#'):
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
68 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
69 if seq_type == 'transcript':
12
e1f188b43750 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 5
diff changeset
70 return 'gff3-transcript'
0
e4f3f2ed4fa5 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents:
diff changeset
71 if seq_type == 'mRNA':
12
e1f188b43750 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 5
diff changeset
72 return 'gff3'
e1f188b43750 planemo upload for repository https://github.com/Yating-L/jbrowse_hub commit f18ea51d27ec7addfa6413716391cfefebc8acbc-dirty
yating-l
parents: 5
diff changeset
73 '''