first pass cleanup of cistematic/genomes; change bamPreprocessing
[erange.git] / gfftocis.py
1 import sys
2
3 print "%s: version 1.0" % sys.argv[0]
4 if len(sys.argv) < 3:
5     print "usage: python %s infile.gff outfile.cis\n" % sys.argv[0]
6     print "\tTHIS SCRIPT WILL MOST LIKELY NEED TO BE EDITED FOR YOUR GFF FILE\n"
7     sys.exit(1)
8
9 index = 1
10 # Cistematic just want's a use set of exons labeled "CDS", "5UTR", and "3UTR"
11 # just put the corresponding type in your GFF file as the key in the key:value pairs 
12 # in the ftypeDict below
13 ftypeDict = {"CDS": "CDS",
14              "mRNA": "mRNA",
15              "five_prime_utr": "5UTR",
16              "three_prime_utr": "3UTR"
17 }
18
19 chrom = ""
20 idfields = ""
21 gene = ""
22 sense = ""
23 start = 0
24 stop = 0
25 ftype = ""
26
27 infile = open(sys.argv[1])
28 outfile = open(sys.argv[2], "w")
29 for line in infile:
30     if line[0]=="#":
31         continue
32
33     fields = line.strip().split()
34     try:
35         if fields[2] in ftypeDict:
36             # this part of the code will need to be customized, most likely
37             # how does the annotation define the gene, geneid, and chromosome
38             # for example, for Anopheles Gambiae we have
39             #chrX    VectorBase      mRNA    582     16387   .       -       .       ID=vectorbase|AGAP000002-RA; stable_id=AGAP000002-RA.1; Parent=vectorbase|AGAP000002;
40             if fields[2] == "mRNA":
41                 chrom = fields[0][3:]
42                 source = fields[1]
43                 idfields = fields[9].split(";")
44                 geneid = idfields[0].split("=")[1]
45                 sense = fields[6]
46             else:
47                 start = int(fields[3])
48                 stop = int(fields[4])
49                 ftype = ftypeDict[fields[2]]
50                 outline = "%s\t%s%d\t%s\t%d\t%d\t%s\t%s\n" % (geneid, source, index, chrom, start, stop, sense, ftype)
51                 outfile.write(outline)
52     except:
53         sys.exit()
54
55     index += 1
56
57 infile.close()
58 outfile.close()