I have another data manipulation question. So I have this .gtf file of tab delimited data and I need to extract certain features. This was simpler before when all I had to do was extract the gene ID, POS1 and POS2 for each "exon" type in each gene. I need to do the same thing, however I first need to find POS1 and POS2 of each exon relative to its position within the gene. Right now the columns POS1 and POS2 are numbered based upon the location of TYPE on the whole genome (which is why the numbers are so high). There is also another catch, if the strand is -, this is reversed. If you look at PITG\_00002, you can see that the stop codon appears to be before the start codon. This is because everything is numbered relative to the + (template) strand. So here is a sample of the data sheet:

```
GENE ID     TYPE        POS1    POS2    STRAND
PITG_00003  start_codon 38775   38777   +   0
PITG_00003  stop_codon  39069   39071   +   0
PITG_00003  exon        38775   39071   +   .
PITG_00003  CDS         38775   39068   +   0
PITG_00004  start_codon 39526   39528   +   0
PITG_00004  stop_codon  41492   41494   +   0
PITG_00004  exon        39526   40416   +   .
PITG_00004  CDS         39526   40416   +   0
PITG_00004  exon        40486   40771   +   .
PITG_00004  CDS         40486   40771   +   0
PITG_00004  exon        40827   41494   +   .
PITG_00004  CDS         40827   41491   +   2
PITG_00002  start_codon 10520   10522   -   0
PITG_00002  stop_codon  10097   10099   -   0
PITG_00002  exon        10474   10522   -   .
PITG_00002  CDS         10474   10522   -   0
PITG_00002  exon        10171   10433   -   .
PITG_00002  CDS         10171   10433   -   2
PITG_00002  exon        10097   10114   -   .
PITG_00002  CDS         10100   10114   -   0

```

So for each gene I need to start the number over at 1 relative to the position of the "start codon" TYPE. Unfortunately, the number is backwards for the Genes listed on the - STRAND (PITG\_00002, for example). So for these cases, the numbering needs to start at 1 relative to POS2 of start\_codon and end at POS1 of exon. 

so for each exon I need to get a new POS1 and POS2, which I will call POSA and POSB.

To get POSA for each exon I would do:

```
POS1 of "exon" - POS1 of "start_codon" + 1 = POSA

```

To get POSB for each exon I would do:

```
POS2 of "exon" - POS1 of "start_codon" + 1 = POSB

```

Using PITG\_00004 as an example:

```
POSA = 39526-39526 + 1 = 1
POSB = 40416 - 39526 + 1 = 891

```

And then just do the same thing for each exon in each gene, using that gene's start\_codon positions to reset the numbering. Except in the case of the negative strand, in which case I have to do:

To get POSA for each exon I would do:

```
POS2 of "start_codon" - POS2 of "exon" + 1 = POSA

```

To get POSB for each exon I would do:

```
POS1 of "start_codon" - POS1 of "exon" + 1 = POSB

```

Ultimately I'd like to get this:

```
PITG_00002 exon 1 49
PITG_00002 exon 90 352
PITG_00002 exon 409 426
PITG_00003 exon 1 297
PITG_00004 exon 1 891
PITG_00004 exon 961 1246
PITG_00004 exon 1302 1969

```

I'm not really sure how to do this one way for the + strand and another way for the - strand. I've been using python more often of late, but I can do perl as well.