---
title: "annotating larval geoduck transcriptome (for ariana)"
author: "Olivia Cattau"
date: "2/24/2022"
output: pdf_document
editor_options: 
  chunk_output_type: console
---
#load count data from kallisto output(s)
```{r}
countMatrix<-read.table(file="https://raw.githubusercontent.com/sr320/nb-2022/main/P_generosa/analyses/kallisto-0207.isoform.counts.matrix", header=TRUE, sep = '\t')
names(countMatrix)[1]<-"target_id"
head(countMatrix)
```

#use binary data to make Venn Diagram
```{r}
library(tidyr)
library(dplyr)
simple_data<-countMatrix
long_data<- reshape(data=simple_data, idvar="target_id", varying=c("heart", "gonad", "ctenidia", "juv_sl", "juv_amb", "larvae"), v.name=c("tpm"), times=c("heart", "gonad", "ctenidia", "juv_sl", "juv_amb", "larvae"), new.row.names=1:8183754, direction="long")
names(long_data)[2]<-"tissue"
summary(long_data)
long_data$binary=ifelse(long_data$tpm > 0, 1, 0)
long_data_zeros<-long_data[apply(long_data, 1, function(row) all(row !=0)),]
df<-long_data_zeros[-c(3,4)] #tissue type and gene names only
table_no_zeros<-as.data.frame(crossprod(table(df)))
#write.csv(table_no_zeros, file="/Users/oliviacattau/Documents/GitHub/code/characterize_larval_transciptome/pgenerosa_table_no_zeros.tab")
simple_data_cleaned<-simple_data[apply(simple_data, 1, function(row) all(row !=0)),]
genelist<-long_data_zeros %>% group_by(tissue)
genelist %>% summarise("total reads (n)"=sum(binary),  "mean tpm" = mean(tpm), "Q4 tpm" = quantile(tpm, c(0.75)))
```

AH's tidy alternative approach: 
```{r}
tidy_data<-countMatrix
head(tidy_data)

#gather data to generate long format from wide format 
tidy_data_long<-tidy_data%>%
  gather(key="time", value="tpm", heart:larvae) #name key as time, name value as tpm, gather columns heart:larvae

head(tidy_data_long)
head(long_data)
#this tidy long dataframe is the same as the ones in above script

#now add the binary column
tidy_data_long$binary=ifelse(tidy_data_long$tpm > 0, 1, 0)
head(tidy_data_long)

#filter out zeros
tidy_data_long_no_zeros<-tidy_data_long%>%
  filter(!binary==0)

#filter zeros out of wide format dataframe 
tidy_data_wide_no_zeros<-tidy_data%>%
  mutate(sum = rowSums(across(where(is.numeric))))%>% #sum across numeric rows
  filter(!sum==0) #remove rows with sum=0 

#generate summary
genelist<-tidy_data_long_no_zeros %>% 
  group_by(time)%>%
  summarise(TotalReadsN=sum(binary), meanTPM = mean(tpm), Q4tpm = quantile(tpm, c(0.75)))
```

