################### Commands required for  for ASED analysis ###################

# Requirements:
# python v. 2.7.3
# samtools v. 0.1.19
# star v. 2.4.2a

# input files:
# <hg19>		:reference human genome hg19
# <N-masked genome>	:hg19 genome with individual specific heterozygous SNPs N-masked
# <RNA-seq read 1>	:gzipped read 1 of Geuvadis RNA-seq reads
# <RNA-seq read 2>	:gzipped read 2 of Geuvadis RNA-seq reads
# <gene annotations>	:ensembl gene annotations release 75
# <annotated RNA editing sites>
			: in bed format (RADAR V2)
# <snp file>		:Table with the following columns:
#			1. SNPID
#			2. chromosome
#			3. position
# 			4. notes
#			5. allele1/allele2 (of phased genotype from 1000 genomes data)


# STAR index
STAR --runMode genomeGenerate --genomeDir <index> --runThreadN 4 --sjdbGTFfile <gene annotations> --genomeFastaFiles <N-masked genome>

# align reads
STAR  --genomeDir <index> --readFilesCommand zcat --readFilesIn <RNA-seq read 1> <RNA-seq read 2> --outFileNamePrefix <outpur dir>/mappingN --alignEndsType EndToEnd --outSAMattributes NH HI NM MD --outSAMtype BAM Unsorted --outSJfilterOverhangMin 8 8 8 8 8 --outFilterType BySJout --outFilterMultimapNmax 20 --outFilterMultimapScoreRange 0 --outFilterMismatchNmax 6 --outFilterIntronMotifs RemoveNoncanonicalUnannotated --alignIntronMax 300000 

# get reads that overlap a heterozygous SNP
samtools view <outpur dir>/mappingNAligned.out.bam | python getoverlap.py <snp file> <overlap file>
samtools view <outpur dir>/mappingNAligned.out.bam | python filter1.py SAMPLE_NAME.overlap filter.sam

# split alignment
python split.py <snp file> filter.sam
samtools view -H mappingNAligned.out.bam > header.sam
cat header.sam filter.sam.hap1 > filter.sam.hap1.h
cat header.sam filter.sam.hap2 > filter.sam.hap2.h
samtools view -bS filter.sam.hap1.h > filter.hap1.bam
samtools view -bS filter.sam.hap2.h > filter.hap2.bam
samtools sort filter.hap1.bam filter.hap1.sort
samtools sort filter.hap2.bam filter.hap2.sort
samtools index filter.hap1.sort.bam
samtools index filter.hap2.sort.bam

# Get read counts for annotated RNA editing sites
samtools mpileup -l <annotated RNA editing sites> -d 1000000 -f <hg19> filter.hap1.sort.bam > filter.hap1.mp
samtools mpileup -l <annotated RNA editing sites> -d 1000000 -f <hg19> filter.hap2.sort.bam > filter.hap2.mp

# Apply preliminary filters
python filter2.py filter.hap1.mp filter2.hap1.mp
python filter2.py filter.hap2.mp filter2.hap2.mp
python filter3.py filter2.hap1.mp hap1.counts.txt <annotated RNA editing sites>
python filter3.py filter2.hap2.mp hap2.counts.txt <annotated RNA editing sites>

# Get connections with RNA editing sites and SNPs
python get.connections.py <snp file> filter.sam <annotated RNA editing sites> connections.txt
python filter.connections.py connections.txt connections.filter.txt

# Join population connections
cat <population level connections> | sort |uniq -c | sort -nr > population.connections.txt



#### Create input file for statistical analysis ####

# extract editing ratio from each file
# For each sample run the following to extract editing ratio 
# for each allele count file (hap1.counts.txt or hap2.counts.txt)
# <out file> = <sample name>.<allele 1 or 2>
python get.ratio.py <allele count file> <out file> <sample name>

# get row names for one sample:
cut -f 1-2 <allele count file> | sed 's/\t/:/g' > row.names.txt

# combine and filter editing ratio files:
paste row.names.txt <list of all out files from get.ratio.py> > joined.list1.txt
python mk.list2.py
python mk.list3.py

# note: requires vcf file containing only the samples 
# of interest in the same order as previous files
python mk.input.py population.connections.txt joined.list3.txt <vcf file> input.txt


################### PAIR-wise ASED analysis ###################
# Requirements: 
# R v. 3.2.3 

# Input file:
# <input>	:Table with the following collumns
#		1. user-defined RNA editing site : SNP pair ID number
#		2. comma separated counts for editing counts for allele1
#		3. comma separated counts for non-editing counts for allele1
#		4. comma separated counts for editing counts for allele2
#		5. comma separated counts for non-editing counts for allele2
#		6. 1 (editing weight)
#		7. 1 (non-editing weight)
# 		note: requires header

# sample input: input.txt

# Run statistical test
Rscript paired-test.R 







